GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
c_exec.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_exec.c
3 
4  \brief Cluster library - Exectute clusterring
5 
6  (C) 2001-2009 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Original author CERL
12 */
13 
14 #include <grass/cluster.h>
15 #include <grass/glocale.h>
16 
17 /*!
18  \param C pointer to Cluster structure
19  \param maxclass maximum number of classes
20  \param iterations maximum number of iterations
21  \param convergence percentage of points stable
22  \param separation minimum distance between class centroids
23  \param min_class_size minimum size of class
24  \param checkpoint routine to be called at various steps
25  \param interrupted boolean to check for interrupt
26 
27  \return 0 ok
28  \return -1 out of memory
29  \return -2 interrupted
30  \return 1 not enough data points
31 */
32 int I_cluster_exec(struct Cluster *C, int maxclass, int iterations,
33  double convergence,
34  double separation, int min_class_size,
35  int (*checkpoint) (), int *interrupted)
36 {
37  int changes;
38 
39  /* set interrupted to false */
40  *interrupted = 0;
41 
42  /* check for valid inputs */
43  if (C->npoints < 2) {
44  G_warning(_("Not enough data points (%d) in cluster"), C->npoints);
45  return 1;
46  }
47 
48  /* check other parms */
49  if (maxclass < 0)
50  maxclass = 1;
51  C->nclasses = maxclass;
52 
53  if (min_class_size <= 0)
54  min_class_size = 17;
55  if (min_class_size < 2)
56  min_class_size = 2;
57 
58  if (iterations <= 0)
59  iterations = 20;
60  if (convergence <= 0.0)
61  convergence = 98.0;
62  if (separation < 0.0)
63  separation = 0.5;
64 
65 
66  /* allocate memory */
68  return -1;
69 
70 
71  /* generate class means */
72  I_cluster_means(C);
73  if (checkpoint)
74  (*checkpoint) (C, 1);
75 
76  /* now assign points to nearest class */
77  I_cluster_assign(C, interrupted);
78  if (*interrupted)
79  return -2;
80  I_cluster_sum2(C);
81  if (checkpoint)
82  (*checkpoint) (C, 2);
83 
84  /* get rid of empty classes now */
85  I_cluster_reclass(C, 1);
86 
87  for (C->iteration = 1;; C->iteration++) {
88  if (*interrupted)
89  return -2;
90 
91  changes = 0;
92 
93  /* re-assign points to nearest class */
94 
95  changes = I_cluster_reassign(C, interrupted);
96  if (*interrupted)
97  return -2;
98 
99  /* if too many points have changed class, re-assign points */
100  C->percent_stable = (C->npoints - changes) * 100.0;
101  C->percent_stable /= (double)C->npoints;
102 
103  if (checkpoint)
104  (*checkpoint) (C, 3);
105 
106  if (C->iteration >= iterations)
107  break;
108 
109  if (C->percent_stable < convergence)
110  continue;
111 
112  /* otherwise merge non-distinct classes */
113 
114  if (I_cluster_distinct(C, separation))
115  break;
116 
117  if (checkpoint)
118  (*checkpoint) (C, 4);
119 
120  I_cluster_merge(C);
121  }
122 
123  /* get rid of small classes */
124  I_cluster_reclass(C, min_class_size);
125  I_cluster_sum2(C);
126 
127  /* compute the resulting signatures */
129 
130 
131  return 0;
132 }
if(!(yy_init))
Definition: sqlp.yy.c:775
int I_cluster_sum2(struct Cluster *)
Compute sum of squares for each class.
Definition: c_sum2.c:23
int nclasses
Definition: cluster.h:27
int I_cluster_reassign(struct Cluster *, int *)
?
Definition: c_reassign.c:25
int I_cluster_exec_allocate(struct Cluster *)
Allocate Cluster structure.
Definition: c_execmem.c:24
int I_cluster_signatures(struct Cluster *)
Create signatures.
Definition: c_sig.c:23
int I_cluster_exec(struct Cluster *C, int maxclass, int iterations, double convergence, double separation, int min_class_size, int(*checkpoint)(), int *interrupted)
Definition: c_exec.c:32
Definition: cluster.h:7
int npoints
Definition: cluster.h:10
int I_cluster_means(struct Cluster *)
Calculate means value.
Definition: c_means.c:24
int I_cluster_distinct(struct Cluster *, double)
Get distinct value.
Definition: c_distinct.c:24
int I_cluster_assign(struct Cluster *, int *)
Assign cluster.
Definition: c_assign.c:26
int I_cluster_merge(struct Cluster *)
?
Definition: c_merge.c:23
int I_cluster_reclass(struct Cluster *, int)
Reclass data.
Definition: c_reclass.c:25
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10