/
mw.py
400 lines (313 loc) · 14.1 KB
/
mw.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import numpy as np
from pymoo.core.problem import Problem
# Based on the C++ implementation by the Ma and Wang
# http://www.escience.cn/people/yongwang1/index.html
from pymoo.problems.many import get_ref_dirs
from pymoo.util.remote import Remote
class MW(Problem):
def __init__(self, n_var, n_obj, n_ieq_constr, **kwargs):
if 'xl' not in kwargs:
kwargs['xl'] = 0
if 'xu' not in kwargs:
kwargs['xu'] = 1
super().__init__(n_var=n_var,
n_obj=n_obj,
n_ieq_constr=n_ieq_constr,
vtype=float, **kwargs)
@staticmethod
def LA1(A, B, C, D, theta):
return A * np.power(np.sin(B * np.pi * np.power(theta, C)), D)
@staticmethod
def LA2(A, B, C, D, theta):
return A * np.power(np.sin(B * np.power(theta, C)), D)
@staticmethod
def LA3(A, B, C, D, theta):
return A * np.power(np.cos(B * np.power(theta, C)), D)
def g1(self, X):
d = self.n_var
n = d - self.n_obj
z = np.power(X[:, self.n_obj - 1:], n)
i = np.arange(self.n_obj - 1, d)
exp = 1 - np.exp(-10.0 * (z - 0.5 - i / (2 * d)) * (z - 0.5 - i / (2 * d)))
distance = 1 + exp.sum(axis=1)
return distance
def g2(self, X):
d = self.n_var
n = d
i = np.arange(self.n_obj - 1, d)
z = 1 - np.exp(-10.0 * (X[:, self.n_obj - 1:] - i / n) * (X[:, self.n_obj - 1:] - i / n))
contrib = (0.1 / (n)) * z * z + 1.5 - 1.5 * np.cos(2 * np.pi * z)
distance = 1 + contrib.sum(axis=1)
return distance
def g3(self, X):
contrib = 2.0 * np.power(
X[:, self.n_obj - 1:] + (X[:, self.n_obj - 2:-1] - 0.5) * (X[:, self.n_obj - 2:-1] - 0.5) - 1.0, 2.0)
distance = 1 + contrib.sum(axis=1)
return distance
class MW1(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 1)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g1(X)
f0 = X[:, 0]
f1 = g * (1 - 0.85 * f0 / g)
g0 = f0 + f1 - 1 - self.LA1(0.5, 2.0, 1.0, 8.0, np.sqrt(2.0) * f1 - np.sqrt(2.0) * f0)
out["F"] = np.column_stack([f0, f1])
out["G"] = g0.reshape((-1, 1))
def _calc_pareto_front(self, ref_dirs=None):
if ref_dirs is None:
F = np.zeros((100, 2))
F[:, 0] = np.linspace(0, 1, 100)
else:
F = ref_dirs
F[:, 1] = 1 - 0.85 * F[:, 0]
l = np.sqrt(2) * F[:, 1] - np.sqrt(2) * F[:, 0]
c = 1 - F[:, 0] - F[:, 1] + 0.5 * np.sin(2 * np.pi * l) ** 8
F = F[c >= 0]
return F
class MW2(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 1)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g2(X)
f0 = X[:, 0]
f1 = g * (1 - f0 / g)
g0 = f0 + f1 - 1 - self.LA1(0.5, 3.0, 1.0, 8.0, np.sqrt(2.0) * f1 - np.sqrt(2.0) * f0)
out["F"] = np.column_stack([f0, f1])
out["G"] = g0.reshape((-1, 1))
def _calc_pareto_front(self, ref_dirs=None):
if ref_dirs is None:
F = np.zeros((100, 2))
F[:, 0] = np.linspace(0, 1, 100)
else:
F = ref_dirs
F[:, 1] = 1 - F[:, 0]
return F
class MW3(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 2)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g3(X)
f0 = X[:, 0]
f1 = g * (1 - f0 / g)
g0 = f0 + f1 - 1.05 - self.LA1(0.45, 0.75, 1.0, 6.0, np.sqrt(2.0) * f1 - np.sqrt(2.0) * f0)
g1 = 0.85 - f0 - f1 + self.LA1(0.3, 0.75, 1.0, 2.0, np.sqrt(2.0) * f1 - np.sqrt(2.0) * f0)
out["F"] = np.column_stack([f0, f1])
out["G"] = np.column_stack([g0, g1])
def _calc_pareto_front(self, ref_dirs=None):
if ref_dirs is None:
F = np.zeros((100, 2))
F[:, 0] = np.linspace(0, 1, 100)
else:
F = ref_dirs
F[:, 1] = 1 - F[:, 0]
invalid = (0.85 - F[:, 0] - F[:, 1] + 0.3 * np.sin(0.75 * np.pi * np.sqrt(2) * (F[:, 1] - F[:, 0])) ** 2) > 0
while invalid.any():
F[invalid, :] *= 1.001
invalid = (0.85 - F[:, 0] - F[:, 1] + 0.3 * np.sin(
0.75 * np.pi * np.sqrt(2) * (F[:, 1] - F[:, 0])) ** 2) > 0
return F
class MW4(MW):
def __init__(self, n_var=None, n_obj=3, **kwargs):
if n_var is None:
n_var = n_obj + 12
super().__init__(n_var, n_obj, 1)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g1(X)
f = g.reshape((-1, 1)) * np.ones((X.shape[0], self.n_obj))
f[:, 1:] *= X[:, (self.n_obj - 2)::-1]
f[:, 0:-1] *= np.flip(np.cumprod(1 - X[:, :(self.n_obj - 1)], axis=1), axis=1)
g0 = f.sum(axis=1) - 1 - self.LA1(0.4, 2.5, 1.0, 8.0, f[:, -1] - f[:, :-1].sum(axis=1))
out["F"] = f
out["G"] = g0.reshape((-1, 1))
def _calc_pareto_front(self, ref_dirs=None):
if ref_dirs is None:
ref_dirs = get_ref_dirs(self.n_obj)
F = ref_dirs
l = F[:, -1] - np.sum(F[:, :-1], axis=1)
c = (1 + 0.4 * np.sin(2.5 * np.pi * l) ** 8) - np.sum(F, axis=1)
return F[c >= 0]
class MW5(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 3)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g1(X)
f0 = g * X[:, 0]
f1 = g * np.sqrt(1.0 - np.power(f0 / g, 2.0))
with np.errstate(divide='ignore'):
atan = np.arctan(f1 / f0)
g0 = f0 ** 2 + f1 ** 2 - np.power(1.7 - self.LA2(0.2, 2.0, 1.0, 1.0, atan), 2.0)
t = 0.5 * np.pi - 2 * np.abs(atan - 0.25 * np.pi)
g1 = np.power(1 + self.LA2(0.5, 6.0, 3.0, 1.0, t), 2.0) - f0 ** 2 - f1 ** 2
g2 = np.power(1 - self.LA2(0.45, 6.0, 3.0, 1.0, t), 2.0) - f0 ** 2 - f1 ** 2
out["F"] = np.column_stack([f0, f1])
out["G"] = np.column_stack([g0, g1, g2])
def _calc_pareto_front(self, **kwargs):
return Remote.get_instance().load("pymoo", "pf", "MW", "MW5.pf")
class MW6(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 1, xl=0.0, xu=1.1)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g2(X)
f0 = g * X[:, 0]
f1 = g * np.sqrt(1.1 * 1.1 - np.power(f0 / g, 2.0))
with np.errstate(divide='ignore'):
atan = np.arctan(f1 / f0)
g0 = f0 ** 2 / np.power(1.0 + self.LA3(0.15, 6.0, 4.0, 10.0, atan), 2.0) + f1 ** 2 / np.power(
1.0 + self.LA3(0.75, 6.0, 4.0, 10.0, atan), 2.0) - 1
out["F"] = np.column_stack([f0, f1])
out["G"] = g0.reshape((-1, 1))
def _calc_pareto_front(self, ref_dirs=None):
if ref_dirs is None:
F = np.zeros((100, 2))
F[:, 0] = np.linspace(0, 1, 100)
else:
F = ref_dirs
F[:, 1] = 1 - F[:, 0]
F = F / np.sqrt(np.sum(F ** 2, axis=1) / 1.21).reshape((-1, 1))
l = np.cos(6 * np.arctan(F[:, 1] / F[:, 0]) ** 4) ** 10
c = 1 - (F[:, 0] / (1 + 0.15 * l)) ** 2 - (F[:, 1] / (1 + 0.75 * l)) ** 2
return F[c >= 0]
class MW7(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 2)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g3(X)
f0 = g * X[:, 0]
f1 = g * np.sqrt(1 - np.power(f0 / g, 2))
with np.errstate(divide='ignore'):
atan = np.arctan(f1 / f0)
g0 = f0 ** 2 + f1 ** 2 - np.power(1.2 + np.abs(self.LA2(0.4, 4.0, 1.0, 16.0, atan)), 2.0)
g1 = np.power(1.15 - self.LA2(0.2, 4.0, 1.0, 8.0, atan), 2.0) - f0 ** 2 - f1 ** 2
out["F"] = np.column_stack([f0, f1])
out["G"] = np.column_stack([g0, g1])
def _calc_pareto_front(self, **kwargs):
return Remote.get_instance().load("pymoo", "pf", "MW", "MW7.pf")
class MW8(MW):
def __init__(self, n_var=None, n_obj=3, **kwargs):
if n_var is None:
n_var = n_obj + 12
super().__init__(n_var, n_obj, 1)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g2(X)
f = g.reshape((-1, 1)) * np.ones((X.shape[0], self.n_obj))
f[:, 1:] *= np.sin(0.5 * np.pi * X[:, (self.n_obj - 2)::-1])
cos = np.cos(0.5 * np.pi * X[:, :(self.n_obj - 1)])
f[:, 0:-1] *= np.flip(np.cumprod(cos, axis=1), axis=1)
f_squared = (f ** 2).sum(axis=1)
g0 = f_squared - (1.25 - self.LA2(0.5, 6.0, 1.0, 2.0, np.arcsin(f[:, -1] / np.sqrt(f_squared)))) * (
1.25 - self.LA2(0.5, 6.0, 1.0, 2.0, np.arcsin(f[:, -1] / np.sqrt(f_squared))))
out["F"] = f
out["G"] = g0.reshape((-1, 1))
def _calc_pareto_front(self, ref_dirs=None):
if ref_dirs is None:
ref_dirs = get_ref_dirs(self.n_obj)
F = ref_dirs
F = F / np.sqrt(np.sum(F ** 2, axis=1)).reshape((-1, 1))
c = (1.25 - 0.5 * np.sin(6 * np.arcsin(F[:, -1])) ** 2) ** 2 - np.sum(F ** 2, axis=1)
return F[c >= 0]
class MW9(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 1)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g1(X)
f0 = g * X[:, 0]
f1 = g * (1.0 - np.power(f0 / g, 0.6))
t1 = (1 - 0.64 * f0 * f0 - f1) * (1 - 0.36 * f0 * f0 - f1)
t2 = (1.35 * 1.35 - (f0 + 0.35) * (f0 + 0.35) - f1) * (1.15 * 1.15 - (f0 + 0.15) * (f0 + 0.15) - f1)
g0 = np.minimum(t1, t2)
out["F"] = np.column_stack([f0, f1])
out["G"] = g0.reshape((-1, 1))
def _calc_pareto_front(self, **kwargs):
return Remote.get_instance().load("pymoo", "pf", "MW", "MW9.pf")
class MW10(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 3)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g2(X)
f0 = g * np.power(X[:, 0], self.n_var)
f1 = g * (1.0 - np.power(f0 / g, 2.0))
g0 = -1.0 * (2.0 - 4.0 * f0 * f0 - f1) * (2.0 - 8.0 * f0 * f0 - f1)
g1 = (2.0 - 2.0 * f0 * f0 - f1) * (2.0 - 16.0 * f0 * f0 - f1)
g2 = (1.0 - f0 * f0 - f1) * (1.2 - 1.2 * f0 * f0 - f1)
out["F"] = np.column_stack([f0, f1])
out["G"] = np.column_stack([g0, g1, g2])
def _calc_pareto_front(self, **kwargs):
return Remote.get_instance().load("pymoo", "pf", "MW", "MW10.pf")
class MW11(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 4, xl=0.0, xu=np.sqrt(2))
def _evaluate(self, X, out, *args, **kwargs):
g = self.g3(X)
f0 = g * X[:, 0]
f1 = g * np.sqrt(2.0 - np.power(f0 / g, 2.0))
g0 = -1.0 * (3.0 - f0 * f0 - f1) * (3.0 - 2.0 * f0 * f0 - f1)
g1 = (3.0 - 0.625 * f0 * f0 - f1) * (3.0 - 7.0 * f0 * f0 - f1)
g2 = -1.0 * (1.62 - 0.18 * f0 * f0 - f1) * (1.125 - 0.125 * f0 * f0 - f1)
g3 = (2.07 - 0.23 * f0 * f0 - f1) * (0.63 - 0.07 * f0 * f0 - f1)
out["F"] = np.column_stack([f0, f1])
out["G"] = np.column_stack([g0, g1, g2, g3])
def _calc_pareto_front(self, **kwargs):
return Remote.get_instance().load("pymoo", "pf", "MW", "MW11.pf")
class MW12(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 2)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g1(X)
f0 = g * X[:, 0]
f1 = g * (0.85 - 0.8 * (f0 / g) - 0.08 * np.abs(np.sin(3.2 * np.pi * (f0 / g))))
g0 = -1.0 * (1 - 0.625 * f0 - f1 + 0.08 * np.sin(2 * np.pi * (f1 - f0 / 1.6))) * (
1.4 - 0.875 * f0 - f1 + 0.08 * np.sin(2 * np.pi * (f1 / 1.4 - f0 / 1.6)))
g1 = (1 - 0.8 * f0 - f1 + 0.08 * np.sin(2 * np.pi * (f1 - f0 / 1.5))) * (
1.8 - 1.125 * f0 - f1 + 0.08 * np.sin(2 * np.pi * (f1 / 1.8 - f0 / 1.6)))
out["F"] = np.column_stack([f0, f1])
out["G"] = np.column_stack([g0, g1])
def _calc_pareto_front(self, ref_dirs=None):
if ref_dirs is None:
F = np.zeros((100, 2))
F[:, 0] = np.linspace(0, 1, 100)
else:
F = ref_dirs
F[:, 1] = 0.85 - 0.8 * F[:, 0] - 0.08 * np.abs(np.sin(3.2 * np.pi * F[:, 0]))
invalid = (1 - 0.8 * F[:, 0] - F[:, 1] + 0.08 * np.sin(2 * np.pi * (F[:, 1] - F[:, 0] / 1.5))) * (
1.8 - 1.125 * F[:, 0] - F[:, 1] + 0.08 * np.sin(2 * np.pi * (F[:, 1] / 1.8 - F[:, 0] / 1.6))) > 0
while invalid.any():
F[invalid, :] *= 1.001
invalid = (1 - 0.8 * F[:, 0] - F[:, 1] + 0.08 * np.sin(2 * np.pi * (F[:, 1] - F[:, 0] / 1.5))) * (
1.8 - 1.125 * F[:, 0] - F[:, 1] + 0.08 * np.sin(
2 * np.pi * (F[:, 1] / 1.8 - F[:, 0] / 1.6))) > 0
return F
class MW13(MW):
def __init__(self, n_var=15, **kwargs):
super().__init__(n_var, 2, 2, xu=1.5)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g2(X)
f0 = g * X[:, 0]
f1 = g * (5.0 - np.exp(f0 / g) - np.abs(0.5 * np.sin(3 * np.pi * f0 / g)))
g0 = -1.0 * (5.0 - (1 + f0 + 0.5 * f0 * f0) - 0.5 * np.sin(3 * np.pi * f0) - f1) * (
5.0 - (1 + 0.7 * f0) - 0.5 * np.sin(3 * np.pi * f0) - f1)
g1 = (5.0 - np.exp(f0) - 0.5 * np.sin(3 * np.pi * f0) - f1) * (
5.0 - (1 + 0.4 * f0) - 0.5 * np.sin(3 * np.pi * f0) - f1)
out["F"] = np.column_stack([f0, f1])
out["G"] = np.column_stack([g0, g1])
def _calc_pareto_front(self, **kwargs):
return Remote.get_instance().load("pymoo", "pf", "MW", "MW13.pf")
class MW14(MW):
def __init__(self, n_var=None, n_obj=3, **kwargs):
if n_var is None:
n_var = n_obj + 12
super().__init__(n_var, n_obj, 1, xu=1.5)
def _evaluate(self, X, out, *args, **kwargs):
g = self.g3(X)
f = np.zeros((X.shape[0], self.n_obj))
f[:, :-1] = X[:, :(self.n_obj - 1)]
LA1 = self.LA1(1.5, 1.1, 2.0, 1.0, f[:, :-1])
inter = (6 - np.exp(f[:, :-1]) - LA1).sum(axis=1)
f[:, -1] = g / (self.n_obj - 1) * inter
alpha = 6.1 - 1 - f[:, :-1] - 0.5 * f[:, :-1] * f[:, :-1] - LA1
g0 = f[:, -1] - 1 / (self.n_obj - 1) * alpha.sum(axis=1)
out["F"] = f
out["G"] = g0.reshape((-1, 1))
def _calc_pareto_front(self, **kwargs):
if self.n_obj == 3:
return Remote.get_instance().load("pymoo", "pf", "MW", "MW14.pf")