Skip to content

Commit 25b892e

Browse files
authored
Merge pull request #56 from IOHprofiler/center-domain
center placement
2 parents 254f8a8 + 3e325d4 commit 25b892e

File tree

14 files changed

+133
-73
lines changed

14 files changed

+133
-73
lines changed

include/bounds.hpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,10 @@ namespace bounds
2121

2222
struct BoundCorrection
2323
{
24-
virtual ~BoundCorrection() = default;
25-
Vector db;
26-
Float diameter;
2724
size_t n_out_of_bounds = 0;
28-
bool has_bounds;
2925

30-
BoundCorrection(const Vector &lb, const Vector &ub) : db(ub - lb),
31-
diameter((ub - lb).norm()),
32-
has_bounds(true)
33-
{
34-
//! find a better way
35-
if (!std::isfinite(diameter))
36-
{
37-
diameter = 10;
38-
has_bounds = false;
39-
}
40-
}
26+
virtual ~BoundCorrection() = default;
27+
BoundCorrection() = default;
4128

4229
void correct(const Eigen::Index i, parameters::Parameters &p);
4330

@@ -81,7 +68,7 @@ namespace bounds
8168
{
8269
sampling::Gaussian sampler;
8370

84-
COTN(Eigen::Ref<const Vector> lb, Eigen::Ref<const Vector> ub) : BoundCorrection(lb, ub), sampler(static_cast<size_t>(lb.size()), rng::normal<Float>(0, 1.0 / 3.)) {}
71+
COTN(const size_t d) : BoundCorrection(), sampler(d, rng::normal<Float>(0, 1.0 / 3.)) {}
8572

8673
Vector correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings) override;
8774
};
@@ -97,7 +84,7 @@ namespace bounds
9784
{
9885
sampling::Uniform sampler;
9986

100-
UniformResample(Eigen::Ref<const Vector> lb, Eigen::Ref<const Vector> ub) : BoundCorrection(lb, ub), sampler(static_cast<size_t>(lb.size())) {}
87+
UniformResample(const size_t d) : BoundCorrection(), sampler(d) {}
10188

10289
Vector correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings) override;
10390
};
@@ -116,26 +103,26 @@ namespace bounds
116103
Vector correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings) override;
117104
};
118105

119-
inline std::shared_ptr<BoundCorrection> get(const parameters::CorrectionMethod &m, const Vector &lb, const Vector &ub)
106+
inline std::shared_ptr<BoundCorrection> get(const parameters::CorrectionMethod &m, const size_t d)
120107
{
121108
using namespace parameters;
122109
switch (m)
123110
{
124111
case CorrectionMethod::MIRROR:
125-
return std::make_shared<Mirror>(lb, ub);
112+
return std::make_shared<Mirror>();
126113
case CorrectionMethod::COTN:
127-
return std::make_shared<COTN>(lb, ub);
114+
return std::make_shared<COTN>(d);
128115
case CorrectionMethod::UNIFORM_RESAMPLE:
129-
return std::make_shared<UniformResample>(lb, ub);
116+
return std::make_shared<UniformResample>(d);
130117
case CorrectionMethod::SATURATE:
131-
return std::make_shared<Saturate>(lb, ub);
118+
return std::make_shared<Saturate>();
132119
case CorrectionMethod::TOROIDAL:
133-
return std::make_shared<Toroidal>(lb, ub);
120+
return std::make_shared<Toroidal>();
134121
case CorrectionMethod::RESAMPLE:
135-
return std::make_shared<Resample>(lb, ub);
122+
return std::make_shared<Resample>();
136123
default:
137124
case CorrectionMethod::NONE:
138-
return std::make_shared<NoCorrection>(lb, ub);
125+
return std::make_shared<NoCorrection>();
139126
}
140127
};
141128
}

include/center_placement.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ namespace center
3030
void operator()(parameters::Parameters &p) override;
3131
};
3232

33+
struct Center : Placement
34+
{
35+
void operator()(parameters::Parameters &p) override;
36+
};
37+
3338

