You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.c 1.9KB Raw Blame History

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 ``````#include #include #include #include #include #include "benchmark-data.h" /* Fonctions */ double distance(const double (*u)[42], const double (*v)[42]); bool trouver_solution_initiale(void); void print_solution(int (*s)[230], const char *name); int solution[230] = {0}; int n_clusters = 5; int main(int argc, char *argv[]) { if (argc == 2) { int k = atoi(argv[1]); if (k > 0) n_clusters = k; } printf("Using %d clusters\n", n_clusters); srand(time(NULL)); /* Solution initiale */ while (!trouver_solution_initiale()) ; /* do it again */ print_solution(&solution, "initiale"); exit(EXIT_SUCCESS); } double distance(const double (*u)[42], const double (*v)[42]) { size_t n = sizeof(*v) / sizeof((*v)[0]); size_t i = 0; double sum = 0.0; printf("distance(): n = %zu\n", n); /* d(u, v) = sqrt( (u1 - v1)^2 + (u2 - v2)^2 + ... + (un - vn)^2 ) */ for (; i < n; i++) sum += pow((*u)[i] - (*v)[i], 2); return sqrt(sum); } bool trouver_solution_initiale(void) { /* This function returns true if the solution that it found is "valid", * i.e., it has at least one element in each cluster. */ bool *cluster_empty = calloc(n_clusters, sizeof *cluster_empty); bool ret = false; size_t i = 0; for (i = 0; i < (size_t) n_clusters; i++) cluster_empty[i] = true; for (i = 0; i < sizeof(solution) / sizeof(solution[0]); i++) cluster_empty[solution[i] = rand() % n_clusters] = false; for (i = 0; !ret && i < (size_t) n_clusters; ++i) ret = (ret || cluster_empty[i]); return !ret; } void print_solution(int (*s)[230], const char *name) { size_t i = 0, n = sizeof(*s) / sizeof((*s)[0]); printf("Solution%s%s:\n[", name ? " " : "", name ? name : ""); while (i < n) { const char *end; if (i + 1 == n) end = "]\n"; else if (i && i % 20 == 0) end = ",\n"; else end = ", "; printf("%d%s", (*s)[i++], end); } }``````