-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjective.py
281 lines (241 loc) · 8.03 KB
/
objective.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
import numpy as np
class Obj(object):
def __init__(self):
self.curr_best_y = np.inf
self.curr_best_x = []
def evaluate_true(self, x, func_name):
f_x = None
if func_name == 'Branin':
f_x = Branin()
elif func_name == 'Alpine1':
f_x = Alpine1()
elif func_name == 'Eggholder':
f_x = Eggholder()
elif func_name == 'Hartmann6':
f_x = Hartmann6()
elif func_name == 'Ackley2':
f_x = Ackley2()
elif func_name == 'Ackley10':
f_x = Ackley10()
elif func_name == 'Rosenbrock2':
f_x = Rosenbrock2()
elif func_name == 'Rosenbrock10':
f_x = Rosenbrock10()
elif func_name == 'BraninForrester':
f_x = BraninForrester()
elif func_name == 'Cosines':
f_x = Cosines()
elif func_name == 'GoldsteinPrice':
f_x = GoldsteinPrice()
elif func_name == 'SixHumpCamel':
f_x = SixHumpCamel()
f_eval = f_x.evaluate(x)
f_eval = float(f_eval)
if (f_eval < self.curr_best_y):
self.curr_best_y = f_eval
self.curr_best_x = x
return np.array([f_eval])
def evaluate(self, x, func_name):
return self.evaluate_true(x, func_name)
class BraninForrester():
def evaluate(self, X):
input_dim = 2
bounds = [(-5, 10), (0, 15)]
a = 1
b = 5.1 / (4 * np.pi ** 2)
c = 5 / np.pi
r = 6
s = 10
t = 1 / (8 * np.pi)
xmin = [-3.689, 13.679]
fmin = -16.64402
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
x1 = X[..., 0]
x2 = X[..., 1]
fval = a * (x2 - b * x1 ** 2 + c * x1 - r) ** 2 + s * (1 - t) * np.cos(x1) + s +5*x1
return fval
class Cosines():
bounds = [(5, 0), (5, 0)]
xmin = [0.3125, 0.3125]
fmin = -1.6
def evaluate(self, X):
input_dim = 2
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
x1 = X[..., 0]
x2 = X[..., 1]
g_x1 = (1.6*x1-0.5)**2
g_x2 = (1.6*x2-0.5)**2
r_x1 = 0.3*np.cos(3*np.pi*(1.6*x1-0.5))
r_x2 = 0.3 * np.cos(3 * np.pi * (1.6 * x2 - 0.5))
fval = -(1 - (g_x1-r_x1)-(g_x2-r_x2))
return fval
class GoldsteinPrice():
def evaluate(self, X):
input_dim = 2
bounds = [(-2, 2), (-2, 2)]
xmin = [0, -1]
fmin = 3
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
x1 = X[..., 0]
x2 = X[..., 1]
part1 = (1 + (x1 + x2 + 1) ** 2 * (19 - 14 * x1 + 3 * x1 ** 2 - 14 * x2 + 6 * x1 * x2 + 3 * x2 ** 2))
part2 = (30 + (2 * x1 - 3 * x2) ** 2 * (
18 - 32 * x1 + 12 * x1 ** 2 + 48 * x2 - 36 * x1 * x2 + 27 * x2 ** 2))
fval = part1 * part2
return fval
class SixHumpCamel():
def evaluate(self, X):
input_dim = 2
bounds = [(-3, 3), (-2, 2)]
xmin = [[0.0898, -0.7126],[-0.0898,0.7126]]
fmin = -1.0316
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
x1 = X[..., 0]
x2 = X[..., 1]
fval = (4-2.1*x1**2 + x1**4 /3)*x1**2 +x1*x2+(-4 + 4*x2**2)*x2**2
return fval
# Branin objective function
class Branin():
def evaluate(self, X):
input_dim = 2
bounds = [(-5, 10), (0, 15)]
a = 1
b = 5.1 / (4 * np.pi ** 2)
c = 5 / np.pi
r = 6
s = 10
t = 1 / (8 * np.pi)
sd = 0
xmin = [(-np.pi, 12.275), (np.pi, 2.275), (9.42478, 2.475)]
fmin = 0.397887
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
x1 = X[..., 0]
x2 = X[..., 1]
fval = a * (x2 - b * x1 ** 2 + c * x1 - r) ** 2 + s * (1 - t) * np.cos(x1) + s
return fval
# Alpine1 objective function
class Alpine1():
bounds = [(-10, 10), (-10, 10), (-10, 10), (-10, 10), (-10, 10)]
xmin = [(0, 0, 0, 0, 0)]
fmin = 0
def evaluate(self, X):
input_dim = 5
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
X = X.reshape(input_dim)
fval = np.abs(X * np.sin(X) + 0.1 * X).sum()
return fval
# Egg Holder objective function
class Eggholder():
def evaluate(self, X):
input_dim = 2
bounds = [(-512, 512), (-512, 512)]
xmin = [(512, 404.2319)]
fmin = -959.6407
X = X.ravel()
assert X.shape[0] == input_dim
x1 = X[..., 0]
x2 = X[..., 1]
fval = -(x2 + 47) * np.sin(np.sqrt(np.abs(x2 + 0.5 * x1 + 47))) - x1 * np.sin(
np.sqrt(np.abs(x1 - (x2 + 47))))
return fval
class Hartmann6():
bounds = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
xmin = (0.20169, 0.150011, 0.476874, 0.275332, 0.311652, 0.6573)
fmin = -3.32237
def evaluate(self, X):
input_dim = 6
A = np.array([[10, 3, 17, 3.5, 1.7, 8],
[0.05, 10, 17, 0.1, 8, 14],
[3, 3.5, 1.7, 10, 17, 8],
[17, 8, 0.05, 10, 0.1, 14]])
P = np.array([
[0.1312, 0.1696, 0.5569, 0.0124, 0.8283, 0.5886],
[0.2329, 0.4135, 0.8307, 0.3736, 0.1004, 0.9991],
[0.2348, 0.1451, 0.3522, 0.2883, 0.3047, 0.6650],
[0.4047, 0.8828, 0.8732, 0.5743, 0.1091, 0.0381]])
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
alp = np.array([1.0, 1.2, 3.0, 3.2])
inner_sum = np.sum(A * (X - P) ** 2, axis=-1)
fval = -(np.sum(alp * np.exp(-inner_sum), axis=-1))
return fval
class Ackley2():
bounds = [(-30, 30), (-30, 30)]
xmin = [(0, 0)]
fmin = 0
def evaluate(self, X):
input_dim = 2
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
x1 = X[..., 0]
x2 = X[..., 1]
fval = -20 * np.exp(-0.02 * np.sqrt((np.sum(x1 ** 2 + x2 ** 2))/input_dim)) - np.exp(
(np.cos(2 * np.pi * x1) + np.cos(2 * np.pi * x2)) / 2) + 20 + np.exp(1)
return fval
class Ackley10():
bounds = [(-30, 30), (-30, 30), (-30, 30), (-30, 30), (-30, 30), (-30, 30), (-30, 30), (-30, 30), (-30, 30),
(-30, 30)]
xmin = [(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)]
fmin = 0
def evaluate(self, X):
input_dim = 10
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
X = X.reshape(input_dim)
fval = -20 * np.exp(-0.02 * np.sqrt((np.sum(X ** 2))/input_dim)) - np.exp(
np.sum(np.cos(2 * np.pi * X))/ input_dim)+ 20 + np.exp(1)
return fval
class Rosenbrock2():
bounds = [(-5, 10), (-5, 10)]
xmin = [(1, 1)]
fmin = 0
def evaluate(self, X):
input_dim = 2
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
X = X.reshape(input_dim)
fval = 0
for i in range(input_dim-1):
fval = fval + 100*(X[i+1]-X[i]**2)**2 + (X[i]-1)**2
return fval
class Rosenbrock10():
bounds = [(-5, 10), (-5, 10), (-5, 10), (-5, 10), (-5, 10), (-5, 10), (-5, 10), (-5, 10), (-5, 10), (-5, 10)]
xmin = [(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)]
fmin = 0
def evaluate(self, X):
input_dim = 10
X = X.ravel()
if X.shape[0] != input_dim:
return 'Wrong input dimension'
else:
X = X.reshape(input_dim)
fval = 0
for i in range(input_dim-1):
fval = fval + 100*(X[i+1]-X[i]**2)**2 + (X[i]-1)**2
return fval