-
Notifications
You must be signed in to change notification settings - Fork 1
/
LevyRO.py
197 lines (155 loc) · 6.3 KB
/
LevyRO.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
#
# file: LevyRO.py
#
# Parallel random optimization using Levy flights
#
# RTK, 07-Dec-2019
# Last update: 16-Dec-2020
#
################################################################
import numpy as np
from math import gamma
################################################################
# LevyRO
#
class LevyRO:
"""Parallel random optimization"""
#-----------------------------------------------------------
# __init__
#
def __init__(self, obj, # the objective function (subclass Objective)
npart=10, # number of particles in the swarm
ndim=3, # number of dimensions in the swarm
max_iter=200, # maximum number of steps
eta=0.1, # max fractional change for candidate positions
levy=1.5, # Levy flight parameter (lambda)
tol=None, # tolerance (done if no done object and gbest < tol)
init=None, # swarm initialization object (subclass Initializer)
done=None, # custom Done object (subclass Done)
bounds=None): # swarm bounds object
self.obj = obj
self.npart = npart
self.ndim = ndim
self.max_iter = max_iter
self.init = init
self.done = done
self.bounds = bounds
self.tol = tol
self.eta = eta
self.levy = levy
self.initialized = False
#-----------------------------------------------------------
# Results
#
def Results(self):
"""Return the current results"""
if (not self.initialized):
return None
return {
"npart": self.npart, # number of particles
"ndim": self.ndim, # number of dimensions
"max_iter": self.max_iter, # maximum possible iterations
"iterations": self.iterations, # iterations actually performed
"tol": self.tol, # tolerance value, if any
"eta": self.eta, # max candidate fraction
"gbest": self.gbest, # sequence of global best function values
"giter": self.giter, # iterations when global best updates happened
"gpos": self.gpos, # global best positions
"gidx": self.gidx, # particle number for new global best
"pos": self.pos, # current particle positions
"vpos": self.vpos, # and objective function values
}
#-----------------------------------------------------------
# Initialize
#
def Initialize(self):
"""Set up the swarm"""
self.initialized = True
self.iterations = 0
self.pos = self.init.InitializeSwarm() # initial swarm positions
self.vpos= self.Evaluate(self.pos) # and objective function values
# Swarm bests
self.gidx = []
self.gbest = []
self.gpos = []
self.giter = []
self.gidx.append(np.argmin(self.vpos))
self.gbest.append(self.vpos[self.gidx[-1]])
self.gpos.append(self.pos[self.gidx[-1]])
self.giter.append(0)
#-----------------------------------------------------------
# Done
#
def Done(self):
"""Check if we are done"""
if (self.done == None):
if (self.tol == None):
return (self.iterations == self.max_iter)
else:
return (self.gbest[-1] < self.tol) or (self.iterations == self.max_iter)
else:
return self.done.Done(self.gbest,
gpos=self.gpos,
pos=self.pos,
max_iter=self.max_iter,
iteration=self.iterations)
#-----------------------------------------------------------
# Evaluate
#
def Evaluate(self, pos):
"""Evaluate a set of positions"""
p = np.zeros(self.npart)
for i in range(self.npart):
p[i] = self.obj.Evaluate(pos[i])
return p
#-----------------------------------------------------------
# LevyFlight
#
def LevyFlight(self):
"""Levy flight distribution"""
s1 = np.power((gamma(1+self.levy) * np.sin((np.pi*self.levy)/2)) \
/ gamma((1+self.levy)/2) * np.power(2, (self.levy - 1) / 2), 1 / self.levy)
s2 = 1
u = np.random.normal(0, s1, size=self.ndim)
v = np.random.normal(0, s2, size=self.ndim)
step = u / np.power(np.fabs(v), 1/self.levy)
return step
#-----------------------------------------------------------
# CandidatePositions
#
def CandidatePositions(self):
"""Return a set of candidate positions using a Levy flight"""
pos = np.zeros((self.npart, self.ndim))
for i in range(self.npart):
pos[i] = self.pos[i] + self.eta * self.LevyFlight()
if (self.bounds != None):
pos = self.bounds.Limits(pos)
return pos
#-----------------------------------------------------------
# Step
#
def Step(self):
"""Do one swarm step"""
new_pos = self.CandidatePositions() # get new candidate positions
p = self.Evaluate(new_pos) # and evaluate them
# For each particle
for i in range(self.npart):
if (p[i] < self.vpos[i]): # is new position better?
self.vpos[i] = p[i] # keep the function value
self.pos[i] = new_pos[i] # and new position
if (p[i] < self.gbest[-1]): # is new position global best?
self.gbest.append(p[i]) # new position is new swarm best
self.gpos.append(new_pos[i]) # keep the position
self.gidx.append(i) # particle number
self.giter.append(self.iterations) # and when it happened
self.iterations += 1
#-----------------------------------------------------------
# Optimize
#
def Optimize(self):
"""Run a full optimization and return the best"""
self.Initialize()
while (not self.Done()):
self.Step()
return self.gbest[-1], self.gpos[-1]
# end RO.py