aboutsummaryrefslogtreecommitdiff
path: root/src/cma-es.h
blob: 1e929e2352a2b1762165556ea96eabee028fbed6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef INCLUDED_CMA_ES_H
#define INCLUDED_CMA_ES_H

#include <stdint.h>

#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