Particle swarm optimization: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace. optimized the SIN function.
m (→‎{{header|REXX}}: added/changed comments and whitespace. optimized the SIN function.)
Line 1,528:
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 &nbsp; <big> '''pi''' </big> &nbsp; variable &nbsp; (in this case, &nbsp; '''110''').
Line 1,538:
Note that REXX uses decimal floating point, not binary.
<lang rexx>/*REXX program calculates Particle Swarm Optimization as it migrates through a solution.*/
numeric digits length( pi() ) - 1 /*sDigs: isuse the #number of displayeddecimal digs in digitspi.*/
parse arg x y d #part sDigs . /*obtain optional arguments from the CL*/
if x=='' | x=="," then x= -0.5 /*Not specified? Then use the default.*/
if y=='' | y=="," then y= -1.5 /* " " " " " " */
if d=='' | d=="," then d= 1 1 /* " " " " " " */
if #part=='' | #part=="," then #part= 1e12 /* " " " " " " */
if sDigs=='' | sDigs=="," then sDigs= 25 /* " " " " " " */
minFold=#part; iters=0; show=sDigs+3; old= /*number#part: of particles is1e12 ≡ is one trillion. */
minF= #part /*the minimum for the function (#part).*/
show= sDigs + 3 /*adjust number decimal digits for show*/
say "══iteration══" center('X',show,"═") center('Y',show,"═") center('D',show,"═")
#= 0 /*the number of iterations for REFINE.*/
call refine x,y /* [↓] same as ÷ by five.*/
do until refine(minX, minY); d=d /*perform .2until the mix is /*decrease"refined". the difference.*/
d=d * .2 /*decrease the difference in the mix. .*/
end /*until*/ /* [↑] stop refining if no difference.*/
say
Line 1,556 ⟶ 1,560:
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); if new=old then return 1
iters#= iters# + 1 /*bump interations# cntiterations. */
say center(iters#,13) new; minF=f; minX=x; minY=y; old=new /*show " " */
minF= f; end minX= x; minY= y; old= new /*yassign new values. */
end /*xy*/
end /*x*/
return 0
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
Line 1,570 ⟶ 1,575:
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865; return pi
r2r: return arg(1) // ( pi() * 2) /*normalize radians ───► a unit circle.*/
sin: procedure; arg x; x= r2r( arg(1) x); z= x; q xx=x*x; do k=2 by 2 until p=z; p=z; x= -x*qxx/ (k*(k+1)); z= z+x; end; return z</lang>
{{out|output|text=&nbsp; when using the default input:}}
<pre>