c_exec.c 2.8 KB

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