Particle swarm optimization: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|Phix}}: syntax coloured) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 43:
=={{header|C sharp|C#}}==
{{trans|java}}
<
namespace ParticleSwarmOptimization {
Line 245:
}
}
}</
{{out}}
<pre>Test Function : McCormick
Line 261:
=={{header|C++}}==
{{trans|D}}
<
#include <functional>
#include <iostream>
Line 535:
state.report("Michalewicz (2D)");
std::cout << "f(2.20, 1.57) : " << michalewicz({ 2.2, 1.57 }) << '\n';
}</
{{out}}
<pre>Test Function : McCormick
Line 551:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.random;
import std.stdio;
Line 716:
state.report("Michalewicz (2D)");
writefln("f(2.20, 1.57) : %.16f", michalewicz([2.2, 1.57]));
}</
{{out}}
<pre>Test Function : McCormick
Line 732:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 888:
st.report("Michalewicz (2D)")
fmt.Println("f(2.20, 1.57) :", michalewicz([]float64{2.2, 1.57}))
</syntaxhighlight>
{{out}}
Line 907:
=={{header|J}}==
<
pso_init =: verb define
Line 951:
)
reportState=: 'Iteration: %j\nGlobalBestPosition: %j\nGlobalBestValue: %j\n' printf 3&{.</
Apply to McCormick Function:<
mccormick =: sin@(+/) + *:@(-/) + 1 + _1.5 2.5 +/@:* ]
Line 965:
Iteration: 40
GlobalBestPosition: _0.547399 _1.54698
GlobalBestValue: _1.91322</
Apply to Michalewicz Function:
<
michalewicz =: [: -@(+/) sin * 20 ^~ sin@(pi %~ >:@i.@# * *:) NB. tacit equivalent
Line 980:
Iteration: 30
GlobalBestPosition: 2.20296 1.57083
GlobalBestValue: _1.8013</
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.Objects;
import java.util.Random;
Line 1,177:
System.out.printf("f(2.20, 1.57) : %.15f\n", michalewicz(new double[]{2.20, 1.57}));
}
}</
{{out}}
<pre>Test Function : McCormick
Line 1,195:
Translation of [[Particle_Swarm_Optimization#J|J]].
<
var nDims= y.min.length;
var pos=[], vel=[], bpos=[], bval=[];
Line 1,306:
state= repeat(function(y){return pso(mccormick,y)}, 40, state);
reportState(state);</
Example displayed result (random numbers are involved so there will be a bit of variance between repeated runs:
<syntaxhighlight lang="javascript">
Iteration: 0
GlobalBestPosition: Infinity
Line 1,317:
Iteration: 40
GlobalBestPosition: -0.5134004259016365,-1.5512442672625184
GlobalBestValue: -1.9114053788600853</
=={{header|Julia}}==
<
const mcclow = [-1.5, -3.0]
Line 1,339:
println(optimize(michalewicz, x0, ParticleSwarm(;lower=miclow, upper=micupp, n_particles=npar[2])))
@time optimize(michalewicz, x0, ParticleSwarm(;lower=miclow, upper=micupp, n_particles=npar[2]))
</
<pre>
Results of Optimization Algorithm
Line 1,382:
=={{header|Kotlin}}==
{{trans|JavaScript}}
<
import java.util.Random
Line 1,538:
state.report("Michalewicz (2D)")
println("f(2.20, 1.57) : ${michalewicz(doubleArrayOf(2.2, 1.57))}")
}</
Sample output:
Line 1,557:
=={{header|Nim}}==
{{trans|Kotlin}}
<
type
Line 1,696:
state = iterate(michalewicz, 30, state)
state.report("Michalewicz (2D)")
echo "f(2.20, 1.57): ", michalewicz(@[2.2, 1.57])</
{{out}}
Line 1,712:
=={{header|ooRexx}}==
<
* Test for McCormick function
*--------------------------------------------------------------------*/
Line 1,747:
res=rxcalcsin(x+y,16,'R')+(x-y)**2-1.5*x+2.5*y+1
Return res
::requires rxmath library</
{{out}}
<pre>-1.5 -2.5 -1.243197504692072
Line 1,767:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 1,877:
) );
%state = %{pso(\&michalewicz, %state)} for 1 .. 30;
report('Michalewicz', %state);</
{{out}}
<pre>McCormick
Line 1,889:
=={{header|Phix}}==
{{trans|Kotlin}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">enum</span> <span style="color: #000000;">OMEGA</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">PHIP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">PHIG</span>
Line 2,027:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</
{{out}}
<pre>
Line 2,045:
=={{header|Python}}==
{{trans|D}}
<
import random
Line 2,176:
print "f(2.20, 1.57) : %.16f" % (michalewicz([2.2, 1.57]))
main()</
{{out}}
<pre>Test Function : McCormick
Line 2,191:
=={{header|Racket}}==
<
(require racket/list racket/math)
Line 2,269:
(PSO (Michalewitz 5) 1000 30 (box 0) (box pi) #:ω 0.3 #:φ_p 0.3 #:φ_g 0.3 #:> <)
(displayln "Michalewitz 10d [-9.66015]")
(PSO (Michalewitz 10) 1000 30 (box 0) (box pi) #:ω 0.3 #:φ_p 0.3 #:φ_g 0.3 #:> <)</
{{out}}
Line 2,305:
(formerly Perl 6)
{{trans|J (via Javascript, Kotlin, D, Python)}}
<syntaxhighlight lang="raku"
my $d = @(%y{'min'});
my $n = %y{'n'};
Line 2,403:
} );
%state = pso(&michalewicz, %state) for 1 .. 30;
report 'Michalewicz', %state;</
{{out}}
<pre>McCormick
Line 2,428:
Note that REXX uses decimal floating point, not binary.
<
numeric digits length( pi() ) - length(.) /*use the number of decimal digs in pi.*/
parse arg x y d #part sDigs . /*obtain optional arguments from the CL*/
Line 2,463:
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=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</
{{out|output|text= when using the default input:}}
<pre>
Line 2,535:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<
import "/dynamic" for Tuple
Line 2,670:
state = iterate.call(michalewicz, 30, state)
report.call(state, "Michalewicz (2D)")
System.print("f(2.20, 1.57) : %(michalewicz.call([2.2, 1.57]))")</
{{out}}
|