aboutsummaryrefslogtreecommitdiff
path: root/km-find.c
diff options
context:
space:
mode:
Diffstat (limited to 'km-find.c')
-rw-r--r--km-find.c49
1 files changed, 18 insertions, 31 deletions
diff --git a/km-find.c b/km-find.c
index 3716123..657c645 100644
--- a/km-find.c
+++ b/km-find.c
@@ -5,11 +5,6 @@
#define MIN_CLUSTER_DISTANCE 0.0001
-typedef struct {
- float sum,
- silouette;
-} find_solve_data_t;
-
static float
get_mean_cluster_size(
const km_set_t * const set
@@ -24,7 +19,7 @@ get_mean_cluster_size(
}
}
- return (num_filled > 0) ? sum / num_filled : 0;
+ return (num_filled > 0) ? (sum / num_filled) : 0;
}
static void
@@ -33,12 +28,12 @@ find_solve_on_stats(
const km_solve_stats_t * const stats,
void * const cb_data
) {
- find_solve_data_t * const solve_data = cb_data;
+ km_find_data_t * const find_data = cb_data;
UNUSED(set);
// save total sum and silouette
- solve_data->sum = stats->sum;
- solve_data->silouette = stats->silouette;
+ find_data->distance_sum = stats->sum;
+ find_data->silouette = stats->silouette;
}
static const km_solve_cbs_t
@@ -67,7 +62,7 @@ km_find(
return false;
}
- float best_score = 0.0;
+ float best_silouette = -2.0;
for (size_t i = 2; i < cbs->max_clusters; i++) {
for (size_t j = 0; j < cbs->num_tests; j++) {
// init cluster set
@@ -77,42 +72,34 @@ km_find(
return false;
}
- // init solve data
- find_solve_data_t solve_data = {
- .sum = 0,
- .silouette = 0,
+ // init find data
+ km_find_data_t find_data = {
+ .cluster_set = &cs,
+ .num_clusters = i,
};
// solve test
- if (!km_solve(&cs, set, &FIND_SOLVE_CBS, &solve_data)) {
+ // (populates sum and silouette)
+ if (!km_solve(&cs, set, &FIND_SOLVE_CBS, &find_data)) {
// return failure
return false;
}
- // init result data
- const km_find_data_t result = {
- .cluster_set = &cs,
- .num_clusters = i,
- .distance_sum = solve_data.sum,
- .silouette = solve_data.silouette,
- .mean_cluster_size = get_mean_cluster_size(&cs),
- };
+ // populate mean cluster size
+ find_data.mean_cluster_size = get_mean_cluster_size(&cs);
// emit result
- cbs->on_data(&result, cb_data);
-
- // score result
- const float score = solve_data.silouette;
+ cbs->on_data(&find_data, cb_data);
- if (score > best_score) {
+ if (find_data.silouette > best_silouette) {
// emit new best result
- if (cbs->on_best && !cbs->on_best(score, &cs, cb_data)) {
+ if (cbs->on_best && !cbs->on_best(find_data.silouette, &cs, cb_data)) {
// return failure
return false;
}
- // update best score
- best_score = score;
+ // update best silouette
+ best_silouette = find_data.silouette;
}
// finalize cluster set