Skip to content

Commit 6d539b5

Browse files
committed
fix rsma
1 parent e8e120f commit 6d539b5

9 files changed

Lines changed: 1380 additions & 12 deletions

File tree

scripts/matrix/get_data.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from time import perf_counter
2+
3+
import numpy as np
4+
import modcma.c_maes as modcma
5+
import ioh
6+
import pandas as pd
7+
8+
from multiprocessing import Pool
9+
10+
11+
from pprint import pprint
12+
13+
np.random.seed(12)
14+
15+
def run_modma(problem: ioh.ProblemType,
16+
x0: np.ndarray,
17+
logger_obj,
18+
matrix_adaptation = modcma.options.COVARIANCE
19+
):
20+
modules = modcma.parameters.Modules()
21+
modules.matrix_adaptation = matrix_adaptation
22+
23+
settings = modcma.Settings(
24+
problem.meta_data.n_variables,
25+
x0=x0,
26+
modules=modules,
27+
lb=problem.bounds.lb,
28+
ub=problem.bounds.ub,
29+
verbose=True,
30+
sigma0=2.0,
31+
target=problem.optimum.y + 1e-8,
32+
budget=problem.meta_data.n_variables * 100_000
33+
)
34+
35+
cma = modcma.ModularCMAES(settings)
36+
37+
start = perf_counter()
38+
while not cma.break_conditions():
39+
if cma.p.criteria.any():
40+
logger_obj.update(cma.p.criteria.items)
41+
cma.step(problem)
42+
43+
cma.run(problem)
44+
stop = perf_counter()
45+
elapsed = stop - start
46+
return elapsed, cma.p.stats.t, problem.state.evaluations, cma.p.stats.n_updates
47+
48+
49+
class RestartCollector:
50+
def __init__(self, strategy = modcma.options.RestartStrategy.NONE):
51+
modules = modcma.parameters.Modules()
52+
modules.restart_strategy = strategy
53+
settings = modcma.Settings(
54+
2,
55+
modules=modules,
56+
)
57+
cma = modcma.ModularCMAES(settings)
58+
self.names = [x.name for x in cma.p.criteria.items]
59+
self.reset()
60+
61+
def update(self, items):
62+
for item in items:
63+
if item.met:
64+
setattr(self, item.name, getattr(self, item.name) + 1)
65+
66+
67+
def reset(self):
68+
for item in self.names:
69+
setattr(self, item, 0)
70+
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
77+
options = modcma.options.MatrixAdaptationType.__members__
78+
del options['COVARIANCE_NO_EIGV']
79+
80+
81+
def collect(name, option):
82+
logger = ioh.logger.Analyzer(
83+
folder_name=name,
84+
algorithm_name=name,
85+
root="data"
86+
)
87+
collector = RestartCollector()
88+
logger.add_run_attributes(collector, collector.names)
89+
for fid in functions:
90+
for d in dims:
91+
problem = ioh.get_problem(fid, 1, d)
92+
problem.attach_logger(logger)
93+
for i in range(n_repeats):
94+
modcma.utils.set_seed(21 + fid * d * i)
95+
collector.reset()
96+
run_modma(problem, np.zeros(d), collector, option)
97+
print(name, fid, d, problem.state.current_best_internal.y, problem.state.evaluations)
98+
problem.reset()
99+
100+
with Pool(len(options)) as p:
101+
p.starmap(collect, options.items())
102+
103+
# problem = ioh.get_problem(fid, 1, d)
104+
# run_modma(problem, np.zeros(d), modcma.options.CMSA)
105+
# print(problem.state.evaluations, problem.state.current_best_internal.y)

scripts/matrix/plots.ipynb

Lines changed: 152 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from time import perf_counter
2+
3+
import numpy as np
4+
import modcma.c_maes as modcma
5+
import ioh
6+
import pandas as pd
7+
import matplotlib.pyplot as plt
8+
9+
from pprint import pprint
10+
11+
np.random.seed(12)
12+
13+
def run_modma(problem: ioh.ProblemType, x0: np.ndarray, matrix_adaptation = modcma.options.COVARIANCE, max_generations=1000):
14+
modules = modcma.parameters.Modules()
15+
modules.matrix_adaptation = matrix_adaptation
16+
settings = modcma.Settings(
17+
problem.meta_data.n_variables,
18+
x0=x0,
19+
modules=modules,
20+
lb=problem.bounds.lb,
21+
ub=problem.bounds.ub,
22+
verbose=True,
23+
max_generations=max_generations
24+
)
25+
26+
cma = modcma.ModularCMAES(settings)
27+
28+
start = perf_counter()
29+
cma.run(problem)
30+
stop = perf_counter()
31+
elapsed = stop - start
32+
assert cma.p.stats.t == max_generations
33+
return elapsed, cma.p.stats.t, problem.state.evaluations, cma.p.stats.n_updates
34+
35+
36+
def collect():
37+
fid = 2
38+
dims = 2, 3, 5, 10, 20, 40, 100, 200, 500, 1000
39+
40+
n_repeats = 15
41+
options = modcma.options.MatrixAdaptationType.__members__
42+
del options['COVARIANCE_NO_EIGV']
43+
44+
pprint(options)
45+
46+
stats = []
47+
for d in dims:
48+
for name, option in options.items():
49+
for _ in range(n_repeats):
50+
problem = ioh.get_problem(fid, 1, d)
51+
time, n_gen, n_evals, n_updates = run_modma(problem, np.zeros(d), option)
52+
stats.append((name, d, time, n_gen, n_evals, n_updates))
53+
print(stats[-1])
54+
55+
stats = pd.DataFrame(stats, columns=["method", "dim", "time", "n_gen", "n_evals", "n_updates"])
56+
stats.to_csv("time_stats.csv")
57+
print(stats)
58+
59+
60+
if __name__ == "__main__":
61+
stats = pd.read_csv("time_stats.csv")
62+
print(stats)
1.07 MB
Loading

0 commit comments

Comments
 (0)