-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathes.cpp
More file actions
100 lines (86 loc) · 2.63 KB
/
es.cpp
File metadata and controls
100 lines (86 loc) · 2.63 KB
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
#include "es.hpp"
#include "bounds.hpp"
namespace es
{
Vector OnePlusOneES::sample()
{
size_t n_rej = 0;
Vector x1;
do
{
const Vector z = (*sampler)();
x1 = x + sigma * z;
const auto mask = corrector->is_out_of_bounds(x, settings);
if (mask.any())
x1 = corrector->correct_x(x1, mask, sigma, settings);
} while (rejection_sampling && n_rej++ < 5*d && bounds::any_out_of_bounds(x1, settings.lb, settings.ub) );
return x1;
}
void OnePlusOneES::step(FunctionType &objective)
{
const auto x1 = sample();
const auto f1 = objective(x1);
const bool has_improved = f1 < f;
sigma *= pow(std::exp(static_cast<Float>(has_improved) - 0.2), decay);
if (has_improved)
{
x = x1;
f = f1;
}
t++;
}
void OnePlusOneES::operator()(FunctionType &objective)
{
while (t < budget && f > target)
step(objective);
}
Vector MuCommaLambdaES::sample(const Vector si)
{
size_t n_rej = 0;
Vector x;
do
{
const Vector z = (*sampler)();
x = m.array() + (si.array() * z.array());
const auto mask = corrector->is_out_of_bounds(x, settings);
if (mask.any())
x = corrector->correct_x(x, mask, si.mean(), settings);
} while (rejection_sampling && n_rej++ < 5*d && bounds::any_out_of_bounds(x, settings.lb, settings.ub));
return x;
}
void MuCommaLambdaES::step(FunctionType &objective)
{
static sampling::Gaussian g_sigma_sampler(1);
for (size_t i = 0; i < lambda; i++)
{
const Float psi_k = std::exp(tau * g_sigma_sampler()[0]);
const Vector psi_kv = (tau_i * (*sigma_sampler)()).array().exp().matrix();
S.col(i) = sigma.array() * psi_kv.array() * psi_k;
X.col(i) = sample(S.col(i));
f(i) = objective(X.col(i));
e++;
}
const auto idx = utils::sort_indexes(f);
X = X(Eigen::all, idx).eval();
S = S(Eigen::all, idx).eval();
f = f(idx).eval();
if (f[0] < f_min)
{
f_min = f[0];
x_min = X.col(0);
}
sigma.setZero();
m.setZero();
for (size_t i = 0; i < mu; i++)
{
m += mu_inv * X.col(i);
sigma += mu_inv * S.col(i);
}
t++;
}
void MuCommaLambdaES::operator()(FunctionType &objective)
{
while (e < budget && f_min > target)
step(objective);
}
}