-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj_functions.py
236 lines (186 loc) · 6.57 KB
/
obj_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
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from enum import Enum, auto
import numpy as np
from dante.utils import Tracker
class BuiltInSyntheticFunction(Enum):
ACKLEY = auto()
RASTRIGIN = auto()
ROSENBROCK = auto()
GRIEWANK = auto()
MICHALEWICZ = auto()
SCHWEFEL = auto()
LEVY = auto()
@dataclass
class ObjectiveFunction(ABC):
dims: int
turn: float
lb: np.ndarray = field(init=False)
ub: np.ndarray = field(init=False)
name: str = "function"
tracker: Tracker = field(init=False)
counter: int = 0
@abstractmethod
def __call__(self, x: np.ndarray) -> float:
pass
@abstractmethod
def scaled(self, y: float) -> float:
pass
def _preprocess(self, x: np.ndarray) -> np.ndarray:
x = np.array(x / self.turn).round(0) * self.turn
self.counter += 1
assert len(x) == self.dims
assert x.ndim == 1
return x
@dataclass
class Ackley(ObjectiveFunction):
dims: int = 3
turn: float = 0.1
name: str = "ackley"
def __post_init__(self):
self.lb = -5 * np.ones(self.dims)
self.ub = 5 * np.ones(self.dims)
self.tracker = Tracker(self.name + str(self.dims))
def scaled(self, y: float) -> float:
return 100 / (y + 0.01)
def __call__(self, x: np.ndarray, apply_scaling: bool = False, track: bool = True) -> float:
x = self._preprocess(x)
y = float(
-20 * np.exp(-0.2 * np.sqrt(np.inner(x, x) / x.size))
- np.exp(np.cos(2 * np.pi * x).sum() / x.size)
+ 20
+ np.e
)
if track:
self.tracker.track(y, x)
return y if not apply_scaling else self.scaled(y)
@dataclass
class Rastrigin(ObjectiveFunction):
dims: int = 3
turn: float = 0.1
a: float = 10
name: str = "rastrigin"
def __post_init__(self):
self.lb = -5 * np.ones(self.dims)
self.ub = 5 * np.ones(self.dims)
self.tracker = Tracker(self.name + str(self.dims))
def scaled(self, y: float) -> float:
return -1 * y
def __call__(self, x: np.ndarray, apply_scaling: bool = False, track: bool = True) -> float:
x = self._preprocess(x)
n = len(x)
sum_ = np.sum(x**2 - self.a * np.cos(2 * np.pi * x))
y = float(self.a * n + sum_)
if track:
self.tracker.track(y, x)
return y if not apply_scaling else self.scaled(y)
@dataclass
class Rosenbrock(ObjectiveFunction):
dims: int = 3
turn: float = 0.1
name: str = "rosenbrock"
def __post_init__(self):
self.lb = -5 * np.ones(self.dims)
self.ub = 5 * np.ones(self.dims)
self.tracker = Tracker(self.name + str(self.dims))
def scaled(self, y: float) -> float:
return 100 / (y / (self.dims * 100) + 0.01)
def __call__(self, x: np.ndarray, apply_scaling: bool = False, track: bool = True) -> float:
x = self._preprocess(x)
y = float(
np.sum(100.0 * (x[1:] - x[:-1] ** 2.0) ** 2.0 + (1 - x[:-1]) ** 2.0)
)
if track:
self.tracker.track(y, x)
return y if not apply_scaling else self.scaled(y)
@dataclass
class Griewank(ObjectiveFunction):
dims: int = 3
turn: float = 1
name: str = "griewank"
def __post_init__(self):
self.lb = -600 * np.ones(self.dims)
self.ub = 600 * np.ones(self.dims)
self.tracker = Tracker(self.name + str(self.dims))
def scaled(self, y: float) -> float:
return 10 / (y / self.dims + 0.001)
def __call__(self, x: np.ndarray, apply_scaling: bool = False, track: bool = True) -> float:
x = self._preprocess(x)
sum_term = np.sum(x**2)
prod_term = np.prod(np.cos(x / np.sqrt(np.arange(1, len(x) + 1))))
y = float(1 + sum_term / 4000 - prod_term)
if track:
self.tracker.track(y, x)
return y if not apply_scaling else self.scaled(y)
@dataclass
class Michalewicz(ObjectiveFunction):
dims: int = 3
turn: float = 0.01
name: str = "michalewicz"
def __post_init__(self):
self.lb = 0 * np.ones(self.dims)
self.ub = np.pi * np.ones(self.dims)
self.tracker = Tracker(self.name + str(self.dims))
def scaled(self, y: float) -> float:
return -1 * y
def __call__(self, x: np.ndarray, apply_scaling:bool=False, m:float=10, track: bool = True) -> float:
x = self._preprocess(x)
d = len(x)
y = 0
for i in range(d):
y += np.sin(x[i]) * np.sin((i + 1) * x[i] ** 2 / np.pi) ** (2 * m)
if track:
self.tracker.track(y, x)
return float(-1 * y) if not apply_scaling else self.scaled(float(-1 * y))
@dataclass
class Schwefel(ObjectiveFunction):
dims: int = 3
turn: float = 1
name: str = "schwefel"
def __post_init__(self):
self.lb = -500 * np.ones(self.dims)
self.ub = 500 * np.ones(self.dims)
self.tracker = Tracker(self.name + str(self.dims))
def scaled(self, y: float):
if y == 0.0:
return 10000.0
return -1 * y / 100
def __call__(self, x: np.ndarray, apply_scaling: bool = False, track: bool = True) -> float:
x = self._preprocess(x)
dimension = len(x)
sum_part = np.sum(-x * np.sin(np.sqrt(np.abs(x))))
if np.all(np.array(x) == 421, axis=0):
return 0.0
y = float(418.9829 * dimension + sum_part)
if track:
self.tracker.track(y, x)
return y if not apply_scaling else self.scaled(y)
@dataclass
class Levy(ObjectiveFunction):
dims: int = 1
turn: float = 0.1
round: int = 1
name: str = "levy"
def __post_init__(self):
self.lb = -10 * np.ones(self.dims)
self.ub = 10 * np.ones(self.dims)
self.tracker = Tracker(self.name + str(self.dims))
def scaled(self, y: float) -> float:
return -1 * y
def __call__(self, x: np.ndarray, apply_scaling: bool = False, track: bool = True) -> float:
x = self._preprocess(x)
w = []
for idx in range(0, len(x)):
w.append(1 + (x[idx] - 1) / 4)
w = np.array(w)
term1 = (np.sin(np.pi * w[0])) ** 2
term3 = (w[-1] - 1) ** 2 * (1 + (np.sin(2 * np.pi * w[-1])) ** 2)
term2 = 0
for idx in range(1, len(w)):
wi = w[idx]
new = (wi - 1) ** 2 * (1 + 10 * (np.sin(np.pi * wi + 1)) ** 2)
term2 = term2 + new
y = float(term1 + term2 + term3)
if track:
self.tracker.track(y, x)
return y if not apply_scaling else self.scaled(y)