-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction.py
More file actions
134 lines (110 loc) · 3.95 KB
/
Copy pathfunction.py
File metadata and controls
134 lines (110 loc) · 3.95 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
import numpy as np
class function:
def __init__(self):
print("function init")
def Parameters(F):
if F=='F1':
ParaValue=[-100,100,30]
elif F=='F2':
ParaValue = [-10, 10, 30]
elif F=='F3':
ParaValue = [-100, 100, 30]
elif F=='F4':
ParaValue = [-100, 100, 30]
elif F=='F5':
ParaValue = [-30, 30, 30]
elif F=='F6':
ParaValue=[-100,100,30]
return ParaValue
def fun(F,X):
if F=='F1':
O=np.sum(X*X)
elif F=='F2':
O=np.sum(np.abs(X))+np.prod(np.abs(X))
elif F=='F3':
O=0
for i in range(len(X)):
O=O+np.square(np.sum(X[0:i+1]))
elif F=='F4':
O=np.max(np.abs(X))
elif F=='F5':
X_len = len(X)
O = np.sum(100 * np.square(X[1:X_len] - np.square(X[0:X_len - 1]))) + np.sum(np.square(X[0:X_len - 1] - 1))
elif F=='F6':
O=np.sum(np.square(np.abs(X+0.5)))
return O
def Bounds(s,Lb,Ub):
temp=s
for i in range(len(s)):
if temp[i]<Lb[0,i]:
temp[i]=Lb[0,i]
elif temp[i]>Ub[0,i]:
temp[i]=Ub[0,i]
return temp
def SSA(pop,M,c,d,dim,f):
#global fit
P_percent=0.2
pNum=round(pop*P_percent)
lb=c*np.ones((1,dim))
ub=d*np.ones((1,dim))
X=np.zeros((pop,dim))
fit=np.zeros((pop,1))
for i in range(pop):
X[i,:]=lb+(ub-lb)*np.random.rand(1,dim)
fit[i,0]=fun(f,X[i,:])
pFit=fit
pX=X
fMin=np.min(fit[:,0])
bestI=np.argmin(fit[:,0])
bestX=X[bestI,:]
Convergence_curve=np.zeros((1,M))
for t in range(M):
sortIndex=np.argsort(pFit.T)
fmax=np.max(pFit[:,0])
B=np.argmax(pFit[:,0])
worse=X[B,:]
r2=np.random.rand(1)
if r2 < 0.8:
for i in range(pNum):
r1=np.random.rand(1)
X[sortIndex[0,i],:]=pX[sortIndex[0,i],:]*np.exp(-(i)/(r1*M))
X[sortIndex[0,i],:]=Bounds(X[sortIndex[0,i],:],lb,ub)
fit[sortIndex[0,i],0]=fun(f,X[sortIndex[0,i],:])
elif r2 >= 0.8:
for i in range(pNum):
X[sortIndex[0,i],:]=pX[sortIndex[0,i],:]+np.random.rand(1)*np.ones((1,dim))
X[sortIndex[0,i],:]=Bounds(X[sortIndex[0,i],:],lb,ub)
fit[sortIndex[0,i],0]=fun(f,X[sortIndex[0,i],:])
bestII=np.argmin(fit[:,0])
bestXX=X[bestII,:]
for ii in range(pop-pNum):
i=ii+pNum
A=np.floor(np.random.rand(1,dim)*2)*2-1
if i> pop/2:
X[sortIndex[0,i],:]=np.random.rand(1)*np.exp(worse-pX[sortIndex[0,i],:]/np.square(i))
else:
X[sortIndex[0,i],:]=bestXX+np.dot(np.abs(pX[sortIndex[0,i],:]-bestXX),1/(A.T*np.dot(A,A.T)))*np.ones((1,dim))
X[sortIndex[0,i],:]=Bounds(X[sortIndex[0,i],:],lb,ub)
fit[sortIndex[0,i],0]=fun(f,X[sortIndex[0,i],:])
arrc = np.arange(len(sortIndex[0,:]))
#c=np.random.shuffle(arrc)
c=np.random.permutation(arrc)
b=sortIndex[0,c[0:20]]
for j in range(len(b)):
if pFit[sortIndex[0,b[j]],0]>fMin:
X[sortIndex[0,b[j]],:]=bestX+np.random.rand(1,dim)*np.abs(pX[sortIndex[0,b[j]],:]-bestX)
else:
X[sortIndex[0,b[j]],:]=pX[sortIndex[0,b[j]],:]+(2*np.random.rand(1)-1)*np.abs(pX[sortIndex[0,b[j]],:]-worse)/(pFit[sortIndex[0,b[j]]]-fmax+10**(-50))
X[sortIndex[0,b[j]],:]=Bounds(X[sortIndex[0,b[j]],:],lb,ub)
fit[sortIndex[0,b[j]],0]=fun(f,X[sortIndex[0,b[j]]])
for i in range(pop):
if fit[i,0]<pFit[i,0]:
pFit[i,0]=fit[i,0]
pX[i,:]=X[i,:]
if pFit[i,0]<fMin:
fMin=pFit[i,0]
bestX=pX[i,:]
Convergence_curve[0,t]=fMin
#print(fMin)
#print(bestX)
return fMin,bestX,Convergence_curve