|
1
|
|
|
import pytest |
|
2
|
|
|
from tqdm import tqdm |
|
3
|
|
|
import numpy as np |
|
4
|
|
|
|
|
5
|
|
|
from surfaces.test_functions.mathematical import SphereFunction, RastriginFunction |
|
6
|
|
|
|
|
7
|
|
|
from gradient_free_optimizers import ( |
|
8
|
|
|
ParallelTemperingOptimizer, |
|
9
|
|
|
ParticleSwarmOptimizer, |
|
10
|
|
|
EvolutionStrategyOptimizer, |
|
11
|
|
|
RandomSearchOptimizer, |
|
12
|
|
|
) |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
opt_pop_l = ( |
|
16
|
|
|
"Optimizer", |
|
17
|
|
|
[ |
|
18
|
|
|
(ParallelTemperingOptimizer), |
|
19
|
|
|
(ParticleSwarmOptimizer), |
|
20
|
|
|
(EvolutionStrategyOptimizer), |
|
21
|
|
|
], |
|
22
|
|
|
) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
obj_func_l = ( |
|
26
|
|
|
"test_function", |
|
27
|
|
|
[ |
|
28
|
|
|
(SphereFunction(n_dim=2, metric="score")), |
|
29
|
|
|
(RastriginFunction(n_dim=2, metric="score")), |
|
30
|
|
|
], |
|
31
|
|
|
) |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
@pytest.mark.parametrize(*obj_func_l) |
|
|
|
|
|
|
35
|
|
|
@pytest.mark.parametrize(*opt_pop_l) |
|
36
|
|
|
def test_pop_perf_0(Optimizer, test_function): |
|
37
|
|
|
search_space = { |
|
38
|
|
|
"x0": np.arange(-100, 101, 0.1), |
|
39
|
|
|
"x1": np.arange(-100, 101, 0.1), |
|
40
|
|
|
} |
|
41
|
|
|
initialize = {"vertices": 4, "random": 6} |
|
42
|
|
|
|
|
43
|
|
|
n_opts = 10 |
|
44
|
|
|
n_iter = 1200 |
|
45
|
|
|
|
|
46
|
|
|
scores = [] |
|
47
|
|
|
scores_rnd = [] |
|
48
|
|
|
for rnd_st in tqdm(range(n_opts)): |
|
49
|
|
|
opt = Optimizer(search_space, initialize=initialize, random_state=rnd_st) |
|
50
|
|
|
opt.search( |
|
51
|
|
|
test_function.objective_function, |
|
52
|
|
|
n_iter=n_iter, |
|
53
|
|
|
memory=False, |
|
54
|
|
|
verbosity=False, |
|
55
|
|
|
) |
|
56
|
|
|
|
|
57
|
|
|
opt_rnd = RandomSearchOptimizer( |
|
58
|
|
|
search_space, initialize=initialize, random_state=rnd_st |
|
59
|
|
|
) |
|
60
|
|
|
opt_rnd.search( |
|
61
|
|
|
test_function.objective_function, |
|
62
|
|
|
n_iter=n_iter, |
|
63
|
|
|
memory=False, |
|
64
|
|
|
verbosity=False, |
|
65
|
|
|
) |
|
66
|
|
|
|
|
67
|
|
|
scores.append(opt.best_score) |
|
68
|
|
|
scores_rnd.append(opt_rnd.best_score) |
|
69
|
|
|
|
|
70
|
|
|
score_mean = np.array(scores).mean() |
|
71
|
|
|
score_mean_rnd = np.array(scores_rnd).mean() |
|
72
|
|
|
|
|
73
|
|
|
print("\n score_mean", score_mean) |
|
74
|
|
|
print("\n score_mean_rnd", score_mean_rnd) |
|
75
|
|
|
|
|
76
|
|
|
score_norm = (score_mean_rnd - score_mean) / (score_mean_rnd + score_mean) |
|
77
|
|
|
print("\n score_norm", score_norm) |
|
78
|
|
|
|
|
79
|
|
|
assert score_norm > 0.1 |
|
80
|
|
|
|