Skip to content

Commit f73cd95

Browse files
committed
weights
1 parent f6c3fe7 commit f73cd95

7 files changed

Lines changed: 126 additions & 34 deletions

File tree

include/restart_criteria.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ namespace restart
119119

120120
struct TolX : Criterion
121121
{
122-
static inline Float tolerance = 10e-12;
122+
static inline Float tolerance = 1e-12;
123123
Vector tolx_vector;
124124
TolX() : Criterion("TolX") {}
125125
void update(const parameters::Parameters& p) override;

include/settings.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ namespace parameters
2525
std::optional<Float> cc;
2626
std::optional<Float> cmu;
2727
std::optional<Float> c1;
28+
std::optional<Float> damps;
2829
bool verbose;
2930
Float volume;
3031
bool one_plus_one;
32+
bool reformulated_learning_rate;
3133

3234
Settings(size_t dim,
3335
std::optional<Modules> mod = std::nullopt,
@@ -44,8 +46,10 @@ namespace parameters
4446
std::optional<Float> cc = std::nullopt,
4547
std::optional<Float> cmu = std::nullopt,
4648
std::optional<Float> c1 = std::nullopt,
49+
std::optional<Float> damps = std::nullopt,
4750
bool verbose = true,
48-
bool always_compute_eigv = false
51+
bool always_compute_eigv = false,
52+
bool reformulated_learning_rate = false
4953
) : dim(dim),
5054
modules(mod.value_or(Modules())),
5155
target(target),
@@ -61,9 +65,11 @@ namespace parameters
6165
cc(cc),
6266
cmu(cmu),
6367
c1(c1),
68+
damps(damps),
6469
verbose(verbose),
6570
volume(0.0),
66-
one_plus_one(false)
71+
one_plus_one(false),
72+
reformulated_learning_rate(reformulated_learning_rate)
6773
{
6874
if (modules.mirrored == Mirror::PAIRWISE and lambda0 % 2 != 0)
6975
lambda0++;

scripts/matrix/get_data.py

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,51 @@
1313
np.random.seed(12)
1414

1515
DIMS = 2, 3, 5, 10, 20, 40, #100
16-
FUNCTIONS = [3, 4, 5, 7] + list(range(15, 25)) #[1, 2, 6, 8, 9, 10, 11, 12, 13, 14]
16+
FUNCTIONS = [13] #[3, 4, 5, 7] + list(range(15, 25)) #[1, 2, 6, 8, 9, 10, 11, 12, 13, 14]
1717
N_REPEATS = 100
1818
BUDGET = 100_000
1919
ROOT = "data"
2020

2121

22+
def ert(runs, target = 1e-8):
23+
total_evals = 0
24+
n_suc = 0
25+
for row in runs:
26+
total_evals += row['evals']
27+
if row['best_y'] <= target:
28+
n_suc += 1
29+
30+
if n_suc <= 0:
31+
return float("inf")
32+
return total_evals / n_suc
33+
2234
def run_modma(problem: ioh.ProblemType,
2335
x0: np.ndarray,
2436
logger_obj,
2537
matrix_adaptation = modcma.options.COVARIANCE
2638
):
2739
modules = modcma.parameters.Modules()
2840
modules.matrix_adaptation = matrix_adaptation
41+
modules.ssa = modcma.options.StepSizeAdaptation.CSA
2942
modules.restart_strategy = modcma.options.RestartStrategy.STOP
3043

44+
options = pycma.CMAOptions()
45+
options['CMA_active'] = False
46+
options["verbose"] = -1
47+
options["CMA_diagonal"] = False
48+
options["CSA_squared"] = False
49+
options['conditioncov_alleviate'] = False
50+
options['ftarget'] = problem.optimum.y + 1e-8
51+
options['maxfevals'] = problem.meta_data.n_variables * BUDGET
52+
53+
pcma = pycma.CMAEvolutionStrategy(x0, 2.0, options=options)
54+
problem2 = ioh.get_problem(problem.meta_data.problem_id, 1, problem.meta_data.n_variables)
55+
56+
while not pcma.stop():
57+
X, y = pcma.ask_and_eval(problem2)
58+
pcma.tell(X, y)
59+
break
60+
3161
settings = modcma.Settings(
3262
problem.meta_data.n_variables,
3363
x0=x0,
@@ -37,10 +67,16 @@ def run_modma(problem: ioh.ProblemType,
3767
verbose=True,
3868
sigma0=2.0,
3969
target=problem.optimum.y + 1e-8,
40-
budget=problem.meta_data.n_variables * BUDGET
70+
budget=problem.meta_data.n_variables * BUDGET,
71+
cs=pcma.adapt_sigma.cs,
72+
c1=pcma.sp.c1,
73+
cc=pcma.sp.cc,
74+
cmu=pcma.sp.cmu,
4175
)
42-
76+
77+
4378
cma = modcma.ModularCMAES(settings)
79+
cma.p.weights.damps = pcma.adapt_sigma.damps
4480

4581
start = perf_counter()
4682
while not cma.break_conditions():
@@ -51,7 +87,18 @@ def run_modma(problem: ioh.ProblemType,
5187
if cma.p.criteria.any():
5288
logger_obj.update(cma.p.criteria.items)
5389

54-
cma.run(problem)
90+
# print("modcma")
91+
# print(cma.p.mutation.sigma)
92+
# print(cma.p.adaptation.C)
93+
# print(cma.p.pop.f)
94+
# print(cma.p.criteria.reason())
95+
# print(problem.state)
96+
# print("\npycma")
97+
# print(pcma.sigma)
98+
# print(pcma.sm.C)
99+
# print(problem2.state)
100+
101+
# cma.run(problem)
55102
stop = perf_counter()
56103
elapsed = stop - start
57104
return elapsed, cma.p.stats.t, problem.state.evaluations, cma.p.stats.n_updates
@@ -91,12 +138,16 @@ def collect(name, option):
91138
for d in DIMS:
92139
problem = ioh.get_problem(fid, 1, d)
93140
problem.attach_logger(logger)
141+
runs = []
94142
for i in range(N_REPEATS):
95143
modcma.utils.set_seed(21 + fid * d * i)
96144
collector.reset()
97145
run_modma(problem, np.zeros(d), collector, option)
98-
print(name, fid, d, problem.state.current_best_internal.y, problem.state.evaluations)
146+
# print(name, fid, d, problem.state.current_best_internal.y, problem.state.evaluations)
147+
runs.append(dict(evals=problem.state.evaluations, best_y=problem.state.current_best_internal.y))
99148
problem.reset()
149+
print("ert:", ert(runs))
150+
print()
100151

101152
def collect_modcma():
102153
options = modcma.options.MatrixAdaptationType.__members__
@@ -159,10 +210,12 @@ def collect_pycma():
159210

160211

161212
if __name__ == "__main__":
162-
p1 = Process(target=collect_modcma)
163-
p2 = Process(target=collect_pycma)
213+
# p1 = Process(target=collect_modcma)
214+
# p2 = Process(target=collect_pycma)
215+
216+
# p1.start()
217+
# p2.start()
218+
# p1.join()
219+
# p2.join()
164220

165-
p1.start()
166-
p2.start()
167-
p1.join()
168-
p2.join()
221+
collect("COVARIANCE-2", modcma.options.MatrixAdaptationType.COVARIANCE)

scripts/matrix/plots.ipynb

Lines changed: 40 additions & 16 deletions
Large diffs are not rendered by default.

src/interface.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ void define_parameters(py::module& main)
606606
std::optional<size_t>, std::optional<size_t>, std::optional<Vector>,
607607
std::optional<Vector>, std::optional<Vector>,
608608
std::optional<Float>, std::optional<Float>, std::optional<Float>,
609-
std::optional<Float>, bool, bool>(),
609+
std::optional<Float>, std::optional<Float>, bool, bool, bool>(),
610610
py::arg("dim"),
611611
py::arg("modules") = std::nullopt,
612612
py::arg("target") = std::nullopt,
@@ -622,8 +622,11 @@ void define_parameters(py::module& main)
622622
py::arg("cc") = std::nullopt,
623623
py::arg("cmu") = std::nullopt,
624624
py::arg("c1") = std::nullopt,
625+
py::arg("damps") = std::nullopt,
625626
py::arg("verbose") = false,
626-
py::arg("always_compute_eigv") = false
627+
py::arg("always_compute_eigv") = false,
628+
py::arg("reformulated_learning_rate") = false
629+
627630
)
628631
.def_readonly("dim", &Settings::dim)
629632
.def_readonly("modules", &Settings::modules)
@@ -640,8 +643,11 @@ void define_parameters(py::module& main)
640643
.def_readwrite("cc", &Settings::cc)
641644
.def_readwrite("cmu", &Settings::cmu)
642645
.def_readwrite("c1", &Settings::c1)
646+
.def_readwrite("damps", &Settings::damps)
643647
.def_readwrite("verbose", &Settings::verbose)
644648
.def_readonly("volume", &Settings::volume)
649+
.def_readonly("one_plus_one", &Settings::one_plus_one)
650+
.def_readonly("reformulated_learning_rate", &Settings::reformulated_learning_rate)
645651
.def("__repr__", [] (Settings& settings)
646652
{
647653
std::stringstream ss;

src/mutation.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ namespace mutation
6161
const Population& old_pop, const parameters::Stats& stats, const size_t lambda)
6262

6363
{
64-
sigma *= std::exp((w.cs / w.damps) * ((adaptation->ps.norm() / w.expected_length_z) - 1));
64+
Float l = (w.cs / w.damps) * ((adaptation->ps.norm() / w.expected_length_z) - 1);
65+
// Clamping as seen in pycma
66+
l = std::min(Float{1.0}, std::max(l, Float{-1.0}));
67+
sigma *= std::exp(l);
6568
}
6669

6770

src/weights.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ namespace parameters
123123
cmu = settings.cmu.value_or(get_default_cmu(settings, d, mueff, c1));
124124
cs = settings.cs.value_or(get_default_cs(settings, mueff, d));
125125
cc = settings.cmu.value_or(get_default_cc(settings, d, mueff, cs));
126-
damps = get_default_damps(settings, mueff, d, cs);
126+
damps = settings.damps.value_or(get_default_damps(settings, mueff, d, cs));
127127

128128
sqrt_cs_mueff = std::sqrt(cs * (2.0 - cs) * mueff);
129129
sqrt_cc_mueff = std::sqrt(cc * (2.0 - cc) * mueff);

0 commit comments

Comments
 (0)