PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ kmeans()

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

Definition at line 89 of file lwkmeans.c.

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

Referenced by lwgeom_cluster_2d_kmeans().

90 {
91  uint32_t i = 0;
92  int* clusters_last;
93  int converged = LW_FALSE;
94  size_t clusters_sz = sizeof(int) * n;
95  uint32_t* weights;
96 
97  weights = lwalloc(sizeof(int) * k);
98 
99  /*
100  * Previous cluster state array. At this time, r doesn't mean anything
101  * but it's ok
102  */
103  clusters_last = lwalloc(clusters_sz);
104 
105  for (i = 0; i < KMEANS_MAX_ITERATIONS && !converged; i++)
106  {
107  LW_ON_INTERRUPT(break);
108 
109  /* Store the previous state of the clustering */
110  memcpy(clusters_last, clusters, clusters_sz);
111 
112  update_r(objs, clusters, n, centers, k);
113  update_means(objs, clusters, n, centers, weights, k);
114 
115  /* if all the cluster numbers are unchanged, we are at a stable solution */
116  converged = memcmp(clusters_last, clusters, clusters_sz) == 0;
117  }
118 
119  lwfree(clusters_last);
120  lwfree(weights);
121  if (!converged)
122  lwerror("%s did not converge after %d iterations", __func__, i);
123  return converged;
124 }
void lwfree(void *mem)
Definition: lwutil.c:244
#define LW_ON_INTERRUPT(x)
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
#define LW_FALSE
Definition: liblwgeom.h:76
#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
void * lwalloc(size_t size)
Definition: lwutil.c:229
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
Here is the call graph for this function:
Here is the caller graph for this function: