Anonymous user
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 <big> '''pi''' </big> variable (in this case, '''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 /*
parse arg x y d #part sDigs . /*obtain optional arguments from the CL*/
if x=='' | x=="," then x= -0.5
if y=='' | y=="," then y= -1.5
if d=='' | d=="," then d=
if #part=='' | #part=="," then #part= 1e12 /* " " " " " " */
if sDigs=='' | sDigs=="," then sDigs= 25 /* " " " " " " */
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 * .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
minF= f;
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(
{{out|output|text= when using the default input:}}
<pre>
|