Anonymous user
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 <big> '''pi''' </big> variable (in this case, '''100''').
This REXX version supports the specifying of '''X''', '''Y''', and '''D''', as well as the number of particles, and the number of
The refinement loop is stopped when the function value stabilizes
<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
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 sDigs=='' | sDigs==',' then sDigs= 25 /* " the # of digits " " */
minF=#part;
say center('X',sDigs+3,'═') center('Y',sDigs+3,'═') center('D',sDigs+3,'═')
call refine x,y
do
call refine minX, minY▼
say
▲ end /*stuff*/ /* [↑] stop refining after TIMES, or */
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;
do x=xx-d to xx+d by
do y=yy-d to yy+d by
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
|