forked from scipy/scipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_functions.py
318 lines (239 loc) · 8.28 KB
/
test_functions.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
from __future__ import division
import time
import numpy as np
from numpy import sin, cos, pi, exp, sqrt, abs
from scipy.optimize import rosen
class SimpleQuadratic(object):
def fun(self, x):
return np.dot(x, x)
def der(self, x):
return 2. * x
def hess(self, x):
return 2. * np.eye(x.size)
class AsymmetricQuadratic(object):
def fun(self, x):
return np.dot(x, x) + x[0]
def der(self, x):
d = 2. * x
d[0] += 1
return d
def hess(self, x):
return 2. * np.eye(x.size)
class SlowRosen(object):
def fun(self, x):
time.sleep(40e-6)
return rosen(x)
class LJ(object):
"""
The Lennard Jones potential
a mathematically simple model that approximates the interaction between a
pair of neutral atoms or molecules.
https://en.wikipedia.org/wiki/Lennard-Jones_potential
E = sum_ij V(r_ij)
where r_ij is the cartesian distance between atom i and atom j, and the
pair potential has the form
V(r) = 4 * eps * ( (sigma / r)**12 - (sigma / r)**6
Notes
-----
the double loop over many atoms makes this *very* slow in Python. If it
were in a compiled language it would be much faster.
"""
def __init__(self, eps=1.0, sig=1.0):
self.sig = sig
self.eps = eps
def vij(self, r):
return 4. * self.eps * ((self.sig / r)**12 - (self.sig / r)**6)
def dvij(self, r):
p7 = 6. / self.sig * (self.sig / r)**7
p13 = -12. / self.sig * (self.sig / r)**13
return 4. * self.eps * (p7 + p13)
def fun(self, coords):
natoms = coords.size // 3
coords = np.reshape(coords, [natoms, 3])
energy = 0.
for i in range(natoms):
for j in range(i + 1, natoms):
dr = coords[j, :] - coords[i, :]
r = np.linalg.norm(dr)
energy += self.vij(r)
return energy
def der(self, coords):
natoms = coords.size // 3
coords = np.reshape(coords, [natoms, 3])
energy = 0.
grad = np.zeros([natoms, 3])
for i in range(natoms):
for j in range(i + 1, natoms):
dr = coords[j, :] - coords[i, :]
r = np.linalg.norm(dr)
energy += self.vij(r)
g = self.dvij(r)
grad[i, :] += -g * dr/r
grad[j, :] += g * dr/r
grad = grad.reshape([natoms * 3])
return grad
def get_random_configuration(self):
rnd = np.random.uniform(-1, 1, [3 * self.natoms])
return rnd * float(self.natoms)**(1. / 3)
class LJ38(LJ):
natoms = 38
target_E = -173.928427
class LJ30(LJ):
natoms = 30
target_E = -128.286571
class LJ20(LJ):
natoms = 20
target_E = -77.177043
class LJ13(LJ):
natoms = 13
target_E = -44.326801
class Booth(object):
target_E = 0.
solution = np.array([1., 3.])
xmin = np.array([-10., -10.])
xmax = np.array([10., 10.])
def fun(self, coords):
x, y = coords
return (x + 2. * y - 7.)**2 + (2. * x + y - 5.)**2
def der(self, coords):
x, y = coords
dfdx = 2. * (x + 2. * y - 7.) + 4. * (2. * x + y - 5.)
dfdy = 4. * (x + 2. * y - 7.) + 2. * (2. * x + y - 5.)
return np.array([dfdx, dfdy])
class Beale(object):
target_E = 0.
solution = np.array([3., 0.5])
xmin = np.array([-4.5, -4.5])
xmax = np.array([4.5, 4.5])
def fun(self, coords):
x, y = coords
p1 = (1.5 - x + x * y)**2
p2 = (2.25 - x + x * y**2)**2
p3 = (2.625 - x + x * y**3)**2
return p1 + p2 + p3
def der(self, coords):
x, y = coords
dfdx = (2. * (1.5 - x + x * y) * (-1. + y) +
2. * (2.25 - x + x * y**2) * (-1. + y**2) +
2. * (2.625 - x + x * y**3) * (-1. + y**3))
dfdy = (2. * (1.5 - x + x * y) * (x) +
2. * (2.25 - x + x * y**2) * (2. * y * x) +
2. * (2.625 - x + x * y**3) * (3. * x * y**2))
return np.array([dfdx, dfdy])
"""
Global Test functions for minimizers.
HolderTable, Ackey and Levi have many competing local minima and are suited
for global minimizers such as basinhopping or differential_evolution.
(https://en.wikipedia.org/wiki/Test_functions_for_optimization)
See also https://mpra.ub.uni-muenchen.de/2718/1/MPRA_paper_2718.pdf
"""
class HolderTable(object):
target_E = -19.2085
solution = [8.05502, 9.66459]
xmin = np.array([-10, -10])
xmax = np.array([10, 10])
stepsize = 2.
temperature = 2.
def fun(self, x):
return - abs(sin(x[0]) * cos(x[1]) * exp(abs(1. - sqrt(x[0]**2 +
x[1]**2) / pi)))
def dabs(self, x):
"""derivative of absolute value"""
if x < 0:
return -1.
elif x > 0:
return 1.
else:
return 0.
#commented out at the because it causes FloatingPointError in
#basinhopping
# def der(self, x):
# R = sqrt(x[0]**2 + x[1]**2)
# g = 1. - R / pi
# f = sin(x[0]) * cos(x[1]) * exp(abs(g))
# E = -abs(f)
#
# dRdx = x[0] / R
# dgdx = - dRdx / pi
# dfdx = cos(x[0]) * cos(x[1]) * exp(abs(g)) + f * self.dabs(g) * dgdx
# dEdx = - self.dabs(f) * dfdx
#
# dRdy = x[1] / R
# dgdy = - dRdy / pi
# dfdy = -sin(x[0]) * sin(x[1]) * exp(abs(g)) + f * self.dabs(g) * dgdy
# dEdy = - self.dabs(f) * dfdy
# return np.array([dEdx, dEdy])
class Ackley(object):
# note: this function is not smooth at the origin. the gradient will never
# converge in the minimizer
target_E = 0.
solution = [0., 0.]
xmin = np.array([-5, -5])
xmax = np.array([5, 5])
def fun(self, x):
E = (-20. * exp(-0.2 * sqrt(0.5 * (x[0]**2 + x[1]**2))) + 20. + np.e -
exp(0.5 * (cos(2. * pi * x[0]) + cos(2. * pi * x[1]))))
return E
def der(self, x):
R = sqrt(x[0]**2 + x[1]**2)
term1 = -20. * exp(-0.2 * R)
term2 = -exp(0.5 * (cos(2. * pi * x[0]) + cos(2. * pi * x[1])))
deriv1 = term1 * (-0.2 * 0.5 / R)
dfdx = 2. * deriv1 * x[0] - term2 * pi * sin(2. * pi * x[0])
dfdy = 2. * deriv1 * x[1] - term2 * pi * sin(2. * pi * x[1])
return np.array([dfdx, dfdy])
class Levi(object):
target_E = 0.
solution = [1., 1.]
xmin = np.array([-10, -10])
xmax = np.array([10, 10])
def fun(self, x):
E = (sin(3. * pi * x[0])**2 + (x[0] - 1.)**2 *
(1. + sin(3 * pi * x[1])**2) +
(x[1] - 1.)**2 * (1. + sin(2 * pi * x[1])**2))
return E
def der(self, x):
dfdx = (2. * 3. * pi *
cos(3. * pi * x[0]) * sin(3. * pi * x[0]) +
2. * (x[0] - 1.) * (1. + sin(3 * pi * x[1])**2))
dfdy = ((x[0] - 1.)**2 * 2. * 3. * pi * cos(3. * pi * x[1]) * sin(3. *
pi * x[1]) + 2. * (x[1] - 1.) *
(1. + sin(2 * pi * x[1])**2) + (x[1] - 1.)**2 *
2. * 2. * pi * cos(2. * pi * x[1]) * sin(2. * pi * x[1]))
return np.array([dfdx, dfdy])
class EggHolder(object):
target_E = -959.6407
solution = [512, 404.2319]
xmin = np.array([-512., -512])
xmax = np.array([512., 512])
def fun(self, x):
a = -(x[1] + 47) * np.sin(np.sqrt(abs(x[1] + x[0]/2. + 47)))
b = -x[0] * np.sin(np.sqrt(abs(x[0] - (x[1] + 47))))
return a + b
class CrossInTray(object):
target_E = -2.06261
solution = [1.34941, -1.34941]
xmin = np.array([-10., -10])
xmax = np.array([10., 10])
def fun(self, x):
arg = abs(100 - sqrt(x[0]**2 + x[1]**2)/pi)
val = np.power(abs(sin(x[0]) * sin(x[1]) * exp(arg)) + 1., 0.1)
return -0.0001 * val
class Schaffer2(object):
target_E = 0
solution = [0., 0.]
xmin = np.array([-100., -100])
xmax = np.array([100., 100])
def fun(self, x):
num = np.power(np.sin(x[0]**2 - x[1]**2), 2) - 0.5
den = np.power(1 + 0.001 * (x[0]**2 + x[1]**2), 2)
return 0.5 + num / den
class Schaffer4(object):
target_E = 0.292579
solution = [0, 1.253131828927371]
xmin = np.array([-100., -100])
xmax = np.array([100., 100])
def fun(self, x):
num = cos(sin(abs(x[0]**2 - x[1]**2)))**2 - 0.5
den = (1+0.001*(x[0]**2 + x[1]**2))**2
return 0.5 + num / den