GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
c_exec.c
Go to the documentation of this file.
1 /*!
2  \file cluster/c_exec.c
3 
4  \brief Cluster library - Execute 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, double separation, int min_class_size,
34  int (*checkpoint)(struct Cluster *, int), int *interrupted)
35 {
36  int changes;
37 
38  /* set interrupted to false */
39  *interrupted = 0;
40 
41  /* check for valid inputs */
42  if (C->npoints < 2) {
43  G_warning(_("Not enough data points (%d) in cluster"), C->npoints);
44  return 1;
45  }
46 
47  /* check other parms */
48  if (maxclass < 0)
49  maxclass = 1;
50  C->nclasses = maxclass;
51 
52  if (min_class_size <= 0)
53  min_class_size = 17;
54  if (min_class_size < 2)
55  min_class_size = 2;
56 
57  if (iterations <= 0)
58  iterations = 20;
59  if (convergence <= 0.0)
60  convergence = 98.0;
61  if (separation < 0.0)
62  separation = 0.5;
63 
64  /* allocate memory */
66  return -1;
67 
68  /* generate class means */
69  I_cluster_means(C);
70  if (checkpoint)
71  (*checkpoint)(C, 1);
72 
73  /* now assign points to nearest class */
74  I_cluster_assign(C, interrupted);
75  if (*interrupted)
76  return -2;
77  I_cluster_sum2(C);
78  if (checkpoint)
79  (*checkpoint)(C, 2);
80 
81  /* get rid of empty classes now */
82  I_cluster_reclass(C, 1);
83 
84  for (C->iteration = 1;; C->iteration++) {
85  if (*interrupted)
86  return -2;
87 
88  changes = 0;
89 
90  /* re-assign points to nearest class */
91 
92  changes = I_cluster_reassign(C, interrupted);
93  if (*interrupted)
94  return -2;
95 
96  /* if too many points have changed class, re-assign points */
97  C->percent_stable = (C->npoints - changes) * 100.0;
98  C->percent_stable /= (double)C->npoints;
99 
100  if (checkpoint)
101  (*checkpoint)(C, 3);
102 
103  if (C->iteration >= iterations)
104  break;
105 
106  if (C->percent_stable < convergence)
107  continue;
108 
109  /* otherwise merge non-distinct classes */
110 
111  if (I_cluster_distinct(C, separation))
112  break;
113 
114  if (checkpoint)
115  (*checkpoint)(C, 4);
116 
117  I_cluster_merge(C);
118  }
119 
120  /* get rid of small classes */
121  I_cluster_reclass(C, min_class_size);
122  I_cluster_sum2(C);
123 
124  /* compute the resulting signatures */
126 
127  return 0;
128 }
int I_cluster_exec(struct Cluster *C, int maxclass, int iterations, double convergence, double separation, int min_class_size, int(*checkpoint)(struct Cluster *, int), int *interrupted)
Definition: c_exec.c:32
int I_cluster_signatures(struct Cluster *)
Create signatures.
Definition: c_sig.c:23
int I_cluster_means(struct Cluster *)
Calculate means value.
Definition: c_means.c:24
int I_cluster_exec_allocate(struct Cluster *)
Allocate Cluster structure.
Definition: c_execmem.c:24
int I_cluster_merge(struct Cluster *)
?
Definition: c_merge.c:23
int I_cluster_reassign(struct Cluster *, int *)
?
Definition: c_reassign.c:25
int I_cluster_sum2(struct Cluster *)
Compute sum of squares for each class.
Definition: c_sum2.c:23
int I_cluster_distinct(struct Cluster *, double)
Get distinct value.
Definition: c_distinct.c:24
int I_cluster_reclass(struct Cluster *, int)
Reclass data.
Definition: c_reclass.c:25
int I_cluster_assign(struct Cluster *, int *)
Assign cluster.
Definition: c_assign.c:26
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
if(!(yy_init))
Definition: sqlp.yy.c:775
Definition: cluster.h:7
int npoints
Definition: cluster.h:9
int nclasses
Definition: cluster.h:26