Jump to content

Particle swarm optimization: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: syntax coloured)
m (syntax highlighting fixup automation)
Line 43:
=={{header|C sharp|C#}}==
{{trans|java}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace ParticleSwarmOptimization {
Line 245:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Test Function : McCormick
Line 261:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <functional>
#include <iostream>
Line 535:
state.report("Michalewicz (2D)");
std::cout << "f(2.20, 1.57) : " << michalewicz({ 2.2, 1.57 }) << '\n';
}</langsyntaxhighlight>
{{out}}
<pre>Test Function : McCormick
Line 551:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight Dlang="d">import std.math;
import std.random;
import std.stdio;
Line 716:
state.report("Michalewicz (2D)");
writefln("f(2.20, 1.57) : %.16f", michalewicz([2.2, 1.57]));
}</langsyntaxhighlight>
{{out}}
<pre>Test Function : McCormick
Line 732:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 888:
st.report("Michalewicz (2D)")
fmt.Println("f(2.20, 1.57) :", michalewicz([]float64{2.2, 1.57}))
</syntaxhighlight>
</lang>
 
{{out}}
Line 907:
 
=={{header|J}}==
<langsyntaxhighlight Jlang="j">load 'format/printf'
 
pso_init =: verb define
Line 951:
)
 
reportState=: 'Iteration: %j\nGlobalBestPosition: %j\nGlobalBestValue: %j\n' printf 3&{.</langsyntaxhighlight>
Apply to McCormick Function:<langsyntaxhighlight Jlang="j"> require 'trig'
mccormick =: sin@(+/) + *:@(-/) + 1 + _1.5 2.5 +/@:* ]
 
Line 965:
Iteration: 40
GlobalBestPosition: _0.547399 _1.54698
GlobalBestValue: _1.91322</langsyntaxhighlight>
Apply to Michalewicz Function:
<langsyntaxhighlight Jlang="j"> michalewicz =: 3 : '- +/ (sin y) * 20 ^~ sin (>: i. #y) * (*:y) % pi'
michalewicz =: [: -@(+/) sin * 20 ^~ sin@(pi %~ >:@i.@# * *:) NB. tacit equivalent
Line 980:
Iteration: 30
GlobalBestPosition: 2.20296 1.57083
GlobalBestValue: _1.8013</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
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}));
}
}</langsyntaxhighlight>
{{out}}
<pre>Test Function : McCormick
Line 1,195:
Translation of [[Particle_Swarm_Optimization#J|J]].
 
<langsyntaxhighlight JavaScriptlang="javascript">function pso_init(y) {
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);</langsyntaxhighlight>
 
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
GlobalBestPosition: Infinity
Line 1,317:
Iteration: 40
GlobalBestPosition: -0.5134004259016365,-1.5512442672625184
GlobalBestValue: -1.9114053788600853</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Optim
 
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]))
</langsyntaxhighlight>{{out}}
<pre>
Results of Optimization Algorithm
Line 1,382:
=={{header|Kotlin}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
import java.util.Random
Line 1,538:
state.report("Michalewicz (2D)")
println("f(2.20, 1.57) : ${michalewicz(doubleArrayOf(2.2, 1.57))}")
}</langsyntaxhighlight>
 
Sample output:
Line 1,557:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import math, random, sequtils, sugar
 
type
Line 1,696:
state = iterate(michalewicz, 30, state)
state.report("Michalewicz (2D)")
echo "f(2.20, 1.57): ", michalewicz(@[2.2, 1.57])</langsyntaxhighlight>
 
{{out}}
Line 1,712:
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX ---------------------------------------------------------------
* 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</langsyntaxhighlight>
{{out}}
<pre>-1.5 -2.5 -1.243197504692072
Line 1,767:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,877:
) );
%state = %{pso(\&michalewicz, %state)} for 1 .. 30;
report('Michalewicz', %state);</langsyntaxhighlight>
{{out}}
<pre>McCormick
Line 1,889:
=={{header|Phix}}==
{{trans|Kotlin}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,045:
=={{header|Python}}==
{{trans|D}}
<langsyntaxhighlight lang="python">import math
import random
 
Line 2,176:
print "f(2.20, 1.57) : %.16f" % (michalewicz([2.2, 1.57]))
 
main()</langsyntaxhighlight>
{{out}}
<pre>Test Function : McCormick
Line 2,191:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket/base
(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 #:> <)</langsyntaxhighlight>
{{out}}
 
Line 2,305:
(formerly Perl 6)
{{trans|J (via Javascript, Kotlin, D, Python)}}
<syntaxhighlight lang="raku" perl6line>sub pso-init (%y) {
my $d = @(%y{'min'});
my $n = %y{'n'};
Line 2,403:
} );
%state = pso(&michalewicz, %state) for 1 .. 30;
report 'Michalewicz', %state;</langsyntaxhighlight>
{{out}}
<pre>McCormick
Line 2,428:
 
Note that REXX uses decimal floating point, not binary.
<langsyntaxhighlight 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.*/
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</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,535:
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
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]))")</langsyntaxhighlight>
 
{{out}}
10,327

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.