CS 420/527 — Biologically Inspired Computation
NetLogo Simulation

PSO

(Particle Swarm Optimization)

This page was automatically generated by NetLogo 4.1. Questions, problems? Contact feedback@ccl.northwestern.edu.

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Sun's Java site. If the display appear cut off with Firefox, then try another browser (Safari works).



powered by NetLogo

view/download model file: PSO.nlogo

WHAT IS IT?

This model demonstrates the Particle Swarm Optimization technique developed by Kennedy and Eberhart (see CREDITS AND REFERENCES below). Particle Swarm Optimization (PSO) uses a population of particles to cooperatively search for the best solution to a problem, represented by positions in a space, where each location has a "quality" ("goodness") determined by some Objective Function or Figure of Merit. It was originally intended as a simple model of socially acquired knowledge, in that each particle's search is guided both by its own private knowledge of the best solution it has found so far and by social knowledge, namely the best solution found by all the particles. PSO is commonly used to search high-dimensional spaces, but in this model we search a two-dimensional space, which is easier to visualize.


HOW IT WORKS

Each particle k has a current position Xk, a current velocity Vk, and a remembered best location Pk (all two-dimensional vectors). In addition, all particles know g, the index of the particle that has found the best position so far, so that Pg is the best location that anyone has found. The velocity update rule is:

Vk' = w Vk + Phi1 (Pk - Xk) + Phi2 (Pg - Xk)

where w is an inertia parameter and Phi1 and Phi2 are random numbers uniformly distributed in [0, 2]. They determine the extent to which the particle steers towards its own best solution (Pk) or the population's best solution (Pg). The particle's position is updated by:

Xk' = Xk + Vk'

In addition, if the quality of Xk' is greater that that of Pk, then the individual best is updated, Pk := Xk'. Also, if the quality of Pk is better than the population's best, Pg, then the latter is updated, Pg := Pk.


HOW TO USE IT

NUMBER controls the number of particles.

SETUP initializes the particles with random positions and velocities.

GO ONCE and GO cause the particles to update their positions and velocities according to the particle swarm equations.

INERTIA sets the particles' inertia (see HOW IT WORKS).

MAX VELOCITY sets the maximum particle velocity; particles that would have a greater velocity (according to the rules) have it scaled back to this maximum, but without changing the direction.

VARIABLE PHIS? determines whether the random Phi1 and Phi2 values for each particle are determined once, when the particles are created, or whether they are recalculated every time a particle moves. The former gives the particles fixed "personality types" wheras the latter allows them to change dispositions at any time.

COLL. AVOIDANCE? determines whether the particles avoid colliding with each other, and maintain greater diversity and ability to explore. When COLL. AVOIDANCE? is turned on, a particle looks ahead to see if its next step would take it to a location where there is already a particle. If so, it negates its velocity vector, so in effect it bounces backward and continues in this new direction.

INIT BACKGROUND initalizes a noisy fitness (objective function) background, with a maximum level determined by BACKGROUND LEVEL. This is included in the fitness evaluation computation if INCLUDE BACKGROUND? is turned on. Note that SETUP clears the background, so INIT BACKGROUND should be done after SETUP.

TEST FUNCTION selects the objective function to be used. "2007" has a peak at coordinates (20, 07) (upper right). "Two peaks" has a global optimum at (20, 07) and a much broader suboptimum at (-20, -07).

SHOW OBJ FUNCTION and HIDE OBJ FUNCTION determine whether the objective function is displayed in the background. Click on SHOW after the objctive function is selected and the fitness background is initialized (if you use the background).

BEST X and BEST Y show the best location found so far; MERIT is the quality of the current best solution, and BEST PARTICLE is the particle that found it.

GLOBAL BEST plots the quality of the best solution.


THINGS TO NOTICE

With the "two peaks" test the particles may get trapped by the suboptimum; this is an example of "premature convergence."


THINGS TO TRY

Try turning on collision avoidance and setting the max velocity high (10-15). The particles bounce around alot more, but do they find the global optimum better?


EXTENDING THE MODEL

Add new test models. Read the PSO literature and try some of the new ideas to improve its performance. Recode the particles to use Netlogo's movement abilities rather than the vector calculations in this simulation.


RELATED MODELS

PSO is related to models of flocking behavior in birds. In what ways is it different?


CREDITS AND REFERENCES

Particle swarm optimization was invented by Kennedy & Eberhart:

Kennedy, J., & Eberhart, R. ÒParticle Swarm Optimization,Ó Proc. IEEE IntÕl. Conf. Neural Networks (Perth, Australia), 1995. See < http://www.engr.iupui.edu/~shi/pso.html >.

