Particle swarm optimization: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed comments and whitespace. optimized the SIN function.)
Line 1,528: Line 1,528:
This REXX version uses a large   ''numeric digits''   (the number of decimal digits in pi),   but only displays '''25''' digits.
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.
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''').
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: Line 1,538:
Note that REXX uses decimal floating point, not binary.
Note that REXX uses decimal floating point, not binary.
<lang rexx>/*REXX program calculates Particle Swarm Optimization as it migrates through a solution.*/
<lang rexx>/*REXX program calculates Particle Swarm Optimization as it migrates through a solution.*/
numeric digits length( pi() ) - 1 /*sDigs: is the # of displayed digits.*/
numeric digits length( pi() ) - 1 /*use the number of decimal digs in pi.*/
parse arg x y d #part sDigs . /*obtain optional arguments from the CL*/
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 x=='' | x=="," then x= -0.5 /*Not specified? Then use the default.*/
if y=='' | y=="," then y= -1.5 /* " " " " " " */
if y=='' | y=="," then y= -1.5 /* " " " " " " */
if d=='' | d=="," then d= 1 /* " " " " " " */
if d=='' | d=="," then d= 1 /* " " " " " " */
if #part=='' | #part=="," then #part= 1e12 /* " " " " " " */
if #part=='' | #part=="," then #part= 1e12 /* " " " " " " */
if sDigs=='' | sDigs=="," then sDigs= 25 /* " " " " " " */
if sDigs=='' | sDigs=="," then sDigs= 25 /* " " " " " " */
minF=#part; iters=0; show=sDigs+3; old= /*number of particles is one trillion.*/
old= /*#part: 1e12 ≡ 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,"═")
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.*/
call refine x,y /* [↓] same as ÷ by five.*/
do until refine(minX, minY); d=d * .2 /*decrease the difference.*/
do until refine(minX, minY) /*perform until the mix is "refined". */
d=d * .2 /*decrease the difference in the mix. .*/
end /*until*/ /* [↑] stop refining if no difference.*/
end /*until*/ /* [↑] stop refining if no difference.*/
say
say
Line 1,556: Line 1,560:
exit /*stick a fork in it, we're all done. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
refine: parse arg xx,yy; h=d * .5 /*compute ½ distance.*/
refine: parse arg xx,yy; h= d * .5 /*compute ½ distance.*/
do x=xx-d to xx+d by h
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
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
new= fmt(x) fmt(y) fmt(f); if new=old then return 1
iters= iters + 1 /*bump interations cnt.*/
#= # + 1 /*bump # iterations. */
say center(iters,13) new; minF=f; minX=x; minY=y; old=new
say center(#,13) new /*show " " */
end /*y*/
minF= f; minX= x; minY= y; old= new /*assign new values. */
end /*x*/
end /*y*/
end /*x*/
return 0
return 0
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
Line 1,570: Line 1,575:
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865; return pi
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865; return pi
r2r: return arg(1) // ( pi() * 2) /*normalize radians ───► a unit circle.*/
r2r: return arg(1) // ( pi() * 2) /*normalize radians ───► a unit circle.*/
sin: procedure; x=r2r( arg(1) ); z=x; q=x*x; do k=2 by 2 until p=z; p=z; x= -x*q/ (k*(k+1)); z= z+x; end; return z</lang>
sin: procedure; arg x; x= r2r(x); z= x; xx=x*x; do k=2 by 2 until p=z; p=z; x= -x*xx/ (k*(k+1)); z= z+x; end; return z</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>