-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpso.py
513 lines (406 loc) · 20 KB
/
cpso.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import numpy as np
import pso
import chaosGen as cg
from collections import deque
class EMPSO (pso.PSO) :
"""
AdaSwarm with random number generators replaced with chaotic generators
Name - AdaSwarm: A Novel PSO optimization Method for the Mathematical Equivalence of Error Gradients
Author - Rohan et. al.
Link - https://arxiv.org/abs/2006.09875
"""
def get_chaotic_swarm (swarm_args) :
""" Returns a warm of particles with specified initialiser and underlying dynamics """
def ret_swarm (obj, llim, rlim, Np) :
D = len(llim)
# Returns the chaotic generator
get_gen = lambda dic : \
None if dic is None else \
lambda x : cg.cgen[dic['name']](*(((Np, D), ) + dic['args'])).chaosPoints(x)
initgen, randgen = get_gen(swarm_args['init_cmap']), get_gen(swarm_args['dyn_cmap'])
return EMPSO(obj, llim, rlim, Np, initgen, randgen)
return ret_swarm
def get_plain_swarm () :
""" Returns a plain swarm """
return lambda obj, llim, rlim, Np : EMPSO(obj, llim, rlim, Np)
def __setcache__ (self) :
"""
Sets all the caches to the empty list if cache parameter
in initialisation is true
"""
######################################################################
# Caches to hold optimization iterations for the last optimization
# performed. Contains the following -
# - position
# - velocity
# - momentum
# - pbest
# - gbest
# - r1
# - r2
######################################################################
if self.cache :
(self.pcache,
self.vcache,
self.mcache,
self.pbcache,
self.gbcache,
self.r1cache,
self.r2cache) = [], [], [], [], [], [], []
else :
(self.pcache,
self.vcache,
self.mcache,
self.pbcache,
self.gbcache,
self.r1cache,
self.r2cache) = None, None, None, None, None, None, None
def __init__ (self, obj, llim, rlim, Np, initgen=None, randgen=None, vrat=0.1, cache=False) :
"""
Constructor of the PSO Optimizer with limits and random
number generators
initer - Position and velocity initialiser
rander - r1, r2 generator
The rest are defined in the base class PSO()
"""
super().__init__(obj, llim, rlim, Np, vrat)
# Chaotic generators for the swarm initialiser
self.initgen = (lambda x : np.random.rand(self.Np, self.D)) \
if initgen is None else initgen
self.randgen = (lambda x : np.random.rand(self.Np, self.D)) \
if randgen is None else randgen
# PSO progress cache for replaying history (used in calculating LE of trajectory)
self.cache = cache
self.__setcache__()
def __str__ (self) :
""" Optimizer descriptor """
return "EMPSO"
def _optim_init (self) :
""" Initialiser of certain state variables before the optimization loop """
pbest, gbest = super()._optim_init()
momentum = np.zeros_like(self.particles)
self.__setcache__()
self.appendCache(self.particles, self.velocity, momentum, pbest, gbest)
return momentum, pbest, gbest
def optimize (self, c1=1, c2=1, alpha=1.2, beta=0.9,
max_iters=10000, tol=1e-2,
print_iters=False) :
"""
Performs the PSO optimization loop
Arguments are default PSO parameters
Returns the optimum found, and lambda function for approximate gradient
"""
momentum, pbest, gbest = self._optim_init()
i = 0
while True :
# Using the first and second internal generators, randgen(1) and radgen(2) respectively
r1, r2 = self.randgen(1), self.randgen(2)
# Momentum and velocity update
momentum = beta*momentum + (1-beta)*self.velocity
self.velocity = momentum + c1*r1*(pbest - self.particles) + c2*r2*(gbest - self.particles)
# Perform velocity clipping before running ipcd() to minimize any violations
self.velocity = pso.vclip(self.velocity, self.vmax)
######################################################################
# Perform "Inverse Parabolic Confined Distribution" technique for
# boundary handling. Also returns updated particle position and velocity
######################################################################
self.particles, self.velocity = pso.ipcd(self.particles, self.velocity, self.llim, self.rlim, alpha)
# Update pbest, gbest
less = self.obj(self.particles) < self.obj(pbest)
pbest[less] = np.copy(self.particles[less])
gbest = min(pbest , key = self.objkey)
self.conv_curve.append(self.objkey(gbest))
# Append to cache after updating particles, velocities, pbest and gbest
self.appendCache (self.particles, self.velocity, momentum, pbest, gbest, r1, r2)
i += 1
if print_iters : print("\r{}".format(i), end="")
# Stopping criteria
if i == max_iters or (np.abs(self.particles - gbest) < tol).all() :
break
# Convert cache list to numpy ndarray
self.numpifyCache ()
grad = lambda x : -(c1*np.sum(r1) + c2*np.sum(r2))*(x - gbest)/(len(r1)*(1-beta))
if print_iters : print("\n", end="")
return self.optRet(gbest, grad, tol, i)
def appendCache (self, p, v, m, pb, gb, r1=None, r2=None) :
""" Called every iteration of optimize() """
if self.cache :
self.pcache.append(np.copy(p))
self.vcache.append(np.copy(v))
self.mcache.append(np.copy(m))
self.pbcache.append(np.copy(pb))
self.gbcache.append(np.copy(gb))
if r1 is not None : self.r1cache.append(np.copy(r1))
if r2 is not None : self.r2cache.append(np.copy(r2))
def numpifyCache (self) :
""" Sets cache list in numpy format. Called before exit of optimize() """
if self.cache :
self.pcache = np.array(self.pcache)
self.vcache = np.array(self.vcache)
self.mcache = np.array(self.mcache)
self.pbcache = np.array(self.pbcache)
self.gbcache = np.array(self.gbcache)
self.r1cache = np.array(self.r1cache)
self.r2cache = np.array(self.r2cache)
def replay (self, seed, c1=0.7, c2=0.7, alpha=1.2, beta=0.9) :
"""
Given a pre-determined sequence of r1, r2 and a starting
position, velocity and momentum, replays the PSO trajectory
Typically, this is meant to be used after perturbing the starting
position slightly.
"""
part, vel, mom, pb, gb, r1s, r2s = seed
seedcopy = ()
for s in seed :
seedcopy += (np.copy(s), )
(part,
vel,
mom,
pb,
gb,
r1s,
r2s) = seedcopy
(pcache,
vcache,
mcache,
pbcache,
gbcache) = [part], [vel], [mom], [pb], [gb]
for r1, r2 in zip(r1s, r2s) :
mom = beta*mom + (1-beta)*vel
vel = mom + c1*r1*(pb - part) + c2*r2*(gb - part)
vel = pso.vclip(vel, self.vmax)
part, vel = pso.ipcd(part, vel, self.llim, self.rlim, alpha)
less = self.obj(part) < self.obj(pb)
pb[less] = part[less]
gb = min(pb , key = lambda x : self.obj(x.reshape(1, -1))[0])
pcache.append(part)
vcache.append(vel)
mcache.append(mom)
pbcache.append(pb)
gbcache.append(gb)
return np.array(pcache), np.array(vcache), np.array(mcache), np.array(pbcache), np.array(gbcache)
class HECS_PSO (pso.PSO) :
"""
Name - A Hybrid Particle Swarm Algorithm with Embedded Chaotic Search
Author - Meng et. al.
Link - https://ieeexplore.ieee.org/document/1460442
"""
def __init__ (self, obj, llim, rlim, Np, stag_tol=1e-3, Nc=6, Gmax=500, rrat=0.2, vrat=0.1) :
"""
Constructor for the hybrid embedded chaotic search PSO optimizer -
stag_tol - Stagnation tolerance for kicking in chaotic search
Nc - Number of iterations to check for stagnation
Gmax - Maximum iterations in the chaotic search
rrat - Carrier wave radius in chaotic search
Rest are defined in the base class
"""
super().__init__(obj, llim, rlim, Np, vrat)
self.stag_tol = stag_tol
self.Nc = Nc
self.Gmax = Gmax
self.rrat = rrat
self.cgen = None
def __str__ (self) :
""" Optimizer descriptor """
return "Hybrid Embedded Chaotic Search PSO"
def _optim_init (self) :
""" Initialiser of certain state variables before the optimization loop """
pbest, gbest = super()._optim_init()
fitness_q = deque(maxlen=self.Nc)
fitness_q.append(self.obj(self.particles))
return fitness_q, pbest, gbest
def optimize (self, w=0.7, c1=1.7, c2=1.7, alpha=1.2,
max_iters=10000, tol=1e-2,
print_iters=False) :
""" Runs the PSO loop """
fitness_q, pbest, gbest = self._optim_init()
# Set the chaotic generator if not previously set
if self.cgen is None :
self.cgen = cg.Logistic((self.Gmax, self.D), gens=1)
i = -1
while True :
i += 1
if print_iters : print("\r{}".format(i), end="")
# Stopping criteria
if i == max_iters or (np.abs(self.particles - gbest) < tol).all() :
break
# Chaotic search
if i >= self.Nc :
fits_ps = np.array(fitness_q).transpose()
for j, fits_p in enumerate(fits_ps) :
if ((fits_p - self.obj(gbest.reshape(1, -1)))/fits_p < self.stag_tol).all() :
chaos_points = self.particles[j] + self.rrat*(self.rlim - self.llim)*(2*self.cgen.chaosPoints(1) - 1)
obj_cp = np.where(np.logical_and(self.llim.reshape(1, -1) <= chaos_points,
chaos_points <= self.rlim.reshape(1, -1)).all(axis=1),
self.obj(chaos_points),
np.inf)
gbest_p = np.argmin(obj_cp).flatten()[0]
# Update after chaotic search if feasible
if obj_cp[gbest_p] != np.inf and obj_cp[gbest_p] < self.objkey(self.particles[j]) :
self.velocity[j] = self.particles[j] - chaos_points[gbest_p]
self.particles[j] = chaos_points[gbest_p]
# Perform velocity clipping before running ipcd() to minimize any violations
self.velocity = pso.vclip(self.velocity, self.vmax)
######################################################################
# Perform "Inverse Parabolic Confined Distribution" technique for
# boundary handling. Also returns updated particle position and velocity
######################################################################
self.particles, self.velocity = pso.ipcd(self.particles, self.velocity, self.llim, self.rlim, alpha)
less = self.obj(self.particles) < self.obj(pbest)
pbest[less] = np.copy(self.particles[less])
gbest = min(pbest, key = self.objkey)
self.conv_curve.append(self.objkey(gbest))
# Appends fitness for tracking whether to enter chaotic search
fitness_q.append(self.obj(self.particles))
# Velocity update
r1, r2 = np.random.rand(self.Np, self.D), np.random.rand(self.Np, self.D)
self.velocity = w*self.velocity + c1*r1*(pbest - self.particles) + c2*r2*(gbest - self.particles)
grad = lambda x : -(c1*np.sum(r1) + c2*np.sum(r2))*(x - gbest)/(len(r1)*w)
if print_iters : print("\n", end="")
return self.optRet(gbest, grad, tol, i)
class PWLC_PSO (pso.PSO) :
"""
Name - An improved particle swarm optimization algorithm combined with piecewise linear chaotic map
Author - Xiang et. al.
Link - https://www.sciencedirect.com/science/article/abs/pii/S0096300307002081
"""
def __init__ (self, obj, llim, rlim, Np, mu=0.7, rrat=0.8, rho=0.9, vrat=0.1) :
"""
Constructor for the hybrid embedded chaotic search PSO optimizer -
mu - Parameter for the piecewise linear chaotic map
rrat - Ratio in terms of dimension size for the chaotic search radius
rho - Reduction factor for the search radius
Rest are defined in the base class
"""
super().__init__(obj, llim, rlim, Np, vrat)
self.mu = mu
self.rrat = rrat
self.rho = rho
self.cgen = None
def __str__ (self) :
""" Optimizer descriptor """
return "Piece-wise Linear Chaotic PSO"
def _optim_init (self) :
""" Initialiser of certain state variables before the optimization loop """
pbest, gbest = super()._optim_init()
return pbest, gbest
def optimize (self, w=0.7, c1=1.7, c2=1.7, alpha=1.2,
chaos_iters=500, max_pso_iters=10000, tol=1e-2,
print_iters=False) :
""" Optimization loop of plain PSO """
pbest, gbest = self._optim_init()
# Set the chaotic generator if not previously set
if self.cgen is None :
self.cgen = cg.Tent((chaos_iters, self.D), mu=self.mu, gens=1)
i = 0
while True :
# Velocity update equation
r1, r2 = np.random.rand(self.Np, self.D), np.random.rand(self.Np, self.D)
self.velocity = w*self.velocity + c1*r1*(pbest - self.particles) + c2*r2*(gbest - self.particles)
# Perform velocity clipping before running ipcd() to minimize any violations
self.velocity = pso.vclip(self.velocity, self.vmax)
######################################################################
# Perform "Inverse Parabolic Confined Distribution" technique for
# boundary handling. Also returns updated particle position and velocity
######################################################################
self.particles, self.velocity = pso.ipcd(self.particles, self.velocity, self.llim, self.rlim, alpha)
# Update pbest, gbest
less = self.obj(self.particles) < self.obj(pbest)
pbest[less] = np.copy(self.particles[less])
gbest_ind = np.argmin(self.obj(pbest)).flatten()[0]
# Chaotic search
cp = pbest[gbest_ind] + self.rrat*(self.rlim - self.llim)*(2*self.cgen.chaosPoints(1) - 1)
obj_cp = np.where(np.logical_and(self.llim.reshape(1,-1) <= cp, cp <= self.rlim.reshape(1,-1)).all(axis=1),
self.obj(cp),
np.inf)
gbest_p = np.argmin(obj_cp).flatten()[0]
# Update after chaotic search if feasible
if obj_cp[gbest_p] != np.inf and obj_cp[gbest_p] < self.objkey(pbest[gbest_ind]) :
new_vel = cp[gbest_p] - self.particles[gbest_ind]
self.velocity[gbest_ind] = np.random.rand(self.D)*self.vmax*new_vel/np.linalg.norm(new_vel)
pbest[gbest_ind] = self.particles[gbest_ind] = cp[gbest_p]
# Copy gbest
gbest = pbest[gbest_ind]
self.conv_curve.append(self.objkey(gbest))
self.rrat *= self.rho
i += 1
if print_iters : print("\r{}".format(i), end="")
# Stopping criteria
if i == max_pso_iters or (np.abs(self.particles - gbest) < tol).all() :
break
grad = lambda x : -(c1*np.sum(r1) + c2*np.sum(r2))*(x - gbest)/(len(r1)*w)
if print_iters : print("\n", end="")
return self.optRet(gbest, grad, tol, i)
class PWLC_EMPSO (pso.PSO) :
"""
PWLCPSO with momentum
"""
def __init__ (self, obj, llim, rlim, Np, mu=0.7, rrat=0.8, rho=0.9, vrat=0.1) :
"""
Constructor for the hybrid embedded chaotic search PSO optimizer -
mu - Parameter for the piecewise linear chaotic map
rrat - Ratio in terms of dimension size for the chaotic search radius
rho - Reduction factor for the search radius
Rest are defined in the base class
"""
super().__init__(obj, llim, rlim, Np, vrat)
self.mu = mu
self.rrat = rrat
self.rho = rho
self.cgen = None
def __str__ (self) :
""" Optimizer descriptor """
return "Piece-wise Linear Chaotic PSO"
def _optim_init (self) :
""" Initialiser of certain state variables before the optimization loop """
pbest, gbest = super()._optim_init()
return np.zeros_like(pbest), pbest, gbest
def optimize (self, beta=0.9, c1=0.7, c2=0.7, alpha=1.2,
chaos_iters=500, max_pso_iters=10000, tol=1e-2,
print_iters=False) :
""" Optimization loop of plain PSO """
momentum, pbest, gbest = self._optim_init()
# Set the chaotic generator if not previously set
if self.cgen is None :
self.cgen = cg.Tent((chaos_iters, self.D), mu=self.mu, gens=1)
i = 0
while True :
# Momentum update
r1, r2 = np.random.rand(self.Np, self.D), np.random.rand(self.Np, self.D)
momentum = beta*momentum + (1-beta)*self.velocity
self.velocity = momentum + c1*r1*(pbest - self.particles) + c2*r2*(gbest - self.particles)
# Perform velocity clipping before running ipcd() to minimize any violations
self.velocity = pso.vclip(self.velocity, self.vmax)
######################################################################
# Perform "Inverse Parabolic Confined Distribution" technique for
# boundary handling. Also returns updated particle position and velocity
######################################################################
self.particles, self.velocity = pso.ipcd(self.particles, self.velocity, self.llim, self.rlim, alpha)
# Update pbest, gbest
less = self.obj(self.particles) < self.obj(pbest)
pbest[less] = np.copy(self.particles[less])
gbest_ind = np.argmin(self.obj(pbest)).flatten()[0]
# Chaotic search
cp = pbest[gbest_ind] + self.rrat*(self.rlim - self.llim)*(2*self.cgen.chaosPoints(1) - 1)
obj_cp = np.where(np.logical_and(self.llim.reshape(1,-1) <= cp, cp <= self.rlim.reshape(1,-1)).all(axis=1),
self.obj(cp),
np.inf)
gbest_p = np.argmin(obj_cp).flatten()[0]
# Update after chaotic search if feasible
if obj_cp[gbest_p] != np.inf and obj_cp[gbest_p] < self.objkey(pbest[gbest_ind]) :
new_vel = cp[gbest_p] - self.particles[gbest_ind]
self.velocity[gbest_ind] = np.random.rand(self.D)*self.vmax*new_vel/np.linalg.norm(new_vel)
momentum[gbest_ind] = 0
pbest[gbest_ind] = self.particles[gbest_ind] = cp[gbest_p]
# Copy gbest
gbest = pbest[gbest_ind]
self.conv_curve.append(self.objkey(gbest))
self.rrat *= self.rho
i += 1
if print_iters : print("\r{}".format(i), end="")
# Stopping criteria
if i == max_pso_iters or (np.abs(self.particles - gbest) < tol).all() :
break
grad = lambda x : -(c1*np.sum(r1) + c2*np.sum(r2))*(x - gbest)/(len(r1)*(1-beta))
if print_iters : print("\n", end="")
return self.optRet(gbest, grad, tol, i)