-
Notifications
You must be signed in to change notification settings - Fork 1
/
objectives.py
337 lines (279 loc) · 8.19 KB
/
objectives.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import autograd.numpy as np
import matplotlib.pyplot as plt
from autograd import grad
from autograd import hessian
class Benchmark(object):
def __init__(self, dimensions):
self._dimensions = dimensions
self.fglob = np.nan
self.global_optimum = None
def __str__(self):
return '{0} ({1} dimensions)'.format(self.__class__.__name__, self.d)
def __repr__(self):
return self.__class__.__name__
def initial_vector(self):
"""
Random initialisation for the benchmark problem.
Returns
-------
x : sequence
a vector of length ``N`` that contains random floating point
numbers that lie between the lower and upper bounds for a given
parameter.
"""
return np.asarray([np.random.uniform(l, u) for l, u in self.bounds])
def eval(self, x):
"""
Evaluation of the benchmark function.
Parameters
----------
x : sequence
The candidate vector for evaluating the benchmark problem. Must
have ``len(x) == self.N``.
Returns
-------
val : float
the evaluated benchmark function
"""
raise NotImplementedError
def gradient(self, x):
raise NotImplementedError
def H(self, x):
raise NotImplementedError
def _eval_2D(self, x, y):
raise NotImplementedError
def _grad_2D(self, x, y):
raise NotImplementedError
def _grad_norm_2D(self, x, y):
raise NotImplementedError
@property
def bounds(self):
"""
The lower/upper bounds to be used for minimizing the problem.
This a list of (lower, upper) tuples that contain the lower and upper
bounds for the problem. The problem should not be asked for evaluation
outside these bounds. ``len(bounds) == N``.
"""
if self.change_dimensionality:
return [self._bounds[0]] * self.d
else:
return self._bounds
@property
def d(self):
"""
The dimensionality of the problem.
Returns
-------
N : int
The dimensionality of the problem
"""
return self._dimensions
@property
def xmin(self):
"""
The lower bounds for the problem
Returns
-------
xmin : sequence
The lower bounds for the problem
"""
return np.asarray([b[0] for b in self.bounds])
@property
def xmax(self):
"""
The upper bounds for the problem
Returns
-------
xmax : sequence
The upper bounds for the problem
"""
return np.asarray([b[1] for b in self.bounds])
def visualize(self, xmin, xmax, ymin, ymax, delta_x=0.1, delta_y=0.1, paths=None, save_name=None):
fig, axs = plt.subplots(1, 3, figsize=(25,5))
""" Contour Lines """
X, Y = np.meshgrid(np.arange(xmin, xmax, delta_x), np.arange(ymin, ymax, delta_y))
Z = self._eval_2D(X, Y)
axs[0].contour(X, Y, Z)
# Set limits
axs[0].set_xlim(xmin, xmax)
axs[0].set_ylim(ymin, ymax)
# Labels
axs[0].set_xlabel(r'$x_1$')
axs[0].set_ylabel(r'$x_2$')
# Title
axs[0].set_title('Level Lines')
# Plot local/global minima
for x_star in self.global_optimum:
axs[0].plot(*x_star, 'r*', markersize=10)
# Plot saddle points
for x_saddle in self.saddle:
axs[0].plot(*x_saddle, 'b+', markersize=10)
# Plot optimization trajectory
if paths is not None:
color=iter(plt.cm.rainbow(np.linspace(0,1.0,len(paths))))
for path in paths:
c=next(color)
trajectory, method = path
axs[0].quiver(
trajectory[0,:-1],
trajectory[1,:-1],
trajectory[0,1:]-trajectory[0,:-1],
trajectory[1,1:]-trajectory[1,:-1],
scale_units='xy',
angles='xy',
scale=1,
color=c,
label=method
)
# Add legend
axs[0].legend()
""" Gradient Field """
X, Y = np.meshgrid(np.arange(xmin, xmax, delta_x), np.arange(ymin, ymax, delta_y))
dX, dY = self._grad_2D(X,Y)
axs[1].quiver(X, Y, -dX, -dY)
# Set limits
axs[1].set_xlim(xmin, xmax)
axs[1].set_ylim(ymin, ymax)
# Title
axs[1].set_title('Anti-Gradient Field')
# Labels
axs[1].set_xlabel(r'$x_1$')
axs[1].set_ylabel(r'$x_2$')
# Plot local/global minima
for x_star in self.global_optimum:
axs[1].plot(*x_star, 'r*', markersize=10)
# Plot saddle points
for x_saddle in self.saddle:
axs[1].plot(*x_saddle, 'b+', markersize=10)
if paths is not None:
color=iter(plt.cm.rainbow(np.linspace(0,1.0,len(paths))))
for path in paths:
c=next(color)
trajectory, method = path
axs[1].quiver(
trajectory[0,:-1],
trajectory[1,:-1],
trajectory[0,1:]-trajectory[0,:-1],
trajectory[1,1:]-trajectory[1,:-1],
scale_units='xy',
angles='xy',
scale=1,
color=c,
label=method
)
""" Gradient Norm """
X, Y = np.meshgrid(np.arange(xmin, xmax, delta_x), np.arange(ymin, ymax, delta_y))
grad_norm = self._grad_norm_2D(X,Y)
axs[2].contour(X, Y, grad_norm)
# Set limits
axs[2].set_xlim(xmin, xmax)
axs[2].set_ylim(ymin, ymax)
# Title
axs[2].set_title('Gradient Norm')
# Labels
axs[2].set_xlabel(r'$x_1$')
axs[2].set_ylabel(r'$x_2$')
# Plot local/global minima
for x_star in self.global_optimum:
axs[2].plot(*x_star, 'r*', markersize=10)
# Plot saddle points
for x_saddle in self.saddle:
axs[2].plot(*x_saddle, 'b+', markersize=10)
if paths is not None:
color=iter(plt.cm.rainbow(np.linspace(0,1.0,len(paths))))
for path in paths:
c=next(color)
trajectory, method = path
axs[2].quiver(
trajectory[0,:-1],
trajectory[1,:-1],
trajectory[0,1:]-trajectory[0,:-1],
trajectory[1,1:]-trajectory[1,:-1],
scale_units='xy',
angles='xy',
scale=1,
color=c,
label=method
)
if save_name is not None:
plt.savefig(save_name+'.pdf')
class SaddleBench(Benchmark):
def __init__(self, d=2):
Benchmark.__init__(self, d)
self._bounds = list(zip([-3.0] * self.d, [3.0] * self.d))
# Stationary points
self.fglob = - (self.d-1)/4
self.saddle = [np.zeros(self.d)]
self.global_optimum = [np.ones(self.d), -np.ones(self.d)]
def eval(self, x):
return np.sum(0.25*np.power(x[:-1],4)) - np.sum(x[:-1])*x[-1] + 0.5*(self.d-1)*np.power(x[-1],2)
def gradient(self,x):
g = np.array([x_i**3 - x[-1] for x_i in x])
g[-1] = (self.d-1)*x[-1] -np.sum(x[:-1])
return g
def H(self, x):
# Evaluate the hessian
h = np.diag(np.append(3*np.power(x[:-1],2), self.d-1))
h[-1,:-1] = -1
h[:-1,-1] = -1
return h
def _eval_2D(self, x, y):
return 0.25*x**4.0 - x*y + 0.5*y**2.0
def _grad_2D(self, x, y):
dx = x**3.0 - y
dy = y - x
return (dx, dy)
def _grad_norm_2D(self, x, y):
dx = x**3.0 - y
dy = y - x
return np.sqrt(dx**2 + dy**2)
class Rastrigin(Benchmark):
def __init__(self, d=2):
Benchmark.__init__(self, d)
self._bounds = list(zip([-5.0] * self.d, [5.0] * self.d))
# Stationary points
self.fglob = 0.0
self.global_optimum = [np.array([0 for _ in range(self.d)])]
# Initialize gradient function
self.grad_func = grad(self.eval)
# Initialize hessian function
self.hess_func = hessian(self.eval)
def eval(self, x, *args):
return 10.0 * self.d + np.sum(x ** 2.0 - 10.0 * np.cos(2.0 * np.pi * x))
def gradient(self,x):
return self.grad_func(x)
def H(self,x):
return self.hess_func(x)
class LeadingEigenvector(Benchmark):
def __init__(self, d=2, M=None):
Benchmark.__init__(self, d)
self._bounds = list(zip([-5.0] * self.d, [5.0] * self.d))
if M is None:
self.M = np.random.random(size=(self.d,self.d))
self.M = np.dot(self.M, np.transpose(self.M))
else:
self.M = M
# Eigenvalue decomposition
L, V = np.linalg.eig(self.M)
i_star = np.argmax(L)
L_star = L[i_star]
V_star = V[:,i_star]
# Global Minima
self.global_optimum = [np.sqrt(L_star)*V_star, -np.sqrt(L_star)*V_star]
self.fglob = self.eval(self.global_optimum[0])
# Saddle Points
i_saddle = np.array(list(set(np.arange(self.d)) - set(np.array([i_star]))))
self.saddle = []
for i in i_saddle:
self.saddle.append(+np.sqrt(L[i])*V[:,i])
self.saddle.append(-np.sqrt(L[i])*V[:,i])
def eval(self, x):
xx_T = np.dot(np.reshape(x,(self.d, 1)), np.reshape(x, (1, self.d)))
return 0.5*(np.linalg.norm(xx_T - self.M, ord='fro'))**2.0
def gradient(self,x):
xx_T = np.dot(np.reshape(x,(self.d, 1)), np.reshape(x, (1, self.d)))
return np.dot(xx_T - self.M, x)
def H(self, x):
# Evaluate the hessian
xx_T = np.dot(np.reshape(x,(self.d, 1)), np.reshape(x, (1, self.d)))
return (np.linalg.norm(x)**2.0)*np.identity(self.d) + 2*xx_T - self.M