diff options
| author | 2026-11-11 06:32:30 +0000 | |
|---|---|---|
| committer | 2026-11-11 06:46:30 +0000 | |
| commit | 5b4a191d1cf310a3d7a99351e716a1a60004be69 (patch) | |
| tree | 4b8d1a82765946f323a90f35344526110e778a3f /src/cma-es.c | |
| download | coursework-master.tar.gz | |
Diffstat (limited to 'src/cma-es.c')
| -rw-r--r-- | src/cma-es.c | 854 |
1 files changed, 854 insertions, 0 deletions
diff --git a/src/cma-es.c b/src/cma-es.c new file mode 100644 index 0000000..bf8dcad --- /dev/null +++ b/src/cma-es.c | |||
| @@ -0,0 +1,854 @@ | |||
| 1 | #include <math.h> | ||
| 2 | #include <float.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include "retiming.h" | ||
| 8 | #include "cma-es.h" | ||
| 9 | |||
| 10 | #ifndef EPS | ||
| 11 | #define EPS 1e-6f | ||
| 12 | #endif | ||
| 13 | |||
| 14 | #ifndef CMA_PI | ||
| 15 | #define CMA_PI 3.14159265358979323846 | ||
| 16 | #endif | ||
| 17 | |||
| 18 | /* ----------------------------- | ||
| 19 | Basic vector helpers | ||
| 20 | ----------------------------- */ | ||
| 21 | |||
| 22 | static inline Vector2 | ||
| 23 | v2_add (Vector2 a, Vector2 b) | ||
| 24 | { | ||
| 25 | Vector2 r = { a.x + b.x, a.y + b.y }; | ||
| 26 | return r; | ||
| 27 | } | ||
| 28 | |||
| 29 | static inline Vector2 | ||
| 30 | v2_sub (Vector2 a, Vector2 b) | ||
| 31 | { | ||
| 32 | Vector2 r = { a.x - b.x, a.y - b.y }; | ||
| 33 | return r; | ||
| 34 | } | ||
| 35 | |||
| 36 | static inline Vector2 | ||
| 37 | v2_scale (Vector2 a, float s) | ||
| 38 | { | ||
| 39 | Vector2 r = { a.x * s, a.y * s }; | ||
| 40 | return r; | ||
| 41 | } | ||
| 42 | |||
| 43 | static inline float | ||
| 44 | v2_dot (Vector2 a, Vector2 b) | ||
| 45 | { | ||
| 46 | return a.x*b.x + a.y*b.y; | ||
| 47 | } | ||
| 48 | |||
| 49 | static inline float | ||
| 50 | v2_len (Vector2 a) | ||
| 51 | { | ||
| 52 | return sqrtf (fmaxf (0.0f, v2_dot (a, a))); | ||
| 53 | } | ||
| 54 | |||
| 55 | static inline Vector2 | ||
| 56 | v2_perp_left (Vector2 a) | ||
| 57 | { | ||
| 58 | Vector2 r = { -a.y, +a.x }; | ||
| 59 | return r; | ||
| 60 | } | ||
| 61 | |||
| 62 | static inline Vector2 | ||
| 63 | v2_normalize_safe (Vector2 a, Vector2 fallback) | ||
| 64 | { | ||
| 65 | float la = v2_len (a); | ||
| 66 | if (la > EPS) | ||
| 67 | return v2_scale (a, 1.0f / la); | ||
| 68 | |||
| 69 | float lf = v2_len (fallback); | ||
| 70 | if (lf > EPS) | ||
| 71 | return v2_scale (fallback, 1.0f / lf); | ||
| 72 | |||
| 73 | { | ||
| 74 | Vector2 ex = { 1.0f, 0.0f }; | ||
| 75 | return ex; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | // void | ||
| 80 | // chromosome_to_control_points (bezier5 cp, struct input in, struct chromosome ch) | ||
| 81 | // { | ||
| 82 | // Vector2 chord = v2_sub (in.r_end, in.r_beg); | ||
| 83 | // Vector2 e_ref = v2_normalize_safe (chord, (Vector2){ 1.0f, 0.0f }); | ||
| 84 | // | ||
| 85 | // Vector2 e_beg = v2_normalize_safe (in.v_beg, e_ref); | ||
| 86 | // Vector2 e_end = v2_normalize_safe (in.v_end, e_ref); | ||
| 87 | // | ||
| 88 | // Vector2 n_beg = v2_perp_left (e_beg); | ||
| 89 | // Vector2 n_end = v2_perp_left (e_end); | ||
| 90 | // | ||
| 91 | // cp[0] = in.r_beg; | ||
| 92 | // cp[1] = v2_add (in.r_beg, v2_scale (e_beg, +ch.rho * ch.tau_1)); | ||
| 93 | // cp[2] = v2_add (in.r_beg, | ||
| 94 | // v2_scale (v2_add (v2_scale (e_beg, ch.alp_2), | ||
| 95 | // v2_scale (n_beg, ch.bet_2)), | ||
| 96 | // +ch.rho)); | ||
| 97 | // cp[3] = v2_add (in.r_end, | ||
| 98 | // v2_scale (v2_add (v2_scale (e_end, ch.alp_3), | ||
| 99 | // v2_scale (n_end, ch.bet_3)), | ||
| 100 | // -ch.rho)); | ||
| 101 | // cp[4] = v2_add (in.r_end, v2_scale (e_end, -ch.rho * ch.tau_4)); | ||
| 102 | // cp[5] = in.r_end; | ||
| 103 | // } | ||
| 104 | |||
| 105 | /* ----------------------------- | ||
| 106 | CMA-side chromosome transform | ||
| 107 | Search in latent z \in R^7, | ||
| 108 | decode to valid chromosome. | ||
| 109 | ----------------------------- */ | ||
| 110 | |||
| 111 | static inline struct chromosome_cma_config | ||
| 112 | chromosome_cma_default_config (void) | ||
| 113 | { | ||
| 114 | struct chromosome_cma_config c; | ||
| 115 | c.rho_min_rel = 0.05f; | ||
| 116 | c.rho_max_rel = 5.00f; | ||
| 117 | c.tau_min = 0.05f; | ||
| 118 | c.alp_limit = 5.00f; | ||
| 119 | c.bet_limit = 5.00f; | ||
| 120 | return c; | ||
| 121 | } | ||
| 122 | |||
| 123 | static inline float | ||
| 124 | clampf (float x, float lo, float hi) | ||
| 125 | { | ||
| 126 | return x < lo ? lo : (x > hi ? hi : x); | ||
| 127 | } | ||
| 128 | |||
| 129 | static inline float | ||
| 130 | sigmoidf (float x) | ||
| 131 | { | ||
| 132 | if (x >= 0.0f) { | ||
| 133 | float e = expf (-x); | ||
| 134 | return 1.0f / (1.0f + e); | ||
| 135 | } else { | ||
| 136 | float e = expf (x); | ||
| 137 | return e / (1.0f + e); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | static inline float | ||
| 142 | logitf (float p) | ||
| 143 | { | ||
| 144 | p = clampf (p, 1e-6f, 1.0f - 1e-6f); | ||
| 145 | return logf (p / (1.0f - p)); | ||
| 146 | } | ||
| 147 | |||
| 148 | static inline float | ||
| 149 | atanh_safe (float x) | ||
| 150 | { | ||
| 151 | x = clampf (x, -0.999999f, 0.999999f); | ||
| 152 | return 0.5f * logf ((1.0f + x) / (1.0f - x)); | ||
| 153 | } | ||
| 154 | |||
| 155 | static inline float | ||
| 156 | map01 (float s, float lo, float hi) | ||
| 157 | { | ||
| 158 | return lo + (hi - lo) * s; | ||
| 159 | } | ||
| 160 | |||
| 161 | void | ||
| 162 | cma_decode_chromosome (struct chromosome *ch, | ||
| 163 | const float z[CMA_DIM], | ||
| 164 | const struct input *in, | ||
| 165 | const struct chromosome_cma_config *cfg) | ||
| 166 | { | ||
| 167 | float D = v2_len (v2_sub (in->r_end, in->r_beg)); | ||
| 168 | if (D < EPS) D = fmaxf (v2_len (in->v_beg), v2_len (in->v_end)); | ||
| 169 | |||
| 170 | { | ||
| 171 | float s_rho = sigmoidf (z[0]); | ||
| 172 | ch->rho = D * map01 (s_rho, cfg->rho_min_rel, cfg->rho_max_rel); | ||
| 173 | } | ||
| 174 | |||
| 175 | { | ||
| 176 | float s1 = sigmoidf (z[1]); | ||
| 177 | float tau1_max = 1.0f - cfg->tau_min; | ||
| 178 | tau1_max = fmaxf (tau1_max, cfg->tau_min + 1e-3f); | ||
| 179 | ch->tau_1 = map01 (s1, cfg->tau_min, tau1_max); | ||
| 180 | |||
| 181 | float s6 = sigmoidf (z[6]); | ||
| 182 | float tau4_max = 1.0f - cfg->tau_min; | ||
| 183 | tau1_max = fmaxf (tau4_max, cfg->tau_min + 1e-3f); | ||
| 184 | ch->tau_4 = map01 (s6, cfg->tau_min, tau4_max); | ||
| 185 | } | ||
| 186 | |||
| 187 | ch->alp_2 = cfg->alp_limit * tanhf (z[2]); | ||
| 188 | ch->bet_2 = cfg->bet_limit * tanhf (z[3]); | ||
| 189 | ch->alp_3 = cfg->alp_limit * tanhf (z[4]); | ||
| 190 | ch->bet_3 = cfg->bet_limit * tanhf (z[5]); | ||
| 191 | } | ||
| 192 | |||
| 193 | void | ||
| 194 | cma_encode_chromosome (float z[CMA_DIM], | ||
| 195 | const struct chromosome *ch_in, | ||
| 196 | const struct input *in, | ||
| 197 | const struct chromosome_cma_config *cfg) | ||
| 198 | { | ||
| 199 | struct chromosome ch = *ch_in; | ||
| 200 | float D = v2_len (v2_sub (in->r_end, in->r_beg)); | ||
| 201 | if (D < EPS) D = fmaxf (v2_len (in->v_beg), v2_len (in->v_end)); | ||
| 202 | |||
| 203 | { | ||
| 204 | float rho_rel = ch.rho / D; | ||
| 205 | float s = (rho_rel - cfg->rho_min_rel) / | ||
| 206 | fmaxf (cfg->rho_max_rel - cfg->rho_min_rel, EPS); | ||
| 207 | z[0] = logitf (s); | ||
| 208 | } | ||
| 209 | |||
| 210 | { | ||
| 211 | float tau1_max = 1.0f - cfg->tau_min; | ||
| 212 | tau1_max = fmaxf (tau1_max, cfg->tau_min + 1e-3f); | ||
| 213 | ch.tau_1 = clampf (ch.tau_1, cfg->tau_min, tau1_max); | ||
| 214 | |||
| 215 | { | ||
| 216 | float s1 = (ch.tau_1 - cfg->tau_min) / | ||
| 217 | fmaxf (tau1_max - cfg->tau_min, EPS); | ||
| 218 | z[1] = logitf (s1); | ||
| 219 | } | ||
| 220 | |||
| 221 | float tau4_max = 1.0f - cfg->tau_min; | ||
| 222 | tau4_max = fmaxf (tau1_max, cfg->tau_min + 1e-3f); | ||
| 223 | ch.tau_4 = clampf (ch.tau_4, cfg->tau_min, tau4_max); | ||
| 224 | |||
| 225 | { | ||
| 226 | float s6 = (ch.tau_4 - cfg->tau_min) / | ||
| 227 | fmaxf (tau4_max - cfg->tau_min, EPS); | ||
| 228 | z[6] = logitf (s6); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | z[2] = atanh_safe (clampf (ch.alp_2 / fmaxf (cfg->alp_limit, EPS), -0.999999f, 0.999999f)); | ||
| 233 | z[3] = atanh_safe (clampf (ch.bet_2 / fmaxf (cfg->bet_limit, EPS), -0.999999f, 0.999999f)); | ||
| 234 | z[4] = atanh_safe (clampf (ch.alp_3 / fmaxf (cfg->alp_limit, EPS), -0.999999f, 0.999999f)); | ||
| 235 | z[5] = atanh_safe (clampf (ch.bet_3 / fmaxf (cfg->bet_limit, EPS), -0.999999f, 0.999999f)); | ||
| 236 | } | ||
| 237 | |||
| 238 | static inline uint64_t | ||
| 239 | cma_rng_next_u64 (struct cma_rng *r) | ||
| 240 | { | ||
| 241 | uint64_t x = r->state; | ||
| 242 | x ^= x >> 12; | ||
| 243 | x ^= x << 25; | ||
| 244 | x ^= x >> 27; | ||
| 245 | r->state = x; | ||
| 246 | return x * UINT64_C(2685821657736338717); | ||
| 247 | } | ||
| 248 | |||
| 249 | static inline void | ||
| 250 | cma_rng_seed (struct cma_rng *r, uint64_t seed) | ||
| 251 | { | ||
| 252 | if (seed == 0) seed = UINT64_C(0x9e3779b97f4a7c15); | ||
| 253 | r->state = seed; | ||
| 254 | if (r->state == 0) r->state = UINT64_C(0x123456789abcdef); | ||
| 255 | r->has_spare = 0; | ||
| 256 | r->spare = 0.0f; | ||
| 257 | } | ||
| 258 | |||
| 259 | static inline float | ||
| 260 | cma_rng_uniform01 (struct cma_rng *r) | ||
| 261 | { | ||
| 262 | uint64_t x = cma_rng_next_u64 (r); | ||
| 263 | return (float)((x >> 11) * (1.0 / 9007199254740992.0)); | ||
| 264 | } | ||
| 265 | |||
| 266 | static inline float | ||
| 267 | cma_rng_normal (struct cma_rng *r) | ||
| 268 | { | ||
| 269 | if (r->has_spare) { | ||
| 270 | r->has_spare = 0; | ||
| 271 | return r->spare; | ||
| 272 | } | ||
| 273 | |||
| 274 | for (;;) { | ||
| 275 | float u1 = cma_rng_uniform01 (r); | ||
| 276 | float u2 = cma_rng_uniform01 (r); | ||
| 277 | |||
| 278 | if (u1 > 1e-12f) { | ||
| 279 | float mag = sqrtf (-2.0f * logf (u1)); | ||
| 280 | float ang = 2.0f * (float)CMA_PI * u2; | ||
| 281 | |||
| 282 | r->spare = mag * sinf (ang); | ||
| 283 | r->has_spare = 1; | ||
| 284 | return mag * cosf (ang); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | #define MIDX(i,j,n) ((i)*(n) + (j)) | ||
| 290 | |||
| 291 | static void | ||
| 292 | mat_identity (float *A, int n) | ||
| 293 | { | ||
| 294 | memset (A, 0, (size_t)n * (size_t)n * sizeof (*A)); | ||
| 295 | for (int i = 0; i < n; ++i) | ||
| 296 | A[MIDX(i,i,n)] = 1.0f; | ||
| 297 | } | ||
| 298 | |||
| 299 | static void | ||
| 300 | mat_copy (float *dst, const float *src, int n) | ||
| 301 | { | ||
| 302 | memcpy (dst, src, (size_t)n * (size_t)n * sizeof (*dst)); | ||
| 303 | } | ||
| 304 | |||
| 305 | static void | ||
| 306 | vec_copy (float *dst, const float *src, int n) | ||
| 307 | { | ||
| 308 | memcpy (dst, src, (size_t)n * sizeof (*dst)); | ||
| 309 | } | ||
| 310 | |||
| 311 | static void | ||
| 312 | vec_set_zero (float *x, int n) | ||
| 313 | { | ||
| 314 | memset (x, 0, (size_t)n * sizeof (*x)); | ||
| 315 | } | ||
| 316 | |||
| 317 | static float | ||
| 318 | vec_dot (const float *a, const float *b, int n) | ||
| 319 | { | ||
| 320 | float s = 0.0f; | ||
| 321 | for (int i = 0; i < n; ++i) | ||
| 322 | s += a[i] * b[i]; | ||
| 323 | return s; | ||
| 324 | } | ||
| 325 | |||
| 326 | static float | ||
| 327 | vec_norm (const float *a, int n) | ||
| 328 | { | ||
| 329 | return sqrtf (fmaxf (0.0f, vec_dot (a, a, n))); | ||
| 330 | } | ||
| 331 | |||
| 332 | static void | ||
| 333 | mat_vec_mul (float *y, const float *A, const float *x, int n) | ||
| 334 | { | ||
| 335 | for (int i = 0; i < n; ++i) { | ||
| 336 | float s = 0.0f; | ||
| 337 | for (int j = 0; j < n; ++j) | ||
| 338 | s += A[MIDX(i,j,n)] * x[j]; | ||
| 339 | y[i] = s; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | |||
| 343 | static void | ||
| 344 | symmetrize (float *A, int n) | ||
| 345 | { | ||
| 346 | for (int i = 0; i < n; ++i) { | ||
| 347 | for (int j = i + 1; j < n; ++j) { | ||
| 348 | float s = 0.5f * (A[MIDX(i,j,n)] + A[MIDX(j,i,n)]); | ||
| 349 | A[MIDX(i,j,n)] = s; | ||
| 350 | A[MIDX(j,i,n)] = s; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | } | ||
| 354 | |||
| 355 | /* Jacobi eigendecomposition for symmetric A. | ||
| 356 | On output: | ||
| 357 | - eval[i] are eigenvalues | ||
| 358 | - V(:,i) is eigenvector i | ||
| 359 | */ | ||
| 360 | static void | ||
| 361 | jacobi_eigen_symmetric (float *A, float *V, float *eval, int n) | ||
| 362 | { | ||
| 363 | mat_identity (V, n); | ||
| 364 | |||
| 365 | for (int sweep = 0; sweep < 64 * n * n; ++sweep) { | ||
| 366 | int p = 0, q = 1; | ||
| 367 | float max_off = fabsf (A[MIDX(0,1,n)]); | ||
| 368 | |||
| 369 | for (int i = 0; i < n; ++i) { | ||
| 370 | for (int j = i + 1; j < n; ++j) { | ||
| 371 | float aij = fabsf (A[MIDX(i,j,n)]); | ||
| 372 | if (aij > max_off) { | ||
| 373 | max_off = aij; | ||
| 374 | p = i; | ||
| 375 | q = j; | ||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | if (max_off < 1e-12f) | ||
| 381 | break; | ||
| 382 | |||
| 383 | { | ||
| 384 | float app = A[MIDX(p,p,n)]; | ||
| 385 | float aqq = A[MIDX(q,q,n)]; | ||
| 386 | float apq = A[MIDX(p,q,n)]; | ||
| 387 | |||
| 388 | float phi = 0.5f * atan2f (2.0f * apq, aqq - app); | ||
| 389 | float c = cosf (phi); | ||
| 390 | float s = sinf (phi); | ||
| 391 | |||
| 392 | for (int k = 0; k < n; ++k) { | ||
| 393 | float aik = A[MIDX(p,k,n)]; | ||
| 394 | float aqk = A[MIDX(q,k,n)]; | ||
| 395 | A[MIDX(p,k,n)] = c*aik - s*aqk; | ||
| 396 | A[MIDX(q,k,n)] = s*aik + c*aqk; | ||
| 397 | } | ||
| 398 | |||
| 399 | for (int k = 0; k < n; ++k) { | ||
| 400 | float akp = A[MIDX(k,p,n)]; | ||
| 401 | float akq = A[MIDX(k,q,n)]; | ||
| 402 | A[MIDX(k,p,n)] = c*akp - s*akq; | ||
| 403 | A[MIDX(k,q,n)] = s*akp + c*akq; | ||
| 404 | } | ||
| 405 | |||
| 406 | for (int k = 0; k < n; ++k) { | ||
| 407 | float vkp = V[MIDX(k,p,n)]; | ||
| 408 | float vkq = V[MIDX(k,q,n)]; | ||
| 409 | V[MIDX(k,p,n)] = c*vkp - s*vkq; | ||
| 410 | V[MIDX(k,q,n)] = s*vkp + c*vkq; | ||
| 411 | } | ||
| 412 | } | ||
| 413 | } | ||
| 414 | |||
| 415 | for (int i = 0; i < n; ++i) | ||
| 416 | eval[i] = A[MIDX(i,i,n)]; | ||
| 417 | |||
| 418 | /* sort ascending eigenvalues */ | ||
| 419 | for (int i = 0; i < n - 1; ++i) { | ||
| 420 | int best = i; | ||
| 421 | for (int j = i + 1; j < n; ++j) | ||
| 422 | if (eval[j] < eval[best]) best = j; | ||
| 423 | |||
| 424 | if (best != i) { | ||
| 425 | float tmp = eval[i]; | ||
| 426 | eval[i] = eval[best]; | ||
| 427 | eval[best] = tmp; | ||
| 428 | |||
| 429 | for (int k = 0; k < n; ++k) { | ||
| 430 | float tv = V[MIDX(k,i,n)]; | ||
| 431 | V[MIDX(k,i,n)] = V[MIDX(k,best,n)]; | ||
| 432 | V[MIDX(k,best,n)] = tv; | ||
| 433 | } | ||
| 434 | } | ||
| 435 | } | ||
| 436 | } | ||
| 437 | |||
| 438 | /* ----------------------------- | ||
| 439 | CMA-ES core | ||
| 440 | ----------------------------- */ | ||
| 441 | |||
| 442 | int | ||
| 443 | cmaes_default_lambda (int n) | ||
| 444 | { | ||
| 445 | return 4 + (int)floorf (3.0f * logf ((float)n)); | ||
| 446 | } | ||
| 447 | |||
| 448 | float | ||
| 449 | cmaes_expected_norm (int n) | ||
| 450 | { | ||
| 451 | float nf = (float)n; | ||
| 452 | return sqrtf (nf) * (1.0f - 1.0f/(4.0f*nf) + 1.0f/(21.0f*nf*nf)); | ||
| 453 | } | ||
| 454 | |||
| 455 | int | ||
| 456 | cmaes_init (struct cmaes *es, | ||
| 457 | int n, | ||
| 458 | const float *mean0, | ||
| 459 | float sigma0, | ||
| 460 | int lambda, | ||
| 461 | uint64_t seed) | ||
| 462 | { | ||
| 463 | memset (es, 0, sizeof (*es)); | ||
| 464 | |||
| 465 | if (n <= 0) return 0; | ||
| 466 | if (sigma0 <= 0.0f) return 0; | ||
| 467 | |||
| 468 | es->n = n; | ||
| 469 | es->lambda = (lambda > 0) ? lambda : cmaes_default_lambda (n); | ||
| 470 | es->mu = es->lambda / 2; | ||
| 471 | es->sigma = sigma0; | ||
| 472 | es->generation = 0; | ||
| 473 | es->evaluations = 0; | ||
| 474 | es->chi_n = cmaes_expected_norm (n); | ||
| 475 | |||
| 476 | es->weights = (float*)calloc ((size_t)es->mu, sizeof (float)); | ||
| 477 | es->xmean = (float*)calloc ((size_t)n, sizeof (float)); | ||
| 478 | es->xold = (float*)calloc ((size_t)n, sizeof (float)); | ||
| 479 | es->pc = (float*)calloc ((size_t)n, sizeof (float)); | ||
| 480 | es->ps = (float*)calloc ((size_t)n, sizeof (float)); | ||
| 481 | es->C = (float*)calloc ((size_t)n*(size_t)n, sizeof (float)); | ||
| 482 | es->B = (float*)calloc ((size_t)n*(size_t)n, sizeof (float)); | ||
| 483 | es->D = (float*)calloc ((size_t)n, sizeof (float)); | ||
| 484 | es->BD = (float*)calloc ((size_t)n*(size_t)n, sizeof (float)); | ||
| 485 | es->invsqrtC= (float*)calloc ((size_t)n*(size_t)n, sizeof (float)); | ||
| 486 | es->ary = (float*)calloc ((size_t)es->lambda*(size_t)n, sizeof (float)); | ||
| 487 | es->arx = (float*)calloc ((size_t)es->lambda*(size_t)n, sizeof (float)); | ||
| 488 | es->fitness = (float*)calloc ((size_t)es->lambda, sizeof (float)); | ||
| 489 | es->index = (int*)calloc ((size_t)es->lambda, sizeof (int)); | ||
| 490 | |||
| 491 | if (!es->weights || !es->xmean || !es->xold || !es->pc || !es->ps || | ||
| 492 | !es->C || !es->B || !es->D || !es->BD || !es->invsqrtC || | ||
| 493 | !es->ary || !es->arx || !es->fitness || !es->index) { | ||
| 494 | return 0; | ||
| 495 | } | ||
| 496 | |||
| 497 | vec_copy (es->xmean, mean0, n); | ||
| 498 | vec_set_zero (es->xold, n); | ||
| 499 | vec_set_zero (es->pc, n); | ||
| 500 | vec_set_zero (es->ps, n); | ||
| 501 | |||
| 502 | { | ||
| 503 | float wsum = 0.0f; | ||
| 504 | for (int i = 0; i < es->mu; ++i) { | ||
| 505 | es->weights[i] = logf ((float)es->mu + 0.5f) - logf ((float)(i + 1)); | ||
| 506 | wsum += es->weights[i]; | ||
| 507 | } | ||
| 508 | |||
| 509 | es->mueff = 0.0f; | ||
| 510 | for (int i = 0; i < es->mu; ++i) { | ||
| 511 | es->weights[i] /= wsum; | ||
| 512 | es->mueff += es->weights[i] * es->weights[i]; | ||
| 513 | } | ||
| 514 | es->mueff = 1.0f / fmaxf (es->mueff, EPS); | ||
| 515 | } | ||
| 516 | |||
| 517 | { | ||
| 518 | float nf = (float)n; | ||
| 519 | es->cc = (4.0f + es->mueff / nf) / (nf + 4.0f + 2.0f * es->mueff / nf); | ||
| 520 | es->cs = (es->mueff + 2.0f) / (nf + es->mueff + 5.0f); | ||
| 521 | es->c1 = 2.0f / (((nf + 1.3f)*(nf + 1.3f)) + es->mueff); | ||
| 522 | es->cmu = fminf (1.0f - es->c1, | ||
| 523 | 2.0f * (es->mueff - 2.0f + 1.0f/es->mueff) / | ||
| 524 | (((nf + 2.0f)*(nf + 2.0f)) + es->mueff)); | ||
| 525 | es->damps = 1.0f + 2.0f * fmaxf (0.0f, sqrtf ((es->mueff - 1.0f)/(nf + 1.0f)) - 1.0f) | ||
| 526 | + es->cs; | ||
| 527 | } | ||
| 528 | |||
| 529 | mat_identity (es->C, n); | ||
| 530 | mat_identity (es->B, n); | ||
| 531 | mat_identity (es->BD, n); | ||
| 532 | mat_identity (es->invsqrtC, n); | ||
| 533 | for (int i = 0; i < n; ++i) | ||
| 534 | es->D[i] = 1.0f; | ||
| 535 | |||
| 536 | cma_rng_seed (&es->rng, seed); | ||
| 537 | return 1; | ||
| 538 | } | ||
| 539 | |||
| 540 | void | ||
| 541 | cmaes_free (struct cmaes *es) | ||
| 542 | { | ||
| 543 | free (es->weights); | ||
| 544 | free (es->xmean); | ||
| 545 | free (es->xold); | ||
| 546 | free (es->pc); | ||
| 547 | free (es->ps); | ||
| 548 | free (es->C); | ||
| 549 | free (es->B); | ||
| 550 | free (es->D); | ||
| 551 | free (es->BD); | ||
| 552 | free (es->invsqrtC); | ||
| 553 | free (es->ary); | ||
| 554 | free (es->arx); | ||
| 555 | free (es->fitness); | ||
| 556 | free (es->index); | ||
| 557 | memset (es, 0, sizeof (*es)); | ||
| 558 | } | ||
| 559 | |||
| 560 | static void | ||
| 561 | cmaes_update_eigensystem (struct cmaes *es) | ||
| 562 | { | ||
| 563 | int n = es->n; | ||
| 564 | float *tmp = (float*)calloc ((size_t)n*(size_t)n, sizeof (float)); | ||
| 565 | float *eval = (float*)calloc ((size_t)n, sizeof (float)); | ||
| 566 | |||
| 567 | if (!tmp || !eval) { | ||
| 568 | free (tmp); | ||
| 569 | free (eval); | ||
| 570 | return; | ||
| 571 | } | ||
| 572 | |||
| 573 | symmetrize (es->C, n); | ||
| 574 | mat_copy (tmp, es->C, n); | ||
| 575 | jacobi_eigen_symmetric (tmp, es->B, eval, n); | ||
| 576 | |||
| 577 | for (int i = 0; i < n; ++i) | ||
| 578 | es->D[i] = sqrtf (fmaxf (eval[i], 1e-20f)); | ||
| 579 | |||
| 580 | for (int i = 0; i < n; ++i) { | ||
| 581 | for (int j = 0; j < n; ++j) | ||
| 582 | es->BD[MIDX(i,j,n)] = es->B[MIDX(i,j,n)] * es->D[j]; | ||
| 583 | } | ||
| 584 | |||
| 585 | for (int i = 0; i < n; ++i) { | ||
| 586 | for (int j = 0; j < n; ++j) { | ||
| 587 | float s = 0.0f; | ||
| 588 | for (int k = 0; k < n; ++k) | ||
| 589 | s += es->B[MIDX(i,k,n)] * (1.0f / es->D[k]) * es->B[MIDX(j,k,n)]; | ||
| 590 | es->invsqrtC[MIDX(i,j,n)] = s; | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | free (tmp); | ||
| 595 | free (eval); | ||
| 596 | } | ||
| 597 | |||
| 598 | void | ||
| 599 | cmaes_ask (struct cmaes *es) | ||
| 600 | { | ||
| 601 | int n = es->n; | ||
| 602 | |||
| 603 | for (int k = 0; k < es->lambda; ++k) { | ||
| 604 | float z[CMA_DIM]; /* n=7 in your use-case */ | ||
| 605 | float *y = &es->ary[k*n]; | ||
| 606 | float *x = &es->arx[k*n]; | ||
| 607 | |||
| 608 | for (int j = 0; j < n; ++j) | ||
| 609 | z[j] = cma_rng_normal (&es->rng); | ||
| 610 | |||
| 611 | mat_vec_mul (y, es->BD, z, n); | ||
| 612 | |||
| 613 | for (int j = 0; j < n; ++j) | ||
| 614 | x[j] = es->xmean[j] + es->sigma * y[j]; | ||
| 615 | } | ||
| 616 | } | ||
| 617 | |||
| 618 | void | ||
| 619 | cmaes_sort_indices (struct cmaes *es) | ||
| 620 | { | ||
| 621 | for (int i = 0; i < es->lambda; ++i) | ||
| 622 | es->index[i] = i; | ||
| 623 | |||
| 624 | for (int i = 1; i < es->lambda; ++i) { | ||
| 625 | int key = es->index[i]; | ||
| 626 | float fkey = es->fitness[key]; | ||
| 627 | int j = i - 1; | ||
| 628 | |||
| 629 | while (j >= 0 && es->fitness[es->index[j]] > fkey) { | ||
| 630 | es->index[j + 1] = es->index[j]; | ||
| 631 | --j; | ||
| 632 | } | ||
| 633 | es->index[j + 1] = key; | ||
| 634 | } | ||
| 635 | } | ||
| 636 | |||
| 637 | void | ||
| 638 | cmaes_tell (struct cmaes *es, const float *fitness) | ||
| 639 | { | ||
| 640 | int n = es->n; | ||
| 641 | memcpy (es->fitness, fitness, (size_t)es->lambda * sizeof (*fitness)); | ||
| 642 | cmaes_sort_indices (es); | ||
| 643 | |||
| 644 | vec_copy (es->xold, es->xmean, n); | ||
| 645 | vec_set_zero (es->xmean, n); | ||
| 646 | |||
| 647 | for (int i = 0; i < es->mu; ++i) { | ||
| 648 | const float *x = &es->arx[es->index[i] * n]; | ||
| 649 | float w = es->weights[i]; | ||
| 650 | for (int j = 0; j < n; ++j) | ||
| 651 | es->xmean[j] += w * x[j]; | ||
| 652 | } | ||
| 653 | |||
| 654 | { | ||
| 655 | float y_w[CMA_DIM]; | ||
| 656 | float z_w[CMA_DIM]; | ||
| 657 | float coeff_ps = sqrtf (es->cs * (2.0f - es->cs) * es->mueff); | ||
| 658 | float coeff_pc = sqrtf (es->cc * (2.0f - es->cc) * es->mueff); | ||
| 659 | |||
| 660 | for (int j = 0; j < n; ++j) | ||
| 661 | y_w[j] = (es->xmean[j] - es->xold[j]) / fmaxf (es->sigma, EPS); | ||
| 662 | |||
| 663 | mat_vec_mul (z_w, es->invsqrtC, y_w, n); | ||
| 664 | |||
| 665 | for (int j = 0; j < n; ++j) | ||
| 666 | es->ps[j] = (1.0f - es->cs) * es->ps[j] + coeff_ps * z_w[j]; | ||
| 667 | |||
| 668 | { | ||
| 669 | float norm_ps = vec_norm (es->ps, n); | ||
| 670 | float denom = sqrtf (1.0f - powf (1.0f - es->cs, 2.0f * (es->generation + 1))); | ||
| 671 | float left = norm_ps / fmaxf (denom * es->chi_n, EPS); | ||
| 672 | int hsig = left < (1.4f + 2.0f / ((float)n + 1.0f)); | ||
| 673 | |||
| 674 | for (int j = 0; j < n; ++j) | ||
| 675 | es->pc[j] = (1.0f - es->cc) * es->pc[j] + (hsig ? coeff_pc * y_w[j] : 0.0f); | ||
| 676 | |||
| 677 | { | ||
| 678 | float alpha = 1.0f - es->c1 - es->cmu | ||
| 679 | + (hsig ? 0.0f : es->c1 * es->cc * (2.0f - es->cc)); | ||
| 680 | |||
| 681 | for (int i = 0; i < n; ++i) { | ||
| 682 | for (int j = 0; j < n; ++j) | ||
| 683 | es->C[MIDX(i,j,n)] *= alpha; | ||
| 684 | } | ||
| 685 | |||
| 686 | for (int i = 0; i < n; ++i) { | ||
| 687 | for (int j = 0; j < n; ++j) | ||
| 688 | es->C[MIDX(i,j,n)] += es->c1 * es->pc[i] * es->pc[j]; | ||
| 689 | } | ||
| 690 | |||
| 691 | for (int a = 0; a < es->mu; ++a) { | ||
| 692 | const float *y = &es->ary[es->index[a] * n]; | ||
| 693 | float w = es->weights[a]; | ||
| 694 | for (int i = 0; i < n; ++i) { | ||
| 695 | for (int j = 0; j < n; ++j) | ||
| 696 | es->C[MIDX(i,j,n)] += es->cmu * w * y[i] * y[j]; | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | symmetrize (es->C, n); | ||
| 701 | |||
| 702 | for (int i = 0; i < n; ++i) | ||
| 703 | es->C[MIDX(i,i,n)] = fmaxf (es->C[MIDX(i,i,n)], 1e-20f); | ||
| 704 | } | ||
| 705 | |||
| 706 | es->sigma *= expf ((es->cs / es->damps) * (norm_ps / es->chi_n - 1.0f)); | ||
| 707 | } | ||
| 708 | } | ||
| 709 | |||
| 710 | es->generation += 1; | ||
| 711 | es->evaluations += (uint64_t)es->lambda; | ||
| 712 | |||
| 713 | cmaes_update_eigensystem (es); | ||
| 714 | } | ||
| 715 | |||
| 716 | const float * | ||
| 717 | cmaes_candidate (const struct cmaes *es, int k) | ||
| 718 | { | ||
| 719 | return &es->arx[k * es->n]; | ||
| 720 | } | ||
| 721 | |||
| 722 | int | ||
| 723 | cmaes_should_stop (const struct cmaes *es) | ||
| 724 | { | ||
| 725 | if (!isfinite (es->sigma) || es->sigma < 1e-10f) | ||
| 726 | return 1; | ||
| 727 | |||
| 728 | for (int i = 0; i < es->n; ++i) { | ||
| 729 | if (!isfinite (es->D[i])) | ||
| 730 | return 1; | ||
| 731 | } | ||
| 732 | |||
| 733 | return 0; | ||
| 734 | } | ||
| 735 | |||
| 736 | struct cmaes_result | ||
| 737 | cmaes_run (struct cmaes *es, | ||
| 738 | int max_generations, | ||
| 739 | cma_objective_fn objective, | ||
| 740 | void *userdata) | ||
| 741 | { | ||
| 742 | struct cmaes_result out; | ||
| 743 | out.best_f = FLT_MAX; | ||
| 744 | out.best_generation = -1; | ||
| 745 | for (int i = 0; i < es->n; ++i) out.best_x[i] = es->xmean[i]; | ||
| 746 | |||
| 747 | for (int gen = 0; gen < max_generations; ++gen) { | ||
| 748 | cmaes_ask (es); | ||
| 749 | |||
| 750 | for (int k = 0; k < es->lambda; ++k) { | ||
| 751 | const float *x = cmaes_candidate (es, k); | ||
| 752 | float f = objective (x, es->n, userdata); | ||
| 753 | if (!isfinite (f)) f = 1e30f; | ||
| 754 | es->fitness[k] = f; | ||
| 755 | |||
| 756 | if (f < out.best_f) { | ||
| 757 | out.best_f = f; | ||
| 758 | out.best_generation = es->generation; | ||
| 759 | for (int j = 0; j < es->n; ++j) | ||
| 760 | out.best_x[j] = x[j]; | ||
| 761 | } | ||
| 762 | } | ||
| 763 | |||
| 764 | cmaes_tell (es, es->fitness); | ||
| 765 | |||
| 766 | if (cmaes_should_stop (es)) | ||
| 767 | break; | ||
| 768 | } | ||
| 769 | |||
| 770 | return out; | ||
| 771 | } | ||
| 772 | |||
| 773 | float | ||
| 774 | spline_cma_objective (const float *z, int n, void *userdata) | ||
| 775 | { | ||
| 776 | (void)n; | ||
| 777 | |||
| 778 | struct spline_objective_ctx *ctx = (struct spline_objective_ctx*)userdata; | ||
| 779 | struct chromosome ch; | ||
| 780 | bezier5 cp; | ||
| 781 | struct path_table p; | ||
| 782 | struct motion_profile m; | ||
| 783 | |||
| 784 | cma_decode_chromosome (&ch, z, &ctx->in, &ctx->cfg); | ||
| 785 | chromosome_to_control_points (cp, ctx->in, ch); | ||
| 786 | |||
| 787 | build_path_table (&p, cp); | ||
| 788 | |||
| 789 | { | ||
| 790 | float v_beg = v2_len (ctx->in.v_beg); | ||
| 791 | float v_end = v2_len (ctx->in.v_end); | ||
| 792 | |||
| 793 | int ok = build_motion_profile (&m, &p, | ||
| 794 | v_beg, v_end, | ||
| 795 | ctx->in.v_max, | ||
| 796 | ctx->in.a_max, | ||
| 797 | ctx->in.j_max); | ||
| 798 | |||
| 799 | { | ||
| 800 | struct fitness_breakdown fb = | ||
| 801 | evaluate_motion_fitness (&p, &m, | ||
| 802 | v_beg, v_end, | ||
| 803 | ctx->in.v_max, | ||
| 804 | ctx->in.a_max, | ||
| 805 | ctx->in.j_max, | ||
| 806 | ctx->weights); | ||
| 807 | |||
| 808 | if (!isfinite (fb.total_cost)) | ||
| 809 | return ctx->hard_fail_cost; | ||
| 810 | |||
| 811 | return fb.total_cost; | ||
| 812 | } | ||
| 813 | } | ||
| 814 | } | ||
| 815 | |||
| 816 | void | ||
| 817 | example_cmaes_usage (const struct input *in, | ||
| 818 | const struct chromosome *nominal_chromosome, | ||
| 819 | const struct fitness_weights *weights, | ||
| 820 | struct chromosome *best_chromosome_out, | ||
| 821 | float *best_cost_out) | ||
| 822 | { | ||
| 823 | struct chromosome_cma_config cfg = chromosome_cma_default_config (); | ||
| 824 | float mean0[CMA_DIM]; | ||
| 825 | struct cmaes es; | ||
| 826 | struct spline_objective_ctx ctx; | ||
| 827 | struct cmaes_result result; | ||
| 828 | |||
| 829 | cma_encode_chromosome (mean0, nominal_chromosome, in, &cfg); | ||
| 830 | |||
| 831 | /* Good starting point for 7D */ | ||
| 832 | if (!cmaes_init (&es, | ||
| 833 | CMA_DIM, | ||
| 834 | mean0, | ||
| 835 | 1.20f, /* sigma0 */ | ||
| 836 | 12, /* lambda */ | ||
| 837 | 1234567u /* seed */ | ||
| 838 | )) { | ||
| 839 | /* handle allocation/init failure */ | ||
| 840 | return; | ||
| 841 | } | ||
| 842 | |||
| 843 | ctx.in = *in; | ||
| 844 | ctx.cfg = cfg; | ||
| 845 | ctx.weights = weights; | ||
| 846 | ctx.hard_fail_cost = 1e9f; | ||
| 847 | |||
| 848 | result = cmaes_run (&es, 100, spline_cma_objective, &ctx); | ||
| 849 | |||
| 850 | cma_decode_chromosome (best_chromosome_out, result.best_x, in, &cfg); | ||
| 851 | if (best_cost_out) *best_cost_out = result.best_f; | ||
| 852 | |||
| 853 | cmaes_free (&es); | ||
| 854 | } | ||
