PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ kmeans()

static int kmeans ( POINT2D **  objs,
int *  clusters,
uint32_t  n,
POINT2D **  centers,
uint32_t  k 
)
static

Definition at line 97 of file lwkmeans.c.

98 {
99  uint32_t i = 0;
100  int* clusters_last;
101  int converged = LW_FALSE;
102  size_t clusters_sz = sizeof(int) * n;
103  uint32_t* weights;
104 
105  weights = lwalloc(sizeof(int) * k);
106 
107  /* previous cluster state array */
108  clusters_last = lwalloc(clusters_sz);
109 
110  for (i = 0; i < KMEANS_MAX_ITERATIONS && !converged; i++)
111  {
112  LW_ON_INTERRUPT(break);
113 
114  /* store the previous state of the clustering */
115  memcpy(clusters_last, clusters, clusters_sz);
116 
117  update_r(objs, clusters, n, centers, k);
118  update_means(objs, clusters, n, centers, weights, k);
119 
120  /* if all the cluster numbers are unchanged, we are at a stable solution */
121  converged = memcmp(clusters_last, clusters, clusters_sz) == 0;
122  }
123 
124  lwfree(clusters_last);
125  lwfree(weights);
126  if (!converged)
127  lwerror("%s did not converge after %d iterations", __func__, i);
128  return converged;
129 }
#define LW_FALSE
Definition: liblwgeom.h:77
void lwfree(void *mem)
Definition: lwutil.c:244
void * lwalloc(size_t size)
Definition: lwutil.c:229
#define LW_ON_INTERRUPT(x)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
#define KMEANS_MAX_ITERATIONS
Definition: lwkmeans.c:23
static void update_r(POINT2D **objs, int *clusters, uint32_t n, POINT2D **centers, uint32_t k)
Definition: lwkmeans.c:26
static void update_means(POINT2D **objs, int *clusters, uint32_t n, POINT2D **centers, uint32_t *weights, uint32_t k)
Definition: lwkmeans.c:65
unsigned int uint32_t
Definition: uthash.h:78

References KMEANS_MAX_ITERATIONS, LW_FALSE, LW_ON_INTERRUPT, lwalloc(), lwerror(), lwfree(), update_means(), and update_r().

Referenced by lwgeom_cluster_2d_kmeans().

Here is the call graph for this function:
Here is the caller graph for this function: