Skip to content

Commit aafbdb3

Browse files
committed
added test problem implementations
1 parent fe6137c commit aafbdb3

File tree

1 file changed

+268
-0
lines changed

1 file changed

+268
-0
lines changed

fireflyalgorithm/problems.py

Lines changed: 268 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,268 @@
1+
import numpy as np
2+
3+
4+
def ackley(x):
5+
a = 20
6+
b = 0.2
7+
c = 2 * np.pi
8+
dim = len(x)
9+
10+
val1 = np.sum(np.square(x))
11+
val2 = np.sum(np.cos(c * x))
12+
13+
temp1 = -b * np.sqrt(val1 / dim)
14+
temp2 = val2 / dim
15+
16+
return -a * np.exp(temp1) - np.exp(temp2) + a + np.exp(1)
17+
18+
19+
def alpine1(x):
20+
return np.sum(np.abs(np.sin(x) + 0.1 * x))
21+
22+
23+
def alpine2(x):
24+
return np.prod(np.sqrt(x) * np.sin(x))
25+
26+
27+
def cigar(x):
28+
return x[0] ** 2 + 1000000 * np.sum(x[1:] ** 2)
29+
30+
31+
def cosine_mixture(x):
32+
return -0.1 * np.sum(np.cos(5 * np.pi * x)) - np.sum(x**2)
33+
34+
35+
def csendes(x):
36+
mask = x != 0
37+
return np.sum(np.power(x[mask], 6) * (2 + np.sin(1 / x[mask])))
38+
39+
40+
def dixon_price(x):
41+
dim = len(x)
42+
indices = np.arange(2, dim)
43+
val = np.sum(indices * (2 * x[2:] ** 2 - x[1 : dim - 1]) ** 2)
44+
return (x[0] - 1) ** 2 + val
45+
46+
47+
def griewank(x):
48+
dim = len(x)
49+
i = np.arange(1, dim + 1)
50+
val1 = np.sum(x**2 / 4000)
51+
val2 = np.prod(np.cos(x / np.sqrt(i)))
52+
return val1 - val2 + 1
53+
54+
55+
def katsuura(x):
56+
dim = len(x)
57+
k = np.atleast_2d(np.arange(1, 33)).T
58+
i = np.arange(0, dim * 1)
59+
inner = np.round(2**k * x) * (2 ** (-k))
60+
return np.prod(np.sum(inner, axis=0) * (i + 1) + 1)
61+
62+
63+
def levy(x):
64+
w = 1 + (x - 1) / 4
65+
wi = w[:-1]
66+
term1 = np.sin(np.pi * w[0]) ** 2
67+
term2 = np.sum((wi - 1) ** 2 * (1 + 10 * np.sin(np.pi * wi + 1)))
68+
term3 = (w[-1] - 1) ** 2 * (1 + np.sin(2 * np.pi * w[-1]) ** 2)
69+
return term1 + term2 + term3
70+
71+
72+
def michalewicz(x):
73+
dim = len(x)
74+
m = 10
75+
i = np.arange(1, dim + 1)
76+
return -np.sum(np.sin(x) * np.sin(i * x**2 / np.pi) ** (2 * m))
77+
78+
79+
def perm1(x):
80+
dim = len(x)
81+
beta = 0.5
82+
k = np.atleast_2d(np.arange(dim) + 1).T
83+
j = np.atleast_2d(np.arange(dim) + 1)
84+
s = (j**k + beta) * ((x / j) ** k - 1)
85+
return np.sum(np.sum(s, axis=1) ** 2)
86+
87+
88+
def perm2(x):
89+
dim = len(x)
90+
beta = 10
91+
k = np.atleast_2d(np.arange(dim) + 1).T
92+
j = np.atleast_2d(np.arange(dim) + 1)
93+
s = (j + beta) * (x**k - (1 / j) ** k)
94+
return np.sum(np.sum(s, axis=1) ** 2)
95+
96+
97+
def pinter(x):
98+
dim = len(x)
99+
x = np.asarray(x)
100+
sub = np.roll(x, 1)
101+
add = np.roll(x, -1)
102+
indices = np.arange(1, dim + 1)
103+
104+
a = sub * np.sin(x) + np.sin(add)
105+
b = (sub * sub) - 2 * x + 3 * add - np.cos(x) + 1
106+
107+
val1 = np.sum(indices * x * x)
108+
val2 = np.sum(20 * indices * np.power(np.sin(a), 2))
109+
val3 = np.sum(indices * np.log10(1 + indices * np.power(b, 2)))
110+
111+
return val1 + val2 + val3
112+
113+
114+
def powell(x):
115+
x1 = x[0::4]
116+
x2 = x[1::4]
117+
x3 = x[2::4]
118+
x4 = x[3::4]
119+
120+
term1 = (x1 + 10 * x2) ** 2
121+
term2 = 5 * (x3 - x4) ** 2
122+
term3 = (x2 - 2 * x3) ** 4
123+
term4 = 10 * (x1 - x4) ** 4
124+
return np.sum(term1 + term2 + term3 + term4)
125+
126+
127+
def quing(x):
128+
dim = len(x)
129+
return np.sum(np.power(x**2 - np.arange(1, dim + 1), 2))
130+
131+
132+
def quintic(x):
133+
return np.sum(np.abs(x**5 - 3 * x**4 + 4 * x**3 + 2 * x**2 - 10 * x - 4))
134+
135+
136+
def rastrigin(x):
137+
dim = len(x)
138+
return 10 * dim + np.sum(x**2 - 10 * np.cos(2 * np.pi * x))
139+
140+
141+
def rosenbrock(x):
142+
return np.sum(100.0 * (x[1:] - x[:-1] ** 2) ** 2 + (1 - x[:-1]) ** 2, axis=0)
143+
144+
145+
def salomon(x):
146+
val = np.sqrt(np.sum(x**2))
147+
return 1 - np.cos(2 * np.pi * val) + 0.1 * val
148+
149+
150+
def schaffer2(x):
151+
return (
152+
0.5
153+
+ (np.sin(x[0] ** 2 - x[1] ** 2) ** 2 - 0.5)
154+
/ (1 + 0.001 * (x[0] ** 2 + x[1] ** 2)) ** 2
155+
)
156+
157+
158+
def schaffer4(x):
159+
return (
160+
0.5
161+
+ (np.cos(np.sin(x[0] ** 2 - x[1] ** 2)) ** 2 - 0.5)
162+
/ (1 + 0.001 * (x[0] ** 2 + x[1] ** 2)) ** 2
163+
)
164+
165+
166+
def schwefel(x):
167+
dim = len(x)
168+
return 418.9829 * dim - np.sum(x * np.sin(np.sqrt(np.abs(x))))
169+
170+
171+
def schwefel21(x):
172+
return np.amax(np.abs(x))
173+
174+
175+
def schwefel22(x):
176+
return np.sum(np.abs(x)) + np.prod(np.abs(x))
177+
178+
179+
def sphere(x):
180+
return np.sum(x**2)
181+
182+
183+
def step(x):
184+
return np.sum(np.floor(np.abs(x)))
185+
186+
187+
def step2(x):
188+
return np.sum(np.floor(x + 0.5) ** 2)
189+
190+
191+
def styblinski_tang(x):
192+
return 0.5 * np.sum(x**4 - 16 * x**2 + 5 * x)
193+
194+
195+
def trid(x):
196+
sum1 = np.sum((x - 1) ** 2)
197+
sum2 = np.sum(x[1:] * x[:-1])
198+
return sum1 - sum2
199+
200+
201+
def weierstrass(x):
202+
dim = len(x)
203+
kmax = 20
204+
a = 0.5
205+
b = 3
206+
207+
k = np.atleast_2d(np.arange(kmax + 1)).T
208+
t1 = a**k * np.cos(2 * np.pi * b**k * (x + 0.5))
209+
t2 = dim * np.sum(a**k.T * np.cos(np.pi * b**k.T))
210+
211+
return np.sum(np.sum(t1, axis=0)) - t2
212+
213+
214+
def whitley(x):
215+
xi = x
216+
xj = np.atleast_2d(x).T
217+
218+
temp = 100 * ((xi**2) - xj) + (1 - xj) ** 2
219+
inner = (temp**2 / 4000) - np.cos(temp) + 1
220+
return np.sum(np.sum(inner, axis=0))
221+
222+
223+
def zakharov(x):
224+
dim = len(x)
225+
sum1 = np.sum(x**2)
226+
sum2 = 0.5 * np.sum(np.arange(1, dim + 1) * x)
227+
return sum1 + sum2**2 + sum2**4
228+
229+
230+
PROBLEMS = {
231+
"ackley": ackley,
232+
"alpine1": alpine1,
233+
"alpine2": alpine2,
234+
"cigar": cigar,
235+
"cosine_mixture": cosine_mixture,
236+
"csendes": csendes,
237+
"dixon_price": dixon_price,
238+
"griewank": griewank,
239+
"katsuura": katsuura,
240+
"levy": levy,
241+
"michalewicz": michalewicz,
242+
"perm1": perm1,
243+
"perm2": perm2,
244+
"pinter": pinter,
245+
"powell": powell,
246+
"quing": quing,
247+
"quintic": quintic,
248+
"rastrigin": rastrigin,
249+
"rosenbrock": rosenbrock,
250+
"salomon": salomon,
251+
"schaffer2": schaffer2,
252+
"schaffer4": schaffer4,
253+
"schwefel": schwefel,
254+
"schwefel21": schwefel21,
255+
"schwefel22": schwefel22,
256+
"sphere": sphere,
257+
"step": step,
258+
"step2": step2,
259+
"styblinski_tang": styblinski_tang,
260+
"trid": trid,
261+
"weierstrass": weierstrass,
262+
"whitley": whitley,
263+
"zakharov": zakharov,
264+
}
265+
266+
267+
def get_problem(name):
268+
return PROBLEMS[name]

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy