1
|
|
|
# Author: Simon Blanke |
2
|
|
|
# Email: [email protected] |
3
|
|
|
# License: MIT License |
4
|
|
|
|
5
|
|
|
import numpy as np |
6
|
|
|
|
7
|
|
|
from hyperactive import Hyperactive |
8
|
|
|
|
9
|
|
|
X, y = np.array([0]), np.array([0]) |
10
|
|
|
memory = False |
11
|
|
|
n_iter = 100 |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
def sphere_function(para, X_train, y_train): |
15
|
|
|
loss = [] |
16
|
|
|
for key in para.keys(): |
17
|
|
|
if key == "iteration": |
18
|
|
|
continue |
19
|
|
|
loss.append(para[key] * para[key]) |
20
|
|
|
|
21
|
|
|
return -np.array(loss).sum() |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
search_config = { |
25
|
|
|
sphere_function: { |
26
|
|
|
"x1": list(np.arange(-3, 3, 0.1)), |
27
|
|
|
"x2": list(np.arange(-3, 3, 0.1)), |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
def test_HillClimbingOptimizer(): |
33
|
|
|
opt = Hyperactive(X, y, memory=memory) |
34
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="HillClimbing") |
35
|
|
|
|
36
|
|
|
for epsilon in [0.01, 0.1, 1]: |
37
|
|
|
opt = Hyperactive(X, y, memory=memory) |
38
|
|
|
opt.search( |
39
|
|
|
search_config, |
40
|
|
|
n_iter=n_iter, |
41
|
|
|
optimizer={"HillClimbing": {"epsilon": epsilon}}, |
42
|
|
|
) |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def test_StochasticHillClimbingOptimizer(): |
46
|
|
|
opt = Hyperactive(X, y, memory=memory) |
47
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="StochasticHillClimbing") |
48
|
|
|
|
49
|
|
|
for p_down in [0.01, 0.1, 1]: |
50
|
|
|
opt = Hyperactive(X, y, memory=memory) |
51
|
|
|
opt.search( |
52
|
|
|
search_config, |
53
|
|
|
n_iter=n_iter, |
54
|
|
|
optimizer={"StochasticHillClimbing": {"p_down": p_down}}, |
55
|
|
|
) |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
def test_TabuOptimizer(): |
59
|
|
|
opt = Hyperactive(X, y, memory=memory) |
60
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="TabuSearch") |
61
|
|
|
|
62
|
|
|
for tabu_memory in [1, 3, 5]: |
63
|
|
|
opt = Hyperactive(X, y, memory=memory) |
64
|
|
|
opt.search( |
65
|
|
|
search_config, |
66
|
|
|
n_iter=n_iter, |
67
|
|
|
optimizer={"TabuSearch": {"tabu_memory": tabu_memory}}, |
68
|
|
|
) |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
def test_RandomSearchOptimizer(): |
72
|
|
|
opt = Hyperactive(X, y, memory=memory) |
73
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="RandomSearch") |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
def test_RandomRestartHillClimbingOptimizer(): |
77
|
|
|
opt = Hyperactive(X, y, memory=memory) |
78
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="RandomRestartHillClimbing") |
79
|
|
|
|
80
|
|
|
for n_restarts in [3, 5, 20]: |
81
|
|
|
opt = Hyperactive(X, y, memory=memory) |
82
|
|
|
opt.search( |
83
|
|
|
search_config, |
84
|
|
|
n_iter=n_iter, |
85
|
|
|
optimizer={"RandomRestartHillClimbing": {"n_restarts": n_restarts}}, |
86
|
|
|
) |
87
|
|
|
|
88
|
|
|
|
89
|
|
View Code Duplication |
def test_RandomAnnealingOptimizer(): |
|
|
|
|
90
|
|
|
opt = Hyperactive(X, y, memory=memory) |
91
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="RandomAnnealing") |
92
|
|
|
|
93
|
|
|
for start_temp in [0.1, 1, 10]: |
94
|
|
|
opt = Hyperactive(X, y, memory=memory) |
95
|
|
|
opt.search( |
96
|
|
|
search_config, |
97
|
|
|
n_iter=n_iter, |
98
|
|
|
optimizer={"RandomAnnealing": {"start_temp": start_temp}}, |
99
|
|
|
) |
100
|
|
|
|
101
|
|
|
|
102
|
|
View Code Duplication |
def test_SimulatedAnnealingOptimizer(): |
|
|
|
|
103
|
|
|
opt = Hyperactive(X, y, memory=memory) |
104
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="SimulatedAnnealing") |
105
|
|
|
|
106
|
|
|
for start_temp in [0.1, 1, 10]: |
107
|
|
|
opt = Hyperactive(X, y, memory=memory) |
108
|
|
|
opt.search( |
109
|
|
|
search_config, |
110
|
|
|
n_iter=n_iter, |
111
|
|
|
optimizer={"SimulatedAnnealing": {"start_temp": start_temp}}, |
112
|
|
|
) |
113
|
|
|
|
114
|
|
|
|
115
|
|
View Code Duplication |
def test_StochasticTunnelingOptimizer(): |
|
|
|
|
116
|
|
|
opt = Hyperactive(X, y, memory=memory) |
117
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="StochasticTunneling") |
118
|
|
|
|
119
|
|
|
for start_temp in [0.1, 1, 10]: |
120
|
|
|
opt = Hyperactive(X, y, memory=memory) |
121
|
|
|
opt.search( |
122
|
|
|
search_config, |
123
|
|
|
n_iter=n_iter, |
124
|
|
|
optimizer={"StochasticTunneling": {"start_temp": start_temp}}, |
125
|
|
|
) |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
def test_ParallelTemperingOptimizer(): |
129
|
|
|
opt = Hyperactive(X, y, memory=memory) |
130
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="ParallelTempering") |
131
|
|
|
|
132
|
|
|
for n_iter_swap in [1, 10, 30]: |
133
|
|
|
opt = Hyperactive(X, y, memory=memory) |
134
|
|
|
opt.search( |
135
|
|
|
search_config, |
136
|
|
|
n_iter=n_iter, |
137
|
|
|
optimizer={"ParallelTempering": {"n_iter_swap": n_iter_swap}}, |
138
|
|
|
) |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
def test_ParticleSwarmOptimizer(): |
142
|
|
|
opt = Hyperactive(X, y, memory=memory) |
143
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="ParticleSwarm") |
144
|
|
|
|
145
|
|
|
for n_particles in [2, 10, 30]: |
146
|
|
|
opt = Hyperactive(X, y, memory=memory) |
147
|
|
|
opt.search( |
148
|
|
|
search_config, |
149
|
|
|
n_iter=n_iter, |
150
|
|
|
optimizer={"ParticleSwarm": {"n_particles": n_particles}}, |
151
|
|
|
) |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
def test_EvolutionStrategyOptimizer(): |
155
|
|
|
opt = Hyperactive(X, y, memory=memory) |
156
|
|
|
opt.search(search_config, n_iter=n_iter, optimizer="EvolutionStrategy") |
157
|
|
|
|
158
|
|
|
for individuals in [2, 10, 30]: |
159
|
|
|
opt = Hyperactive(X, y, memory=memory) |
160
|
|
|
opt.search( |
161
|
|
|
search_config, |
162
|
|
|
n_iter=n_iter, |
163
|
|
|
optimizer={"EvolutionStrategy": {"individuals": individuals}}, |
164
|
|
|
) |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
def test_BayesianOptimizer(): |
168
|
|
|
opt = Hyperactive(X, y, memory=memory) |
169
|
|
|
opt.search(search_config, n_iter=int(n_iter / 33), optimizer="Bayesian") |
170
|
|
|
|
171
|
|
|
for warm_start_smbo in [True]: |
172
|
|
|
opt = Hyperactive(X, y, memory="long") |
173
|
|
|
opt.search( |
174
|
|
|
search_config, |
175
|
|
|
n_iter=int(n_iter / 33), |
176
|
|
|
optimizer={"Bayesian": {"warm_start_smbo": warm_start_smbo}}, |
177
|
|
|
) |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
test_BayesianOptimizer() |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
def test_TPE(): |
184
|
|
|
opt = Hyperactive(X, y, memory=memory) |
185
|
|
|
opt.search(search_config, n_iter=int(n_iter / 5), optimizer="TPE") |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
def test_DecisionTreeOptimizer(): |
189
|
|
|
opt = Hyperactive(X, y, memory=memory) |
190
|
|
|
opt.search(search_config, n_iter=int(n_iter / 33), optimizer="DecisionTree") |
191
|
|
|
|