-
Notifications
You must be signed in to change notification settings - Fork 3
/
pso.lua
630 lines (524 loc) · 16.2 KB
/
pso.lua
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
--- PSO - A Lua module for particle swarm optmization.
--- (c) 2005-2012 Alexandre Erwin Ittner <[email protected]>
--- For more information, see: http://ittner.github.com/abelhas/
---
---
--- Permission is hereby granted, free of charge, to any person obtaining
--- a copy of this software and associated documentation files (the
--- "Software"), to deal in the Software without restriction, including
--- without limitation the rights to use, copy, modify, merge, publish,
--- distribute, sublicense, and/or sell copies of the Software, and to
--- permit persons to whom the Software is furnished to do so, subject to
--- the following conditions:
---
--- The above copyright notice and this permission notice shall be
--- included in all copies or substantial portions of the Software.
---
--- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
--- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
--- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
--- IN NO EVENT SHALL THE AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY
--- CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
--- TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
--- SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
---
--- If you use this package in a product, an acknowledgment in the product
--- documentation would be greatly appreciated (but it is not required).
---
---
local M = { }
local MT = { }
M.VERSION = "1.1"
M.TERM_CONVERGED = 1
M.TERM_MAX_ITERATIONS = 2
M.TERM_MAX_STAGNATION = 3
local unpack = unpack or table.unpack
---
--- pso.new(dimensions)
---
--- Constructor. Returns a new swarm with the given number of dimensions.
---
function M.new(dims)
local sw = {
fitfunc = nil, -- Fitness function
dims = dims, -- Number of dimensions
prec = {}, -- Precision (decimal places, per dimension)
minp = {}, -- Minimum value, per dimension
maxp = {}, -- Maximum value, per dimension
maxs = {}, -- Maximum particle speed, per dimension
c1 = 0.5, -- c1, cognitive factor
c2 = 0.5, -- c2, social factor
nparts = 20, -- Number of particles
repl = 0, -- Particle replacement.
fitr = nil, -- Fitness rounding
maxfit = nil, -- Maximum fitness
maxiter = nil, -- Maximum iterations
maxstag = nil, -- Maximum fitness stagnation
gbest = nil, -- Index of the best particle in the swarm
parts = {}, -- Particles
nbhoook = nil, -- New best hook
replhook = nil, -- Replacement hook
iterhook = nil -- Iteration hook
}
setmetatable(sw, { __index = MT })
return sw
end
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- Rounds the number 'n' to 'p' decimal places.
local math_floor, math_ceil = math.floor, math.ceil
local function round(n, p)
if not p then
return n
end
local m = 10.0 ^ p
if (m*n - math_floor(m*n)) >= 0.5 then
return math_ceil(m*n)/m
end
return math_floor(m*n)/m
end
-- Returns the number 'b' if it is within the range [a,c]; otherwise,
-- returns a or c
local math_max, math_min = math.max, math.min
local function range(a, b, c)
return math_max(a, math_min(b, c))
end
-- Implements a continuous and closed space between 'min' and 'max'
local function cspace(min, x, max)
if (min <= x) and (x <= max) then
return x
else
return min + (x % max)
end
end
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
---
--- sw:setPrecision(decs)
---
--- Sets the precision for all dimensions (in number of decimal places).
--- 'nil' disables this feature and allows the values to assume the maximum
--- precision allowed by the Lua numbers.
---
function MT.setPrecision(self, decs)
for i = 1, self.dims do
self:setPrecisionDim(i, decs)
end
end
---
--- sw:setPrecisionDim(dim, decs)
---
--- Sets the precision for the dimension 'dim', for 'decs' decimal places.
--- 'nil' disables this feature and allows the values to assume the maximum
--- precision allowed by the Lua numbers.
---
function MT.setPrecisionDim(self, dim, decs)
assert(not decs or decs >= 0, "Bad number of decimal places.")
assert(dim > 0 and dim <= self.dims, "Bad dimension")
self.prec[dim] = decs
end
---
--- sw:getPrecisionDim(dim)
---
--- Returns the precision for the dimension 'dim', as the number of decimal
--- places.
---
function MT.getPrecisionDim(self, dim)
assert(dim > 0 and dim <= self.dims, "Bad dimension")
return self.prec[dim]
end
---
--- sw:setC1(c)
---
--- Sets the cognitive factor (number, 0 or greater).
---
function MT.setC1(self, c)
-- assert(c >= 0 and c <= 1, "Value out of range")
assert(c >= 0, "Value out of range")
self.c1 = c
end
---
--- sw:getC1()
---
--- Returns the cognitive factor (number, 0 or greater).
---
function MT.getC1(self)
return self.c1
end
---
--- sw:setC2(c)
---
--- Sets the social factor (number, 0 or greater).
---
function MT.setC2(self, c)
-- assert(c >= 0 and c <= 1, "Value out of range")
assert(c >= 0, "Value out of range")
self.c2 = c
end
---
--- sw:getC2()
---
--- Returns the social factor (number, 0 or greater).
---
function MT.getC2(self)
return self.c2
end
---
--- sw:setMaxSpeedDim(dimension, speed)
---
--- Sets the maximum speed for a particle in the given dimension
---
function MT.setMaxSpeedDim(self, dim, spd)
assert(dim > 0 and dim <= self.dims, "Bad dimension")
self.maxs[dim] = spd
end
---
--- sw:getMaxSpeed(dimension, speed)
---
--- Returns the maximum speed for a particle in the given dimension.
---
function MT.getMaxSpeedDim(self, dim)
assert(dim > 0 and dim <= self.dims, "Bad dimension")
return self.maxs[dim]
end
---
--- sw:setMaxSpeed(speed)
---
--- Sets the maximum speed for all dimensions.
---
function MT.setMaxSpeed(self, spd)
for dim = 1, self.dims do
self:setMaxSpeedDim(dim, spd)
end
end
---
--- sw:setReplacementProb(prob)
---
--- Sets the probability of a particle being replaced by another, randomly
--- generated, one. This feature tries to avoid local optima by the simulation
--- of the death and replacement of a particle. The probability must be a
--- number between 0 and 1. The best particle in the swarm is never replaced.
---
function MT.setReplacementProb(self, prob)
assert(0 <= prob and prob <= 1, "Bad replacement probability")
self.repl = prob
end
---
--- sw:getReplacementProb(prob)
---
--- Gets the probability of a particle being replaced by another.
---
function MT.getReplacementProb(self)
return self.repl
end
---
--- sw:setParticles(number)
---
--- Sets the number of particles.
---
function MT.setParticles(self, n)
assert(n > 0, "Bad number of particles")
self.nparts = n
end
---
--- sw:getParticles()
---
--- Returns the number of particles.
---
function MT.getParticles(self)
return self.nparts
end
---
--- sw:setFitnessFunction(function (...) .... end)
---
--- Sets the fitness function. The particle position will be passed as an
--- argument for each dimension. The fitness function must return the
--- fitness of the given particle as a number with higher values for better
--- solutions.
---
function MT.setFitnessFunction(self, func)
if type(func) ~= "function" then
error("Bad function")
end
self.fitfunc = func
end
---
--- sw:setLimitsDim(dimension, min, max)
---
--- Sets the limits for the given dimension.
---
function MT.setLimitsDim(self, dim, min, max)
assert(dim > 0 and dim <= self.dims, "Bad dimension")
self.minp[dim] = min
self.maxp[dim] = max
end
---
--- sw:getLimitsDim(dimension)
---
--- Returns the minimum and maximum values for the given dimension.
---
function MT.getLimitsDim(self, dim)
assert(dim > 0 and dim <= self.dims, "Bad dimension")
return self.minp[dim], self.maxp[dim]
end
--- sw:setLimits(min, max)
---
--- Sets the limits for all dimensions.
---
function MT.setLimits(self, min, max)
for dim = 1, self.dims do
self:setLimitsDim(dim, min, max)
end
end
---
--- sw:setFitnessRounding(decs)
---
--- Makes the solver round up the fitness to 'decs' decimal places. The value
--- must be a positive integer or 'nil' to disable this feature.
---
function MT.setFitnessRounding(self, decs)
assert(not decs or decs >= 0, "Bad number of decimal places.")
self.fitr = decs
end
---
--- sw:getFitnessRounding()
---
--- Returns the number of decimal places used to round up the fitness values,
--- or 'nil' if this feature is not used.
---
function MT.setFitnessRouding(self, decs)
assert(decs > 0, "Bad number of decimal places.")
self.fitr = decs
end
---
--- sw:setMaxFitness(maxf)
---
--- Sets the maximum fitness as a termination criterium. Fitness must be a
--- number or 'nil' to disable its use as termination criterium.
---
function MT.setMaxFitness(self, max)
self.maxfit = max
end
---
--- sw:getMaxFitness()
---
--- Returns the maximum fitness, or 'nil' if it is not used as termination
--- criteria.
---
function MT.getMaxFitness(self)
return self.maxfit
end
---
--- sw:setMaxIterations(maxi)
---
--- Sets the maximum number of iterations as a termination criterium. This
--- must be a integer greater than zero or 'nil' to disable its use as
--- termination criterium.
---
function MT.setMaxIterations(self, max)
assert(max > 0, "Bad number of iterations")
self.maxiter = max
end
---
--- sw:getMaxIterations()
---
--- Returns the maximum number of iterations, or 'nil' if it is not used as
--- termination criteria.
---
function MT.getMaxIterations(self)
return self.maxiter
end
---
--- sw:setMaxStagnation(maxs)
---
--- Sets the maximum number of stagnated iterations as a termination
--- criterium. This must be a integer greater than zero or 'nil' to disable
--- its use as termination criterium.
---
function MT.setMaxStagnation(self, max)
assert(max > 0, "Bad number of iterations")
self.maxstag = max
end
---
--- sw:getMaxStagnation()
---
--- Returns the maximum number of stagnated iterations, or 'nil' if it is not
--- used as termination criteria.
---
function MT.getMaxStagnation(self)
return self.maxstag
end
---
--- sw:setNewBestHook(function(...) end)
---
--- Sets a function to be called when a new best particle if found. The
--- function will receive the particle position, an per dimension. Passing
--- 'nil' disables this feature.
---
function MT.setNewBestHook(self, func)
if func and type(func) ~= "function" then
error("Bad function")
end
self.nbhook = func
end
---
--- sw:setReplacementHook(function(...) end)
---
--- Sets a function to be called when a particle is replaced. The function
--- will receive the position of the dead particle, an per dimension. Passing
--- 'nil' disables this feature.
---
function MT.setReplacementHook(self, func)
if func and type(func) ~= "function" then
error("Bad function")
end
self.replhook = func
end
---
--- sw:setIterationHook(function(parts, iter) ... end)
---
--- Sets a function to be called for each iteration of the optimizer. The
--- function will receive a array of particles as first argument and the
--- number of the current iteration as second argument. Each particle in
--- the particle array is a table with the following fields:
---
--- fit the fitness value for the best position (number);
--- x particle's position in the n-dimensional space (array of numbers);
--- b particle's best position (array of numbers);
--- v particle's velocity (array of numbers).
---
--- these values may be read, but SHOULD NOT be changed or redefined.
--- Passing 'nil' disables this feature.
---
--- Warning: Abuse of this feature may slow the algorithm down!
---
function MT.setIterationHook(self, func)
if func and type(func) ~= "function" then
error("Bad function")
end
self.iterhook = func
end
-- Evaluates a particle.
local function evalpart(self, p)
local fit = round(self.fitfunc(unpack(p.x)), self.fitr)
if p.fit then
if fit > p.fit then -- New particle's best found.
p.fit = fit
for i = 1, self.dims do
p.b[i] = p.x[i]
end
end
else
p.fit = fit
end
end
-- Makes a new random particle or randomizes an existing one.
local function randomizeParticle(self, p)
p = p or {}
p.fit = nil -- 'nil' is the worst possible fitness.
p.x = {} -- particle's position
p.b = {} -- particle's best position (pbest)
p.v = {} -- particle's velocity
for i = 1, self.dims do
p.x[i] = (self.maxp[i] - self.minp[i]) * math.random() + self.minp[i]
p.b[i] = p.x[i]
p.v[i] = 2 * self.maxs[i] * (math.random() - 0.5)
end
evalpart(self, p)
return p
end
-- Updates the velocity and positonf of a particle.
local function updateParticle(self, i)
local p = self.parts[i]
local b = self.parts[self.gbest]
local prec = self.prec -- Optimization
local rnd = math.random
for i = 1, self.dims do
p.v[i] = range(
-self.maxs[i],
self.c1 * rnd() * (p.b[i] - p.x[i]) + -- Cognitive
self.c2 * rnd() * (b.b[i] - p.x[i]), -- Social
self.maxs[i])
local x = p.x[i] + p.v[i]
if prec[i] then -- Position rounding
x = round(x, prec[i])
end
p.x[i] = cspace(self.minp[i], x, self.maxp[i])
end
end
---
--- sw:run()
---
--- Runs the algorithm and returns a array with the position of the particle,
--- the fitness, the reason of termination (pso.TERM_CONVERGED,
--- pso.TERM_MAX_ITERATIONS, or pso.TERM_MAX_STAGNATION) and the total of
--- iterations.
---
function MT.run(self)
local iter = 0
local stag = 0
local p
assert(self.fitfunc, "No fitness function defined.")
assert(self.maxfit or self.maxiter or self.maxstag,
"No termination criteria defined.")
for i = 1, self.dims do
assert(self.maxs[i], "Required value for maximum speed not given.")
assert(self.maxp[i], "Required value for maximum position not given.")
assert(self.minp[i], "Required value for minimum position not given.")
end
for i = 1, self.nparts do
self.parts[i] = randomizeParticle(self)
end
while true do
for i = 1, self.nparts do
-- Particle replacement.
if i ~= self.gbest and math.random() < self.repl then
if self.replhook then
self.replhook(unpack(self.parts[i].b))
end
randomizeParticle(self, self.parts[i])
end
evalpart(self, self.parts[i])
if self.gbest then
if self.parts[i].fit > self.parts[self.gbest].fit then
stag = 0
self.gbest = i
if self.nbhook then
self.nbhook(unpack(self.parts[self.gbest].b))
end
end
else
-- The new best hook is never called for the first best.
self.gbest = i
stag = 0
end
end
if self.maxfit and (self.parts[self.gbest].fit >= self.maxfit) then
return self.parts[self.gbest].b, self.parts[self.gbest].fit,
M.TERM_CONVERGED, iter
end
iter = iter + 1
if self.iterhook then
self.iterhook(self.parts, iter)
end
if self.maxiter and (iter > self.maxiter) then
return self.parts[self.gbest].b, self.parts[self.gbest].fit,
M.TERM_MAX_ITERATIONS, iter
end
stag = stag + 1
if self.maxstag and (stag > self.maxstag) then
return self.parts[self.gbest].b, self.parts[self.gbest].fit,
M.TERM_MAX_STAGNATION, iter
end
for i = 1, self.nparts do
updateParticle(self, i)
end
end
return nil
end
-- Debug utilities:
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
local function printParticles(self)
for i = 1, self.nparts do
print(unpack(self.parts[i].b))
end
end
return M