Particle swarm optimization: Difference between revisions

Content added Content deleted
(Added c#)
m (→‎{{header|REXX}}: added an ~iteration~ column in the output.)
Line 1,195: Line 1,195:
=={{header|REXX}}==
=={{header|REXX}}==
{{trans|ooRexx}}
{{trans|ooRexx}}

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.


Line 1,214: Line 1,215:
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; old= /*number of particles is one billion. */
minF=#part; iters=0; show=sDigs+3; old= /*number of particles is one billion. */
say center('X', sDigs+3, "═") center('Y', sDigs+3, "═") center('D', sDigs+3, "═")
say "══iteration══" center('X',show,"═") center('Y',show,"═") center('D',show,"═")
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); d=d * .2 /*decrease the difference.*/
end /*until*/ /* [↑] stop refining if no difference.*/
end /*until*/ /* [↑] stop refining if no difference.*/
say
say
$= 1 + (sDigs+3) * 2 /*compute the indentation for alignment*/
$= 13 + 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 global minimum for f(-.54719, -1.54719) ───► ', $) fmt(f(-.54719, -1.54719))
say right('The published global minimum is:' , $) fmt( -1.9133 )
say right('The published global minimum is:' , $) fmt( -1.9133 )
Line 1,229: Line 1,230:
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
say new; minF=f; minX=x; minY=y; old=new
iters= iters + 1 /*bump interations cnt.*/
say center(iters,13) new; minF=f; minX=x; minY=y; old=new
end /*y*/
end /*y*/
end /*x*/
end /*x*/
Line 1,241: Line 1,243:
{{out|output|text=  when using the default input:}}
{{out|output|text=  when using the default input:}}
<pre>
<pre>
═════════════X══════════════ ═════════════Y══════════════ ═════════════D══════════════
══iteration══ ═════════════X══════════════ ═════════════Y══════════════ ═════════════D══════════════
-1.5 -2.5 -1.2431975046920717486273609
1 -1.5 -2.5 -1.2431975046920717486273609
-1 -2 -1.6411200080598672221007448
2 -1 -2 -1.6411200080598672221007448
-0.5 -1.5 -1.9092974268256816953960199
3 -0.5 -1.5 -1.9092974268256816953960199
-0.54 -1.54 -1.9131329795075164948766768
4 -0.54 -1.54 -1.9131329795075164948766768
-0.548 -1.548 -1.9132218400165267634506035
5 -0.548 -1.548 -1.9132218400165267634506035
-0.548 -1.5472 -1.9132220344928294065568196
6 -0.548 -1.5472 -1.9132220344928294065568196
-0.5472 -1.5472 -1.9132229549706499208388746
7 -0.5472 -1.5472 -1.9132229549706499208388746
-0.5472 -1.54719872 -1.9132229549737311254290577
8 -0.5472 -1.54719872 -1.9132229549737311254290577
-0.54719872 -1.54719872 -1.9132229549786702369612333
9 -0.54719872 -1.54719872 -1.9132229549786702369612333
-0.54719872 -1.54719744 -1.91322295497891365438682
10 -0.54719872 -1.54719744 -1.91322295497891365438682
-0.54719744 -1.54719744 -1.9132229549810149766572388
11 -0.54719744 -1.54719744 -1.9132229549810149766572388
-0.5471975424 -1.5471975424 -1.9132229549810362588916172
12 -0.5471975424 -1.5471975424 -1.9132229549810362588916172
-0.54719755264 -1.54719755264 -1.9132229549810363893093655
13 -0.54719755264 -1.54719755264 -1.9132229549810363893093655
-0.547197550592 -1.547197550592 -1.9132229549810363922848065
14 -0.547197550592 -1.547197550592 -1.9132229549810363922848065
-0.5471975514112 -1.5471975514112 -1.9132229549810363928381695
15 -0.5471975514112 -1.5471975514112 -1.9132229549810363928381695
-0.5471975510016 -1.5471975510016 -1.9132229549810363928520779
16 -0.5471975510016 -1.5471975510016 -1.9132229549810363928520779
-0.54719755116544 -1.54719755116544 -1.9132229549810363929162561
17 -0.54719755116544 -1.54719755116544 -1.9132229549810363929162561
-0.547197551198208 -1.547197551198208 -1.9132229549810363929179331
18 -0.547197551198208 -1.547197551198208 -1.9132229549810363929179331
-0.547197551198208 -1.54719755119755264 -1.9132229549810363929179344
19 -0.547197551198208 -1.54719755119755264 -1.9132229549810363929179344
-0.54719755119755264 -1.54719755119755264 -1.9132229549810363929179361
20 -0.54719755119755264 -1.54719755119755264 -1.9132229549810363929179361
-0.54719755119755264 -1.54719755119689728 -1.9132229549810363929179365
21 -0.54719755119755264 -1.54719755119689728 -1.9132229549810363929179365
-0.54719755119689728 -1.54719755119689728 -1.9132229549810363929179375
22 -0.54719755119689728 -1.54719755119689728 -1.9132229549810363929179375
-0.54719755119689728 -1.547197551196766208 -1.9132229549810363929179375
23 -0.54719755119689728 -1.547197551196766208 -1.9132229549810363929179375
-0.547197551196766208 -1.547197551196766208 -1.9132229549810363929179376
24 -0.547197551196766208 -1.547197551196766208 -1.9132229549810363929179376
-0.547197551196766208 -1.547197551196635136 -1.9132229549810363929179376
25 -0.547197551196766208 -1.547197551196635136 -1.9132229549810363929179376
-0.547197551196635136 -1.547197551196635136 -1.9132229549810363929179376
26 -0.547197551196635136 -1.547197551196635136 -1.9132229549810363929179376
-0.547197551196635136 -1.5471975511966089216 -1.9132229549810363929179376
27 -0.547197551196635136 -1.5471975511966089216 -1.9132229549810363929179376
-0.5471975511966089216 -1.5471975511966089216 -1.9132229549810363929179376
28 -0.5471975511966089216 -1.5471975511966089216 -1.9132229549810363929179376
-0.5471975511966089216 -1.54719755119660367872 -1.9132229549810363929179376
29 -0.5471975511966089216 -1.54719755119660367872 -1.9132229549810363929179376
-0.54719755119660367872 -1.54719755119660367872 -1.9132229549810363929179376
30 -0.54719755119660367872 -1.54719755119660367872 -1.9132229549810363929179376
-0.54719755119660367872 -1.54719755119659843584 -1.9132229549810363929179376
31 -0.54719755119660367872 -1.54719755119659843584 -1.9132229549810363929179376
-0.54719755119659843584 -1.54719755119659843584 -1.9132229549810363929179376
32 -0.54719755119659843584 -1.54719755119659843584 -1.9132229549810363929179376
-0.547197551196597387264 -1.547197551196597387264 -1.9132229549810363929179376
33 -0.547197551196597387264 -1.547197551196597387264 -1.9132229549810363929179376
-0.5471975511965978066944 -1.5471975511965978066944 -1.9132229549810363929179376
34 -0.5471975511965978066944 -1.5471975511965978066944 -1.9132229549810363929179376
-0.5471975511965978066944 -1.54719755119659776475136 -1.9132229549810363929179376
35 -0.5471975511965978066944 -1.54719755119659776475136 -1.9132229549810363929179376
-0.54719755119659776475136 -1.54719755119659776475136 -1.9132229549810363929179376
36 -0.54719755119659776475136 -1.54719755119659776475136 -1.9132229549810363929179376
-0.54719755119659776475136 -1.547197551196597756362752 -1.9132229549810363929179376
37 -0.54719755119659776475136 -1.547197551196597756362752 -1.9132229549810363929179376
-0.547197551196597756362752 -1.547197551196597756362752 -1.9132229549810363929179376
38 -0.547197551196597756362752 -1.547197551196597756362752 -1.9132229549810363929179376
-0.547197551196597756362752 -1.547197551196597747974144 -1.9132229549810363929179376
39 -0.547197551196597756362752 -1.547197551196597747974144 -1.9132229549810363929179376
-0.547197551196597747974144 -1.547197551196597747974144 -1.9132229549810363929179376
40 -0.547197551196597747974144 -1.547197551196597747974144 -1.9132229549810363929179376
-0.547197551196597747974144 -1.5471975511965977462964224 -1.9132229549810363929179376
41 -0.547197551196597747974144 -1.5471975511965977462964224 -1.9132229549810363929179376
-0.5471975511965977462964224 -1.5471975511965977462964224 -1.9132229549810363929179376
42 -0.5471975511965977462964224 -1.5471975511965977462964224 -1.9132229549810363929179376
-0.5471975511965977462964224 -1.5471975511965977462293135 -1.9132229549810363929179376
43 -0.5471975511965977462964224 -1.5471975511965977462293135 -1.9132229549810363929179376
-0.5471975511965977462293135 -1.5471975511965977462293135 -1.9132229549810363929179376
44 -0.5471975511965977462293135 -1.5471975511965977462293135 -1.9132229549810363929179376
-0.5471975511965977462293135 -1.5471975511965977461622047 -1.9132229549810363929179376
45 -0.5471975511965977462293135 -1.5471975511965977461622047 -1.9132229549810363929179376
-0.5471975511965977461622047 -1.5471975511965977461622047 -1.9132229549810363929179376
46 -0.5471975511965977461622047 -1.5471975511965977461622047 -1.9132229549810363929179376
-0.5471975511965977461487829 -1.5471975511965977461487829 -1.9132229549810363929179376
47 -0.5471975511965977461487829 -1.5471975511965977461487829 -1.9132229549810363929179376
-0.5471975511965977461541516 -1.5471975511965977461541516 -1.9132229549810363929179376
48 -0.5471975511965977461541516 -1.5471975511965977461541516 -1.9132229549810363929179376
-0.547197551196597746154259 -1.547197551196597746154259 -1.9132229549810363929179376
49 -0.547197551196597746154259 -1.547197551196597746154259 -1.9132229549810363929179376
-0.547197551196597746154259 -1.5471975511965977461542375 -1.9132229549810363929179376
50 -0.547197551196597746154259 -1.5471975511965977461542375 -1.9132229549810363929179376
-0.5471975511965977461542375 -1.5471975511965977461542375 -1.9132229549810363929179376
51 -0.5471975511965977461542375 -1.5471975511965977461542375 -1.9132229549810363929179376
-0.5471975511965977461542375 -1.547197551196597746154216 -1.9132229549810363929179376
52 -0.5471975511965977461542375 -1.547197551196597746154216 -1.9132229549810363929179376
-0.547197551196597746154216 -1.547197551196597746154216 -1.9132229549810363929179376
53 -0.547197551196597746154216 -1.547197551196597746154216 -1.9132229549810363929179376
-0.547197551196597746154216 -1.5471975511965977461542152 -1.9132229549810363929179376
54 -0.547197551196597746154216 -1.5471975511965977461542152 -1.9132229549810363929179376
-0.5471975511965977461542152 -1.5471975511965977461542152 -1.9132229549810363929179376
55 -0.5471975511965977461542152 -1.5471975511965977461542152 -1.9132229549810363929179376
-0.5471975511965977461542152 -1.5471975511965977461542143 -1.9132229549810363929179376
56 -0.5471975511965977461542152 -1.5471975511965977461542143 -1.9132229549810363929179376
-0.5471975511965977461542143 -1.5471975511965977461542143 -1.9132229549810363929179376
57 -0.5471975511965977461542143 -1.5471975511965977461542143 -1.9132229549810363929179376
-0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376
58 -0.5471975511965977461542145 -1.5471975511965977461542145 -1.9132229549810363929179376


The global minimum for f(-.54719, -1.54719) ───► -1.9132229548822735814541188
The global minimum for f(-.54719, -1.54719) ───► -1.9132229548822735814541188
The published global minimum is: -1.9133
The published global minimum is: -1.9133
</pre>
</pre>
Output note: &nbsp; the published global minimum (referenced above, as well as the function's arguments) can be found at:
Output note: &nbsp; the published global minimum (referenced above, as well as the function's arguments) can be found at: