-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
135 lines (120 loc) · 4.33 KB
/
benchmark.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import mpmath as mp
import math
def rosenbrock_2D(data):
"""
Rosenbrock’s function in the 2D case.
Whose global minimum f∗ = 0 occurs at x∗ = (1, 1, ..., 1)
in the domain −5 ≤ xi ≤ 5 where i = 1, 2, ..., n.
"""
return (data[0]-1)**2 + 100*(data[1]-data[0]**2)**2
def rosenbrock(data):
"""
Rosenbrock’s function in the 2D case.
Whose global minimum f∗ = 0 occurs at x∗ = (1, 1, ..., 1)
in the domain −5 ≤ xi ≤ 5 where i = 1, 2, ..., n.
"""
aux = 0
for i in range(len(data)-1):
aux += (data[i]-1)**2 + 100*((data[i+1]-(data[i]**2))**2)
return aux
def rastrigin(data):
"""
Rastrigin’s function
Whose global minimum is f∗ = 0 at (0, 0, ..., 0). This function is highly multimodal.
"""
res2 = 10 * len(data) + np.sum(np.square(data)-10*np.cos(2*np.pi*data))
return res2
def schwefel(data):
"""
Schwefel’s function
Whose global minimum f∗ ≈ −418.9829n occurs at xi = 420.9687 where i = 1, 2, ..., n.
"""
alpha = 418.9828872724339
# fitness = 0
# for i in range(len(data)):
# fitness -= data[i]*math.sin(math.sqrt(math.fabs(data[i])))
# return float(fitness) + alpha*len(data)
# dimensions = len(data)
# aux = 0
# for i in range(len(data)):
# aux += data[i]*mp.sin(mp.sqrt(mp.fabs(data[i])))
# return float(alpha*dimensions-aux)
# data = np.asarray_chkfinite(data)
# n = len(data)
# return 418.9829*n - np.sum( data * np.sin( np.sqrt( np.abs( data ))))
aux1 = alpha*len(data)-np.sum(np.multiply(data,np.sin(np.sqrt(np.abs(data)))))
# print(aux)
# assert aux1 == -aux
return aux1
def easom(data):
"""
Easom’s function
Whose global minimum is f∗ = −1 at x∗ = (π, π) within −100 ≤ x, y ≤ 100. It has many
local minima.
"""
return float(-mp.cos(data[0])*mp.cos(data[1])*mp.exp(-(data[0]-mp.pi)**2 - (data[1]-mp.pi)**2))
def shubert(data, n = 5):
"""
Shubert’s function
Which has 18 global minima f ∗ ≈ −186.7309 for n = 5 in the search domain −10 ≤ x, y ≤ 10.
"""
aux, aux1 = 0, 0
for i in range(1,n+1,1):
aux += i * np.cos(i + (i+1)*data[0])
aux1 += i * np.cos(i + (i+1)*data[1])
return aux * aux1
def yang(data, dimensions = 16):
"""
Yang’s forest-like function has a global minimum f∗ = 0 at (0, 0, ..., 0).
"""
return float(np.sum(abs(data)) * mp.exp(-(np.sum(np.sin(np.square(data))))))
def michaelwicz(data):
aux = 0
for i in range(0, len(data),1):
aux += np.sin(data[i]) * np.sin(i+1*np.square(data[i])/np.pi)**20
return - aux
def ackley(data, a = 20, b = 0.2, c = 2*np.pi ):
data = np.asarray_chkfinite(data) # ValueError if any NaN or Inf
n = len(data)
s1 = np.sum(np.square(data))
s2 = np.sum(np.cos(c*data))
return -a * np.exp(-b*np.sqrt(s1/n)) - np.exp(s2/n)+a+np.exp(1)
def setup_benchmark_function(args):
# if args.dimensions is None:
dimensions = args.dimensions
# rastrigin
if args.rastrigin:
# __function = [benchmark, 'rastrigin']
benchmark, optimum_solution, domain = rastrigin, 0, [-5.12, 5.12]
# rosenbrock
elif args.rosenbrock:
# __function = [benchmark, 'rosenbrock']
benchmark, optimum_solution, domain = rosenbrock, 0, [-5,5]
# easom
elif args.easom:
# __function = [benchmark, 'easom']
benchmark, optimum_solution, domain = easom, -1, [-100,100]
# shubert
elif args.shubert:
# __function = [benchmark, 'shubert']
benchmark, optimum_solution, domain = shubert, -186.7309, [-10,10]
# yang
elif args.yang:
# __function = [benchmark, 'yang']
benchmark, optimum_solution, domain = yang, 0, [-2*np.pi,2*np.pi]
# michaelwicz
# elif args.michaelwicz:
__function = [benchmark, 'michaelwicz']
# benchmark, optimum_solution, domain = michaelwicz, -1.803, [0, np.pi]
# ackley
elif args.ackley:
# __function = [benchmark, 'ackley']
benchmark, optimum_solution, domain = ackley, 0, [-32.768, 32.768]
# schwefel
else:
# __function = [benchmark, 'schwefel']
benchmark, optimum_solution, domain = schwefel, 0, [-500,500]
return dimensions, benchmark, optimum_solution, domain