Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 12 additions & 25 deletions include/bounds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,10 @@ namespace bounds

struct BoundCorrection
{
virtual ~BoundCorrection() = default;
Vector db;
Float diameter;
size_t n_out_of_bounds = 0;
bool has_bounds;

BoundCorrection(const Vector &lb, const Vector &ub) : db(ub - lb),
diameter((ub - lb).norm()),
has_bounds(true)
{
//! find a better way
if (!std::isfinite(diameter))
{
diameter = 10;
has_bounds = false;
}
}
virtual ~BoundCorrection() = default;
BoundCorrection() = default;

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

Expand Down Expand Up @@ -81,7 +68,7 @@ namespace bounds
{
sampling::Gaussian sampler;

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.)) {}
COTN(const size_t d) : BoundCorrection(), sampler(d, rng::normal<Float>(0, 1.0 / 3.)) {}

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

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

Vector correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings) override;
};
Expand All @@ -116,26 +103,26 @@ namespace bounds
Vector correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings) override;
};

inline std::shared_ptr<BoundCorrection> get(const parameters::CorrectionMethod &m, const Vector &lb, const Vector &ub)
inline std::shared_ptr<BoundCorrection> get(const parameters::CorrectionMethod &m, const size_t d)
{
using namespace parameters;
switch (m)
{
case CorrectionMethod::MIRROR:
return std::make_shared<Mirror>(lb, ub);
return std::make_shared<Mirror>();
case CorrectionMethod::COTN:
return std::make_shared<COTN>(lb, ub);
return std::make_shared<COTN>(d);
case CorrectionMethod::UNIFORM_RESAMPLE:
return std::make_shared<UniformResample>(lb, ub);
return std::make_shared<UniformResample>(d);
case CorrectionMethod::SATURATE:
return std::make_shared<Saturate>(lb, ub);
return std::make_shared<Saturate>();
case CorrectionMethod::TOROIDAL:
return std::make_shared<Toroidal>(lb, ub);
return std::make_shared<Toroidal>();
case CorrectionMethod::RESAMPLE:
return std::make_shared<Resample>(lb, ub);
return std::make_shared<Resample>();
default:
case CorrectionMethod::NONE:
return std::make_shared<NoCorrection>(lb, ub);
return std::make_shared<NoCorrection>();
}
};
}
7 changes: 7 additions & 0 deletions include/center_placement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ namespace center
void operator()(parameters::Parameters &p) override;
};

struct Center : Placement
{
void operator()(parameters::Parameters &p) override;
};


inline std::shared_ptr<Placement> get(const parameters::CenterPlacement &p)
{
Expand All @@ -41,6 +46,8 @@ namespace center
return std::make_shared<Uniform>();
case CenterPlacement::ZERO:
return std::make_shared<Zero>();
case CenterPlacement::CENTER:
return std::make_shared<Center>();
default:
case CenterPlacement::X0:
return std::make_shared<X0>();
Expand Down
12 changes: 12 additions & 0 deletions include/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ inline std::ostream &operator<<(std::ostream &os, const Solution &s)

namespace utils
{
/**
* @brief Sort an array of indexes idx inplace based
* based on comparing values in v using std::stable_sort
*
* @param v Vector
* @param idx std::vector<size_t>
* @return void
*/
void sort_index_inplace(const Vector &v, std::vector<size_t>& idx);

void sort_index_inplace(const std::vector<size_t> &v, std::vector<size_t>& idx);

/**
* @brief Return an array of indexes of the sorted array
* sort indexes based on comparing values in v using std::stable_sort instead
Expand Down
4 changes: 2 additions & 2 deletions include/es.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace es
x(x0), f(f0), t(1), budget(budget), target(target),
rejection_sampling(modules.bound_correction == parameters::CorrectionMethod::RESAMPLE),
sampler(sampling::get(d, modules, 1)),
corrector(bounds::get(modules.bound_correction, Vector::Ones(d) * -5.0, Vector::Ones(d) * 5.0)),
corrector(bounds::get(modules.bound_correction, d)),
settings{d}
{
settings.modules = modules;
Expand Down Expand Up @@ -74,7 +74,7 @@ namespace es
sampler(sampling::get(d, modules, lambda)),
sigma_sampler(std::make_shared<sampling::Gaussian>(d)),
rejection_sampling(modules.bound_correction == parameters::CorrectionMethod::RESAMPLE),
corrector(bounds::get(modules.bound_correction, Vector::Ones(d) * -5.0, Vector::Ones(d) * 5.0)),
corrector(bounds::get(modules.bound_correction, d)),
settings(d)
{
// tau = 1.0 / sampler->expected_length();
Expand Down
1 change: 1 addition & 0 deletions include/modules.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ namespace parameters
X0,
ZERO,
UNIFORM,
CENTER
};

struct Modules
Expand Down
4 changes: 4 additions & 0 deletions include/mutation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ namespace mutation

Vector combined;

std::vector<size_t> idx;
std::vector<size_t> oidx;


using Strategy::Strategy;

void adapt(const parameters::Weights& w, std::shared_ptr<matrix_adaptation::Adaptation> adaptation, Population& pop,
Expand Down
25 changes: 21 additions & 4 deletions include/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,23 @@ namespace parameters
size_t mu0;

std::optional<Vector> x0;

Vector lb;
Vector ub;
Vector db;
Vector center;
Float diameter;
Float volume;
bool has_bounds;

std::optional<Float> cs;
std::optional<Float> cc;
std::optional<Float> cmu;
std::optional<Float> c1;
std::optional<Float> damps;
std::optional<Float> acov;

bool verbose;
Float volume;
bool one_plus_one;

Settings(size_t dim,
Expand Down Expand Up @@ -60,15 +67,19 @@ namespace parameters
mu0(mu.value_or(lambda0 / 2)),
x0(x0),
lb(lb.value_or(Vector::Ones(dim) * -5)),
ub(ub.value_or(Vector::Ones(dim)* 5)),
ub(ub.value_or(Vector::Ones(dim) * 5)),
db(this->ub - this->lb),
center(this->lb + (db * 0.5)),
diameter(db.norm()),
volume(db.prod()),
has_bounds(true),
cs(cs),
cc(cc),
cmu(cmu),
c1(c1),
damps(damps),
acov(acov),
verbose(verbose),
volume(0.0),
one_plus_one(false)
{
if (modules.mirrored == Mirror::PAIRWISE and lambda0 % 2 != 0)
Expand Down Expand Up @@ -113,7 +124,13 @@ namespace parameters
if (modules.restart_strategy == RestartStrategyType::BIPOP || modules.restart_strategy == RestartStrategyType::IPOP)
modules.restart_strategy = RestartStrategyType::RESTART;
}
volume = (this->ub.cwiseMin(10 * sigma0) - this->lb.cwiseMax(-10 * sigma0)).prod();

if (!std::isfinite(diameter)){
has_bounds = false;
diameter = 10;
volume = pow(diameter, dim);
center.setConstant(0);
}
}
};

Expand Down
14 changes: 7 additions & 7 deletions src/bounds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace bounds

Vector BoundCorrection::delta_out_of_bounds(const Vector &xi, const Mask &oob, const parameters::Settings &settings) const
{
return (oob).select((xi - settings.lb).cwiseQuotient(db), xi);
return (oob).select((xi - settings.lb).cwiseQuotient(settings.db), xi);
;
}

void BoundCorrection::correct(const Eigen::Index i, parameters::Parameters &p)
{
if (!has_bounds)
if (!p.settings.has_bounds)
return;

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

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

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

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

Vector Toroidal::correct_x(const Vector &xi, const Mask &oob, const Float sigma, const parameters::Settings &settings)
{
const Vector y = delta_out_of_bounds(xi, oob, settings);
return (oob).select(
settings.lb.array() + db.array() * (y.array() - y.array().floor()).abs(), y);
settings.lb.array() + settings.db.array() * (y.array() - y.array().floor()).abs(), y);
}
}
12 changes: 7 additions & 5 deletions src/center_placement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ namespace center
{
void X0::operator()(parameters::Parameters &p)
{
const auto default_x0 = (p.settings.lb + p.settings.ub) / 2.0;

p.adaptation->m = p.settings.x0.value_or(default_x0);
p.adaptation->m = p.settings.x0.value_or(p.settings.center);
}

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

void Zero::operator()(parameters::Parameters &p)
{
p.adaptation->m.setZero();
}

void Center::operator()(parameters::Parameters& p)
{
p.adaptation->m = p.settings.center;
}
}
26 changes: 16 additions & 10 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,37 @@ namespace constants

namespace utils
{
std::vector<size_t> sort_indexes(const Vector& v)
void sort_index_inplace(const Vector& v, std::vector<size_t>& idx)
{
std::vector<size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);

std::stable_sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2)
{
return v[i1] < v[i2];
});

return idx;
}

std::vector<size_t> sort_indexes(const std::vector<size_t>& v)
void sort_index_inplace(const std::vector<size_t>& v, std::vector<size_t>& idx)
{
std::vector<size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);

std::stable_sort(idx.begin(), idx.end(),
[&v](size_t i1, size_t i2)
{
return v[i1] < v[i2];
});
}

std::vector<size_t> sort_indexes(const Vector& v)
{
std::vector<size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);
sort_index_inplace(v, idx);
return idx;
}

std::vector<size_t> sort_indexes(const std::vector<size_t>& v)
{
std::vector<size_t> idx(v.size());
std::iota(idx.begin(), idx.end(), 0);
sort_index_inplace(v, idx);
return idx;
}

Expand Down
Loading