aboutsummaryrefslogtreecommitdiff
path: root/km-find.c
diff options
context:
space:
mode:
Diffstat (limited to 'km-find.c')
-rw-r--r--km-find.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/km-find.c b/km-find.c
index 2dd1ba1..7ed9a6a 100644
--- a/km-find.c
+++ b/km-find.c
@@ -5,6 +5,14 @@
#define MIN_CLUSTER_DISTANCE 0.0001
+static float
+get_score(
+ const float mean_distance,
+ const size_t num_empty
+) {
+ return 1.0 / (mean_distance + num_empty);
+}
+
typedef struct {
float distance_sum,
variance_sum;
@@ -95,6 +103,7 @@ km_find(
return false;
}
+ float best_score = 0.0;
for (size_t i = 2; i < cbs->max_clusters; i++) {
for (size_t j = 0; j < cbs->num_tests; j++) {
// init cluster set
@@ -130,6 +139,20 @@ km_find(
// emit result
cbs->on_data(&result, cb_data);
+ // score result
+ float score = get_score(result.mean_distance, result.num_empty_clusters);
+
+ if (score > best_score) {
+ // emit new best result
+ if (cbs->on_best && !cbs->on_best(score, &cs, cb_data)) {
+ // return failure
+ return false;
+ }
+
+ // update best score
+ best_score = score;
+ }
+
// finalize cluster set
if (!cbs->on_fini(&cs, cb_data)) {
// return failure