|
1
|
|
|
import numpy as np |
|
2
|
|
|
import pandas as pd |
|
3
|
|
|
from tqdm import tqdm |
|
4
|
|
|
import matplotlib.pyplot as plt |
|
5
|
|
|
|
|
6
|
|
|
from gradient_free_optimizers import ( |
|
7
|
|
|
HillClimbingOptimizer, |
|
8
|
|
|
StochasticHillClimbingOptimizer, |
|
9
|
|
|
TabuOptimizer, |
|
10
|
|
|
RandomSearchOptimizer, |
|
11
|
|
|
RandomRestartHillClimbingOptimizer, |
|
12
|
|
|
RandomAnnealingOptimizer, |
|
13
|
|
|
SimulatedAnnealingOptimizer, |
|
14
|
|
|
ParallelTemperingOptimizer, |
|
15
|
|
|
ParticleSwarmOptimizer, |
|
16
|
|
|
EvolutionStrategyOptimizer, |
|
17
|
|
|
BayesianOptimizer, |
|
18
|
|
|
TreeStructuredParzenEstimators, |
|
19
|
|
|
DecisionTreeOptimizer, |
|
20
|
|
|
EnsembleOptimizer, |
|
21
|
|
|
) |
|
22
|
|
|
from gradient_free_optimizers.converter import Converter |
|
23
|
|
|
|
|
24
|
|
|
one_init = 1 |
|
25
|
|
|
two_init = 2 |
|
26
|
|
|
six_init = 6 |
|
27
|
|
|
n_inits = 4 |
|
28
|
|
|
|
|
29
|
|
|
""" |
|
30
|
|
|
"Stochastic hill climbing": (StochasticHillClimbingOptimizer, one_init, 0), |
|
31
|
|
|
"Tabu search": (TabuOptimizer, one_init, 0), |
|
32
|
|
|
"Random search": (RandomSearchOptimizer, one_init, 1), |
|
33
|
|
|
"Random restart hill climbing": ( |
|
34
|
|
|
RandomRestartHillClimbingOptimizer, |
|
35
|
|
|
one_init, |
|
36
|
|
|
9, |
|
37
|
|
|
), |
|
38
|
|
|
"Random annealing": (RandomAnnealingOptimizer, one_init, 1), |
|
39
|
|
|
"Simulated annealing": (SimulatedAnnealingOptimizer, one_init, 0), |
|
40
|
|
|
"Parallel tempering": (ParallelTemperingOptimizer, two_init, 0), |
|
41
|
|
|
"Particle swarm optimizer": (ParticleSwarmOptimizer, n_inits, 0), |
|
42
|
|
|
"Evolution strategy": (EvolutionStrategyOptimizer, n_inits, 0), |
|
43
|
|
|
"Bayesian optimizer": (BayesianOptimizer, six_init, 0), |
|
44
|
|
|
"Tree structured parzen estimators": ( |
|
45
|
|
|
TreeStructuredParzenEstimators, |
|
46
|
|
|
six_init, |
|
47
|
|
|
0, |
|
48
|
|
|
), |
|
49
|
|
|
"Decision tree optimizer": (DecisionTreeOptimizer, six_init, 0), |
|
50
|
|
|
"Ensemble optimizer": (EnsembleOptimizer, six_init, 0), |
|
51
|
|
|
""" |
|
52
|
|
|
|
|
53
|
|
|
optimizer_dict = { |
|
54
|
|
|
"Hill climbing": (HillClimbingOptimizer, one_init, 0), |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
def plot_search_path( |
|
59
|
|
|
optimizer_key, |
|
60
|
|
|
n_iter, |
|
61
|
|
|
objective_function, |
|
62
|
|
|
objective_function_np, |
|
63
|
|
|
search_space, |
|
64
|
|
|
): |
|
65
|
|
|
opt_class, n_inits, random_state = optimizer_dict[optimizer_key] |
|
66
|
|
|
opt = opt_class(search_space, rand_rest_p=0) |
|
67
|
|
|
|
|
68
|
|
|
opt.search( |
|
69
|
|
|
objective_function, |
|
70
|
|
|
n_iter=n_iter, |
|
71
|
|
|
random_state=random_state, |
|
72
|
|
|
memory=False, |
|
73
|
|
|
verbosity=False, |
|
74
|
|
|
initialize={"vertices": n_inits}, |
|
75
|
|
|
) |
|
76
|
|
|
|
|
77
|
|
|
conv = Converter(search_space) |
|
78
|
|
|
|
|
79
|
|
|
plt.figure(figsize=(10, 8)) |
|
80
|
|
|
plt.set_cmap("jet_r") |
|
81
|
|
|
|
|
82
|
|
|
x_all, y_all = search_space["x"], search_space["y"] |
|
83
|
|
|
xi, yi = np.meshgrid(x_all, y_all) |
|
84
|
|
|
zi = objective_function_np(xi, yi) |
|
85
|
|
|
|
|
86
|
|
|
plt.imshow( |
|
87
|
|
|
zi, |
|
88
|
|
|
alpha=0.15, |
|
89
|
|
|
# vmin=z.min(), |
|
90
|
|
|
# vmax=z.max(), |
|
91
|
|
|
# origin="lower", |
|
92
|
|
|
extent=[x_all.min(), x_all.max(), y_all.min(), y_all.max()], |
|
93
|
|
|
) |
|
94
|
|
|
|
|
95
|
|
|
# print("\n Results \n", opt.results) |
|
96
|
|
|
|
|
97
|
|
|
for n, opt_ in enumerate(tqdm(opt.optimizers)): |
|
98
|
|
|
pos_list = np.array(opt_.pos_new_list) |
|
99
|
|
|
score_list = np.array(opt_.score_new_list) |
|
100
|
|
|
|
|
101
|
|
|
values_list = conv.positions2values(pos_list) |
|
102
|
|
|
values_list = np.array(values_list) |
|
103
|
|
|
|
|
104
|
|
|
plt.plot( |
|
105
|
|
|
values_list[:, 0], |
|
106
|
|
|
values_list[:, 1], |
|
107
|
|
|
linestyle="--", |
|
108
|
|
|
marker=",", |
|
109
|
|
|
color="black", |
|
110
|
|
|
alpha=0.33, |
|
111
|
|
|
label=n, |
|
112
|
|
|
linewidth=0.5, |
|
113
|
|
|
) |
|
114
|
|
|
plt.scatter( |
|
115
|
|
|
values_list[:, 0], |
|
116
|
|
|
values_list[:, 1], |
|
117
|
|
|
c=score_list, |
|
118
|
|
|
marker="H", |
|
119
|
|
|
s=15, |
|
120
|
|
|
vmin=-20000, |
|
121
|
|
|
vmax=0, |
|
122
|
|
|
label=n, |
|
123
|
|
|
edgecolors="black", |
|
124
|
|
|
linewidth=0.3, |
|
125
|
|
|
) |
|
126
|
|
|
|
|
127
|
|
|
plt.xlabel("x") |
|
128
|
|
|
plt.ylabel("y") |
|
129
|
|
|
|
|
130
|
|
|
nth_iteration = "\n\nnth Iteration: " + str(n_iter) |
|
131
|
|
|
|
|
132
|
|
|
plt.title(optimizer_key + nth_iteration) |
|
133
|
|
|
|
|
134
|
|
|
plt.xlim((-101, 101)) |
|
135
|
|
|
plt.ylim((-101, 101)) |
|
136
|
|
|
plt.colorbar() |
|
137
|
|
|
# plt.legend(loc="upper left", bbox_to_anchor=(-0.10, 1.2)) |
|
138
|
|
|
|
|
139
|
|
|
plt.tight_layout() |
|
140
|
|
|
plt.savefig( |
|
141
|
|
|
"./_plots/" |
|
142
|
|
|
+ str(opt.__class__.__name__) |
|
143
|
|
|
+ "_" |
|
144
|
|
|
+ "{0:0=2d}".format(n_iter) |
|
145
|
|
|
+ ".jpg", |
|
146
|
|
|
dpi=300, |
|
147
|
|
|
) |
|
148
|
|
|
plt.close() |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
# n_iter = 50 |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
def objective_function(pos_new): |
|
155
|
|
|
score = -(pos_new["x"] * pos_new["x"] + pos_new["y"] * pos_new["y"]) |
|
156
|
|
|
return score |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
def objective_function_np(x1, x2): |
|
160
|
|
|
score = -(x1 * x1 + x2 * x2) |
|
161
|
|
|
return score |
|
162
|
|
|
|
|
163
|
|
|
|
|
164
|
|
|
search_space = {"x": np.arange(-100, 101, 1), "y": np.arange(-100, 101, 1)} |
|
165
|
|
|
|
|
166
|
|
|
n_iter_list = range(1, 51) |
|
167
|
|
|
|
|
168
|
|
|
for optimizer_key in optimizer_dict.keys(): |
|
169
|
|
|
print(optimizer_key) |
|
170
|
|
|
|
|
171
|
|
|
for n_iter in tqdm(n_iter_list): |
|
172
|
|
|
plot_search_path( |
|
173
|
|
|
optimizer_key, |
|
174
|
|
|
n_iter, |
|
175
|
|
|
objective_function, |
|
176
|
|
|
objective_function_np, |
|
177
|
|
|
search_space, |
|
178
|
|
|
) |
|
179
|
|
|
|