Krink, T., Vesterstr¿m, J.S., and Riget, J. ÒParticle Swarm Optimization with Spatial Particle Extension,Ó World Conference on Computational Intelligence, 2002. This paper describes and tests collision avoidance. See < http://www.evalife.dk >, < krink,jve,riget@daimi.au.dk >.

To refer to this model in academic publications, please use: MacLennan, B.J. (2007). NetLogo Particle Swarm Optimization model. http://www.cs.utk.edu/~mclennan. Dept. of Electrical Engineering & Computer Science, Univ. of Tennessee, Knoxville.

In other publications, please use: Copyright 2007 Bruce MacLennan. All rights reserved. See http://www.cs.utk.edu/~mclennan/420/NetLogo/PSO.html for terms of use.


PROCEDURES

breed [particles particle]

particles-own [
cur-pos
velocity
best-pos
phi-1
phi-2
]

patches-own [
background-value
]

globals [
best-particle
global-best-pos
]

to setup
ca
create-particles number
ask particles [ initialize-particle ]
set best-particle one-of particles
set global-best-pos [cur-pos] of best-particle
end
to initialize-particle
fd random world-width / 2
set shape "circle"
set color red
set cur-pos list xcor ycor
set best-pos cur-pos
set velocity list
(
random-float max-velocity - max-velocity / 2)
(
random-float max-velocity - max-velocity / 2)
set phi-1 random-float 2
set phi-2 random-float 2
end
to go
ask particles [
update-velocity
update-position
update-best ]
update-global-best
plot evaluate global-best-pos
end
to update-velocity
if variable-phis? [
set phi-1 random-float 2
set phi-2 random-float 2
]
set velocity vec-sum (vec-scale inertia velocity)
(vec-sum (vec-scale phi-1 (vec-dif best-pos cur-pos))
(vec-scale phi-2 (vec-dif global-best-pos cur-pos)))
if magsq velocity > max-velocity ^ 2
[
set velocity vec-scale (max-velocity / sqrt (magsq velocity)) velocity ]
end
to update-position
let new-pos vec-sum cur-pos velocity
if coll-avoidance? [
if any? particles-at (item 0 velocity) (item 1 velocity) [
set velocity vec-scale -1 velocity
set new-pos vec-sum cur-pos velocity
]
]
set cur-pos new-pos
setxy (item 0 cur-pos) (item 1 cur-pos)
end
to update-best
if evaluate cur-pos > evaluate best-pos
[
set best-pos cur-pos ]
end
to update-global-best
let new-best-particle max-one-of particles [ evaluate cur-pos ]
if evaluate [cur-pos] of new-best-particle > evaluate global-best-pos
[
ask best-particle [ set color red ]
set best-particle new-best-particle
ask best-particle [ set color yellow ]
set global-best-pos [cur-pos] of best-particle ]
end
to-report evaluate [pos]
ifelse test-function = "2007" [ report test-2007 pos ] [
ifelse test-function = "two peaks" [ report test-two-peaks pos ] [
]]
end
to-report test-2007 [pos]
let max-dist sqrt (world-width ^ 2 + world-height ^ 2) / 2
let pos-dist sqrt (magsq vec-dif pos [20 07])
report 100 * (1 - pos-dist / max-dist)
+ opt-background-value pos
end
to-report test-two-peaks [pos]
let max-dist sqrt (world-width ^ 2 + world-height ^ 2) / 2
let pos-dist sqrt (magsq vec-dif pos [20 07])
report 9 * max list 0 (10 - sqrt (magsq vec-dif pos [20 07]))
+ 10 * (1 - pos-dist / max-dist)
+ 70 * (1 - sqrt (magsq vec-dif pos [-20 -07]) / max-dist)
+ opt-background-value pos
end
to initialize-background
ask patches [ set background-value random-float background-level ]
end
to-report opt-background-value [pos]
ifelse include-background?
[
report [background-value] of
patch limit (round (item 0 pos)) min-pxcor max-pxcor
limit (
round (item 1 pos)) min-pycor max-pycor ]
[
report 0 ]
end
to-report limit [val LB UB]
report min list UB (max list LB val)
end
to-report vec-sum [u v]
report (map [?1 + ?2] u v)
end
to-report vec-dif [u v]
report (map [?1 - ?2] u v)
end
to-report vec-scale [s v]
report map [s * ?] v
end
to-report magsq [v]
report (item 0 v) ^ 2 + (item 1 v) ^ 2
end
to show-obj-function
ask patches [ set pcolor scale-color green (evaluate (list pxcor pycor)) 0 100 ]
end
to hide-obj-function
ask patches [ set pcolor black ]
end

Return to CS 420/527 home page

Return to MacLennan's home page

Send mail to Bruce MacLennan / MacLennan@utk.edu

Valid HTML 4.01!This page is web.eecs.utk.edu/~mclennan/Classes/420/NetLogo/PSO.html
Last updated: 2010-11-29.