-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_functions.py
150 lines (110 loc) · 4 KB
/
test_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
import torch
import numpy as np
def Griewank(x):
# evaluates function
# inputs:
# x = feature vector of size (n_samples x n_features)
# outputs:
# f_val = scalar function evaluated at input features of size (n_samples x 1)
n_features = x.shape[1]
n_samples = x.shape[0]
temp_vec = torch.FloatTensor(range(1,n_features+1))
denom = torch.sqrt(temp_vec)
cos_vec = torch.cos(x/denom)
f_val = 1 + torch.sum(x*x, dim=1)/4000 - torch.prod(cos_vec, dim=1)
return f_val
def Drop_Wave(x):
n_features = x.shape[1]
n_samples = x.shape[0]
f_val = torch.zeros(n_samples)
squared_sum = x[:,0]**2 + x[:,1]**2
f_val = ( 1 + torch.cos(12*torch.sqrt(squared_sum)) ) / ( 0.5 * squared_sum + 2 )
f_val = -f_val
return f_val
def AlpineN1(x):
n_features = x.shape[1]
n_samples = x.shape[0]
f_val = torch.zeros(n_samples)
f_val = torch.sum(torch.abs(x * torch.sin(x) + 0.1*x), dim=1)
# for i in range(n_features):
# f_val = f_val + torch.abs(x[:,i] * torch.sin(x[:,i]) + 0.1*x[:,i])
return f_val
def Ackley(x):
n_features = x.shape[1]
n_samples = x.shape[0]
f_val = torch.zeros(n_samples)
a = 20
b = 0.2
c = 2*np.pi
cos_sum_term = (1/n_features) * torch.sum( torch.cos(c*x), dim=1)
quad_sum_term = (1/n_features) * torch.sum( x*x, dim=1)
f_val = -a * torch.exp( -b * torch.sqrt( quad_sum_term) ) - torch.exp(cos_sum_term) + a + np.exp(1)
return f_val
def Levy(x):
n_features = x.shape[1]
n_samples = x.shape[0]
f_val = torch.zeros(n_samples)
w = 1 + (x-1)/4
init_term = torch.sin(np.pi*w[:,0]) ** 2
fin_term = (w[:,n_features-1] - 1)**2 * (1 + torch.sin(2*np.pi*w[:,n_features-1]))
for i in range(n_features-1):
f_val = f_val + (w[:,i]-1)**2 * ( 1 + 10*torch.sin( np.pi*w[:,i] + 1)**2 )
f_val = f_val + init_term + fin_term
return f_val
def Rastrigin(x):
n_features = x.shape[1]
n_samples = x.shape[0]
f_val = torch.zeros(n_samples)
f_val = torch.sum(x**2 - 10*torch.cos(2*np.pi*x), dim=1)
f_val = f_val + 10*n_features
return f_val
# ----------------------------------------------------------------------
# Numpy Versions of functions above for built-in python solvers
# ----------------------------------------------------------------------
def Griewank_numpy(x):
dim = x.shape[0]
# temp_vec = np.asarray(range(1,dim+1))
# denom = np.sqrt(temp_vec)
# cos_vec = np.cos(x/denom)
cos_vec = np.zeros(dim)
for i in range(dim):
cos_vec[i] = np.cos(x[i]/np.sqrt(i+1))
f_val = 1 + np.sum(x*x)/4000 - np.prod(cos_vec)
return f_val
def Drop_Wave_numpy(x):
# must be 2D
sum_squared_term = x[0]**2 + x[1]**2
f_val = -( 1 + np.cos(12 * np.sqrt(sum_squared_term)) )/( 0.5 * sum_squared_term + 2)
return f_val
def Rastrigin_numpy(x):
n_features = len(x)
f_val = np.sum(x**2 - 10*np.cos(2*np.pi*x))
f_val = f_val + 10*n_features
return float(f_val)
def Levy_numpy(x):
n_features = x.shape[0]
f_val = 0.0
w = 1 + (x-1)/4
init_term = np.sin(np.pi*w[0]) ** 2
fin_term = (w[n_features-1] - 1)**2 * (1 + np.sin(2*np.pi*w[n_features-1]))
for i in range(n_features-1):
f_val = f_val + (w[i]-1)**2 * ( 1 + 10*np.sin( np.pi*w[i] + 1)**2 )
f_val = f_val + init_term + fin_term
return f_val
def AlpineN1_numpy(x):
n_features = x.shape[0]
f_val = 0.0
f_val = np.sum(np.abs(x * np.sin(x) + 0.1*x))
# for i in range(n_features):
# f_val = f_val + torch.abs(x[:,i] * torch.sin(x[:,i]) + 0.1*x[:,i])
return f_val
def Ackley_numpy(x):
n_features = x.shape[0]
f_val = 0.0
a = 20
b = 0.2
c = 2*np.pi
cos_sum_term = (1/n_features) * np.sum( np.cos(c*x))
quad_sum_term = (1/n_features) * np.sum( x*x)
f_val = -a * np.exp( -b * np.sqrt( quad_sum_term) ) - np.exp(cos_sum_term) + a + np.exp(1)
return f_val