Anonymous user
Particle swarm optimization: Difference between revisions
m
→{{header|REXX}}: added/changed whitespace and comments.
SqrtNegInf (talk | contribs) m (→{{header|Perl 6}}: output formatting) |
m (→{{header|REXX}}: added/changed whitespace and comments.) |
||
Line 2,148:
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() ) -
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.*/
Line 2,159:
show= sDigs + 3 /*adjust number decimal digits for show*/
say "══iteration══" center('X',show,"═") center('Y',show,"═") center('D',show,"═")
#= 0; call refine x,
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.*/
▲$= 15 + show * 2 /*compute the indentation for alignment*/
say right('The global minimum for f(-.54719, -1.54719) ───► ', $) fmt(f(-.54719, -1.54719))
say right('The published global minimum is:' , $) fmt( -1.9133 )
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
refine: parse arg xx,yy; h= d * .5
do x=xx-d to xx+d by h
do y=yy-d to yy+d by h; f= f(x, y);
new= fmt(x) fmt(y) fmt(f); if new=old then return 1
#= # + 1; say center(#,13)
end /*y*/
end /*x*/
Line 2,182 ⟶ 2,179:
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
f: procedure: parse arg a,b; return sin(a+b) + (a-b)**2 - 1.5*a + 2.5*b + 1
fmt: ?= format(arg(1), , sDigs);
pi: pi=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865; return pi
r2r: return arg(1) // ( pi() * 2) /*normalize radians ───► a unit circle.*/
sin: procedure; arg x; x= r2r(x); z=
{{out|output|text= when using the default input:}}
<pre>
|