3439
inline std::shared_ptr<Placement> get(const parameters::CenterPlacement &p)
3540
{
@@ -41,6 +46,8 @@ namespace center
4146
return std::make_shared<Uniform>();
4247
case CenterPlacement::ZERO:
4348
return std::make_shared<Zero>();
49+
case CenterPlacement::CENTER:
50+
return std::make_shared<Center>();
4451
default:
4552
case CenterPlacement::X0:
4653
return std::make_shared<X0>();

include/common.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ inline std::ostream &operator<<(std::ostream &os, const Solution &s)
128128

129129
namespace utils
130130
{
131+
/**
132+
* @brief Sort an array of indexes idx inplace based
133+
* based on comparing values in v using std::stable_sort
134+
*
135+
* @param v Vector
136+
* @param idx std::vector<size_t>
137+
* @return void
138+
*/
139+
void sort_index_inplace(const Vector &v, std::vector<size_t>& idx);
140+
141+
void sort_index_inplace(const std::vector<size_t> &v, std::vector<size_t>& idx);
142+
131143
/**
132144
* @brief Return an array of indexes of the sorted array
133145
* sort indexes based on comparing values in v using std::stable_sort instead

include/es.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace es
2121
x(x0), f(f0), t(1), budget(budget), target(target),
2222
rejection_sampling(modules.bound_correction == parameters::CorrectionMethod::RESAMPLE),
2323
sampler(sampling::get(d, modules, 1)),
24-
corrector(bounds::get(modules.bound_correction, Vector::Ones(d) * -5.0, Vector::Ones(d) * 5.0)),
24+
corrector(bounds::get(modules.bound_correction, d)),
2525
settings{d}
2626
{
2727
settings.modules = modules;
@@ -74,7 +74,7 @@ namespace es
7474
sampler(sampling::get(d, modules, lambda)),
7575
sigma_sampler(std::make_shared<sampling::Gaussian>(d)),
7676
rejection_sampling(modules.bound_correction == parameters::CorrectionMethod::RESAMPLE),
77-
corrector(bounds::get(modules.bound_correction, Vector::Ones(d) * -5.0, Vector::Ones(d) * 5.0)),
77+
corrector(bounds::get(modules.bound_correction, d)),
7878
settings(d)
7979
{
8080
// tau = 1.0 / sampler->expected_length();

include/modules.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ namespace parameters
8585
X0,
8686
ZERO,
8787
UNIFORM,
88+
CENTER
8889
};
8990

9091
struct Modules

include/mutation.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ namespace mutation
140140

141141
Vector combined;
142142

143+
std::vector<size_t> idx;
144+
std::vector<size_t> oidx;
145+
146+
143147
using Strategy::Strategy;
144148

145149
void adapt(const parameters::Weights& w, std::shared_ptr<matrix_adaptation::Adaptation> adaptation, Population& pop,

include/settings.hpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,23 @@ namespace parameters
1919
size_t mu0;
2020

2121
std::optional<Vector> x0;
22+
2223
Vector lb;
2324
Vector ub;
25+
Vector db;
26+
Vector center;
27+
Float diameter;
28+
Float volume;
29+
bool has_bounds;
30+
2431
std::optional<Float> cs;
2532
std::optional<Float> cc;
2633
std::optional<Float> cmu;
2734
std::optional<Float> c1;
2835
std::optional<Float> damps;
2936
std::optional<Float> acov;
37+
3038
bool verbose;
31-
Float volume;
3239
bool one_plus_one;
3340

3441
Settings(size_t dim,
@@ -60,15 +67,19 @@ namespace parameters
6067
mu0(mu.value_or(lambda0 / 2)),
6168
x0(x0),
6269
lb(lb.value_or(Vector::Ones(dim) * -5)),
63-
ub(ub.value_or(Vector::Ones(dim)* 5)),
70+
ub(ub.value_or(Vector::Ones(dim) * 5)),
71+
db(this->ub - this->lb),
72+
center(this->lb + (db * 0.5)),
73+
diameter(db.norm()),
74+
volume(db.prod()),
75+
has_bounds(true),
6476
cs(cs),
6577
cc(cc),
6678
cmu(cmu),
6779
c1(c1),
6880
damps(damps),
6981
acov(acov),
7082
verbose(verbose),
71-
volume(0.0),
7283
one_plus_one(false)
7384
{
7485
if (modules.mirrored == Mirror::PAIRWISE and lambda0 % 2 != 0)
@@ -113,7 +124,13 @@ namespace parameters
113124
if (modules.restart_strategy == RestartStrategyType::BIPOP || modules.restart_strategy == RestartStrategyType::IPOP)
114125
modules.restart_strategy = RestartStrategyType::RESTART;
115126
}
116-
volume = (this->ub.cwiseMin(10 * sigma0) - this->lb.cwiseMax(-10 * sigma0)).prod();
127+
128+
if (!std::isfinite(diameter)){
129+
has_bounds = false;
130+
diameter = 10;
131+
volume = pow(diameter, dim);
132+
center.setConstant(0);
133+
}
117134
}
118135
};
119136

src/bounds.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ namespace bounds
2727

2828
Vector BoundCorrection::delta_out_of_bounds(const Vector &xi, const Mask &oob, const parameters::Settings &settings) const
2929
{
30-
return (oob).select((xi - settings.lb).cwiseQuotient(db), xi);
30+
return (oob).select((xi - settings.lb).cwiseQuotient(settings.db), xi);
3131
;
3232
}
3333

3434
void BoundCorrection::correct(const Eigen::Index i, parameters::Parameters &p)
3535
{
36-
if (!has_bounds)
36+
if (!p.settings.has_bounds)
3737
return;
3838

3939
const auto oob = is_out_of_bounds(p.pop.X.col(i), p.settings);
@@ -53,33 +53,33 @@ namespace bounds
5353
{
5454
const Vector y = delta_out_of_bounds(xi, oob, settings);
5555
return (oob).select(
56-
settings.lb.array() + db.array() * ((y.array() > 0).cast<Float>() - (sigma * sampler().array().abs())).abs(), y);
56+
settings.lb.array() + settings.db.array() * ((y.array() > 0).cast<Float>() - (sigma * sampler().array().abs())).abs(), y);
5757
}
5858

5959
Vector Mirror::correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings)
6060
{
6161
const Vector y = delta_out_of_bounds(xi, oob, settings);
6262
return (oob).select(
63-
settings.lb.array() + db.array() * (y.array() - y.array().floor() - y.array().floor().unaryExpr(&modulo2)).abs(),
63+
settings.lb.array() + settings.db.array() * (y.array() - y.array().floor() - y.array().floor().unaryExpr(&modulo2)).abs(),
6464
y);
6565
}
6666

6767
Vector UniformResample::correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings)
6868
{
69-
return (oob).select(settings.lb + sampler().cwiseProduct(db), xi);
69+
return (oob).select(settings.lb + sampler().cwiseProduct(settings.db), xi);
7070
}
7171

7272
Vector Saturate::correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings)
7373
{
7474
const Vector y = delta_out_of_bounds(xi, oob, settings);
7575
return (oob).select(
76-
settings.lb.array() + db.array() * (y.array() > 0).cast<Float>(), y);
76+
settings.lb.array() + settings.db.array() * (y.array() > 0).cast<Float>(), y);
7777
}
7878

7979
Vector Toroidal::correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings)
8080
{
8181
const Vector y = delta_out_of_bounds(xi, oob, settings);
8282
return (oob).select(
83-
settings.lb.array() + db.array() * (y.array() - y.array().floor()).abs(), y);
83+
settings.lb.array() + settings.db.array() * (y.array() - y.array().floor()).abs(), y);
8484
}
8585
}

src/center_placement.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@ namespace center
55
{
66
void X0::operator()(parameters::Parameters &p)
77
{
8-
const auto default_x0 = (p.settings.lb + p.settings.ub) / 2.0;
9-
10-
p.adaptation->m = p.settings.x0.value_or(default_x0);
8+
p.adaptation->m = p.settings.x0.value_or(p.settings.center);
119
}
1210

1311
void Uniform::operator()(parameters::Parameters &p)
1412
{
15-
// Only works for square spaces
16-
p.adaptation->m = Vector::Random(p.settings.dim) * p.settings.ub;
13+
p.adaptation->m = p.settings.lb + (Vector::Random(p.settings.dim) * p.settings.db);
1714
}
1815

1916
void Zero::operator()(parameters::Parameters &p)
2017
{
2118
p.adaptation->m.setZero();
2219
}
20+
21+
void Center::operator()(parameters::Parameters& p)
22+
{
23+
p.adaptation->m = p.settings.center;
24+
}
2325
}

src/common.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,31 +20,37 @@ namespace constants
2020

2121
namespace utils
2222
{
23-
std::vector<size_t> sort_indexes(const Vector& v)
23+
void sort_index_inplace(const Vector& v, std::vector<size_t>& idx)
2424
{
25-
std::vector<size_t> idx(v.size());
26-
std::iota(idx.begin(), idx.end(), 0);
27-
2825
std::stable_sort(idx.begin(), idx.end(),
2926
[&v](size_t i1, size_t i2)
3027
{
3128
return v[i1] < v[i2];
3229
});
33-
34-
return idx;
3530
}
3631

37-
std::vector<size_t> sort_indexes(const std::vector<size_t>& v)
32+
void sort_index_inplace(const std::vector<size_t>& v, std::vector<size_t>& idx)
3833
{
39-
std::vector<size_t> idx(v.size());
40-
std::iota(idx.begin(), idx.end(), 0);
41-
4234
std::stable_sort(idx.begin(), idx.end(),
4335
[&v](size_t i1, size_t i2)
4436
{
4537
return v[i1] < v[i2];
4638
});
39+
}
40+
41+
std::vector<size_t> sort_indexes(const Vector& v)
42+
{
43+
std::vector<size_t> idx(v.size());
44+
std::iota(idx.begin(), idx.end(), 0);
45+
sort_index_inplace(v, idx);
46+
return idx;
47+
}
4748

49+
std::vector<size_t> sort_indexes(const std::vector<size_t>& v)
50+
{
51+
std::vector<size_t> idx(v.size());
52+
std::iota(idx.begin(), idx.end(), 0);
53+
sort_index_inplace(v, idx);
4854
return idx;
4955
}
5056

0 commit comments

Comments
 (0)