1
|
|
|
import pytest |
2
|
|
|
import time |
3
|
|
|
import numpy as np |
4
|
|
|
import pandas as pd |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
from ._parametrize import optimizers_non_deterministic as optimizers |
8
|
|
|
from surfaces.test_functions import AckleyFunction |
9
|
|
|
from gradient_free_optimizers import DirectAlgorithm |
10
|
|
|
|
11
|
|
|
ackkley_function = AckleyFunction() |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def objective_function(para): |
15
|
|
|
score = -(para["x0"] * para["x0"] + para["x1"] * para["x1"]) |
16
|
|
|
return score |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
search_space = { |
20
|
|
|
"x0": np.arange(-75, 100, 1), |
21
|
|
|
"x1": np.arange(-100, 75, 1), |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
err = 0.000001 |
25
|
|
|
|
26
|
|
|
n_iter = 10 |
27
|
|
|
n_random = 2 |
28
|
|
|
|
29
|
|
|
n_last = n_iter - n_random |
30
|
|
|
|
31
|
|
|
|
32
|
|
View Code Duplication |
@pytest.mark.parametrize(*optimizers) |
|
|
|
|
33
|
|
|
def test_random_state_0(Optimizer): |
34
|
|
|
opt0 = Optimizer(search_space, initialize={"random": n_random}, random_state=1) |
35
|
|
|
opt0.search( |
36
|
|
|
ackkley_function, |
37
|
|
|
n_iter=n_iter, |
38
|
|
|
) |
39
|
|
|
|
40
|
|
|
opt1 = Optimizer(search_space, initialize={"random": n_random}, random_state=1) |
41
|
|
|
opt1.search( |
42
|
|
|
ackkley_function, |
43
|
|
|
n_iter=n_iter, |
44
|
|
|
) |
45
|
|
|
|
46
|
|
|
print("\n opt0.search_data \n", opt0.search_data) |
47
|
|
|
print("\n opt1.search_data \n", opt1.search_data) |
48
|
|
|
|
49
|
|
|
n_last_scores0 = list(opt0.search_data["score"].values)[-n_last:] |
50
|
|
|
n_last_scores1 = list(opt1.search_data["score"].values)[-n_last:] |
51
|
|
|
|
52
|
|
|
assert abs(np.sum(n_last_scores0) - np.sum(n_last_scores1)) < err |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
@pytest.mark.parametrize(*optimizers) |
56
|
|
|
def test_random_state_1(Optimizer): |
57
|
|
|
opt0 = Optimizer(search_space, initialize={"random": n_random}, random_state=10) |
58
|
|
|
opt0.search( |
59
|
|
|
ackkley_function, |
60
|
|
|
n_iter=n_iter, |
61
|
|
|
) |
62
|
|
|
|
63
|
|
|
opt1 = Optimizer(search_space, initialize={"random": n_random}, random_state=10) |
64
|
|
|
opt1.search( |
65
|
|
|
ackkley_function, |
66
|
|
|
n_iter=n_iter, |
67
|
|
|
) |
68
|
|
|
|
69
|
|
|
n_last_scores0 = list(opt0.search_data["score"].values)[-n_last:] |
70
|
|
|
n_last_scores1 = list(opt1.search_data["score"].values)[-n_last:] |
71
|
|
|
|
72
|
|
|
assert abs(np.sum(n_last_scores0) - np.sum(n_last_scores1)) < err |
73
|
|
|
|
74
|
|
|
|
75
|
|
View Code Duplication |
@pytest.mark.parametrize(*optimizers) |
|
|
|
|
76
|
|
|
def test_random_state_2(Optimizer): |
77
|
|
|
opt0 = Optimizer(search_space, initialize={"random": n_random}, random_state=1) |
78
|
|
|
opt0.search( |
79
|
|
|
ackkley_function, |
80
|
|
|
n_iter=n_iter, |
81
|
|
|
) |
82
|
|
|
|
83
|
|
|
opt1 = Optimizer(search_space, initialize={"random": n_random}, random_state=10) |
84
|
|
|
opt1.search( |
85
|
|
|
ackkley_function, |
86
|
|
|
n_iter=n_iter, |
87
|
|
|
) |
88
|
|
|
|
89
|
|
|
print("\n opt0.search_data \n", opt0.search_data) |
90
|
|
|
print("\n opt1.search_data \n", opt1.search_data) |
91
|
|
|
|
92
|
|
|
n_last_scores0 = list(opt0.search_data["score"].values)[-n_last:] |
93
|
|
|
n_last_scores1 = list(opt1.search_data["score"].values)[-n_last:] |
94
|
|
|
|
95
|
|
|
assert abs(np.sum(n_last_scores0) - np.sum(n_last_scores1)) > err |
96
|
|
|
|
97
|
|
|
|
98
|
|
View Code Duplication |
def test_random_state_direct(): |
|
|
|
|
99
|
|
|
opt0 = DirectAlgorithm( |
100
|
|
|
search_space, initialize={"random": n_random}, random_state=1 |
101
|
|
|
) |
102
|
|
|
opt0.search( |
103
|
|
|
ackkley_function, |
104
|
|
|
n_iter=n_iter, |
105
|
|
|
) |
106
|
|
|
|
107
|
|
|
opt1 = DirectAlgorithm( |
108
|
|
|
search_space, initialize={"random": n_random}, random_state=10 |
109
|
|
|
) |
110
|
|
|
opt1.search( |
111
|
|
|
ackkley_function, |
112
|
|
|
n_iter=n_iter, |
113
|
|
|
) |
114
|
|
|
|
115
|
|
|
print("\n opt0.search_data \n", opt0.search_data) |
116
|
|
|
print("\n opt1.search_data \n", opt1.search_data) |
117
|
|
|
|
118
|
|
|
n_last_scores0 = list(opt0.search_data["score"].values)[-n_last:] |
119
|
|
|
n_last_scores1 = list(opt1.search_data["score"].values)[-n_last:] |
120
|
|
|
|
121
|
|
|
assert abs(np.sum(n_last_scores0) - np.sum(n_last_scores1)) < err |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
@pytest.mark.parametrize(*optimizers) |
125
|
|
|
def test_no_random_state_0(Optimizer): |
126
|
|
|
opt0 = Optimizer(search_space, initialize={"random": n_random}) |
127
|
|
|
opt0.search(ackkley_function, n_iter=n_iter) |
128
|
|
|
|
129
|
|
|
opt1 = Optimizer(search_space, initialize={"random": n_random}) |
130
|
|
|
opt1.search(ackkley_function, n_iter=n_iter) |
131
|
|
|
|
132
|
|
|
print("\n opt0.search_data \n", opt0.search_data) |
133
|
|
|
print("\n opt1.search_data \n", opt1.search_data) |
134
|
|
|
|
135
|
|
|
n_last_scores0 = list(opt0.search_data["score"].values)[-n_last:] |
136
|
|
|
n_last_scores1 = list(opt1.search_data["score"].values)[-n_last:] |
137
|
|
|
|
138
|
|
|
assert abs(np.sum(n_last_scores0) - np.sum(n_last_scores1)) > err |
139
|
|
|
|