GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_exec.c
Go to the documentation of this file.
1 
2 /***************************************************************
3  *
4  * I_cluster_exec (C, maxclass, iterations,
5  * convergence, separation, min_class_size,
6  * checkpoint, interrupted)
7  *
8  * maxclass maximum number of classes
9  * iterations maximum number of iterations
10  * convergence percentage of points stable
11  * separation minimum distance between class centroids
12  * checkpoint routine to be called at various steps
13  * interrupted boolean to check for interrupt
14  *
15  * returns:
16  * 0 ok
17  * -1 out of memory
18  * -2 interrupted
19  * 1 not enough data points
20  *************************************************************/
21 #include <grass/cluster.h>
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 
30  /* set interrupted to false */
31  *interrupted = 0;
32 
33  /* check for valid inputs */
34  if (C->npoints < 2) {
35  fprintf(stderr, "cluster: not enough data points (%d)\n", C->npoints);
36  return 1;
37  }
38 
39  /* check other parms */
40  if (maxclass < 0)
41  maxclass = 1;
42  C->nclasses = maxclass;
43 
44  if (min_class_size <= 0)
45  min_class_size = 17;
46  if (min_class_size < 2)
47  min_class_size = 2;
48 
49  if (iterations <= 0)
50  iterations = 20;
51  if (convergence <= 0.0)
52  convergence = 98.0;
53  if (separation < 0.0)
54  separation = 0.5;
55 
56 
57  /* allocate memory */
59  return -1;
60 
61 
62  /* generate class means */
63  I_cluster_means(C);
64  if (checkpoint)
65  (*checkpoint) (C, 1);
66 
67  /* now assign points to nearest class */
68  I_cluster_assign(C, interrupted);
69  if (*interrupted)
70  return -2;
71  I_cluster_sum2(C);
72  if (checkpoint)
73  (*checkpoint) (C, 2);
74 
75  /* get rid of empty classes now */
76  I_cluster_reclass(C, 1);
77 
78  for (C->iteration = 1;; C->iteration++) {
79  if (*interrupted)
80  return -2;
81 
82  changes = 0;
83 
84  /* re-assign points to nearest class */
85 
86  changes = I_cluster_reassign(C, interrupted);
87  if (*interrupted)
88  return -2;
89 
90  /* if too many points have changed class, re-assign points */
91  C->percent_stable = (C->npoints - changes) * 100.0;
92  C->percent_stable /= (double)C->npoints;
93 
94  if (checkpoint)
95  (*checkpoint) (C, 3);
96 
97  if (C->iteration >= iterations)
98  break;
99 
100  if (C->percent_stable < convergence)
101  continue;
102 
103  /* otherwise merge non-distinct classes */
104 
105  if (I_cluster_distinct(C, separation))
106  break;
107 
108  if (checkpoint)
109  (*checkpoint) (C, 4);
110 
111  I_cluster_merge(C);
112  }
113 
114  /* get rid of small classes */
115  I_cluster_reclass(C, min_class_size);
116  I_cluster_sum2(C);
117 
118  /* compute the resulting signatures */
120 
121 
122  return 0;
123 }
int I_cluster_merge(struct Cluster *C)
Definition: c_merge.c:3
int I_cluster_exec_allocate(struct Cluster *C)
Definition: c_execmem.c:3
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:23
int I_cluster_means(struct Cluster *C)
Definition: c_means.c:4
#define C
Definition: intr_char.c:17
int I_cluster_assign(struct Cluster *C, int *interrupted)
Definition: c_assign.c:4
int I_cluster_sum2(struct Cluster *C)
Definition: c_sum2.c:4
int I_cluster_reclass(struct Cluster *C, int minsize)
Definition: c_reclass.c:3
if(!YY_CURRENT_BUFFER)
Definition: lex.yy.c:799
int I_cluster_distinct(struct Cluster *C, double separation)
Definition: c_distinct.c:3
int I_cluster_reassign(struct Cluster *C, int *interrupted)
Definition: c_reassign.c:4
int I_cluster_signatures(struct Cluster *C)
Definition: c_sig.c:2