-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_para.py
378 lines (328 loc) · 11.3 KB
/
main_para.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#
# Created in 2023 by Gaëtan Serré
#
import numpy as np
import os
import argparse
from multiprocessing import Pool, cpu_count
# benchmark functions
from benchmark.ackley import Ackley
from benchmark.himmelblau import Himmelblau
from benchmark.holder import Holder
from benchmark.rastrigin import Rastrigin
from benchmark.rosenbrock import Rosenbrock
from benchmark.sphere import Sphere
from benchmark.cos import Cos
from benchmark.branin import Branin
from benchmark.eggholder import EggHolder
from benchmark.drop_wave import Drop_Wave
from benchmark.goldstein_price import Goldstein_Price
from benchmark.camel import Camel
from benchmark.levy import Levy
from benchmark.michalewicz import Michalewicz
from benchmark.epidemiology.simulation import Simulation
# Optimizations algorithms
from optims.PRS import PRS
from optims.AdaLIPO_P import AdaLIPO_P
from optims.SBS import SBS
from optims.SBS_particles import SBS_particles
from optims.SBS_particles_hybrid import SBS_particles_hybrid
from optims.SBS_hybrid import SBS_hybrid
from optims.BayesOpt import BayesOpt
from optims.N_CMA_ES import CMA_ES
from optims.WOA import WOA
from optims.Langevin import Langevin
from optims.Simulated_Annealing import SimulatedAnnealing
from optims.CBO import CBO
from utils.utils import *
from utils.pretty_printer import pprint_results_get_rank, pprint_rank
from utils.mk_latex_table import mk_table
from utils.init_epidemiology import init_epidemiology
def match_optim(optim_cls, bounds, num_evals, is_sim=False):
if is_sim:
init_epidemiology()
if optim_cls == PRS:
return optim_cls(bounds, num_evals=num_evals)
elif optim_cls == AdaLIPO_P:
return optim_cls(bounds, max_evals=num_evals)
elif optim_cls == SBS:
return optim_cls(
bounds,
n_particles=500,
k_iter=[10_000],
svgd_iter=300,
sigma=1 / 500**2,
lr=0.1 if is_sim else 0.2,
)
elif optim_cls == SBS_particles:
return optim_cls(
bounds,
n_particles=500,
k_iter=[10_000],
svgd_iter=300,
sigma=lambda N: 1 / N**2,
lr=0.1 if is_sim else 0.2,
)
elif optim_cls == SBS_hybrid:
return optim_cls(
bounds,
n_particles=200,
k_iter=[10_000],
svgd_iter=1000,
warm_start_iter=1000,
sigma=1e-10,
lr=0.1 if is_sim else 1e-3,
)
elif optim_cls == SBS_particles_hybrid:
return optim_cls(
bounds,
n_particles=500,
k_iter=[10_000],
svgd_iter=1000,
sigma=1e-10,
lr=0.1 if is_sim else 1e-3,
warm_start_iter=1000,
)
elif optim_cls == BayesOpt:
return optim_cls(bounds, n_iter=70)
elif optim_cls == CMA_ES:
m_0 = np.random.uniform(bounds[:, 0], bounds[:, 1])
return optim_cls(bounds, m_0, max_evals=num_evals)
elif optim_cls == WOA:
return optim_cls(bounds, n_gen=30, n_sol=num_evals // 30)
elif optim_cls == SimulatedAnnealing:
return optim_cls(bounds, num_evals)
elif optim_cls == Langevin:
return optim_cls(bounds, n_particles=500, n_iter=150, kappa=10_000, init_lr=0.2)
elif optim_cls == CBO:
return optim_cls(bounds, n_iter=1600, n_particles=500)
else:
raise NotImplementedError(f"{optim_cls} not implemented.")
def cli():
parser = argparse.ArgumentParser(description="Run the benchmark.")
parser.add_argument(
"--big-dimension",
"-bd",
action="store_true",
help="Run the big dimensions benchmark.",
)
parser.add_argument(
"--huge-dimension",
"-hd",
action="store_true",
help="Run the huge dimensions benchmark.",
)
parser.add_argument(
"--num-exp",
"-ne",
type=int,
default=10,
help="Number of experiment to run.",
)
parser.add_argument(
"--latex",
"-lt",
action="store_true",
help="Print the LateX table associated with the benchmark.",
)
parser.add_argument(
"--latex-std",
"-lt-std",
action="store_true",
help="Print the LateX table associated with the benchmark with std.",
)
return parser.parse_args()
def run_optimizer(args):
optimizer_cls, num_eval, function, bounds, num_exp = args
print_blue(f"Optimizer: {optimizer_cls.__name__}.")
optimizer = match_optim(
optimizer_cls,
bounds,
num_eval,
is_sim=function.__class__.__name__ == "Simulation",
)
times = []
best_values = []
num_f_evals = 0
for _ in range(num_exp):
function.n = 0
ret, time = time_it(
optimizer.optimize,
{
"function": function,
"verbose": False,
},
)
best_point, points, values = ret
num_f_evals += function.n
times.append(time)
best_values.append(abs(function(best_point[0]) - function.min))
print_green(f"{optimizer_cls.__name__} done.")
return {
"mean": np.mean(best_values),
"std": np.std(best_values),
"eval": f"{num_f_evals / num_exp:.2f}",
"time": f"{np.mean(times):.4f}",
}
if __name__ == "__main__":
args = cli()
# Set Numpy random seed
np.random.seed(42)
num_exp = args.num_exp
if args.huge_dimension:
functions = {
"Ackley": [Ackley(), create_bounds(-32.768, 32.768, 100)],
"Michalewicz": [Michalewicz(100), create_bounds(0, np.pi, 100)],
"Rastrigin": [Rastrigin(), create_bounds(-5.12, 5.12, 100)],
"Rosenbrock": [Rosenbrock(), create_bounds(-3, 3, 100)],
"Levy": [Levy(), create_bounds(-10, 10, 100)],
"Sphere": [Sphere(), create_bounds(-10, 10, 100)],
}
optimizers_cls = [
CMA_ES,
SBS,
SBS_particles,
SBS_hybrid,
SBS_particles_hybrid,
]
num_evals = [8_000_000, 0, 0, 0, 0]
elif args.big_dimension:
functions = {
"Ackley": [Ackley(), create_bounds(-32.768, 32.768, 50)],
"Michalewicz": [Michalewicz(50), create_bounds(0, np.pi, 50)],
"Rastrigin": [Rastrigin(), create_bounds(-5.12, 5.12, 50)],
"Rosenbrock": [Rosenbrock(), create_bounds(-3, 3, 50)],
"Levy": [Levy(), create_bounds(-10, 10, 50)],
"Sphere": [Sphere(), create_bounds(-10, 10, 50)],
}
optimizers_cls = [
WOA,
CMA_ES,
Langevin,
SBS,
SBS_particles,
SBS_hybrid,
SBS_particles_hybrid,
CBO,
]
num_evals = [8_000_000, 8_000_000, 8_000_000, 0, 0, 0, 0, 0]
else:
functions = {
"Ackley": [Ackley(), create_bounds(-32.768, 32.768, 2)],
"Michalewicz": [Michalewicz(2), create_bounds(0, np.pi, 2)],
"Branin": [Branin(), np.array([(-5, 10), (0, 15)])],
"Drop Wave": [Drop_Wave(), create_bounds(-5.12, 5.12, 2)],
"Egg Holder": [EggHolder(), create_bounds(-512, 512, 2)],
"Goldstein Price": [Goldstein_Price(), create_bounds(-2, 2, 2)],
"Himmelblau": [Himmelblau(), create_bounds(-4, 4, 2)],
"Holder Table": [Holder(), create_bounds(-10, 10, 2)],
"Rastrigin": [Rastrigin(), create_bounds(-5.12, 5.12, 2)],
"Rosenbrock": [Rosenbrock(), create_bounds(-3, 3, 2)],
"Camel": [Camel(), create_bounds(-3, 3, 2)],
"Levy": [Levy(), create_bounds(-10, 10, 2)],
"Sphere": [Sphere(), create_bounds(-10, 10, 2)],
}
optimizers_cls = [
AdaLIPO_P,
BayesOpt,
Langevin,
SBS,
SBS_particles,
SBS_hybrid,
SBS_particles_hybrid,
CMA_ES,
WOA,
CBO,
]
num_evals = [2000, 100, 800_000, 0, 0, 0, 0, 800_000, 800_000, 0]
# Number of core to use
nb_core = min(cpu_count(), len(optimizers_cls))
print_green(f"Using {nb_core} core(s).")
# Data to store
all_ranks = {}
sota_methods = {}
proposed_methods = {}
sbs_pf_economy = []
sbs_pf_hybrid_economy = []
for function_name, objects in functions.items():
print_pink(f"Function: {function_name}.")
function = objects[0]
bounds = objects[1]
dim = bounds.shape[0]
plot_figures = False # dim <= 2
if plot_figures:
from utils.fig_generator import FigGenerator
fig_gen = FigGenerator(function, bounds)
if not os.path.exists("figures"):
os.makedirs("figures")
opt_dict = {}
args_list = [
(optimizers_cls[i], num_evals[i], function, bounds, num_exp)
for i in range(len(optimizers_cls))
]
with Pool(nb_core) as p:
l = p.map(run_optimizer, args_list)
for i in range(len(optimizers_cls)):
opt_dict[optimizers_cls[i].__name__] = l[i]
new_ranks = pprint_results_get_rank(opt_dict)
all_ranks = merge_ranks(all_ranks, new_ranks)
for m_name in opt_dict.keys():
if "SBS" in m_name:
add_dict_entry(
proposed_methods,
m_name,
{
function_name: {
"mean": opt_dict[m_name]["mean"],
"std": opt_dict[m_name]["std"],
}
},
)
else:
add_dict_entry(
sota_methods,
m_name,
{
function_name: {
"mean": opt_dict[m_name]["mean"],
"std": opt_dict[m_name]["std"],
}
},
)
if "SBS" in opt_dict and "SBS_particles" in opt_dict:
sbs_pf_economy.append(
100
- float(opt_dict["SBS_particles"]["eval"])
/ float(opt_dict["SBS"]["eval"])
* 100
)
if "SBS_hybrid" in opt_dict and "SBS_particles_hybrid" in opt_dict:
sbs_pf_hybrid_economy.append(
100
- float(opt_dict["SBS_particles_hybrid"]["eval"])
/ float(opt_dict["SBS_hybrid"]["eval"])
* 100
)
pprint_rank(all_ranks)
if sbs_pf_economy != []:
print(
f"Average economy of SBS_particles over SBS: {np.mean(sbs_pf_economy):.2f}%."
)
if sbs_pf_hybrid_economy != []:
print(
f"Average economy of SBS_particles_hybrid over SBS_hybrid: {np.mean(sbs_pf_hybrid_economy):.2f}%."
)
if args.latex or args.latex_std:
functions_mins = {}
for f_name in functions.keys():
functions_mins[f_name] = functions[f_name][0].min
if args.latex:
mk_table(functions_mins, sota_methods, proposed_methods, all_ranks, False)
if args.latex_std:
mk_table(
functions_mins,
sota_methods,
proposed_methods,
all_ranks,
True,
)