-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_funs.py
More file actions
238 lines (185 loc) · 8.27 KB
/
benchmark_funs.py
File metadata and controls
238 lines (185 loc) · 8.27 KB
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
import torch
import torch.nn as nn
import warnings
class BenchmarkFunction(nn.Module):
"""Some Information about BenchmarkFunction"""
def __init__(self):
super().__init__()
self.name: str = self.__class__.__name__
self.description: str = self.__class__.__doc__ or "No description"
# 子类只需要写下面的部分
self.dimension: int = 0
self.lb: float = 0 # 简化为一个数
self.ub: float = 0
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
self.larger_better: bool = False
self._device_test = nn.Parameter(torch.rand(1)) # 用于测试device
def forward(self, x: torch.Tensor) -> torch.Tensor:
"""抽象的适应度函数,子类必须实现这个函数。
Args:
x (torch.Tensor): 维度应该为(population_size, self.dimension)
Returns:
torch.Tensor: 返回适应度数值, 维度应该为 (population_size)
"""
warnings.warn("警告:调用了抽象函数。")
return torch.zeros(x.shape[0])
def is_input_valid(self, x: torch.Tensor) -> bool:
"""判断输入是否合法。 子类无需实现,而且每次forward不应当调用这个函数,节省时间。
Args:
x (torch.Tensor): 维度应该为(population_size, self.dimension)
Returns:
bool: 返回是否合法
"""
return x.shape[1] == self.dimension and bool(torch.all(x >= self.lb)) and bool(torch.all(x <= self.ub))
def get_device(self) -> torch.device:
"""获取device。 子类无需实现。
https://stackoverflow.com/questions/58926054/how-to-get-the-device-type-of-a-pytorch-module-conveniently
forward中生成的局部变量,需要指定device。因此需要这样一个测试函数。
Returns:
torch.device: 返回device
"""
return self._device_test.device
def get_budget(self, alpha: int = 10000) -> int:
"""获取预算。 子类无需实现。
"""
return self.dimension*alpha
class NegativeProxyObjective(BenchmarkFunction):
"""Convert an maximization problem to a minimization problem, or vice versa. """
def __init__(self, bf: BenchmarkFunction):
super().__init__()
self.bf = bf
self.name = bf.name
self.description = bf.description
self.dimension = bf.dimension
self.lb = bf.lb
self.ub = bf.ub
self.optinum = bf.optinum
self.optival = -bf.optival
self.larger_better = not bf.larger_better
def forward(self, x: torch.Tensor) -> torch.Tensor:
return -self.bf(x)
class Sphere(BenchmarkFunction):
"""Sphere Model"""
def __init__(self, dimension: int = 30):
super().__init__()
self.dimension: int = dimension
self.lb: float = -100 # 简化为一个数
self.ub: float = 100
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.sum(x**2, dim=1)
class Rosenbrock(BenchmarkFunction):
"""Rosenbrock's Function"""
def __init__(self, dimension: int = 30):
super().__init__()
self.dimension: int = dimension
self.lb: float = -30 # 简化为一个数
self.ub: float = 30
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.sum(100*(x[:, 1:]-x[:, :-1]**2)**2+(x[:, :-1]-1)**2, dim=1)
class Step(BenchmarkFunction):
"""Step Function"""
def __init__(self, dimension: int = 30):
super().__init__()
self.dimension: int = dimension
self.lb: float = -100 # 简化为一个数
self.ub: float = 100
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.sum(torch.floor(x+0.5)**2, dim=1)
class NoisyQuartic(BenchmarkFunction):
"""Noisy Quartic"""
def __init__(self, dimension: int = 30):
super().__init__()
self.dimension: int = dimension
self.lb: float = -1.28 # 简化为一个数
self.ub: float = 1.28
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.sum(torch.arange(1, x.shape[1]+1).to(x.device)*x**4+torch.rand(1).to(x.device), dim=1)
class Schwefel226(BenchmarkFunction):
"""Schwefel's Problem 2.26"""
def __init__(self, dimension: int = 30):
super().__init__()
self.dimension: int = dimension
self.lb: float = -500 # 简化为一个数
self.ub: float = 500
self.optinum: torch.Tensor = 420.9687*torch.ones(self.dimension)
self.optival: float = -12569.5
def forward(self, x: torch.Tensor) -> torch.Tensor:
abs_x = torch.abs(x)
return -torch.sum(x*torch.sin(torch.sqrt(abs_x)), dim=1)
class Ackley(BenchmarkFunction):
"""Ackley"""
def __init__(self, dimension: int = 30):
super().__init__()
self.dimension: int = dimension
self.lb: float = -32 # 简化为一个数
self.ub: float = 32
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
def forward(self, x: torch.Tensor) -> torch.Tensor:
return -20 * torch.exp(
-0.2 * torch.sqrt(torch.sum(x**2, dim=1) / x.shape[1])
) - torch.exp(torch.sum(torch.cos(2 * torch.pi * x), dim=1) / x.shape[1]) + 20 + torch.e
class Schwefel222(BenchmarkFunction):
"""Schwefel's Problem 2.22"""
def __init__(self, dimension: int = 30):
super().__init__()
self.dimension: int = dimension
self.lb: float = -10 # 简化为一个数
self.ub: float = 10
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
def forward(self, x: torch.Tensor) -> torch.Tensor:
abs_x = torch.abs(x)
return torch.sum(abs_x**2, dim=1)+torch.prod(abs_x, dim=1)
class Holder(BenchmarkFunction):
"""Holder Table"""
def __init__(self, dimension: int = 2):
super().__init__()
self.dimension: int = dimension
self.lb: float = -10 # 简化为一个数
self.ub: float = 10
self.optinum: torch.Tensor = torch.Tensor([8.05502, 9.66459])
# 还可以有其他解,就是这个解的正负任取。
self.optival: float = -19.2085
def forward(self, x: torch.Tensor) -> torch.Tensor:
x1 = x[:, 0]
x2 = x[:, 1]
return -torch.abs(torch.sin(x1)*torch.cos(x2)*torch.exp(torch.abs(1-torch.sqrt(x1**2+x2**2)/torch.pi)))
class Camel3(BenchmarkFunction):
"""Three Hump Camel"""
def __init__(self, dimension: int = 2):
super().__init__()
self.dimension: int = dimension
self.lb: float = -5 # 简化为一个数
self.ub: float = 5
self.optinum: torch.Tensor = torch.zeros(self.dimension)
self.optival: float = 0
def forward(self, x: torch.Tensor) -> torch.Tensor:
x1 = x[:, 0]
x2 = x[:, 1]
return 2*x1**2 - 1.05*x1**4 + x1**6/6 + x1*x2 + x2**2
class Michal(BenchmarkFunction):
"""Michalewicz"""
def __init__(self, dimension: int = 2, m:int =10):
super().__init__()
self.dimension: int = dimension
self.lb: float = 0 # 简化为一个数
self.ub: float = torch.pi
self.optinum: torch.Tensor = torch.Tensor([2.20, 1.57])
self.optival: float = -1.8013
self.m = m
def forward(self, x: torch.Tensor) -> torch.Tensor:
y = torch.sin(x) * torch.sin(torch.arange(1,
x.shape[1]+1).to(x.device)*x**2 / torch.pi)**(2 * self.m)
return -torch.sum(y, dim=1)
benchmark_functions = [Sphere(), Rosenbrock(), Step(), NoisyQuartic(
), Schwefel226(), Ackley(), Schwefel222(), Holder(), Camel3(), Michal()]