PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ kmeans()

static uint32_t kmeans ( POINT4D objs,
uint32_t *  clusters,
uint32_t  n,
POINT4D centers,
double *  radii,
uint32_t  min_k,
double  max_radius 
)
static

Definition at line 276 of file lwkmeans.c.

283{
284 uint8_t converged = LW_FALSE;
285 uint32_t cur_k = min_k;
286
287 kmeans_init(objs, n, centers, cur_k);
288 /* One iteration of kmeans needs to happen without shortcuts to fully initialize structures */
289 update_r(objs, clusters, n, centers, radii, cur_k);
290 update_means(objs, clusters, n, centers, cur_k);
291 for (uint32_t t = 0; t < KMEANS_MAX_ITERATIONS; t++)
292 {
293 /* Standard KMeans loop */
294 for (uint32_t i = 0; i < KMEANS_MAX_ITERATIONS; i++)
295 {
296 LW_ON_INTERRUPT(break);
297 converged = update_r(objs, clusters, n, centers, radii, cur_k);
298 if (converged)
299 break;
300 update_means(objs, clusters, n, centers, cur_k);
301 }
302 if (!converged || !max_radius)
303 break;
304
305 /* XMeans-inspired improve_structure pass to split clusters bigger than limit into 2 */
306 uint32_t new_k = improve_structure(objs, clusters, n, centers, radii, cur_k, max_radius);
307 if (new_k == cur_k)
308 break;
309 cur_k = new_k;
310 }
311
312 if (!converged)
313 {
314 lwerror("%s did not converge after %d iterations", __func__, KMEANS_MAX_ITERATIONS);
315 return 0;
316 }
317 return cur_k;
318}
#define LW_FALSE
Definition liblwgeom.h:94
#define LW_ON_INTERRUPT(x)
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
#define KMEANS_MAX_ITERATIONS
Definition lwkmeans.c:20
static void kmeans_init(POINT4D *objs, uint32_t n, POINT4D *centers, uint32_t k)
Definition lwkmeans.c:175
static uint8_t update_r(POINT4D *objs, uint32_t *clusters, uint32_t n, POINT4D *centers, double *radii, uint32_t k)
Definition lwkmeans.c:109
static void update_means(POINT4D *objs, uint32_t *clusters, uint32_t n, POINT4D *centers, uint32_t k)
Definition lwkmeans.c:149
static uint32_t improve_structure(POINT4D *objs, uint32_t *clusters, uint32_t n, POINT4D *centers, double *radii, uint32_t k, double max_radius)
Definition lwkmeans.c:42

References improve_structure(), kmeans_init(), KMEANS_MAX_ITERATIONS, LW_FALSE, LW_ON_INTERRUPT, lwerror(), update_means(), and update_r().

Referenced by improve_structure(), and lwgeom_cluster_kmeans().

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