Skip to content

Commit 579ea6d

Browse files
committed
extra tests
1 parent 9809c40 commit 579ea6d

File tree

4 files changed

+152
-12
lines changed

4 files changed

+152
-12
lines changed

scripts/timing/test_bbob5d.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
from time import perf_counter
2+
import ioh
3+
import modcma.c_maes as modcma
4+
import iohinspector as ins
5+
import matplotlib.colors as mcolors
6+
import numpy as np
7+
8+
9+
def timeit(f):
10+
def inner(*args, **kwargs):
11+
start = perf_counter()
12+
result = f(*args, **kwargs)
13+
stop = perf_counter()
14+
elapsed = stop - start
15+
return elapsed
16+
return inner
17+
18+
19+
@timeit
20+
def run_modma(f: ioh.ProblemType, dim: int, n_evaluations):
21+
modules = modcma.parameters.Modules()
22+
# modules.restart_strategy = modcma.options.RestartStrategy.IPOP
23+
# modules.active = True
24+
settings = modcma.Settings(
25+
dim,
26+
budget=n_evaluations,
27+
target=f.optimum.y + 1e-8,
28+
lb=f.bounds.lb,
29+
ub=f.bounds.ub,
30+
sigma0=2.0,
31+
modules=modules,
32+
verbose=False
33+
)
34+
cma = modcma.ModularCMAES(settings)
35+
cma.run(f)
36+
return cma
37+
38+
def fix_legend_labels(ax, n_split, algs, groupby_word = None, reorder=False):
39+
colors = dict(zip(algs, mcolors.TABLEAU_COLORS))
40+
lines = ax.get_lines()[::]
41+
if reorder:
42+
lines = lines[::2] + lines[1::2]
43+
44+
for line, line_label in zip(lines[:len(lines)//2], lines[len(lines)//2:]):
45+
if (lab:=line_label.get_label()) in colors:
46+
for l in (line, line_label):
47+
l.set_color(colors[lab])
48+
l.set_linewidth(3)
49+
if groupby_word is not None and groupby_word in lab:
50+
l.set_linestyle('dashed')
51+
else:
52+
l.set_linestyle('solid')
53+
54+
handles, labels = ax.get_legend_handles_labels()
55+
labels = [l[n_split:] for l in labels[:]]
56+
idx = np.argsort(labels)
57+
ax.legend(np.array(handles)[idx], np.array(labels)[idx], fancybox=True, shadow=True, fontsize=13)
58+
return handles, labels
59+
60+
def place_legend_below(ax, handles, labels, show = True, legend_nrow = 1, start_legend = 3, loc_y = -.11):
61+
box = ax.get_position()
62+
ax.set_position([box.x0, box.y0 + box.height * 0.1,
63+
box.width, box.height * 0.9])
64+
65+
ax.legend().remove()
66+
if show:
67+
ax.legend(np.array(handles), np.array(labels), loc='upper center',
68+
fontsize=13, bbox_to_anchor=(start_legend, loc_y), fancybox=True, shadow=True, ncol=np.ceil(len(labels) / legend_nrow),
69+
)
70+
71+
72+
if __name__ == "__main__":
73+
# modcma.utils.set_seed(43)
74+
# modcma.constants.calc_eigv = True
75+
# name = f"CMA-ES eig={modcma.constants.calc_eigv}"
76+
77+
# logger = ioh.logger.Analyzer(
78+
# folder_name=name,
79+
# algorithm_name=name,
80+
# root="data"
81+
# )
82+
83+
# dim = 5
84+
# n_rep = 5
85+
# n_instances = 5
86+
87+
# budget = 50_000 * dim
88+
# for i in range(1, 25):
89+
# for ii in range(1, n_instances + 1):
90+
# problem = ioh.get_problem(i, ii, dim)
91+
# problem.attach_logger(logger)
92+
# for r in range(n_rep):
93+
# run_modma(problem, dim, budget)
94+
# print(problem.state.evaluations, problem.state.current_best_internal.y)
95+
# problem.reset()
96+
97+
import os
98+
manager = ins.DataManager()
99+
algs = []
100+
for folder in os.listdir("data"):
101+
algs.append(folder)
102+
manager.add_folder(f"data/{folder}")
103+
104+
105+
106+
import matplotlib.pyplot as plt
107+
108+
f, axes = plt.subplots(5, 5, figsize=(25, 13), sharex=True, sharey=True)
109+
110+
x_values = ins.get_sequence(1, 50_000 * 5, 50, True, True)
111+
for fid, ax in enumerate(axes.ravel(), 1):
112+
if fid > 24:
113+
break
114+
dt = manager.select(function_ids=[fid]).load(True, True, x_values=x_values)
115+
ins.plot.single_function_fixedbudget(data=dt, ax=ax)
116+
h,l = fix_legend_labels(ax, 1, algs, None)
117+
place_legend_below(ax, h, l, fid == 24, 1)
118+
119+
plt.tight_layout()
120+
plt.show()

scripts/timing/test_timing.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ def inner(*args, **kwargs):
3636
@timeit
3737
def run_fcmaes(f: ioh.ProblemType, dim: int, n_evaluations, x0: np.ndarray):
3838

39+
lamb = 4 + np.floor(3 * np.log(dim)).astype(int)
3940
bounds = np.array([f.bounds.lb, f.bounds.ub])
4041
res = optimizer.cmaescpp.minimize(
4142
f, x0=x0, max_evaluations=n_evaluations,
4243
stop_hist=0, accuracy=1e-10, stop_fitness=-700,
43-
popsize=4
44+
popsize=lamb, workers=1, delayed_update=False
4445
)
4546

4647

@@ -50,19 +51,29 @@ def run_fcmaes(f: ioh.ProblemType, dim: int, n_evaluations, x0: np.ndarray):
5051

5152
@timeit
5253
def run_modma(f: ioh.ProblemType, dim: int, n_evaluations, x0: np.ndarray):
54+
modcma.constants.calc_eigv = True
5355
modules = modcma.parameters.Modules()
54-
modules.sample_transformation = modcma.options.SCALED_UNIFORM
56+
# modules.sample_transformation = modcma.options.SCALED_UNIFORM
5557
modules.matrix_adaptation = modcma.options.COVARIANCE
56-
settings = modcma.Settings(dim, budget=n_evaluations, x0=x0, modules=modules, verbose=True)
58+
settings = modcma.Settings(dim,
59+
budget=n_evaluations,
60+
x0=x0,
61+
modules=modules,
62+
lb=f.bounds.lb,
63+
ub=f.bounds.ub,
64+
verbose=True
65+
)
5766

5867
cma = modcma.ModularCMAES(settings)
5968

6069

6170
maxp = 1/(10 * dim * (cma.p.weights.c1 +cma.p.weights.cmu))
6271
# print(dim, max(1, maxp), maxp)
6372
# breakpoint()
64-
65-
cma.run(f)
73+
74+
while cma.step(f):
75+
pass
76+
# cma.run(f)
6677
print(cma.p.stats.t, cma.p.stats.n_updates)
6778
assert f.state.evaluations >= n_evaluations
6879
return cma
@@ -92,7 +103,7 @@ def run_pycma(f: ioh.ProblemType, dim: int, n_evaluations: int, x0: np.ndarray):
92103
n_iters = 1
93104
n_evals = 1_000
94105
fid = 12
95-
dimensions = [100]
106+
dimensions = [50]
96107
names, functions = zip(
97108
*[
98109
(name, obj)

setup.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@
2020
cxx_std=17,
2121
)
2222
if platform.system() in ("Linux", "Darwin"):
23-
os.environ["CC"] = "g++"
24-
os.environ["CXX"] = "g++"
25-
flags = ["-O3", "-fno-math-errno", "-msse2", "-mavx", "-mfma", "-mtune=native"]
23+
os.environ["CC"] = "clang"
24+
os.environ["CXX"] = "clang"
25+
flags = [
26+
"-O3",
27+
# "-fno-math-errno", "-msse2", "-mavx", "-mfma", "-mtune=native",
28+
# "-march=native", "-ffast-math", "-flto", "-funroll-loops", "-ftree-vectorize"
29+
]
2630

2731
if platform.system() == "Darwin":
2832
flags.append("-mmacosx-version-min=10.15")
29-
else:
30-
flags.append("-march=native")
33+
# else:
34+
# flags.append("-march=native")
3135

3236
ext._add_cflags(flags)
3337
ext._add_ldflags(flags)

src/interface.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,6 @@ class constants_w
898898
void define_constants(py::module &m)
899899
{
900900
py::class_<constants_w>(m, "constants")
901-
902901
.def_property_static(
903902
"cache_max_doubles",
904903
[](py::object)
@@ -923,6 +922,12 @@ void define_constants(py::module &m)
923922
{ return constants::clip_sigma; },
924923
[](py::object, bool a)
925924
{ constants::clip_sigma = a; })
925+
.def_property_static(
926+
"calc_eigv",
927+
[](py::object)
928+
{ return constants::calc_eigv; },
929+
[](py::object, bool a)
930+
{ constants::calc_eigv = a; })
926931
;
927932
}
928933

0 commit comments

Comments
 (0)