-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpso.py
313 lines (250 loc) · 10.1 KB
/
pso.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
import numpy as np
# from copy import deepcopy
#dont use that
def basic_pso(f, S, n, bounds, maxit, w, phi_p, phi_g, verbose):
# basic particle swarm optimization (PSO)
# f is the function to minimize, f:R^n -> R
# S is the number of particle in the swarm
# n is the dimension of the search space
# bounds is the lower and higher bounds on each dimension, shape = (n,2)
# maxit is the maximum number of iteration
# w, phi_p and phi_g are user specified paramters controlling the behavior and efficacity of PSO
# verbose print iteration progress if == 1
# initialise S particle in Uniform of bounds
space_width = bounds[:, 1] - bounds[:, 0]
swarm = np.tile(bounds[:, 0], (S, 1)) + np.random.rand(S, n) * np.tile(space_width, (S, 1))
# particles best known position
p = swarm.copy()
# evaluate particle's best position value
fp = np.zeros(S)
for part in range(S):
fp[part] = f(p[part, :].squeeze())
# evaluate particle's position value
fswarm = fp.copy()
# swarm's best known position
g = p[np.argmin(fp), :]
# swarm best known value
fg = np.min(fp)
# initialise particles velocity
v = np.tile(-space_width, (S, 1)) + np.random.rand(S, n) * np.tile(2 * space_width, (S, 1))
iter = 0
while iter < maxit:
# update velocity
v = w * v + phi_p * np.random.rand(S, n) * (p - swarm) + phi_g * np.random.rand(S, n) * (np.tile(g, (S, 1)) - swarm)
# update swarm
swarm += v
# update particle's position value and best position
for part in range(S):
fswarm[part] = f(swarm[part, :].squeeze())
if fswarm[part] < fp[part]:
fp[part] = fswarm[part]
p[part, :] = swarm[part, :]
# update swarm best known position
if np.min(fp) < fg:
fg = np.min(fp)
g = p[np.argmin(fp), :]
if verbose:
if (iter) % 1 == 0:
print('iter = {}, best value = {}'.format(iter, fg))
iter += 1
return g, fg
def B_N_pso(f, S, n, maxit, w, phi_p, phi_g, soft_reset, good_enough, verbose):
# basic particle swarm optimization (PSO)
# f is the function to minimize, f:R^n -> R
# S is the number of particle in the swarm
# n is the dimension of the search space
# bounds is the lower and higher bounds on each dimension, shape = (n,2)
# maxit is the maximum number of iteration
# w, phi_p and phi_g are user specified paramters controlling the behavior and efficacity of PSO
# verbose print iteration progress if == 1
# initialise S particle in Uniform random of [0,1]
swarm = np.random.rand(S, n)
# particles best known position
p = swarm.copy()
# evaluate particle's best position value
fp = np.zeros(S)
for part in range(S):
fp[part] = f(p[part, :].squeeze())
# evaluate particle's position value
fswarm = fp.copy()
# swarm's best known position
g = p[np.argmin(fp), :]
# swarm best known value
fg = np.min(fp)
# initialise particles velocity
v = (np.tile(-1, (S, 1)) + np.random.rand(S, n) * np.tile(2, (S, 1)))
all_best = np.zeros(maxit + 1)
all_best[0] = fg
iter = 0
# number of iteration before we activated soft_reset
flag = 75
while (iter < maxit) and (fg > good_enough):
# update velocity
v = w * v + phi_p * np.random.rand(S, n) * (p - swarm) + phi_g * np.random.rand(S, n) * (np.tile(g, (S, 1)) - swarm)
# update swarm
swarm += v
#bound the parameter space and kill out off bound velocity
v[swarm < 0] = 0
swarm[swarm < 0] = 0
v[swarm > 1] = 0
swarm[swarm > 1] = 1
# update particle's position value and best position
for part in range(S):
fswarm[part] = f(swarm[part, :].squeeze())
if fswarm[part] < fp[part]:
fp[part] = fswarm[part]
p[part, :] = swarm[part, :]
# update swarm best known position
if np.min(fp) < fg:
fg = np.min(fp)
g = p[np.argmin(fp), :]
all_best[iter + 1] = fg
# if soft_reset == 1:
# #random reset every 25 iter
# if ((iter - 24) % 25) == 0:
# swarm = np.random.rand(S, n)
# v = np.zeros((S, n))
# if ((iter - 49) % 50) == 0:
# p = swarm.copy()
# fp = np.inf * np.ones(S)
if soft_reset == 1:
if flag <= 0:
if (all_best[iter - 25] - all_best[iter]) < 0.025 * all_best[iter - 25]:
#randomly move particule
swarm = np.random.rand(S, n)
v = np.zeros((S, n))
p = swarm.copy()
fp = np.inf * np.ones(S)
#number of iteration after a soft_reset before we can reset again
flag = 25
if verbose:
print('iter = {}, best value = {}, RESET!'.format(iter, fg))
if verbose:
if (iter % 25) == 0:
print('iter = {}, best value = {}'.format(iter, fg))
iter += 1
flag -= 1
if verbose:
print('iter = {}, best value = {}'.format(iter, fg))
return g, fg
#this is crap
def B_N_H_pso(f, S, n, maxit, w, phi_p, phi_g, verbose):
# basic particle swarm optimization (PSO)
# f is the function to minimize, f:R^n -> R
# S is the number of particle in the swarm
# n is the dimension of the search space
# bounds is the lower and higher bounds on each dimension, shape = (n,2)
# maxit is the maximum number of iteration
# w, phi_p and phi_g are user specified paramters controlling the behavior and efficacity of PSO
# verbose print iteration progress if == 1
# np.random.seed(1)
init_boost = 10
# initialise 10*S particle in Uniform random of [0,1]
# keep S best
swarm = np.random.rand(init_boost * S, n)
# particles best known position
p = swarm.copy()
# evaluate particle's best position value
fp = np.zeros(init_boost * S)
for part in range(init_boost * S):
fp[part] = f(p[part, :].squeeze())
# prune init particules
idx = np.argsort(fp)
swarm = swarm[idx[:S], :]
p = swarm.copy()
fp = fp[idx[:S]]
# evaluate particle's position value
fswarm = fp.copy()
# swarm's best known position
# g = p[np.argmin(fp),:]
g = p[0, :]
# swarm best known value
# fg = np.min(fp)
fg = fp[0]
# initialise particles velocity
v = (np.tile(-1, (S, 1)) + np.random.rand(S, n) * np.tile(2, (S, 1)))
iter = 0
while iter < maxit:
# update velocity
v = w * v + phi_p * np.random.rand(S, n) * (p - swarm) + phi_g * np.random.rand(S, n) * (np.tile(g, (S, 1)) - swarm)
# update swarm
swarm += v
#bound the parameter space and kill out off bound velocity
v[swarm < 0] = 0
swarm[swarm < 0] = 0
v[swarm > 1] = 0
swarm[swarm > 1] = 1
# update particle's position value and best position
for part in range(S):
fswarm[part] = f(swarm[part, :].squeeze())
if fswarm[part] < fp[part]:
fp[part] = fswarm[part]
p[part, :] = swarm[part, :]
# update swarm best known position
if np.min(fp) < fg:
fg = np.min(fp)
g = p[np.argmin(fp), :]
if verbose:
if (iter % 5) == 0:
print('iter = {}, best value = {}'.format(iter, fg))
iter += 1
print('iter = {}, best value = {}'.format(iter, fg))
return g, fg
# this is crap**2
def neigh_pso(f, S, n, bounds, maxit, w, phi_p, phi_g, m, verbose):
# particle swarm optimization (PSO) using neighborhood
# f is the function to minimize, f:R^n -> R
# S is the number of particle in the swarm
# n is the dimension of the search space
# bounds is the lower and higher bounds on each dimension, shape = (n,2)
# maxit is the maximum number of iteration
# w, phi_p and phi_g are user specified paramters controlling the behavior and efficacity of PSO
# m is the number of neighboor to include in "swarm's best"
# verbose print iteration progress if == 1
# initialise S particle in Uniform of bounds
space_width = bounds[:, 1] - bounds[:, 0]
swarm = np.tile(bounds[:, 0], (S, 1)) + np.random.rand(S, n) * np.tile(space_width, (S, 1))
# particles best known position
p = deepcopy(swarm)
# evaluate particle's best position value
fp = np.zeros(S)
for part in range(S):
fp[part] = f(p[part, :].squeeze())
# evaluate particle's position value
fswarm = deepcopy(fp)
# swarm's best known position
g = p[np.argmin(fp), :]
# swarm best known value
fg = np.min(fp)
# initialise particles velocity
v = np.tile(-space_width, (S, 1)) + np.random.rand(S, n) * np.tile(2 * space_width, (S, 1))
iter = 0
while iter < maxit:
# update velocity
for part in range(S):
# find part's m closest neighbor
neigh_dist = ((swarm - np.tile(swarm[part, :], (S, 1))) ** 2).sum(1)
neigh_dist[part] = np.inf
neigh = np.argsort(neigh_dist)
neigh = neigh[:m]
# find best know position amongst neighbor
g_loc = p[neigh[np.argmin(fp[neigh])], :]
# update velocity with neighborhood
v[part, :] = w * v[part, :] + phi_p * np.random.rand(1, n) * (p[part, :] - swarm[part, :]) + phi_g * np.random.rand(1, n) * (g_loc - swarm[part, :])
# update swarm
swarm += v
# update particle's position value and best position
for part in range(S):
fswarm[part] = f(swarm[part, :].squeeze())
if fswarm[part] < fp[part]:
fp[part] = fswarm[part]
p[part, :] = swarm[part, :]
# update swarm best known position
if np.min(fp) < fg:
fg = np.min(fp)
g = p[np.argmin(fp), :]
if verbose:
if (iter) % 25 == 0:
print('iter = {}, best value = {}'.format(iter, fg))
iter += 1
return g, fg