Skip to content

Commit 69ea6fc

Browse files
committed
added scripts
1 parent 6d539b5 commit 69ea6fc

File tree

3 files changed

+115
-30
lines changed

3 files changed

+115
-30
lines changed

scripts/matrix/get_data.py

Lines changed: 59 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
from time import perf_counter
2-
2+
import warnings
33
import numpy as np
44
import modcma.c_maes as modcma
55
import ioh
66
import pandas as pd
77

88
from multiprocessing import Pool
9-
9+
import cma as pycma
1010

1111
from pprint import pprint
1212

1313
np.random.seed(12)
1414

15+
DIMS = 2, 3, 5, 10, 20, 40, 100
16+
FUNCTIONS = [1, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14]
17+
N_REPEATS = 100
18+
1519
def run_modma(problem: ioh.ProblemType,
1620
x0: np.ndarray,
1721
logger_obj,
@@ -68,16 +72,10 @@ def reset(self):
6872
for item in self.names:
6973
setattr(self, item, 0)
7074

71-
72-
if __name__ == "__main__":
73-
dims = 2, 3, 5, 10, 20, 40, 100
74-
functions = [1, 2, 6, 7, 8, 9, 10, 11, 12, 13, 14]
75-
76-
n_repeats = 100
75+
def collect_modcma():
7776
options = modcma.options.MatrixAdaptationType.__members__
7877
del options['COVARIANCE_NO_EIGV']
7978

80-
8179
def collect(name, option):
8280
logger = ioh.logger.Analyzer(
8381
folder_name=name,
@@ -86,11 +84,11 @@ def collect(name, option):
8684
)
8785
collector = RestartCollector()
8886
logger.add_run_attributes(collector, collector.names)
89-
for fid in functions:
90-
for d in dims:
87+
for fid in FUNCTIONS:
88+
for d in DIMS:
9189
problem = ioh.get_problem(fid, 1, d)
9290
problem.attach_logger(logger)
93-
for i in range(n_repeats):
91+
for i in range(N_REPEATS):
9492
modcma.utils.set_seed(21 + fid * d * i)
9593
collector.reset()
9694
run_modma(problem, np.zeros(d), collector, option)
@@ -100,6 +98,55 @@ def collect(name, option):
10098
with Pool(len(options)) as p:
10199
p.starmap(collect, options.items())
102100

101+
102+
def run_pycma(problem: ioh.ProblemType, x0: np.ndarray):
103+
options = pycma.CMAOptions()
104+
options['CMA_active'] = False
105+
options["verbose"] = -1
106+
options["CMA_diagonal"] = False
107+
108+
cma = pycma.CMAEvolutionStrategy(x0, 2.0, options=options)
109+
settings = modcma.Settings(problem.meta_data.n_variables)
110+
assert settings.lambda0 == cma.sp.popsize
111+
start = perf_counter()
112+
113+
target = problem.optimum.y + 1e-8
114+
budget = problem.meta_data.n_variables * 100_000
115+
116+
with warnings.catch_warnings():
117+
warnings.simplefilter("ignore")
118+
while problem.state.evaluations < budget:
119+
X, y = cma.ask_and_eval(problem)
120+
cma.tell(X, y)
121+
if problem.current_best.y <= target:
122+
break
123+
124+
stop = perf_counter()
125+
elapsed = stop - start
126+
127+
return elapsed, cma.countiter, problem.state.evaluations, cma.sm.count_eigen
128+
129+
def collect_pycma():
130+
logger = ioh.logger.Analyzer(
131+
folder_name="pycma",
132+
algorithm_name="pycma",
133+
root="data"
134+
)
135+
for fid in FUNCTIONS:
136+
for d in DIMS:
137+
problem = ioh.get_problem(fid, 1, d)
138+
problem.attach_logger(logger)
139+
for i in range(N_REPEATS):
140+
np.seed(21 + fid * d * i)
141+
modcma.utils.set_seed()
142+
run_pycma(problem, np.zeros(d))
143+
print("pycma", fid, d, problem.state.current_best_internal.y, problem.state.evaluations)
144+
problem.reset()
145+
146+
147+
148+
if __name__ == "__main__":
149+
collect_pycma()
103150
# problem = ioh.get_problem(fid, 1, d)
104151
# run_modma(problem, np.zeros(d), modcma.options.CMSA)
105152
# print(problem.state.evaluations, problem.state.current_best_internal.y)

scripts/matrix/test_timing_modules.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from time import perf_counter
2+
import warnings
23

34
import numpy as np
45
import modcma.c_maes as modcma
56
import ioh
67
import pandas as pd
78
import matplotlib.pyplot as plt
9+
import cma as pycma
810

911
from pprint import pprint
1012

@@ -33,6 +35,28 @@ def run_modma(problem: ioh.ProblemType, x0: np.ndarray, matrix_adaptation = modc
3335
return elapsed, cma.p.stats.t, problem.state.evaluations, cma.p.stats.n_updates
3436

3537

38+
def run_pycma(problem: ioh.ProblemType, x0: np.ndarray, max_generations=1000):
39+
options = pycma.CMAOptions()
40+
options['CMA_active'] = False
41+
# options['maxfevals'] = n_evaluations
42+
options["verbose"] = -1
43+
options["CMA_diagonal"] = False
44+
# pprint(options)
45+
46+
cma = pycma.CMAEvolutionStrategy(x0, 2.0, options=options)
47+
settings = modcma.Settings(problem.meta_data.n_variables)
48+
assert settings.lambda0 == cma.sp.popsize
49+
start = perf_counter()
50+
with warnings.catch_warnings():
51+
warnings.simplefilter("ignore")
52+
for i in range(max_generations):
53+
X, y = cma.ask_and_eval(problem)
54+
cma.tell(X, y)
55+
stop = perf_counter()
56+
elapsed = stop - start
57+
58+
return elapsed, cma.countiter, problem.state.evaluations, cma.sm.count_eigen
59+
3660
def collect():
3761
fid = 2
3862
dims = 2, 3, 5, 10, 20, 40, 100, 200, 500, 1000
@@ -58,5 +82,17 @@ def collect():
5882

5983

6084
if __name__ == "__main__":
61-
stats = pd.read_csv("time_stats.csv")
85+
fid = 2
86+
dims = 2, 3, 5, 10, 20, 40, 100, 200, 500, 1000
87+
n_repeats = 15
88+
89+
stats = []
90+
for d in dims:
91+
for _ in range(n_repeats):
92+
problem = ioh.get_problem(fid, 1, d)
93+
time, n_gen, n_evals, n_updates = run_pycma(problem, np.zeros(d))
94+
stats.append(("pycma", d, time, n_gen, n_evals, n_updates))
95+
print(stats[-1])
96+
stats = pd.DataFrame(stats, columns=["method", "dim", "time", "n_gen", "n_evals", "n_updates"])
97+
stats.to_csv("time_stats_pycma.csv")
6298
print(stats)

src/matrix_adaptation.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ namespace matrix_adaptation
3737
adapt_evolution_paths_inner(pop, w, stats, settings, mu, lambda);
3838
}
3939

40+
void None::adapt_evolution_paths_inner(const Population& pop, const Weights& w,
41+
const Stats& stats, const parameters::Settings& settings, const size_t mu, const size_t lambda)
42+
{
43+
if (!settings.one_plus_one)
44+
ps = (1.0 - w.cs) * ps + (w.sqrt_cs_mueff * dz);
45+
}
46+
47+
Vector None::compute_y(const Vector& zi)
48+
{
49+
return zi;
50+
}
51+
52+
53+
Vector None::invert_y(const Vector& yi)
54+
{
55+
return yi;
56+
}
57+
58+
4059

4160
void CovarianceAdaptation::adapt_ps(const Weights& w)
4261
{
@@ -274,23 +293,6 @@ namespace matrix_adaptation
274293
return M_inv * yi;
275294
}
276295

277-
void None::adapt_evolution_paths_inner(const Population& pop, const Weights& w,
278-
const Stats& stats, const parameters::Settings& settings, const size_t mu, const size_t lambda)
279-
{
280-
if (!settings.one_plus_one)
281-
ps = (1.0 - w.cs) * ps + (w.sqrt_cs_mueff * dz);
282-
}
283-
284-
Vector None::compute_y(const Vector& zi)
285-
{
286-
return zi;
287-
}
288-
289-
290-
Vector None::invert_y(const Vector& yi)
291-
{
292-
return yi;
293-
}
294296

295297
void CholeskyAdaptation::adapt_evolution_paths_inner(const Population& pop,
296298
const parameters::Weights& w,

0 commit comments

Comments
 (0)