-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
157 lines (135 loc) · 4.75 KB
/
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
import numpy as np
f1_alpha=1000 #set alpha of f1
f3_epsilon=1e-6 #set epsilon of f3
f45_q=10**8
def f1(x): #ellipsoid function
dim=x.shape[0] #dimension number
result=0
for i in range(dim):
result+=f1_alpha**(i/(dim-1))*x[i]**2
return result
def g1(x):
dim=x.shape[0]
result=np.zeros(dim)
for i in range(dim):
result[i]=2*(f1_alpha**(i/(dim-1)))*x[i]
return result
def h1(x):
dim=x.shape[0]
result=np.zeros((dim,dim))
for i in range(dim):
result[i,i]=2*(f1_alpha**(i/(dim-1)))
return result
f2 = lambda x: (1-x[0])**2+100*(x[1]-x[0]**2)**2; #Rosenbrok function
g2 = lambda x: np.array([-400*x[0]*(x[1]-x[0]**2)-2*(1-x[0]), 200*(x[1]-x[0]**2)])
h2 = lambda x: np.array([[2+1200*x[0]**2-400*x[1], -400*x[0]], [-400*x[0], 200]])
f3 = lambda x: np.log(f3_epsilon+f1(x)) #log ellipsoid function
def g3(x):
dim=x.shape[0]
result=np.zeros(dim)
for i in range(dim):
result[i]=(2*f1_alpha**(i/(dim-1))*x[i])/(f3_epsilon+f1(x))
return result
def h3(x):
dim=x.shape[0]
result=np.zeros((dim,dim))
f1_elli=f1(x)
for i in range(dim):
for j in range(dim):
if(i==j):
result[i,j]=((2*f1_alpha**(i/(dim-1)))/(f3_epsilon+f1_elli)
- (2*x[i]**2)/(f3_epsilon+f1_elli)**2)
else:
result[i,j]=((-4*f1_alpha**((i+j)/(dim-1))*x[i]*x[j])
/ (f3_epsilon+f1_elli)**2)
return result
funch = lambda x: (np.log(1+np.exp(-np.absolute(f45_q*x)))+np.maximum(f45_q*x,0))/f45_q
def f4(x):
if(isinstance(x, int) or isinstance(x, float)):
return (funch(x) + 100*funch(-x))
else:
dim=x.shape[0] #dimension number
result=0
for i in range(dim):
result+=funch(x[i])+100*funch(-x[i])
return result
'''
def f4_g(x):
dim=x.shape[0] #dimension number
result=np.zeros(dim)
for i in range(dim):
result[i]=(np.exp(f45_q*x[i]))/(1+np.exp(f45_q*x[i]))-100*(np.exp(-f45_q*x[i])/(1+np.exp(-f45_q*x[i])))
return result
def f4_h(x):
dim=x.shape[0] #dimension number
result=np.zeros((dim,dim))
for i in range(dim):
for j in range(dim):
if(i==j):
result[i,j]=(101*f45_q*np.exp(-f45_q*x[i]))/(1+np.exp(-f45_q*x[i]))**2
else:
result[i,j]=0
return result
'''
def h_d1(x):
if x >= 0:
return 1 / (1 + np.exp(-x * f45_q))
return np.exp(x * f45_q) / (1 + np.exp(x * f45_q))
def h_d11(x):
if x >= 0:
return -(np.exp(-f45_q * x) / (1 + np.exp(-f45_q * x)))
return -(1 / (1 + np.exp(f45_q * x)))
def h_d2(x):
if x >= 0:
return (f45_q * np.exp(-x * f45_q)) / (1 + np.exp(-x * f45_q))**2
return (f45_q * np.exp(x * f45_q)) / (1 + np.exp(x * f45_q)) ** 2
def g4(x):
if isinstance(x, int) or isinstance(x, float):
return h_d1(x) - 100 * h_d1(-x)
else:
d = len(x)
grad = np.zeros(d)
for i in range(d):
grad[i] = h_d1(x[i]) - 100 * h_d1(-x[i])
return grad
def h4(x):
if isinstance(x, int) or isinstance(x, float):
return h_d2(x) + 100 * h_d2(-x)
else:
d = len(x)
hessian = np.zeros((d, d))
for i in range(d):
hessian[i, i] = h_d2(x[i]) + 100 * h_d2(-x[i])
return hessian
def f5(x):
if(isinstance(x, int) or isinstance(x, float)):
return funch(x)**2 + 100*funch(-x)**2
else:
dim=x.shape[0] #dimension number
result=0;
for i in range(dim):
result+=funch(x[i])**2+100*funch(-x[i])**2
return result
def g5(x):
if isinstance(x, int) or isinstance(x, float):
return 2 * h(x) * h_d1(x) - 100 * 2 * h(-x) * h_d1(-x)
else:
d = len(x)
grad = np.zeros(d)
for i in range(d):
grad[i] = 2 * funch(x[i]) * h_d1(x[i]) - 100 * 2 * funch(-x[i]) * h_d1(-x[i])
return grad
def h5(x):
if isinstance(x, int) or isinstance(x, float):
return 2 * np.exp(f45_q * x) * (np.exp(f45_q * x) + np.log(np.exp(f45_q * x) + 1)) / (
np.exp(f45_q * x) + 1) ** 2 + 200 * np.exp(-2 * f45_q * x) * (
np.exp(f45_q * x) * np.log(np.exp(-f45_q * x) + 1) + 1) / (np.exp(-f45_q * x) + 1) ** 2
else:
d = len(x)
hessian = np.zeros((d, d))
for i in range(d):
hessian[i, i] = 2 * h_d1(x[i])**2 + 2*funch(x[i])*h_d2(x[i]) + 200*h_d11(x[i])**2
# 2 * np.exp(f45_q * x[i]) * (np.exp(f45_q * x[i]) + np.log(np.exp(f45_q * x[i]) + 1)) / (
# np.exp(f45_q * x[i]) + 1) ** 2 + 200 * np.exp(-2 * f45_q * x[i]) * (
# np.exp(f45_q * x[i]) * np.log(np.exp(-f45_q * x[i]) + 1) + 1) / (np.exp(-f45_q * x[i]) + 1) ** 2
return hessian