diff options
| author | 2026-11-10 06:15:19 +0000 | |
|---|---|---|
| committer | 2026-11-10 06:15:19 +0000 | |
| commit | e575815702e42efe5ba9d2732a1c6ae5dc752585 (patch) | |
| tree | fa91e01ec90f7cd298c190c7fb38fe1bbec5903a /src/evolution-strategy.c | |
| download | lab-05-master.tar.gz | |
Diffstat (limited to 'src/evolution-strategy.c')
| -rw-r--r-- | src/evolution-strategy.c | 638 |
1 files changed, 638 insertions, 0 deletions
diff --git a/src/evolution-strategy.c b/src/evolution-strategy.c new file mode 100644 index 0000000..8a5042a --- /dev/null +++ b/src/evolution-strategy.c | |||
| @@ -0,0 +1,638 @@ | |||
| 1 | // evolution-strategy.c --- evolution strategy | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | #include <stdarg.h> | ||
| 7 | #include <math.h> | ||
| 8 | #include <time.h> | ||
| 9 | #include <float.h> | ||
| 10 | #include <string.h> | ||
| 11 | #include <assert.h> | ||
| 12 | |||
| 13 | #define POPULATION_CAPACITY 360 | ||
| 14 | #define CHROMOSOME_SIZE 2 | ||
| 15 | #define GENERATION_MAX 2000 | ||
| 16 | #define FITNESS_OPTIMAL -1 | ||
| 17 | #define FITNESS_TRESHOLD_EPSILON 1e-6 | ||
| 18 | #define MINMAX -1 | ||
| 19 | #define X_MIN ((float) -100) | ||
| 20 | #define X_MAX ((float) +100) | ||
| 21 | #define Y_MIN X_MIN | ||
| 22 | #define Y_MAX X_MAX | ||
| 23 | #define SIGMA_INITIAL 2.0 | ||
| 24 | #define TAU (1/sqrtf (2*sqrtf (CHROMOSOME_SIZE))) | ||
| 25 | |||
| 26 | #define Z_MIN ((float) -1.0) | ||
| 27 | #define Z_MAX ((float) +0.5) | ||
| 28 | #define X_MIN_ZOOM ((float) M_PI - 2) | ||
| 29 | #define X_MAX_ZOOM ((float) M_PI + 2) | ||
| 30 | #define Y_MIN_ZOOM X_MIN_ZOOM | ||
| 31 | #define Y_MAX_ZOOM X_MAX_ZOOM | ||
| 32 | #define PLOT_SAMPLE_COUNT 100 | ||
| 33 | |||
| 34 | // #define SEED time (NULL) | ||
| 35 | #define SEED 1762730873 | ||
| 36 | |||
| 37 | #define MEASUREMENT_RUNS 100 | ||
| 38 | |||
| 39 | struct population_size { int mu, lambda; } population_sizes [] = { | ||
| 40 | { 10, 70 }, | ||
| 41 | { 20, 70 }, | ||
| 42 | { 20, 140 }, | ||
| 43 | { 40, 140 }, | ||
| 44 | { 40, 280 }, | ||
| 45 | { 80, 280 }, | ||
| 46 | }; | ||
| 47 | |||
| 48 | float initial_sigmas [] = { | ||
| 49 | 0.1, | ||
| 50 | 0.2, | ||
| 51 | 0.4, | ||
| 52 | 0.8, | ||
| 53 | 1.0, | ||
| 54 | 2.0, | ||
| 55 | 4.0, | ||
| 56 | 8.0, | ||
| 57 | 15.0, | ||
| 58 | 30.0, | ||
| 59 | 50.0, | ||
| 60 | 100.0, | ||
| 61 | }; | ||
| 62 | |||
| 63 | struct { | ||
| 64 | float min; | ||
| 65 | float max; | ||
| 66 | } variable_limits[CHROMOSOME_SIZE] = { | ||
| 67 | { X_MIN, X_MAX }, | ||
| 68 | { Y_MIN, Y_MAX }, | ||
| 69 | }; | ||
| 70 | |||
| 71 | #define ASSERT assert | ||
| 72 | #define CTZL __builtin_ctzl | ||
| 73 | #define EPSILON 1e-12 | ||
| 74 | |||
| 75 | #define PRINT_EXP(FMT, ...) printf (#__VA_ARGS__" = "FMT"\n", __VA_ARGS__) | ||
| 76 | |||
| 77 | #define ARRAY_LENGTH(T) (sizeof (T)/sizeof (*(T))) | ||
| 78 | #define FOR_EACH_ELEMENT(ELEMENT, ARRAY) \ | ||
| 79 | for (typeof (*(ARRAY)) *(ELEMENT) = (ARRAY); \ | ||
| 80 | (ELEMENT) < (ARRAY) + ARRAY_LENGTH (ARRAY); ++(ELEMENT)) \ | ||
| 81 | |||
| 82 | struct gene { | ||
| 83 | float value; | ||
| 84 | float sigma; | ||
| 85 | }; | ||
| 86 | |||
| 87 | struct chromosome { | ||
| 88 | float fitness; | ||
| 89 | struct gene genes[CHROMOSOME_SIZE]; | ||
| 90 | }; | ||
| 91 | |||
| 92 | struct measurement { | ||
| 93 | float fitness; | ||
| 94 | float time; | ||
| 95 | int generations; | ||
| 96 | }; | ||
| 97 | |||
| 98 | struct chromosome population [POPULATION_CAPACITY]; | ||
| 99 | struct chromosome generation_bests[GENERATION_MAX + 1]; | ||
| 100 | float generation_average_fitness[GENERATION_MAX + 1]; | ||
| 101 | |||
| 102 | float plot_samples [PLOT_SAMPLE_COUNT][PLOT_SAMPLE_COUNT][3]; | ||
| 103 | float plot_samples_zoom[PLOT_SAMPLE_COUNT][PLOT_SAMPLE_COUNT][3]; | ||
| 104 | struct measurement measurements[ARRAY_LENGTH (initial_sigmas )] | ||
| 105 | [ARRAY_LENGTH (population_sizes)]; | ||
| 106 | |||
| 107 | float | ||
| 108 | easom (float x1, float x2) | ||
| 109 | { | ||
| 110 | return -(cosf (x1)*cosf (x2)*expf (-((x1 - M_PI)*(x1 - M_PI) + | ||
| 111 | (x2 - M_PI)*(x2 - M_PI)))); | ||
| 112 | } | ||
| 113 | |||
| 114 | float | ||
| 115 | fitness_function (float *values, size_t stride) | ||
| 116 | { | ||
| 117 | float x1 = *values; values += stride / sizeof(*values); | ||
| 118 | float x2 = *values; | ||
| 119 | return easom (x1, x2); | ||
| 120 | } | ||
| 121 | |||
| 122 | float | ||
| 123 | rand_normal (float mean, float stddev) { | ||
| 124 | float u1 = ((float) rand () + 1)/((float) RAND_MAX + 1); | ||
| 125 | float u2 = ((float) rand () + 1)/((float) RAND_MAX + 1); | ||
| 126 | float z0 = sqrtf (-2.0*logf (u1))*cosf (2*M_PI*u2); | ||
| 127 | return mean + z0*stddev; | ||
| 128 | } | ||
| 129 | |||
| 130 | float | ||
| 131 | rand_from_range (float min, float max) | ||
| 132 | { | ||
| 133 | return min + (max - min)*(rand () /(float) RAND_MAX); | ||
| 134 | } | ||
| 135 | |||
| 136 | void | ||
| 137 | create_initial_population (int population_size, float sigma_initial) | ||
| 138 | { | ||
| 139 | ASSERT (0 < population_size && population_size <= POPULATION_CAPACITY); | ||
| 140 | |||
| 141 | for (int i = 0; i < population_size; ++i) { | ||
| 142 | for (int j = 0; j < CHROMOSOME_SIZE; ++j) { | ||
| 143 | population[i].genes[j].value = rand_from_range (variable_limits[j].min, | ||
| 144 | variable_limits[j].max); | ||
| 145 | population[i].genes[j].sigma = sigma_initial; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | void | ||
| 151 | measure_fitness (int from, int to) | ||
| 152 | { | ||
| 153 | ASSERT (0 <= from && from <= POPULATION_CAPACITY); | ||
| 154 | ASSERT (0 <= to && to <= POPULATION_CAPACITY); | ||
| 155 | ASSERT (from <= to); | ||
| 156 | |||
| 157 | for (int i = from; i < to; ++i) { | ||
| 158 | population[i].fitness = fitness_function (&population[i].genes[0].value, | ||
| 159 | sizeof (*population[i].genes)); | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 163 | bool | ||
| 164 | should_stop (int generation) | ||
| 165 | { | ||
| 166 | if (generation >= GENERATION_MAX) | ||
| 167 | return true; | ||
| 168 | |||
| 169 | if (fabs (generation_bests[generation].fitness - FITNESS_OPTIMAL) < FITNESS_TRESHOLD_EPSILON) | ||
| 170 | return true; | ||
| 171 | |||
| 172 | return false; | ||
| 173 | } | ||
| 174 | |||
| 175 | void | ||
| 176 | crossover_oprator ( struct chromosome *to, | ||
| 177 | const struct chromosome *from_a, | ||
| 178 | const struct chromosome *from_b) | ||
| 179 | { | ||
| 180 | for (int k = 0; k < CHROMOSOME_SIZE; ++k) { | ||
| 181 | const struct chromosome *from = rand () % 2 ? from_a : from_b; | ||
| 182 | to->genes[k].value = from->genes[k].value; | ||
| 183 | to->genes[k].sigma = from->genes[k].sigma; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | void | ||
| 188 | crossover (int mu, int lambda) | ||
| 189 | { | ||
| 190 | ASSERT (mu > 0); | ||
| 191 | ASSERT (lambda > 0); | ||
| 192 | ASSERT (mu + lambda <= POPULATION_CAPACITY); | ||
| 193 | |||
| 194 | for (int i = 0; i < lambda; ++i) { | ||
| 195 | crossover_oprator (&population[mu + i], | ||
| 196 | &population[rand () % mu], | ||
| 197 | &population[rand () % mu]); | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | float | ||
| 202 | clamp (float value, float min, float max) | ||
| 203 | { | ||
| 204 | if (value < min) return min; | ||
| 205 | if (value > max) return max; | ||
| 206 | return value; | ||
| 207 | } | ||
| 208 | |||
| 209 | void | ||
| 210 | mutation_operator (struct chromosome *chromosome) | ||
| 211 | { | ||
| 212 | for (int k = 0; k < CHROMOSOME_SIZE; ++k) { | ||
| 213 | struct gene *gene = &chromosome->genes[k]; | ||
| 214 | gene->sigma *= exp (TAU*rand_normal (0, 1)); | ||
| 215 | gene->value = clamp (gene->value + rand_normal (0, gene->sigma), | ||
| 216 | variable_limits[k].min, variable_limits[k].max); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | void | ||
| 221 | mutation (int mu, int lambda, float probability) | ||
| 222 | { | ||
| 223 | for (int i = 0; i < lambda; ++i) { | ||
| 224 | if (rand () /(float) RAND_MAX > probability) continue; | ||
| 225 | mutation_operator (&population[mu + i]); | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | void | ||
| 230 | plot (const char *output, int population_size, int generation, | ||
| 231 | float x_min, float x_max, float y_min, float y_max, | ||
| 232 | float samples[PLOT_SAMPLE_COUNT][PLOT_SAMPLE_COUNT][3]) | ||
| 233 | { | ||
| 234 | const char *ext = "tex"; | ||
| 235 | FILE *gp = popen ("gnuplot", "w"); | ||
| 236 | // FILE *gp = fopen ("test.gp", "w"); | ||
| 237 | // fprintf (gp, "set term pngcairo\n"); | ||
| 238 | fprintf (gp, "set term cairolatex png input blacktext notransparent" | ||
| 239 | " nocrop size 9cm,9cm noheader\n"); | ||
| 240 | fprintf (gp, "set output '%s.%s'\n", output, ext); | ||
| 241 | |||
| 242 | fprintf (gp, "set lmargin at screen 0.30\n"); | ||
| 243 | fprintf (gp, "set rmargin at screen 0.83\n"); | ||
| 244 | fprintf (gp, "set bmargin at screen 0.05\n"); | ||
| 245 | fprintf (gp, "set tmargin at screen 1.00\n"); | ||
| 246 | |||
| 247 | fprintf (gp, "set xlabel '\\(x_1\\)'\n"); | ||
| 248 | fprintf (gp, "set ylabel '\\(x_2\\)'\n"); | ||
| 249 | fprintf (gp, "set zlabel '\\(f(x_1, x_2)\\)' offset graph -0.1, 0 rotate by 90\n"); | ||
| 250 | fprintf (gp, "set xrange [%f:%f]\n", x_min, x_max); | ||
| 251 | fprintf (gp, "set yrange [%f:%f]\n", y_min, y_max); | ||
| 252 | fprintf (gp, "set zrange [%f:%f]\n", Z_MIN, Z_MAX); | ||
| 253 | fprintf (gp, "set xtics offset graph -0.05, graph -0.10\n"); | ||
| 254 | fprintf (gp, "set ytics offset graph +0.05, graph +0.05\n"); | ||
| 255 | fprintf (gp, "set ztics offset graph +0.02, graph +0.05\n"); | ||
| 256 | fprintf (gp, "unset key\n"); | ||
| 257 | |||
| 258 | fprintf (gp, "set pm3d at bs interpolate 0,0 explicit\n"); | ||
| 259 | fprintf (gp, "set ticslevel 0\n"); | ||
| 260 | |||
| 261 | fprintf (gp, "set view equal xy\n"); | ||
| 262 | fprintf (gp, "unset colorbox\n"); | ||
| 263 | fprintf (gp, "splot "); | ||
| 264 | fprintf (gp, "'-' smooth acsplines with pm3d title '%s', ", "\\small{Целевая функция}"); | ||
| 265 | if (generation > 0) | ||
| 266 | fprintf (gp, "'-' with points pt 3 ps 1 lc 'yellow' title '\\small{Лучшие особи}',"); | ||
| 267 | fprintf (gp, "'-' with points pt 3 ps 1.2 lc 'magenta' title '\\small{Минимум}',"); | ||
| 268 | fprintf (gp, "'-' with points pt 7 ps 1 lc 'green' title '\\small{Лучшая особь поколения}',"); | ||
| 269 | fprintf (gp, "'-' with points pt 7 ps 0.5 lc 'red' title '\\small{Популяция}'\n"); | ||
| 270 | |||
| 271 | for (int x = 0; x < PLOT_SAMPLE_COUNT; ++x) { | ||
| 272 | for (int y = 0; y < PLOT_SAMPLE_COUNT; ++y) { | ||
| 273 | fprintf (gp, "%f %f %f\n", | ||
| 274 | samples[x][y][0], | ||
| 275 | samples[x][y][1], | ||
| 276 | samples[x][y][2]); | ||
| 277 | } fprintf (gp, "\n"); | ||
| 278 | } fprintf (gp, "e\n"); | ||
| 279 | |||
| 280 | if (generation > 0) { | ||
| 281 | for (int i = 0; i < generation; ++i) { | ||
| 282 | for (int j = 0; j < CHROMOSOME_SIZE; ++j) | ||
| 283 | fprintf (gp, "%f ", generation_bests[i].genes[j].value); | ||
| 284 | fprintf (gp, "%f\n", generation_bests[i].fitness); | ||
| 285 | } fprintf (gp, "e\n"); | ||
| 286 | } | ||
| 287 | |||
| 288 | fprintf (gp, "%f %f %f\ne\n", M_PI, M_PI, -1.); | ||
| 289 | |||
| 290 | for (int j = 0; j < CHROMOSOME_SIZE; ++j) | ||
| 291 | fprintf (gp, "%f ", generation_bests[generation].genes[j].value); | ||
| 292 | fprintf (gp, "%f\ne\n", generation_bests[generation].fitness); | ||
| 293 | |||
| 294 | for (int i = 0; i < population_size; ++i) { | ||
| 295 | for (int j = 0; j < CHROMOSOME_SIZE; ++j) | ||
| 296 | fprintf (gp, "%f ", population[i].genes[j].value); | ||
| 297 | fprintf (gp, "%f\n", population[i].fitness); | ||
| 298 | } fprintf (gp, "e\n"); | ||
| 299 | |||
| 300 | fprintf (gp, "set term cairolatex png input blacktext notransparent" | ||
| 301 | " nocrop size 7cm,7cm noheader\n"); | ||
| 302 | fprintf (gp, "set output '%s-map.%s'\n", output, ext); | ||
| 303 | fprintf (gp, "set lmargin at screen 0.15\n"); | ||
| 304 | fprintf (gp, "set rmargin at screen 0.85\n"); | ||
| 305 | fprintf (gp, "set bmargin at screen 0.15\n"); | ||
| 306 | fprintf (gp, "set tmargin at screen 0.90\n"); | ||
| 307 | fprintf (gp, "set xtics offset graph 0, graph +0.02\n"); | ||
| 308 | fprintf (gp, "set ytics nooffset\n"); | ||
| 309 | fprintf (gp, "set xlabel offset graph 0, graph +0.05\n"); | ||
| 310 | // fprintf (gp, "set title offset 0,-1\n"); | ||
| 311 | fprintf (gp, "set view map\n"); | ||
| 312 | fprintf (gp, "unset key\n"); | ||
| 313 | fprintf (gp, "set colorbox\n"); | ||
| 314 | fprintf (gp, "refresh\n"); | ||
| 315 | |||
| 316 | fclose (gp); | ||
| 317 | } | ||
| 318 | |||
| 319 | int | ||
| 320 | chromosome__compare_fitness (const void *lhs, const void *rhs) | ||
| 321 | { | ||
| 322 | const struct chromosome *lhs_chromosome = lhs; | ||
| 323 | const struct chromosome *rhs_chromosome = rhs; | ||
| 324 | |||
| 325 | float diff = MINMAX*(rhs_chromosome->fitness - lhs_chromosome->fitness); | ||
| 326 | if (diff < 0) return -1; | ||
| 327 | if (diff > 0) return 1; | ||
| 328 | return 0; | ||
| 329 | } | ||
| 330 | |||
| 331 | void | ||
| 332 | plot__fitness_over_generations (const char *output, int generations) | ||
| 333 | { | ||
| 334 | FILE *gp = popen ("gnuplot", "w"); | ||
| 335 | // FILE *gp = fopen ("test.gp", "w"); | ||
| 336 | |||
| 337 | // fprintf (gp, "set term pngcairo\n"); | ||
| 338 | // fprintf (gp, "set output '%s.png'\n", output); | ||
| 339 | |||
| 340 | fprintf (gp, "set term cairolatex pdf input blacktext notransparent" | ||
| 341 | " nocrop size 15cm,10cm noheader\n"); | ||
| 342 | fprintf (gp, "set output '%s.tex'\n", output); | ||
| 343 | |||
| 344 | fprintf (gp, "set xrange [0:%d]\n", generations); | ||
| 345 | fprintf (gp, "set yrange [-1.1:0.1]\n"); | ||
| 346 | fprintf (gp, "set xlabel '№ поколения'\n"); | ||
| 347 | fprintf (gp, "set ylabel 'Значение фитнесс-функции' offset graph -0.02,0\n"); | ||
| 348 | |||
| 349 | fprintf (gp, "plot "); | ||
| 350 | fprintf (gp, "'-' with lines title 'Лучшее значение',"); | ||
| 351 | fprintf (gp, "'-' with lines title 'Среднее значение',"); | ||
| 352 | fprintf (gp, "-1 dt 2 title 'Оптимальное значение'\n"); | ||
| 353 | |||
| 354 | for (int i = 0; i <= generations; ++i) { | ||
| 355 | fprintf (gp, "%d %f\n", i, generation_bests[i].fitness); | ||
| 356 | } fprintf (gp, "e\n"); | ||
| 357 | |||
| 358 | for (int i = 0; i <= generations; ++i) { | ||
| 359 | fprintf (gp, "%d %f\n", i, generation_average_fitness[i]); | ||
| 360 | } fprintf (gp, "e\n"); | ||
| 361 | |||
| 362 | fclose (gp); | ||
| 363 | } | ||
| 364 | |||
| 365 | void | ||
| 366 | run_experiment__plot (int mu, int lambda, float probability_mutation) | ||
| 367 | { | ||
| 368 | void | ||
| 369 | plot_generation (int generation) | ||
| 370 | { | ||
| 371 | static char plot_output[256]; | ||
| 372 | |||
| 373 | snprintf (plot_output, sizeof (plot_output), "res/mu%d-lambda%d-mp%4.2f-g%04d", | ||
| 374 | mu, lambda, probability_mutation, generation); | ||
| 375 | plot (plot_output, mu, generation, | ||
| 376 | X_MIN, X_MAX, Y_MIN, Y_MAX, plot_samples); | ||
| 377 | |||
| 378 | snprintf (plot_output, sizeof (plot_output), "res/mu%d-lambda%d-mp%4.2f-g%04d-zoom", | ||
| 379 | mu, lambda, probability_mutation, generation); | ||
| 380 | plot (plot_output, mu, generation, | ||
| 381 | X_MIN_ZOOM, X_MAX_ZOOM, Y_MIN_ZOOM, Y_MAX_ZOOM, plot_samples_zoom); | ||
| 382 | } | ||
| 383 | |||
| 384 | create_initial_population (mu, SIGMA_INITIAL); | ||
| 385 | measure_fitness (0, mu); | ||
| 386 | int min = 0; | ||
| 387 | for (int i = 1; i < mu; ++i) { | ||
| 388 | if (population[i].fitness < population[min].fitness) | ||
| 389 | min = i; | ||
| 390 | } memcpy (&generation_bests[0], &population[min], sizeof (struct chromosome)); | ||
| 391 | int generation; | ||
| 392 | for (generation = 0; generation < GENERATION_MAX; ++generation) { | ||
| 393 | bool stop = should_stop (generation); | ||
| 394 | |||
| 395 | float sum = 0; | ||
| 396 | for (int i = 1; i < mu; ++i) { | ||
| 397 | sum += population[i].fitness; | ||
| 398 | } generation_average_fitness[generation] = sum/mu; | ||
| 399 | |||
| 400 | printf ("generation: %4d, fitness: %g, ", | ||
| 401 | generation, generation_bests[generation].fitness); | ||
| 402 | printf ("chromosome:"); | ||
| 403 | for (int i = 0; i < CHROMOSOME_SIZE; ++i) { | ||
| 404 | printf (" %g/%g", | ||
| 405 | generation_bests[generation].genes[i].value, | ||
| 406 | generation_bests[generation].genes[i].sigma); | ||
| 407 | } printf ("\n"); | ||
| 408 | // plot_generation (generation); | ||
| 409 | |||
| 410 | if (stop) break; | ||
| 411 | crossover (mu, lambda); | ||
| 412 | mutation (mu, lambda, probability_mutation); | ||
| 413 | measure_fitness (mu, mu + lambda); | ||
| 414 | qsort (population, mu + lambda, sizeof (struct chromosome), | ||
| 415 | chromosome__compare_fitness); | ||
| 416 | |||
| 417 | memcpy (&generation_bests[generation + 1], &population[0], | ||
| 418 | sizeof (struct chromosome)); | ||
| 419 | } | ||
| 420 | |||
| 421 | plot__fitness_over_generations ("res/convergence", generation); | ||
| 422 | } | ||
| 423 | |||
| 424 | struct measurement | ||
| 425 | run_experiment__measure (int mu, int lambda, float initial_sigma) | ||
| 426 | { | ||
| 427 | float total_fitness = 0; | ||
| 428 | float total_time = 0; | ||
| 429 | int total_generations = 0; | ||
| 430 | for (int run = 0; run < MEASUREMENT_RUNS; ++run) { | ||
| 431 | clock_t begin = clock (); | ||
| 432 | create_initial_population (mu, initial_sigma); | ||
| 433 | int generation = 0; | ||
| 434 | measure_fitness (0, mu); | ||
| 435 | int min = 0; | ||
| 436 | for (int i = 1; i < mu; ++i) { | ||
| 437 | if (population[i].fitness < population[min].fitness) | ||
| 438 | min = i; | ||
| 439 | } memcpy (&generation_bests[generation], &population[min], | ||
| 440 | sizeof (struct chromosome)); | ||
| 441 | while (!should_stop (generation++)) { | ||
| 442 | crossover (mu, lambda); | ||
| 443 | mutation (mu, lambda, 1); | ||
| 444 | measure_fitness (mu, mu + lambda); | ||
| 445 | qsort (population, mu + lambda, sizeof (struct chromosome), | ||
| 446 | chromosome__compare_fitness); | ||
| 447 | memcpy (&generation_bests[generation], &population[0], | ||
| 448 | sizeof (struct chromosome)); | ||
| 449 | } | ||
| 450 | |||
| 451 | float time = (clock () - begin) /(float) CLOCKS_PER_SEC; | ||
| 452 | total_time += time; | ||
| 453 | total_generations += generation; | ||
| 454 | total_fitness += generation_bests[generation - (generation == GENERATION_MAX)].fitness; | ||
| 455 | } | ||
| 456 | |||
| 457 | return (struct measurement) { | ||
| 458 | .fitness = total_fitness/MEASUREMENT_RUNS, | ||
| 459 | .time = total_time/MEASUREMENT_RUNS, | ||
| 460 | .generations = (total_generations + MEASUREMENT_RUNS/2)/MEASUREMENT_RUNS, | ||
| 461 | }; | ||
| 462 | } | ||
| 463 | |||
| 464 | void | ||
| 465 | sample_fitness_function (float x_min, float x_max, | ||
| 466 | float y_min, float y_max, | ||
| 467 | float samples[PLOT_SAMPLE_COUNT][PLOT_SAMPLE_COUNT][3]) | ||
| 468 | { | ||
| 469 | float x = x_min; | ||
| 470 | float dx = (x_max - x_min)/(PLOT_SAMPLE_COUNT - 1); | ||
| 471 | for (int i = 0; i < PLOT_SAMPLE_COUNT; ++i) { | ||
| 472 | float y = y_min; | ||
| 473 | float dy = (y_max - y_min)/(PLOT_SAMPLE_COUNT - 1); | ||
| 474 | for (int j = 0; j < PLOT_SAMPLE_COUNT; ++j) { | ||
| 475 | samples[i][j][0] = x; | ||
| 476 | samples[i][j][1] = y; | ||
| 477 | samples[i][j][2] = fitness_function ((float *) samples[i][j], sizeof (float)); | ||
| 478 | y += dy; | ||
| 479 | } x += dx; | ||
| 480 | } | ||
| 481 | } | ||
| 482 | |||
| 483 | typedef int (*print_function)(void *user, const char *template, ...); | ||
| 484 | typedef int (*cell_function)(int x, int y, print_function, void *); | ||
| 485 | |||
| 486 | int | ||
| 487 | print__measure (void *user, const char *template, ...) | ||
| 488 | { | ||
| 489 | va_list ap; | ||
| 490 | va_start (ap, template); | ||
| 491 | int len = vsnprintf (NULL, 0, template, ap); | ||
| 492 | va_end (ap); | ||
| 493 | return len; | ||
| 494 | } | ||
| 495 | |||
| 496 | int | ||
| 497 | print__stream (void *stream, const char *template, ...) | ||
| 498 | { | ||
| 499 | va_list ap; | ||
| 500 | va_start (ap, template); | ||
| 501 | int len = vfprintf (stream, template, ap); | ||
| 502 | va_end (ap); | ||
| 503 | return len; | ||
| 504 | } | ||
| 505 | |||
| 506 | void | ||
| 507 | print_org_table (const char *filename, const char *name, const char *caption, | ||
| 508 | const char *align, int rows, int cols, cell_function cell) | ||
| 509 | { | ||
| 510 | int cell_widths[rows][cols]; | ||
| 511 | int column_widths[cols]; | ||
| 512 | memset (column_widths, 0, sizeof (column_widths)); | ||
| 513 | for (int y = 0; y < rows; ++y) { | ||
| 514 | for (int x = 0; x < cols; ++x) { | ||
| 515 | cell_widths[y][x] = cell (x, y, print__measure, NULL); | ||
| 516 | if (cell_widths[y][x] > column_widths[x]) | ||
| 517 | column_widths[x] = cell_widths[y][x];; | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 521 | FILE *stream = fopen (filename, "w"); | ||
| 522 | if (name ) fprintf (stream, "#+name: %s\n", name); | ||
| 523 | if (caption) fprintf (stream, "#+caption: %s\n", caption); | ||
| 524 | if (align ) fprintf (stream, "#+attr_latex: :align %s\n", align); | ||
| 525 | for (int y = 0; y < rows; ++y) { | ||
| 526 | fprintf (stream, "|"); | ||
| 527 | for (int x = 0; x < cols; ++x) { | ||
| 528 | fprintf (stream, " "); | ||
| 529 | if (x == 0) { | ||
| 530 | for (int i = 0; i < column_widths[x] - cell_widths[y][x]; ++i) | ||
| 531 | fprintf (stream, " "); | ||
| 532 | } | ||
| 533 | cell (x, y, print__stream, stream); | ||
| 534 | if (x > 0) { | ||
| 535 | for (int i = 0; i < column_widths[x] - cell_widths[y][x]; ++i) | ||
| 536 | fprintf (stream, " "); | ||
| 537 | } | ||
| 538 | fprintf (stream, " |"); | ||
| 539 | } fprintf (stream, "\n"); | ||
| 540 | if (y == 0) { | ||
| 541 | fprintf (stream, "|"); | ||
| 542 | for (int x = 0; x < cols; ++x) { | ||
| 543 | for (int i = 0; i < column_widths[x] + 2; ++i) | ||
| 544 | fprintf (stream, "-"); | ||
| 545 | fprintf (stream, "|"); | ||
| 546 | } fprintf (stream, "\n"); | ||
| 547 | } | ||
| 548 | } | ||
| 549 | |||
| 550 | fclose (stream); | ||
| 551 | } | ||
| 552 | |||
| 553 | struct measurement *best_measurement; | ||
| 554 | int | ||
| 555 | table_cell (int x, int y, print_function print, void *user) | ||
| 556 | { | ||
| 557 | int p = x - 1; | ||
| 558 | int s = y - 1; | ||
| 559 | if (x == 0 && y == 0) return print (user, "\\(\\sigma^0 \\;\\backslash\\; \\mu/\\lambda\\)"); | ||
| 560 | if ( y == 0) return print (user, "%d/%d", population_sizes[p].mu, population_sizes[p].lambda); | ||
| 561 | if (x == 0 ) return print (user, "%.2g", initial_sigmas[s]); | ||
| 562 | |||
| 563 | int len = 0; | ||
| 564 | bool is_best = &measurements[s][p] == best_measurement; | ||
| 565 | if (is_best) print (user, "*"); | ||
| 566 | len += print (user, "\\makecell{%.2f (%lu) \\\\ %f}", | ||
| 567 | measurements[s][p].time*1e+3, | ||
| 568 | measurements[s][p].generations, | ||
| 569 | measurements[s][p].fitness); | ||
| 570 | if (is_best) print (user, "*"); | ||
| 571 | |||
| 572 | return len; | ||
| 573 | } | ||
| 574 | |||
| 575 | int | ||
| 576 | main (int argc, char *argv[]) | ||
| 577 | { | ||
| 578 | PRINT_EXP ("%d", POPULATION_CAPACITY); | ||
| 579 | PRINT_EXP ("%d", CHROMOSOME_SIZE); | ||
| 580 | PRINT_EXP ("%d", GENERATION_MAX); | ||
| 581 | PRINT_EXP ("%f", FITNESS_TRESHOLD_EPSILON); | ||
| 582 | PRINT_EXP ("%+f", MINMAX); | ||
| 583 | PRINT_EXP ("%+g", X_MIN); | ||
| 584 | PRINT_EXP ("%+g", X_MAX); | ||
| 585 | PRINT_EXP ("%+g", Y_MIN); | ||
| 586 | PRINT_EXP ("%+g", Y_MAX); | ||
| 587 | PRINT_EXP ("%d", SIGMA_INITIAL); | ||
| 588 | PRINT_EXP ("%+f", Z_MIN); | ||
| 589 | PRINT_EXP ("%+f", Z_MAX); | ||
| 590 | PRINT_EXP ("%+f", X_MIN_ZOOM); | ||
| 591 | PRINT_EXP ("%+f", X_MAX_ZOOM); | ||
| 592 | PRINT_EXP ("%+f", Y_MIN_ZOOM); | ||
| 593 | PRINT_EXP ("%+f", Y_MAX_ZOOM); | ||
| 594 | PRINT_EXP ("%d", PLOT_SAMPLE_COUNT); | ||
| 595 | PRINT_EXP ("%lu", SEED); | ||
| 596 | PRINT_EXP ("%d", MEASUREMENT_RUNS); | ||
| 597 | |||
| 598 | srand (SEED); | ||
| 599 | |||
| 600 | sample_fitness_function (X_MIN, X_MAX, | ||
| 601 | Y_MIN, Y_MAX, plot_samples); | ||
| 602 | sample_fitness_function (X_MIN_ZOOM, X_MAX_ZOOM, | ||
| 603 | Y_MIN_ZOOM, Y_MAX_ZOOM, plot_samples_zoom); | ||
| 604 | run_experiment__plot (40, 240, 1); | ||
| 605 | return 0; | ||
| 606 | |||
| 607 | best_measurement = &measurements[0][0]; | ||
| 608 | struct measurement *measurement = &measurements[0][0]; | ||
| 609 | FOR_EACH_ELEMENT (population_size, population_sizes) { | ||
| 610 | FOR_EACH_ELEMENT (initial_sigma, initial_sigmas) { | ||
| 611 | printf ("experiment: [%3d/%3d] %7.3f", | ||
| 612 | population_size->mu, | ||
| 613 | population_size->lambda, *initial_sigma); | ||
| 614 | fflush (stdout); | ||
| 615 | *measurement = run_experiment__measure (population_size->mu, | ||
| 616 | population_size->lambda, | ||
| 617 | *initial_sigma); | ||
| 618 | printf (" @ %+8.6f/%4d (%6.2f ms)\n", measurement->fitness, measurement->generations, measurement->time*1e+3); | ||
| 619 | if (measurement->time < best_measurement->time) | ||
| 620 | best_measurement = measurement; | ||
| 621 | measurement += 1; | ||
| 622 | } | ||
| 623 | } | ||
| 624 | |||
| 625 | char *table_filename = NULL; | ||
| 626 | char *table_name = NULL; | ||
| 627 | char *table_caption = NULL; | ||
| 628 | asprintf (&table_filename, "res/results.org"); | ||
| 629 | asprintf (&table_name, "results"); | ||
| 630 | asprintf (&table_caption, "Результаты"); | ||
| 631 | print_org_table (table_filename, table_name, table_caption, "ccc", | ||
| 632 | 1 + ARRAY_LENGTH (initial_sigmas), | ||
| 633 | 1 + ARRAY_LENGTH (population_sizes), table_cell); | ||
| 634 | |||
| 635 | return EXIT_SUCCESS; | ||
| 636 | } | ||
| 637 | |||
| 638 | // evolution-strategy.c ends here | ||
