-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
287 lines (256 loc) · 8.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from optimizers import DFoptimizers as dfo
from optimizers import GBoptimizers as gbo
from functions.functions import function as f
from functions.functions import gradient as g
from functions.functions import hessian as h
from functions.functions import optinfo
from functions.functions import getcons
from collections import defaultdict
from functools import partial
from utils import graph
import matplotlib.pyplot as plt
import numpy as np
from time import time
import seaborn as sns
import pandas as pd
from tqdm import tqdm
np.random.seed(100)
# Optimization algorithm stop parameters
MAX_ITER = 1000
FTOL = 1e-14
GTOL = 1e-14
XTOL = 1e-14
EPSILON = 1e-8
CONVERGENCE_THRESHOLD = 1e-3 # for plot_box
IMPOSSIBLEPAIRS = {
("Newton Line Search", "Norm1SphereWithSphereCons"),
("Newton Basic", "CrossInTray"),
("Newton Basic", "Holder"),
("GD", "CrossInTray"),
("GD", "Holder"),
}
def init_optimizers(n, name, opts_list, **kwargs):
fct = partial(f, name)
gdt = partial(g, name)
hes = partial(h, name)
optinf = partial(optinfo, name)
cons = getcons(name, n)
opts = {}
# Tolerance hyperparameters
max_iter = kwargs.pop('max_iter', MAX_ITER)
ftol = kwargs.pop('ftol', FTOL)
gtol = kwargs.pop('gtol', GTOL)
xtol = kwargs.pop('xtol', XTOL)
epsilon = kwargs.pop('epsilon', EPSILON)
if "MADS" in opts_list:
params = kwargs.pop("MADS", {})
opts["MADS"] = dfo.MADSOptimizer(
dim=n,
function=fct,
constraints=cons,
getoptinfo=optinf,
max_iter=max_iter,
ftol=ftol,
xtol=xtol,
mu_min=params.pop("mu_min", 1 / (4**14)),
use_minibasis=params.pop("use_minibasis", False),
)
if "CMAES" in opts_list:
params = kwargs.pop("CMAES", {})
opts["CMAES"] = dfo.CMAESOptimizer(
dim=n,
function=fct,
constraints=cons,
getoptinfo=optinf,
max_iter=max_iter,
ftol=ftol,
xtol=xtol,
learning_rate=params.pop("learning_rate", 1),
lambd=params.pop("lambd", None),
)
if "Newton Line Search" in opts_list:
params = kwargs.pop("Newton Line Search", {})
opts["Newton Line Search"] = gbo.NewtonLineSearchOptimizer(
dim=n,
function=fct,
gradient=gdt,
hessian=hes,
constraints=cons,
getoptinfo=optinf,
max_iter=max_iter,
ftol=ftol,
gtol=gtol,
xtol=xtol,
epsilon=params.pop("epsilon", epsilon),
)
if "Newton Log Barrier" in opts_list:
params = kwargs.pop("Newton Log Barrier", {})
opts["Newton Log Barrier"] = gbo.NewtonLogBarrierOptimizer(
dim=n,
function=fct,
gradient=gdt,
hessian=hes,
constraints=cons,
getoptinfo=optinf,
max_iter=max_iter,
ftol=ftol,
gtol=gtol,
xtol=xtol,
epsilon=epsilon,
mu=params.pop("mu", 1.05),
theta0=params.pop("theta0", 100),
)
if "Newton Basic" in opts_list:
opts["Newton Basic"] = gbo.NewtonBasicOptimizer(
dim=n,
function=fct,
gradient=gdt,
hessian=hes,
constraints=cons,
getoptinfo=optinf,
max_iter=max_iter,
ftol=ftol,
gtol=gtol,
xtol=xtol
)
if "GD" in opts_list:
params = kwargs.pop("GD", {})
opts["GD"] = gbo.GradientDescentOptimizer(
dim=n,
function=fct,
gradient=gdt,
hessian=hes,
constraints=cons,
getoptinfo=optinf,
max_iter=max_iter,
ftol=ftol,
gtol=gtol,
xtol=xtol,
epsilon=epsilon,
learning_rate=params.pop("learning_rate", 1e-2)
)
return opts, cons
def plot_optimization(name, opts_list, x0, hp):
# Problem definition
n = x0.shape[0]
opts, _ = init_optimizers(n, name, opts_list, **hp[name])
# Optimize and plot
res = {}
for opt in opts.keys():
res[opt] = opts[opt].optimize(
x0=x0,
verbose=True
)
graph.plot_track(res[opt], opt, name)
plt.show()
def draw_x0(name, cons, xmin=-10, xmax=10):
if len(cons):
if name == "Norm1SphereWithSphereCons":
xmin, xmax = -np.sqrt(dim * 3), np.sqrt(dim * 3)
x0 = np.random.uniform(low=xmin, high=xmax, size=dim).reshape(-1, 1)
while not cons.test(x0):
x0 = np.random.uniform(low=xmin, high=xmax, size=dim).reshape(-1, 1)
else:
x0 = np.random.uniform(low=xmin, high=xmax, size=dim).reshape(-1, 1)
return x0
def plot_box(opts_list, hp, n_iter=10, dim=2, names=['Sphere'], cv_threshold=CONVERGENCE_THRESHOLD, latex=False):
data = []
for name in names:
print("\n%s\n" % "Optimization for {:s} function starting".format(name).center(150, "-"))
for _ in tqdm(range(n_iter), total=n_iter):
opts, cons = init_optimizers(dim, name, opts_list=opts_list, **hp[name])
x0 = draw_x0(name, cons)
for opt in opts_list:
if (opt, name) in IMPOSSIBLEPAIRS:
continue
track = opts[opt].optimize(
x0=x0,
verbose=False
)
_, _, timeperiter, _, _, xoptdiffs, foptdiffs = map(np.array, zip(*track))
data.append([opt, name, np.sum(timeperiter[1:]), len(track) - 1, xoptdiffs[-1], foptdiffs[-1]])
data = pd.DataFrame(data, columns=['Optimizer', 'Function', 'Timestamp', 'NbIter', 'Finalxoptdiff', 'Finalfoptdiff'])
data["Convergence"] = (data["Finalfoptdiff"] <= cv_threshold) & (data["Finalxoptdiff"] <= cv_threshold)
graph.print_statistics(data, opts_list, names, n_iter, cv_threshold=CONVERGENCE_THRESHOLD, latex=latex)
graph.plot_box(data, names, n_iter, cv_threshold=CONVERGENCE_THRESHOLD)
plt.show()
if __name__ == "__main__":
# Main parameters
dim = 2
constrained = False
# Objective function
names = []
if constrained:
names += ["SphereWithLinCons"]
names += ["Norm1SphereWithSphereCons"]
names += ["StyblinskiTangWithPosCons"]
else:
names += ["Sphere"]
names += ["Rosenbrock"]
names += ["Rastigrin"]
names += ["StyblinskiTang"]
if dim == 2:
names += ["Levy13"]
names += ["Easom"]
names += ["CrossInTray"]
names += ["Holder"]
pass
opts_list = []
if constrained:
opts_list += ["MADS"]
opts_list += ["CMAES"]
opts_list += ["Newton Line Search"]
opts_list += ["Newton Log Barrier"]
else:
opts_list += ["MADS"]
opts_list += ["CMAES"]
opts_list += ['Newton Basic']
opts_list += ["GD"]
# hp template
hyperparameters = defaultdict(lambda: {})
hyperparameters["Rastigrin"] = {
'CMAES': {
'lambd': int(2 * np.log(dim) * 4 + int(3 * np.log(dim)))
}
}
hyperparameters["StyblinskiTang"] = {
'xtol': -1,
'CMAES': {
'lambd': int(2 * np.log(dim) * 4 + int(3 * np.log(dim)))
}
}
hyperparameters["Easom"] = {
'max_iter': 2000,
'ftol': 0,
'CMAES': {
'lambd': int(2 * np.log(dim) * 4 + int(3 * np.log(dim)))
}
}
hyperparameters["Holder"] = {
'ftol': -1,
'CMAES': {
'lambd': int(2 * np.log(dim) * 4 + int(3 * np.log(dim)))
}
}
hyperparameters["SphereWithLinCons"] = {
"MADS": {
"mu_min": 1 / (4**12),
},
}
hyperparameters["Norm1SphereWithSphereCons"] = {
"MADS": {
"mu_min": 1 / (4**12),
},
}
hyperparameters["StyblinskiTangWithPosCons"] = {
'xtol': -1,
'CMAES': {
'lambd': int(2 * np.log(dim) * 4 + int(3 * np.log(dim)))
},
"Newton Line Search": {
"epsilon": 1e-8
}
}
plot_box(opts_list=opts_list, hp=hyperparameters, n_iter=100, dim=dim, names=names, latex=False)
# x0 = np.array([-50, -50]).reshape(-1, 1)
# plot_optimization(names[0], opts_list, x0, hyperparameters)