Anonymous user
Particle swarm optimization: Difference between revisions
m
→{{header|REXX}}: shortened some statements, optimized a function, added/changed comments and whitespace.
m (→{{header|REXX}}: shortened some statements, optimized a function, added/changed comments and whitespace.) |
|||
Line 977:
=={{header|REXX}}==
{{trans|ooRexx}}
This REXX version uses a large ''numeric digits'' (the number of decimal digits in pi), but only displays '''25''' digits
Classic REXX doesn't have a '''sine''' function, so a RYO version is included here.
The numeric precision is only limited to the number of decimal digits defined in the <big> '''pi''' </big> variable (in this case, '''
This REXX version supports the specifying of '''X''', '''Y''', and '''D''', as well as the number of particles, and the number of decimal digits to be displayed.
The refinement loop is stopped when the calculation of the function value stabilizes.
Note that REXX
<lang rexx>/*REXX program calculates Particle Swarm Optimization as it migrates through a solution.*/
numeric digits length( pi() )
parse arg x y d #part sDigs
if x=='' |
if y=='' |
if d=='' |
if #part=='' | #part=="," then #part=
if sDigs=='' | sDigs=="," then sDigs=
minF=#part;
say center('X', sDigs+3, "═") center('Y', sDigs+3, "═") center('D', sDigs+3, "═")
call refine x,y /* [↓] same as ÷ by five.*/
do until refine(minX, minY); d=d * .2
end /*until
say
say right('The global minimum for f(-.54719, -1.54719) ───► ',
say right('The published global minimum is:' ,
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
refine: parse arg xx,yy; h=d * .5 /*compute ½ distance.*/
do x=xx-d to xx+d by h
do y=yy-d to yy+d by h; f=f(x, y); if f>=minF then iterate
new=fmt(x) fmt(y) fmt(f);
say new; minF=f; minX=x; minY=y; old=new
end /*y*/
end /*x*/
return 0
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
f: procedure: parse arg a,b;
fmt:
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865; return pi
r2r: return arg(1) // ( pi() * 2)
sin: procedure
<pre>
═════════════X══════════════ ═════════════Y══════════════ ═════════════D══════════════
|