1
|
|
|
import numpy as np |
2
|
|
|
|
3
|
|
|
from hyperactive.optimizers import HillClimbingOptimizer |
4
|
|
|
from hyperactive.experiment import BaseExperiment |
5
|
|
|
from hyperactive.search_config import SearchConfig |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class Experiment(BaseExperiment): |
9
|
|
|
def objective_function(self, opt): |
10
|
|
|
score = -(opt["x1"] * opt["x1"] + opt["x2"] * opt["x2"]) |
11
|
|
|
return score |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
experiment = Experiment() |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
search_config = SearchConfig( |
18
|
|
|
x1=list(np.arange(-1000, 1000, 0.1)), |
19
|
|
|
x2=list(np.arange(-1000, 1000, 0.1)), |
20
|
|
|
) |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
err = 0.001 |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
def test_random_state_n_jobs_0(): |
27
|
|
|
n_jobs = 2 |
28
|
|
|
|
29
|
|
|
hyper = HillClimbingOptimizer() |
30
|
|
|
hyper.add_search( |
31
|
|
|
experiment, |
32
|
|
|
search_config, |
33
|
|
|
n_iter=5, |
34
|
|
|
initialize={"random": 1}, |
35
|
|
|
random_state=1, |
36
|
|
|
n_jobs=n_jobs, |
37
|
|
|
) |
38
|
|
|
hyper.run() |
39
|
|
|
|
40
|
|
|
results = hyper.search_data(experiment) |
41
|
|
|
|
42
|
|
|
no_dup = results.drop_duplicates(subset=list(search_config.keys())) |
43
|
|
|
print("no_dup", no_dup) |
44
|
|
|
print("results", results) |
45
|
|
|
|
46
|
|
|
print(int(len(results) / n_jobs)) |
47
|
|
|
print(len(no_dup)) |
48
|
|
|
|
49
|
|
|
assert int(len(results) / n_jobs) != len(no_dup) |
50
|
|
|
|
51
|
|
|
|
52
|
|
View Code Duplication |
def test_random_state_n_jobs_1(): |
|
|
|
|
53
|
|
|
n_jobs = 3 |
54
|
|
|
|
55
|
|
|
hyper = HillClimbingOptimizer() |
56
|
|
|
hyper.add_search( |
57
|
|
|
experiment, |
58
|
|
|
search_config, |
59
|
|
|
n_iter=5, |
60
|
|
|
initialize={"random": 1}, |
61
|
|
|
random_state=1, |
62
|
|
|
n_jobs=n_jobs, |
63
|
|
|
) |
64
|
|
|
hyper.run() |
65
|
|
|
|
66
|
|
|
results = hyper.search_data(experiment) |
67
|
|
|
|
68
|
|
|
no_dup = results.drop_duplicates(subset=list(search_config.keys())) |
69
|
|
|
print("no_dup", no_dup) |
70
|
|
|
print("results", results) |
71
|
|
|
|
72
|
|
|
assert int(len(results) / n_jobs) != len(no_dup) |
73
|
|
|
|
74
|
|
|
|
75
|
|
View Code Duplication |
def test_random_state_n_jobs_2(): |
|
|
|
|
76
|
|
|
n_jobs = 4 |
77
|
|
|
|
78
|
|
|
hyper = HillClimbingOptimizer() |
79
|
|
|
hyper.add_search( |
80
|
|
|
experiment, |
81
|
|
|
search_config, |
82
|
|
|
n_iter=5, |
83
|
|
|
initialize={"random": 1}, |
84
|
|
|
random_state=1, |
85
|
|
|
n_jobs=n_jobs, |
86
|
|
|
) |
87
|
|
|
hyper.run() |
88
|
|
|
|
89
|
|
|
results = hyper.search_data(experiment) |
90
|
|
|
|
91
|
|
|
no_dup = results.drop_duplicates(subset=list(search_config.keys())) |
92
|
|
|
print("no_dup", no_dup) |
93
|
|
|
print("results", results) |
94
|
|
|
|
95
|
|
|
assert int(len(results) / n_jobs) != len(no_dup) |
96
|
|
|
|
97
|
|
|
|
98
|
|
View Code Duplication |
def test_random_state_0(): |
|
|
|
|
99
|
|
|
hyper0 = HillClimbingOptimizer() |
100
|
|
|
hyper0.add_search( |
101
|
|
|
experiment, |
102
|
|
|
search_config, |
103
|
|
|
n_iter=10, |
104
|
|
|
initialize={"random": 1}, |
105
|
|
|
random_state=1, |
106
|
|
|
) |
107
|
|
|
hyper0.run() |
108
|
|
|
|
109
|
|
|
hyper1 = HillClimbingOptimizer() |
110
|
|
|
hyper1.add_search( |
111
|
|
|
experiment, |
112
|
|
|
search_config, |
113
|
|
|
n_iter=10, |
114
|
|
|
initialize={"random": 1}, |
115
|
|
|
random_state=1, |
116
|
|
|
) |
117
|
|
|
hyper1.run() |
118
|
|
|
|
119
|
|
|
best_score0 = hyper0.best_score(experiment) |
120
|
|
|
best_score1 = hyper1.best_score(experiment) |
121
|
|
|
|
122
|
|
|
assert abs(best_score0 - best_score1) < err |
123
|
|
|
|
124
|
|
|
|
125
|
|
View Code Duplication |
def test_random_state_1(): |
|
|
|
|
126
|
|
|
hyper0 = HillClimbingOptimizer() |
127
|
|
|
hyper0.add_search( |
128
|
|
|
experiment, |
129
|
|
|
search_config, |
130
|
|
|
n_iter=10, |
131
|
|
|
initialize={"random": 1}, |
132
|
|
|
random_state=10, |
133
|
|
|
) |
134
|
|
|
hyper0.run() |
135
|
|
|
|
136
|
|
|
hyper1 = HillClimbingOptimizer() |
137
|
|
|
hyper1.add_search( |
138
|
|
|
experiment, |
139
|
|
|
search_config, |
140
|
|
|
n_iter=10, |
141
|
|
|
initialize={"random": 1}, |
142
|
|
|
random_state=10, |
143
|
|
|
) |
144
|
|
|
hyper1.run() |
145
|
|
|
|
146
|
|
|
best_score0 = hyper0.best_score(experiment) |
147
|
|
|
best_score1 = hyper1.best_score(experiment) |
148
|
|
|
|
149
|
|
|
assert abs(best_score0 - best_score1) < err |
150
|
|
|
|
151
|
|
|
|
152
|
|
View Code Duplication |
def test_random_state_2(): |
|
|
|
|
153
|
|
|
hyper0 = HillClimbingOptimizer() |
154
|
|
|
hyper0.add_search( |
155
|
|
|
experiment, |
156
|
|
|
search_config, |
157
|
|
|
n_iter=10, |
158
|
|
|
initialize={"random": 1}, |
159
|
|
|
random_state=1, |
160
|
|
|
) |
161
|
|
|
hyper0.run() |
162
|
|
|
|
163
|
|
|
hyper1 = HillClimbingOptimizer() |
164
|
|
|
hyper1.add_search( |
165
|
|
|
experiment, |
166
|
|
|
search_config, |
167
|
|
|
n_iter=10, |
168
|
|
|
initialize={"random": 1}, |
169
|
|
|
random_state=10, |
170
|
|
|
) |
171
|
|
|
hyper1.run() |
172
|
|
|
|
173
|
|
|
best_score0 = hyper0.best_score(experiment) |
174
|
|
|
best_score1 = hyper1.best_score(experiment) |
175
|
|
|
|
176
|
|
|
assert abs(best_score0 - best_score1) > err |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
def test_no_random_state_0(): |
180
|
|
|
hyper0 = HillClimbingOptimizer() |
181
|
|
|
hyper0.add_search( |
182
|
|
|
experiment, |
183
|
|
|
search_config, |
184
|
|
|
n_iter=10, |
185
|
|
|
initialize={"random": 1}, |
186
|
|
|
) |
187
|
|
|
hyper0.run() |
188
|
|
|
|
189
|
|
|
hyper1 = HillClimbingOptimizer() |
190
|
|
|
hyper1.add_search( |
191
|
|
|
experiment, |
192
|
|
|
search_config, |
193
|
|
|
n_iter=10, |
194
|
|
|
initialize={"random": 1}, |
195
|
|
|
) |
196
|
|
|
hyper1.run() |
197
|
|
|
|
198
|
|
|
best_score0 = hyper0.best_score(experiment) |
199
|
|
|
best_score1 = hyper1.best_score(experiment) |
200
|
|
|
|
201
|
|
|
assert abs(best_score0 - best_score1) > err |
202
|
|
|
|