From 5b4a191d1cf310a3d7a99351e716a1a60004be69 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Wed, 11 Nov 2026 09:32:30 +0300 Subject: publish --- src/cma-es.h | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/cma-es.h (limited to 'src/cma-es.h') 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 @@ +#ifndef INCLUDED_CMA_ES_H +#define INCLUDED_CMA_ES_H + +#include + +#define CMA_DIM 7 + +struct input { + Vector2 r_beg; + Vector2 r_end; + Vector2 v_beg; + Vector2 v_end; + float v_max; + float a_max; + float j_max; +}; + +struct chromosome { + float rho; + float tau_1; + float alp_2; + float bet_2; + float alp_3; + float bet_3; + float tau_4; +}; + +struct cmaes_result { + float best_x[CMA_DIM]; + float best_f; + int best_generation; +}; + +struct chromosome_cma_config { + /* rho = D * [rho_min_rel, rho_max_rel] */ + float rho_min_rel; + float rho_max_rel; + + /* tau in [tau_min, 1 - tau_min] */ + float tau_min; + + /* alpha / beta bounds */ + float alp_limit; + float bet_limit; +}; + +struct spline_objective_ctx { + struct input in; + struct chromosome_cma_config cfg; + const struct fitness_weights *weights; + float hard_fail_cost; +}; + +typedef float (*cma_objective_fn) (const float *x, int n, void *userdata); + +struct cma_rng { + uint64_t state; + int has_spare; + float spare; +}; + +struct cmaes { + int n; + int lambda; + int mu; + + int generation; + uint64_t evaluations; + + float sigma; + float chi_n; + float mueff; + + float cc, cs, c1, cmu, damps; + + float *weights; /* mu */ + + float *xmean; /* n */ + float *xold; /* n */ + float *pc; /* n */ + float *ps; /* n */ + + float *C; /* n*n */ + float *B; /* n*n */ + float *D; /* n */ + float *BD; /* n*n */ + float *invsqrtC; /* n*n */ + + float *ary; /* lambda*n : y_k = B D z_k */ + float *arx; /* lambda*n : x_k = mean + sigma*y_k */ + + float *fitness; /* lambda */ + int *index; /* lambda */ + + struct cma_rng rng; +}; + +void chromosome_to_control_points (bezier5, struct input, struct chromosome); +void cma_decode_chromosome (struct chromosome *, + const float z[CMA_DIM], + const struct input *, + const struct chromosome_cma_config *); +void cma_encode_chromosome (float z[CMA_DIM], const struct chromosome *, + const struct input *, + const struct chromosome_cma_config *); + +int cmaes_default_lambda (int n); +float cmaes_expected_norm (int n); +int cmaes_init (struct cmaes *, int n, const float *mean0, float sigma0, + int lambda, uint64_t seed); +void cmaes_free (struct cmaes *); +void cmaes_ask (struct cmaes *); +void cmaes_sort_indices (struct cmaes *); +void cmaes_tell (struct cmaes *, const float *fitness); +const float *cmaes_candidate (const struct cmaes *es, int k); +int cmaes_should_stop (const struct cmaes *); +struct cmaes_result cmaes_run (struct cmaes *, int max_generations, + cma_objective_fn objective, void *userdata); +float spline_cma_objective (const float *z, int n, void *userdata); +void example_cmaes_usage (const struct input *, + const struct chromosome *nominal, + const struct fitness_weights *, + struct chromosome *best_chromosome_out, + float *best_cost_out); + +#endif // INCLUDED_CMA_ES_H -- cgit v1.3.1