-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestfunctions.py
93 lines (67 loc) · 2.66 KB
/
testfunctions.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
""" Testfunctions, should be evaluated on [0,1]. """
import numpy as np
class Testfunction:
def __str__(self):
return type(self).__name__
class Highfreq(Testfunction):
def __call__(self, x):
return np.exp(3*x) + 4*np.sin(5*x) + np.sin(80*x) + 2
class Lowfreq(Testfunction):
def __call__(self, x):
return np.exp(3*x) + 4*np.sin(5*x) + 2
class Jump(Testfunction):
def __call__(self, x):
return np.piecewise(x,
[x <= 0.5, x > 0.5 ],
[2, 4])
class Constant(Testfunction):
def __init__(self, value):
self.value = value
def __str__(self):
return "Constant" + str(self.value)
def __call__(self, x):
return np.full_like(x, self.value)
# For 3d function coords is expected to be an n x 2 array.
# [ [x1, y1], [x2, y2], [x3, y3], ...]
class Eggholder(Testfunction):
""" According to https://en.wikipedia.org/wiki/Test_functions_for_optimization
Eggholder function shifted to [0,1]x[0,1]. """
def __call__(self, coords):
x = (coords[:,0] - 0.5) * 512
y = (coords[:,1] - 0.5) * 512
return -(y+47) * np.sin(np.sqrt(np.abs(x/2 + y+47))) - x*np.sin(np.sqrt(np.abs(x-(y+47)))) + 400
class Rosenbrock(Testfunction):
""" https://en.wikipedia.org/wiki/Rosenbrock_function. Shifted from [-2,2] to [0,1] """
def __call__(self, coords):
a = 1
b = 100
x = (coords[:,0] - 0.5) * 4
y = (coords[:,1] - 0.5) * 4
return np.square(a-x) + b * np.square((y-np.square(x)))
# return scipy.optimize.rosen(coords)
class Platform(Testfunction):
""" A jump between 4 different niveaus. """
def __call__(self, coords):
""" Excepts an array of coordinates: [ [x,y], ... ] """
vectors = np.piecewise(coords,
[
(coords[:,0] <= 0.5) & (coords[:,1] <= 0.5), # lower right
(coords[:,0] <= 0.5) & (coords[:,1] > 0.5), # lower left
(coords[:,0] > 0.5) & (coords[:,1] > 0.5), # upper left
(coords[:,0] > 0.5) & (coords[:,1] <= 0.5) # upper right
],
[ 2, 5, 7, 8 ]
)
return vectors[:,0]
import matplotlib.pyplot as plt
if __name__ == "__main__":
x = np.linspace(0, 1, 50)
y = np.linspace(0, 1, 50)
grid = np.meshgrid(x, y)
coords = np.vstack(np.meshgrid(x,y)).reshape(2, -1).T
z = platform(coords)
print(z)
plt.contourf(x, y, z.reshape(50,50))
plt.legend()
plt.show()
set_trace()