Particle swarm optimization: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 43: Line 43:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|java}}
{{trans|java}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;


namespace ParticleSwarmOptimization {
namespace ParticleSwarmOptimization {
Line 245: Line 245:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Test Function : McCormick
<pre>Test Function : McCormick
Line 261: Line 261:
=={{header|C++}}==
=={{header|C++}}==
{{trans|D}}
{{trans|D}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <functional>
#include <functional>
#include <iostream>
#include <iostream>
Line 535: Line 535:
state.report("Michalewicz (2D)");
state.report("Michalewicz (2D)");
std::cout << "f(2.20, 1.57) : " << michalewicz({ 2.2, 1.57 }) << '\n';
std::cout << "f(2.20, 1.57) : " << michalewicz({ 2.2, 1.57 }) << '\n';
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Test Function : McCormick
<pre>Test Function : McCormick
Line 551: Line 551:
=={{header|D}}==
=={{header|D}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang D>import std.math;
<syntaxhighlight lang="d">import std.math;
import std.random;
import std.random;
import std.stdio;
import std.stdio;
Line 716: Line 716:
state.report("Michalewicz (2D)");
state.report("Michalewicz (2D)");
writefln("f(2.20, 1.57) : %.16f", michalewicz([2.2, 1.57]));
writefln("f(2.20, 1.57) : %.16f", michalewicz([2.2, 1.57]));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Test Function : McCormick
<pre>Test Function : McCormick
Line 732: Line 732:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 888: Line 888:
st.report("Michalewicz (2D)")
st.report("Michalewicz (2D)")
fmt.Println("f(2.20, 1.57) :", michalewicz([]float64{2.2, 1.57}))
fmt.Println("f(2.20, 1.57) :", michalewicz([]float64{2.2, 1.57}))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 907: Line 907:


=={{header|J}}==
=={{header|J}}==
<lang J>load 'format/printf'
<syntaxhighlight lang="j">load 'format/printf'


pso_init =: verb define
pso_init =: verb define
Line 951: Line 951:
)
)


reportState=: 'Iteration: %j\nGlobalBestPosition: %j\nGlobalBestValue: %j\n' printf 3&{.</lang>
reportState=: 'Iteration: %j\nGlobalBestPosition: %j\nGlobalBestValue: %j\n' printf 3&{.</syntaxhighlight>
Apply to McCormick Function:<lang J> require 'trig'
Apply to McCormick Function:<syntaxhighlight lang="j"> require 'trig'
mccormick =: sin@(+/) + *:@(-/) + 1 + _1.5 2.5 +/@:* ]
mccormick =: sin@(+/) + *:@(-/) + 1 + _1.5 2.5 +/@:* ]


Line 965: Line 965:
Iteration: 40
Iteration: 40
GlobalBestPosition: _0.547399 _1.54698
GlobalBestPosition: _0.547399 _1.54698
GlobalBestValue: _1.91322</lang>
GlobalBestValue: _1.91322</syntaxhighlight>
Apply to Michalewicz Function:
Apply to Michalewicz Function:
<lang J> michalewicz =: 3 : '- +/ (sin y) * 20 ^~ sin (>: i. #y) * (*:y) % pi'
<syntaxhighlight lang="j"> michalewicz =: 3 : '- +/ (sin y) * 20 ^~ sin (>: i. #y) * (*:y) % pi'
michalewicz =: [: -@(+/) sin * 20 ^~ sin@(pi %~ >:@i.@# * *:) NB. tacit equivalent
michalewicz =: [: -@(+/) sin * 20 ^~ sin@(pi %~ >:@i.@# * *:) NB. tacit equivalent
Line 980: Line 980:
Iteration: 30
Iteration: 30
GlobalBestPosition: 2.20296 1.57083
GlobalBestPosition: 2.20296 1.57083
GlobalBestValue: _1.8013</lang>
GlobalBestValue: _1.8013</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang java>import java.util.Arrays;
<syntaxhighlight lang="java">import java.util.Arrays;
import java.util.Objects;
import java.util.Objects;
import java.util.Random;
import java.util.Random;
Line 1,177: Line 1,177:
System.out.printf("f(2.20, 1.57) : %.15f\n", michalewicz(new double[]{2.20, 1.57}));
System.out.printf("f(2.20, 1.57) : %.15f\n", michalewicz(new double[]{2.20, 1.57}));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Test Function : McCormick
<pre>Test Function : McCormick
Line 1,195: Line 1,195:
Translation of [[Particle_Swarm_Optimization#J|J]].
Translation of [[Particle_Swarm_Optimization#J|J]].


<lang JavaScript>function pso_init(y) {
<syntaxhighlight lang="javascript">function pso_init(y) {
var nDims= y.min.length;
var nDims= y.min.length;
var pos=[], vel=[], bpos=[], bval=[];
var pos=[], vel=[], bpos=[], bval=[];
Line 1,306: Line 1,306:
state= repeat(function(y){return pso(mccormick,y)}, 40, state);
state= repeat(function(y){return pso(mccormick,y)}, 40, state);


reportState(state);</lang>
reportState(state);</syntaxhighlight>


Example displayed result (random numbers are involved so there will be a bit of variance between repeated runs:
Example displayed result (random numbers are involved so there will be a bit of variance between repeated runs:


<syntaxhighlight lang="javascript">
<lang Javascript>
Iteration: 0
Iteration: 0
GlobalBestPosition: Infinity
GlobalBestPosition: Infinity
Line 1,317: Line 1,317:
Iteration: 40
Iteration: 40
GlobalBestPosition: -0.5134004259016365,-1.5512442672625184
GlobalBestPosition: -0.5134004259016365,-1.5512442672625184
GlobalBestValue: -1.9114053788600853</lang>
GlobalBestValue: -1.9114053788600853</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Optim
<syntaxhighlight lang="julia">using Optim


const mcclow = [-1.5, -3.0]
const mcclow = [-1.5, -3.0]
Line 1,339: Line 1,339:
println(optimize(michalewicz, x0, ParticleSwarm(;lower=miclow, upper=micupp, n_particles=npar[2])))
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]))
@time optimize(michalewicz, x0, ParticleSwarm(;lower=miclow, upper=micupp, n_particles=npar[2]))
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Results of Optimization Algorithm
Results of Optimization Algorithm
Line 1,382: Line 1,382:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|JavaScript}}
{{trans|JavaScript}}
<lang scala>// version 1.1.51
<syntaxhighlight lang="scala">// version 1.1.51


import java.util.Random
import java.util.Random
Line 1,538: Line 1,538:
state.report("Michalewicz (2D)")
state.report("Michalewicz (2D)")
println("f(2.20, 1.57) : ${michalewicz(doubleArrayOf(2.2, 1.57))}")
println("f(2.20, 1.57) : ${michalewicz(doubleArrayOf(2.2, 1.57))}")
}</lang>
}</syntaxhighlight>


Sample output:
Sample output:
Line 1,557: Line 1,557:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>import math, random, sequtils, sugar
<syntaxhighlight lang="nim">import math, random, sequtils, sugar


type
type
Line 1,696: Line 1,696:
state = iterate(michalewicz, 30, state)
state = iterate(michalewicz, 30, state)
state.report("Michalewicz (2D)")
state.report("Michalewicz (2D)")
echo "f(2.20, 1.57): ", michalewicz(@[2.2, 1.57])</lang>
echo "f(2.20, 1.57): ", michalewicz(@[2.2, 1.57])</syntaxhighlight>


{{out}}
{{out}}
Line 1,712: Line 1,712:


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang oorexx>/* REXX ---------------------------------------------------------------
<syntaxhighlight lang="oorexx">/* REXX ---------------------------------------------------------------
* Test for McCormick function
* Test for McCormick function
*--------------------------------------------------------------------*/
*--------------------------------------------------------------------*/
Line 1,747: Line 1,747:
res=rxcalcsin(x+y,16,'R')+(x-y)**2-1.5*x+2.5*y+1
res=rxcalcsin(x+y,16,'R')+(x-y)**2-1.5*x+2.5*y+1
Return res
Return res
::requires rxmath library</lang>
::requires rxmath library</syntaxhighlight>
{{out}}
{{out}}
<pre>-1.5 -2.5 -1.243197504692072
<pre>-1.5 -2.5 -1.243197504692072
Line 1,767: Line 1,767:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,877: Line 1,877:
) );
) );
%state = %{pso(\&michalewicz, %state)} for 1 .. 30;
%state = %{pso(\&michalewicz, %state)} for 1 .. 30;
report('Michalewicz', %state);</lang>
report('Michalewicz', %state);</syntaxhighlight>
{{out}}
{{out}}
<pre>McCormick
<pre>McCormick
Line 1,889: Line 1,889:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: Line 2,027:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,045: Line 2,045:
=={{header|Python}}==
=={{header|Python}}==
{{trans|D}}
{{trans|D}}
<lang python>import math
<syntaxhighlight lang="python">import math
import random
import random


Line 2,176: Line 2,176:
print "f(2.20, 1.57) : %.16f" % (michalewicz([2.2, 1.57]))
print "f(2.20, 1.57) : %.16f" % (michalewicz([2.2, 1.57]))


main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre>Test Function : McCormick
<pre>Test Function : McCormick
Line 2,191: Line 2,191:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>#lang racket/base
<syntaxhighlight lang="racket">#lang racket/base
(require racket/list racket/math)
(require racket/list racket/math)


Line 2,269: Line 2,269:
(PSO (Michalewitz 5) 1000 30 (box 0) (box pi) #:ω 0.3 #:φ_p 0.3 #:φ_g 0.3 #:> <)
(PSO (Michalewitz 5) 1000 30 (box 0) (box pi) #:ω 0.3 #:φ_p 0.3 #:φ_g 0.3 #:> <)
(displayln "Michalewitz 10d [-9.66015]")
(displayln "Michalewitz 10d [-9.66015]")
(PSO (Michalewitz 10) 1000 30 (box 0) (box pi) #:ω 0.3 #:φ_p 0.3 #:φ_g 0.3 #:> <)</lang>
(PSO (Michalewitz 10) 1000 30 (box 0) (box pi) #:ω 0.3 #:φ_p 0.3 #:φ_g 0.3 #:> <)</syntaxhighlight>
{{out}}
{{out}}


Line 2,305: Line 2,305:
(formerly Perl 6)
(formerly Perl 6)
{{trans|J (via Javascript, Kotlin, D, Python)}}
{{trans|J (via Javascript, Kotlin, D, Python)}}
<lang perl6>sub pso-init (%y) {
<syntaxhighlight lang="raku" line>sub pso-init (%y) {
my $d = @(%y{'min'});
my $d = @(%y{'min'});
my $n = %y{'n'};
my $n = %y{'n'};
Line 2,403: Line 2,403:
} );
} );
%state = pso(&michalewicz, %state) for 1 .. 30;
%state = pso(&michalewicz, %state) for 1 .. 30;
report 'Michalewicz', %state;</lang>
report 'Michalewicz', %state;</syntaxhighlight>
{{out}}
{{out}}
<pre>McCormick
<pre>McCormick
Line 2,428: Line 2,428:


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.*/
<syntaxhighlight lang="rexx">/*REXX program calculates Particle Swarm Optimization as it migrates through a solution.*/
numeric digits length( pi() ) - length(.) /*use the number of decimal digs in pi.*/
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*/
parse arg x y d #part sDigs . /*obtain optional arguments from the CL*/
Line 2,463: Line 2,463:
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; 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>
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</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 2,535: Line 2,535:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-dynamic}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/dynamic" for Tuple
import "/dynamic" for Tuple


Line 2,670: Line 2,670:
state = iterate.call(michalewicz, 30, state)
state = iterate.call(michalewicz, 30, state)
report.call(state, "Michalewicz (2D)")
report.call(state, "Michalewicz (2D)")
System.print("f(2.20, 1.57) : %(michalewicz.call([2.2, 1.57]))")</lang>
System.print("f(2.20, 1.57) : %(michalewicz.call([2.2, 1.57]))")</syntaxhighlight>


{{out}}
{{out}}