Professional Documents
Culture Documents
class ParticleProgram
static !oi" #ain(string$% args)
&onsole'(rite)ine(*Particle Swarm Optimization*);
int "im = 2; ++ pro,lem "imensions
int numParticles = -;
int max.poc/s = 0111;
"ou,le exit.rror = 1'1; ++ exit early
"ou,le min2 = -01'1; ++ pro,lem-"epen"ent
"ou,le max2 = 01'1;
&onsole'(rite)ine(*3nStarting PSO*);
"ou,le$% ,estPos = Sol!e("im4 numParticles4
min24 max24 max.poc/s4 exit.rror);
"ou,le ,est.rror = .rror(,estPosition);
&onsole'(rite)ine(*5est position6oun"7*);
6or (int i = 1; i 8 ,estPos')engt/; ++i)
&onsole'(rite(*x* + i + * = *);
&onsole'(rite)ine(,estPos$i%'9oString(*:;*) + * *);
<
&onsole'(rite)ine(**);
&onsole'(rite(*:inal ,est error = *);
&onsole'(rite)ine(,est.rror'9oString(*:-*));
&onsole'(rite)ine(*3n.n" PSO "emo3n*);
&onsole'=ea")ine();
< ++ #ain
static "ou,le .rror("ou,le$% x) ' ' <
static "ou,le$% Sol!e(int "im4 int numParticles4 "ou,le min24
"ou,le max24 int max.poc/s4 "ou,le exit.rror) ' ' <
<
pu,lic class Particle ' ' <
<
+e Particle ,lass
+e Particle class #e"ines a particle "or use b) te PSO al$oritm& ,lass Particle is
presente# in !isting 2& 5or simplicit)( all class members are #eclare# usin$ public
scope& .rra) position represents te current position o" a particle( wic in turn
represents a possible solution to an optimization problem& .rra) *elocit)
represents te current spee# an# #irection o" a particle( presumabl) towar#s a new(
better position/solution& 5iel# error is te error associate# wit te particle2s
current position& .rra) bestPosition ol#s te position o" te particle wic
)iel#e# te best (lowest) error *alue& 5iel# best?rror is te error *alue associate#
wit location bestPosition&
Listin$ 27 +e Particle ,lass
pu,lic class Particle
,estBlo,al.rror = swarm$i%'error;
swarm$i%'position'&opy9o(,estBlo,alPosition4 1);
<
."ter swarm initialization( meto# Sol*e prepares te main PSO processin$ loop7
"ou,le w = 1'F2A; ++ inertia
"ou,le c0 = 0'>A>>-; ++ cogniti!e weig/t
"ou,le c2 = 0'>A>>-; ++ social weig/t
"ou,le r04 r2; ++ cogniti!e4 social ran"omizations
"ou,le pro,Eeat/ = 1'10;
int epoc/ = 1;
"ou,le$% new?elocity = new "ou,le$"im%;
"ou,le$% newPosition = new "ou,le$"im%;
"ou,le new.rror;
Ben a particle mo*es in PSO( a new *elocit) is "irst compute#& +en( te new
*elocit) is use# to compute te new position& . particle2s new *elocit) #epen#s on
tree "actors7 te particle2s current *elocit)( te particle2s best4known position an#
te best4known position o" an) particle in te swarm& Aariable w is calle# te
inertia wei$t an# #etermines te in"luence o" te current *elocit)& Aariables c1
an# c2 are calle# te co$niti*e an# social wei$ts an# #etermine te in"luence o"
te particle2s best position an# te $lobal best4known position( respecti*el)& +e
*alues use# ere a*e been su$$este# b) PSO researc an# )ou can tink o" tem
as ma$ic constants&
Aariables r1 an# r2 are *alues tat a## a ran#omization e""ect to a particle2s
mo*ement( wic elps to pre*ent te particle "rom becomin$ stuck in a #ea#
en#& Aariable prob3eat is set to a small *alue an# is use# to ran#oml) kill an#
ten re$enerate a particle& +e #eat4birt process is optional in PSO( but '2*e
"oun# it to be a use"ul enancement&
'n pseu#o4co#e( te main PSO processin$ loop is7
loop until "one
6or eac/ particle
compute new !elocity
use !elocity to compute new position
compute new error at position
c/ecG i6 new error is a particle ,est
c/ecG i6 new error is a swarm ,est
Gill particle wit/ small pro,a,ility
en" eac/ particle
en" loop
return ,est position 6oun"
+e new *elocit) "or te current particle( currP( is compute# like so7
Particle currP = swarm$i%;
6or (int C = 1; C 8 currP'!elocity')engt/; ++C)
r0 = rn"'DextEou,le();
r2 = rn"'DextEou,le();
new?elocity$C% = (w * currP'!elocity$C%) +
(c0 * r0 * (currP',estPosition$C% - currP'position$C%)) +
(c2 * r2 * (,estBlo,alPosition$C% - currP'position$C%));
<
+is is te eart o" te PSO al$oritm& '" )ou look closel)( )ou2ll see ow eac
component o" te current particle2s *elocit) #epen#s on te current *elocit)( te
particle2s best4known position an# te swarm2s best4known position& +e net e""ect
is tat particles ten# to mo*e towar#s better solutions( an# teir *elocit) ten#s to
#ecrease as teir position impro*es&
+e new position "or te current particle( currP( is compute# like tis7
6or (int C = 1; C 8 currP'position')engt/; ++C)