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.h | |
| download | coursework-master.tar.gz | |
Diffstat (limited to 'src/cma-es.h')
| -rw-r--r-- | src/cma-es.h | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/cma-es.h b/src/cma-es.h new file mode 100644 index 0000000..1e929e2 --- /dev/null +++ b/src/cma-es.h | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | #ifndef INCLUDED_CMA_ES_H | ||
| 2 | #define INCLUDED_CMA_ES_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | |||
| 6 | #define CMA_DIM 7 | ||
| 7 | |||
| 8 | struct input { | ||
| 9 | Vector2 r_beg; | ||
| 10 | Vector2 r_end; | ||
| 11 | Vector2 v_beg; | ||
| 12 | Vector2 v_end; | ||
| 13 | float v_max; | ||
| 14 | float a_max; | ||
| 15 | float j_max; | ||
| 16 | }; | ||
| 17 | |||
| 18 | struct chromosome { | ||
| 19 | float rho; | ||
| 20 | float tau_1; | ||
| 21 | float alp_2; | ||
| 22 | float bet_2; | ||
| 23 | float alp_3; | ||
| 24 | float bet_3; | ||
| 25 | float tau_4; | ||
| 26 | }; | ||
| 27 | |||
| 28 | struct cmaes_result { | ||
| 29 | float best_x[CMA_DIM]; | ||
| 30 | float best_f; | ||
| 31 | int best_generation; | ||
| 32 | }; | ||
| 33 | |||
| 34 | struct chromosome_cma_config { | ||
| 35 | /* rho = D * [rho_min_rel, rho_max_rel] */ | ||
| 36 | float rho_min_rel; | ||
| 37 | float rho_max_rel; | ||
| 38 | |||
| 39 | /* tau in [tau_min, 1 - tau_min] */ | ||
| 40 | float tau_min; | ||
| 41 | |||
| 42 | /* alpha / beta bounds */ | ||
| 43 | float alp_limit; | ||
| 44 | float bet_limit; | ||
| 45 | }; | ||
| 46 | |||
| 47 | struct spline_objective_ctx { | ||
| 48 | struct input in; | ||
| 49 | struct chromosome_cma_config cfg; | ||
| 50 | const struct fitness_weights *weights; | ||
| 51 | float hard_fail_cost; | ||
| 52 | }; | ||
| 53 | |||
| 54 | typedef float (*cma_objective_fn) (const float *x, int n, void *userdata); | ||
| 55 | |||
| 56 | struct cma_rng { | ||
| 57 | uint64_t state; | ||
| 58 | int has_spare; | ||
| 59 | float spare; | ||
| 60 | }; | ||
| 61 | |||
| 62 | struct cmaes { | ||
| 63 | int n; | ||
| 64 | int lambda; | ||
| 65 | int mu; | ||
| 66 | |||
| 67 | int generation; | ||
| 68 | uint64_t evaluations; | ||
| 69 | |||
| 70 | float sigma; | ||
| 71 | float chi_n; | ||
| 72 | float mueff; | ||
| 73 | |||
| 74 | float cc, cs, c1, cmu, damps; | ||
| 75 | |||
| 76 | float *weights; /* mu */ | ||
| 77 | |||
| 78 | float *xmean; /* n */ | ||
| 79 | float *xold; /* n */ | ||
| 80 | float *pc; /* n */ | ||
| 81 | float *ps; /* n */ | ||
| 82 | |||
| 83 | float *C; /* n*n */ | ||
| 84 | float *B; /* n*n */ | ||
| 85 | float *D; /* n */ | ||
| 86 | float *BD; /* n*n */ | ||
| 87 | float *invsqrtC; /* n*n */ | ||
| 88 | |||
| 89 | float *ary; /* lambda*n : y_k = B D z_k */ | ||
| 90 | float *arx; /* lambda*n : x_k = mean + sigma*y_k */ | ||
| 91 | |||
| 92 | float *fitness; /* lambda */ | ||
| 93 | int *index; /* lambda */ | ||
| 94 | |||
| 95 | struct cma_rng rng; | ||
| 96 | }; | ||
| 97 | |||
| 98 | void chromosome_to_control_points (bezier5, struct input, struct chromosome); | ||
| 99 | void cma_decode_chromosome (struct chromosome *, | ||
| 100 | const float z[CMA_DIM], | ||
| 101 | const struct input *, | ||
| 102 | const struct chromosome_cma_config *); | ||
| 103 | void cma_encode_chromosome (float z[CMA_DIM], const struct chromosome *, | ||
| 104 | const struct input *, | ||
| 105 | const struct chromosome_cma_config *); | ||
| 106 | |||
| 107 | int cmaes_default_lambda (int n); | ||
| 108 | float cmaes_expected_norm (int n); | ||
| 109 | int cmaes_init (struct cmaes *, int n, const float *mean0, float sigma0, | ||
| 110 | int lambda, uint64_t seed); | ||
| 111 | void cmaes_free (struct cmaes *); | ||
| 112 | void cmaes_ask (struct cmaes *); | ||
| 113 | void cmaes_sort_indices (struct cmaes *); | ||
| 114 | void cmaes_tell (struct cmaes *, const float *fitness); | ||
| 115 | const float *cmaes_candidate (const struct cmaes *es, int k); | ||
| 116 | int cmaes_should_stop (const struct cmaes *); | ||
| 117 | struct cmaes_result cmaes_run (struct cmaes *, int max_generations, | ||
| 118 | cma_objective_fn objective, void *userdata); | ||
| 119 | float spline_cma_objective (const float *z, int n, void *userdata); | ||
| 120 | void example_cmaes_usage (const struct input *, | ||
| 121 | const struct chromosome *nominal, | ||
| 122 | const struct fitness_weights *, | ||
| 123 | struct chromosome *best_chromosome_out, | ||
| 124 | float *best_cost_out); | ||
| 125 | |||
| 126 | #endif // INCLUDED_CMA_ES_H | ||
