c_exec.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*!
  2. \file cluster/c_exec.c
  3. \brief Cluster library - Exectute clusterring
  4. (C) 2001-2009 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <grass/cluster.h>
  10. /*!
  11. \param maxclass maximum number of classes
  12. \param iterations maximum number of iterations
  13. \param convergence percentage of points stable
  14. \param separation minimum distance between class centroids
  15. \param checkpoint routine to be called at various steps
  16. \param interrupted boolean to check for interrupt
  17. \return 0 ok
  18. \return -1 out of memory
  19. \return -2 interrupted
  20. \return 1 not enough data points
  21. */
  22. int I_cluster_exec(struct Cluster *C, int maxclass, int iterations,
  23. double convergence,
  24. double separation, int min_class_size,
  25. int (*checkpoint) (), int *interrupted)
  26. {
  27. int changes;
  28. /* set interrupted to false */
  29. *interrupted = 0;
  30. /* check for valid inputs */
  31. if (C->npoints < 2) {
  32. G_warning(_("Not enough data points (%d) in cluster"), C->npoints);
  33. return 1;
  34. }
  35. /* check other parms */
  36. if (maxclass < 0)
  37. maxclass = 1;
  38. C->nclasses = maxclass;
  39. if (min_class_size <= 0)
  40. min_class_size = 17;
  41. if (min_class_size < 2)
  42. min_class_size = 2;
  43. if (iterations <= 0)
  44. iterations = 20;
  45. if (convergence <= 0.0)
  46. convergence = 98.0;
  47. if (separation < 0.0)
  48. separation = 0.5;
  49. /* allocate memory */
  50. if (!I_cluster_exec_allocate(C))
  51. return -1;
  52. /* generate class means */
  53. I_cluster_means(C);
  54. if (checkpoint)
  55. (*checkpoint) (C, 1);
  56. /* now assign points to nearest class */
  57. I_cluster_assign(C, interrupted);
  58. if (*interrupted)
  59. return -2;
  60. I_cluster_sum2(C);
  61. if (checkpoint)
  62. (*checkpoint) (C, 2);
  63. /* get rid of empty classes now */
  64. I_cluster_reclass(C, 1);
  65. for (C->iteration = 1;; C->iteration++) {
  66. if (*interrupted)
  67. return -2;
  68. changes = 0;
  69. /* re-assign points to nearest class */
  70. changes = I_cluster_reassign(C, interrupted);
  71. if (*interrupted)
  72. return -2;
  73. /* if too many points have changed class, re-assign points */
  74. C->percent_stable = (C->npoints - changes) * 100.0;
  75. C->percent_stable /= (double)C->npoints;
  76. if (checkpoint)
  77. (*checkpoint) (C, 3);
  78. if (C->iteration >= iterations)
  79. break;
  80. if (C->percent_stable < convergence)
  81. continue;
  82. /* otherwise merge non-distinct classes */
  83. if (I_cluster_distinct(C, separation))
  84. break;
  85. if (checkpoint)
  86. (*checkpoint) (C, 4);
  87. I_cluster_merge(C);
  88. }
  89. /* get rid of small classes */
  90. I_cluster_reclass(C, min_class_size);
  91. I_cluster_sum2(C);
  92. /* compute the resulting signatures */
  93. I_cluster_signatures(C);
  94. return 0;
  95. }