-
Notifications
You must be signed in to change notification settings - Fork 21
/
cso.py
268 lines (180 loc) · 9.27 KB
/
cso.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
################################################################################
# #
# UJJWAL KHANDELWAL #
# CSO (CUCKOO SEARCH OPTIMIZATION) #
# PYTHON 3.7.10 #
# #
################################################################################
####################### IMPORT DEPENDENCIES ################################
import numpy as np
import matplotlib.pyplot as plt
from math import gamma
########################### CSO CLASS ########################################
class CSO:
def __init__(self, fitness, P=150, n=2, pa=0.25, beta=1.5, bound=None,
plot=False, min=True, verbose=False, Tmax=300):
'''
PARAMETERS:
fitness: A FUNCTION WHICH EVALUATES COST (OR THE FITNESS) VALUE
P: POPULATION SIZE
n: TOTAL DIMENSIONS
pa: ASSIGNED PROBABILITY
beta: LEVY PARAMETER
bound: AXIS BOUND FOR EACH DIMENSION
X: PARTICLE POSITION OF SHAPE (P,n)
################ EXAMPLE #####################
If ith egg Xi = [x,y,z], n = 3, and if
bound = [(-5,5),(-1,1),(0,5)]
Then, x∈(-5,5); y∈(-1,1); z∈(0,5)
##############################################
Tmax: MAXIMUM ITERATION
best: GLOBAL BEST POSITION OF SHAPE (n,1)
'''
self.fitness = fitness
self.P = P
self.n = n
self.Tmax = Tmax
self.pa = pa
self.beta = beta
self.bound = bound
self.plot = plot
self.min = min
self.verbose = verbose
# X = (U-L)*rand + L (U AND L ARE UPPER AND LOWER BOUND OF X)
# U AND L VARY BASED ON THE DIFFERENT DIMENSION OF X
self.X = []
if bound is not None:
for (U, L) in bound:
x = (U-L)*np.random.rand(P,) + L
self.X.append(x)
self.X = np.array(self.X).T
else:
self.X = np.random.randn(P,n)
def update_position_1(self):
'''
ACTION:
TO CALCULATE THE CHANGE OF POSITION 'X = X + rand*C' USING LEVY FLIGHT METHOD
C = 0.01*S*(X-best) WHERE S IS THE RANDOM STEP, and β = beta (TAKEN FROM [1])
u
S = -----
1/β
|v|
beta = 1.5
u ~ N(0,σu) # NORMAL DISTRIBUTION WITH ZERO MEAN AND 'σu' STANDARD DEVIATION
v ~ N(0,σv) # NORMAL DISTRIBUTION WITH ZERO MEAN AND 'σv' STANDARD DEVIATION
σv = 1
Γ(1+β)*sin(πβ/2)
σu^β = --------------------------
Γ((1+β)/2)*β*(2^((β-1)/2))
Γ IS THE GAMMA FUNCTION
'''
num = gamma(1+self.beta)*np.sin(np.pi*self.beta/2)
den = gamma((1+self.beta)/2)*self.beta*(2**((self.beta-1)/2))
σu = (num/den)**(1/self.beta)
σv = 1
u = np.random.normal(0, σu, self.n)
v = np.random.normal(0, σv, self.n)
S = u/(np.abs(v)**(1/self.beta))
# DEFINING GLOBAL BEST SOLUTION BASED ON FITNESS VALUE
for i in range(self.P):
if i==0:
self.best = self.X[i,:].copy()
else:
self.best = self.optimum(self.best, self.X[i,:])
Xnew = self.X.copy()
for i in range(self.P):
Xnew[i,:] += np.random.randn(self.n)*0.01*S*(Xnew[i,:]-self.best)
self.X[i,:] = self.optimum(Xnew[i,:], self.X[i,:])
def update_position_2(self):
'''
ACTION:
TO REPLACE SOME NEST WITH NEW SOLUTIONS
HOST BIRD CAN THROW EGG AWAY (ABANDON THE NEST) WITH FRACTION
pa ∈ [0,1] (ALSO CALLED ASSIGNED PROBABILITY) AND BUILD A COMPLETELY
NEW NEST. FIRST WE CHOOSE A RANDOM NUMBER r ∈ [0,1] AND IF r < pa,
THEN 'X' IS SELECTED AND MODIFIED ELSE IT IS KEPT AS IT IS.
'''
Xnew = self.X.copy()
Xold = self.X.copy()
for i in range(self.P):
d1,d2 = np.random.randint(0,5,2)
for j in range(self.n):
r = np.random.rand()
if r < self.pa:
Xnew[i,j] += np.random.rand()*(Xold[d1,j]-Xold[d2,j])
self.X[i,:] = self.optimum(Xnew[i,:], self.X[i,:])
def optimum(self, best, particle_x):
'''
PARAMETERS:
best: GLOBAL BEST SOLUTION 'best'
particle_x: PARTICLE POSITION
ACTION:
COMPARE PARTICLE'S CURRENT POSITION WITH GLOBAL BEST POSITION
1. IF PROBLEM IS MINIMIZATION (min=TRUE), THEN CHECKS WHETHER FITNESS VALUE OF 'best'
IS LESS THAN THE FITNESS VALUE OF 'particle_x' AND IF IT IS GREATER, THEN IT
SUBSTITUTES THE CURRENT PARTICLE POSITION AS THE BEST (GLOBAL) SOLUTION
2. IF PROBLEM IS MAXIMIZATION (min=FALSE), THEN CHECKS WHETHER FITNESS VALUE OF 'best'
IS GREATER THAN THE FITNESS VALUE OF 'particle_x' AND IF IT IS LESS, THEN IT
SUBSTITUTES THE CURRENT PARTICLE POSITION AS THE BEST (GLOBAL) SOLUTION
'''
if self.min:
if self.fitness(best) > self.fitness(particle_x):
best = particle_x.copy()
else:
if self.fitness(best) < self.fitness(particle_x):
best = particle_x.copy()
return best
def clip_X(self):
# IF BOUND IS SPECIFIED THEN CLIP 'X' VALUES SO THAT THEY ARE IN THE SPECIFIED RANGE
if self.bound is not None:
for i in range(self.n):
xmin, xmax = self.bound[i]
self.X[:,i] = np.clip(self.X[:,i], xmin, xmax)
def execute(self):
'''
PARAMETERS:
t: ITERATION NUMBER
fitness_time: LIST STORING FITNESS (OR COST) VALUE FOR EACH ITERATION
time: LIST STORING ITERATION NUMBER ([0,1,2,...])
ACTION:
AS THE NAME SUGGESTS, THIS FUNCTION EXECUTES CUCKOO SEARCH ALGORITHM
BASED ON THE TYPE OF PROBLEM (MAXIMIZATION OR MINIMIZATION).
NOTE: THIS FUNCTION PRINTS THE GLOBAL FITNESS VALUE FOR EACH ITERATION
IF THE VERBOSE IS TRUE
'''
self.fitness_time, self.time = [], []
for t in range(self.Tmax):
self.update_position_1()
self.clip_X()
self.update_position_2()
self.clip_X()
self.fitness_time.append(self.fitness(self.best))
self.time.append(t)
if self.verbose:
print('Iteration: ',t,'| best global fitness (cost):',round(self.fitness(self.best),7))
print('\nOPTIMUM SOLUTION\n >', np.round(self.best.reshape(-1),7).tolist())
print('\nOPTIMUM FITNESS\n >', np.round(self.fitness(self.best),7))
print()
if self.plot:
self.Fplot()
def Fplot(self):
# PLOTS GLOBAL FITNESS (OR COST) VALUE VS ITERATION GRAPH
plt.plot(self.time, self.fitness_time)
plt.title('Fitness value vs Iteration')
plt.xlabel('Iteration')
plt.ylabel('Fitness value')
plt.show()
################################################# END OF CSO CLASS ######################################################################
#########################################################################################################################################
# #
# REFERENCES: #
# #
# [1] X. YANG AND SUASH DEB, "CUCKOO SEARCH VIA LÉVY FLIGHTS," #
# 2009 WORLD CONGRESS ON NATURE & BIOLOGICALLY INSPIRED COMPUTING (NABIC), #
# 2009, PP. 210-214, DOI: 10.1109/NABIC.2009.5393690. #
# #
# [2] RAJIB KUMAR BHATTACHARJYA, INTRODUCTION TO PARTICLE SWARM OPTIMIZATION #
# (http://www.iitg.ac.in/rkbc/CE602/CE602/Particle%20Swarm%20Algorithms.pdf) #
# #
#########################################################################################################################################
##################################################### THATS ALL FOLKS! ##############################################################