Particle swarm optimization: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments, simplified a function, added a better (computation) cessation algorithm.
(=={{header|Racket}}== implementation added)
m (→‎{{header|REXX}}: added/changed whitespace and comments, simplified a function, added a better (computation) cessation algorithm.)
Line 418:
The numeric precision is only limited to the number of decimal digits defined in the &nbsp; <big> '''pi''' </big> &nbsp; variable &nbsp; (in this case, '''100''').
 
This REXX version supports the specifying of '''X''', '''Y''', and '''D''', &nbsp; as well as the number of particles, and the number of timesdecimal thedigits to be displayed.
<br>computation loop is performed, and the number of decimal digits to be displayed.
 
The refinement loop is stopped when the function value stabilizes, or the limit of iterations is reached.
<lang rexx>/*REXX pgm calc. Particle Swarm Optimization as it migrates through a solution*/
numeric digits length(pi()) /*sDigs: is the # of displayed digits.*/
parse arg x y d #part times sDigs . /*obtain optional arguments from the CL*/
if x=='' | x==',' then x= -0.5 /*is X not defined?*/
if y=='' | y==',' then y= -1.5 /* " Y " " */
if d=='' | d==',' then d= 1 /* " D " " */
if #part=='' | #part==',' then #part=1e12 /* " the # particles " " */
if times=='' | times==',' then times= 40 /* " the # of times " " */
if sDigs=='' | sDigs==',' then sDigs= 25 /* " the # of digits " " */
minF=#part; old= /*number of particles is one billion. */
say center('X',sDigs+3,'═') center('Y',sDigs+3,'═') center('D',sDigs+3,'═')
call refine x,y
do stuff=1 for times until old=frefine(minX,minY); d=d*.2; old=f
end /*stuffuntil ···*/ /* [↑] stop refining after TIMES, if orno difference.*/
call refine minX, minY
say
end /*stuff*/ /* [↑] stop refining after TIMES, or */
say indent=1 + (sDigs+3) * 2 /* whencompute the value ofindentation Ffor stabilizes.alignment*/
indent=1 + 2*(sDigs+3) /*compute the indentation for alignment*/
say right('The global minimum for f(-.54719, -1.54719) ───► ', indent) fmt(f(-.54719, -1.54719))
say right('The published global minimum is:' , indent) fmt( -1.9133 )
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
refine: parse arg xx,yy; dhh=d * 0.5
do x=xx-d to xx+d by dhh
do y=yy-d to yy+d by dhh; f=f(x,y); if f>=minF then iterate
say new=fmt(x) fmt(y) fmt(f); minF=f; minX=x; if new=old then minY=yreturn 1
end /* say new; minF=f; minX=x; minY=y*/; old=new
end end /*xy*/
callend refine minX, minY /*x*/
return
return 0
/*──────────────────────────────────────────────────────────────────────────────────one─liner subroutines───────────────────────────────*/
f: procedure: parse arg a,b; return sin(a+b) + (a-b)**2 - 1.5*a + 2.5*b + 1
Line 516 ⟶ 514:
-0.5471975511965977461542152 -1.5471975511965977461542143 -1.9132229549810363929179376
-0.5471975511965977461542143 -1.5471975511965977461542143 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376