Simulated annealing: Difference between revisions

Content deleted Content added
Chemoelectric (talk | contribs)
Thundergnat (talk | contribs)
m syntax highlighting fixup automation
Line 98: Line 98:




<lang ada>----------------------------------------------------------------------
<syntaxhighlight lang="ada">----------------------------------------------------------------------
--
--
-- The Rosetta Code simulated annealing task in Ada.
-- The Rosetta Code simulated annealing task in Ada.
Line 508: Line 508:
end simanneal;
end simanneal;


----------------------------------------------------------------------</lang>
----------------------------------------------------------------------</syntaxhighlight>




Line 558: Line 558:
Some might notice the calculations of random integers are done in a way that may introduce a bias, which is miniscule as long as the integer is much smaller than 2 to the 31st power. I mention this now so no one will complain about it later.
Some might notice the calculations of random integers are done in a way that may introduce a bias, which is miniscule as long as the integer is much smaller than 2 to the 31st power. I mention this now so no one will complain about it later.


<lang c>#include <math.h>
<syntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 824: Line 824:


return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 866: Line 866:
=={{header|C++}}==
=={{header|C++}}==
'''Compiler:''' [[MSVC]] (19.27.29111 for x64)
'''Compiler:''' [[MSVC]] (19.27.29111 for x64)
<lang c++>
<syntaxhighlight lang="c++">
#include<array>
#include<array>
#include<utility>
#include<utility>
Line 1,039: Line 1,039:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,067: Line 1,067:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang="scheme">
(lib 'math)
(lib 'math)
;; distances
;; distances
Line 1,151: Line 1,151:
(printf "E(s_final) %d" Emin)
(printf "E(s_final) %d" Emin)
(writeln 'Path s))
(writeln 'Path s))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,183: Line 1,183:




<lang fortran>module simanneal_support
<syntaxhighlight lang="fortran">module simanneal_support
implicit none
implicit none


Line 1,526: Line 1,526:
write (*, 10)
write (*, 10)


end program simanneal</lang>
end program simanneal</syntaxhighlight>




Line 1,568: Line 1,568:
=={{header|Go}}==
=={{header|Go}}==
{{trans|zkl}}
{{trans|zkl}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,682: Line 1,682:
func main() {
func main() {
sa(1e6, 1)
sa(1e6, 1)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,721: Line 1,721:




<lang icon>link printf
<syntaxhighlight lang="icon">link printf
link random
link random


Line 1,899: Line 1,899:
}
}
return path
return path
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 1,943: Line 1,943:
Implementation:
Implementation:


<lang J>dist=: +/&.:*:@:-"1/~10 10#:i.100
<syntaxhighlight lang="j">dist=: +/&.:*:@:-"1/~10 10#:i.100


satsp=:4 :0
satsp=:4 :0
Line 1,968: Line 1,968:
end.
end.
0,s,0
0,s,0
)</lang>
)</syntaxhighlight>


Notes:
Notes:
Line 1,984: Line 1,984:
Sample run:
Sample run:


<lang J> 1e6 satsp dist
<syntaxhighlight lang="j"> 1e6 satsp dist
0 1 538.409
0 1 538.409
100000 0.9 174.525
100000 0.9 174.525
Line 1,996: Line 1,996:
900000 0.1 101.657
900000 0.1 101.657
1e6 0 101.657
1e6 0 101.657
0 1 2 3 4 13 23 24 34 44 43 33 32 31 41 42 52 51 61 62 53 54 64 65 55 45 35 25 15 14 5 6 7 17 16 26 27 37 36 46 47 48 38 28 18 8 9 19 29 39 49 59 69 79 78 68 58 57 56 66 67 77 76 75 85 86 87 88 89 99 98 97 96 95 94 84 74 73 63 72 82 83 93 92 91 90 80 81 71 70 60 50 40 30 20 21 22 12 11 10 0</lang>
0 1 2 3 4 13 23 24 34 44 43 33 32 31 41 42 52 51 61 62 53 54 64 65 55 45 35 25 15 14 5 6 7 17 16 26 27 37 36 46 47 48 38 28 18 8 9 19 29 39 49 59 69 79 78 68 58 57 56 66 67 77 76 75 85 86 87 88 89 99 98 97 96 95 94 84 74 73 63 72 82 83 93 92 91 90 80 81 71 70 60 50 40 30 20 21 22 12 11 10 0</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
Line 2,002: Line 2,002:


'''Module''':
'''Module''':
<lang julia>module TravelingSalesman
<syntaxhighlight lang="julia">module TravelingSalesman


using Random, Printf
using Random, Printf
Line 2,077: Line 2,077:
end
end


end # module TravelingSalesman</lang>
end # module TravelingSalesman</syntaxhighlight>


'''Main''':
'''Main''':
<lang julia>distance(a, b) = sqrt(sum((a .- b) .^ 2))
<syntaxhighlight lang="julia">distance(a, b) = sqrt(sum((a .- b) .^ 2))
const _citydist = collect(distance((ci % 10, ci ÷ 10), (cj % 10, cj ÷ 10)) for ci in 1:100, cj in 1:100)
const _citydist = collect(distance((ci % 10, ci ÷ 10), (cj % 10, cj ÷ 10)) for ci in 1:100, cj in 1:100)


TravelingSalesman.findpath(_citydist, 1_000_000, 1)</lang>
TravelingSalesman.findpath(_citydist, 1_000_000, 1)</syntaxhighlight>


{{out}}
{{out}}
Line 2,126: Line 2,126:


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import math, random, sequtils, strformat
<syntaxhighlight lang="nim">import math, random, sequtils, strformat


const
const
Line 2,208: Line 2,208:
echo fmt"path: {s}"
echo fmt"path: {s}"


main()</lang>
main()</syntaxhighlight>


Compile and run: <pre>nim c -r -d:release --opt:speed travel_sa.nim</pre>
Compile and run: <pre>nim c -r -d:release --opt:speed travel_sa.nim</pre>
Line 2,230: Line 2,230:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use utf8;
<syntaxhighlight lang="perl">use utf8;
use strict;
use strict;
use warnings;
use warnings;
Line 2,308: Line 2,308:
printf "@{['%4d' x 20]}\n", @s[$l*20 .. ($l+1)*20 - 1];
printf "@{['%4d' x 20]}\n", @s[$l*20 .. ($l+1)*20 - 1];
}
}
printf " 0\n";</lang>
printf " 0\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>k: 0 T: 1.0 Es: 519.2
<pre>k: 0 T: 1.0 Es: 519.2
Line 2,331: Line 2,331:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|zkl}}
{{trans|zkl}}
<!--<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;">function</span> <span style="color: #000000;">hypot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">hypot</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span><span style="color: #0000FF;">+</span><span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 2,412: Line 2,412:
<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;">sa</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">sa</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1_000_000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,437: Line 2,437:


=={{header|Racket}}==
=={{header|Racket}}==
<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket
(require racket/fixnum)
(require racket/fixnum)
Line 2,550: Line 2,550:


(module+ main
(module+ main
(Simulated-annealing))</lang>
(Simulated-annealing))</syntaxhighlight>
{{out}}
{{out}}
<pre>T:1 E:552.4249706051347
<pre>T:1 E:552.4249706051347
Line 2,568: Line 2,568:
(formerly Perl 6)
(formerly Perl 6)
{{trans|Go}}
{{trans|Go}}
<lang perl6># simulation setup
<syntaxhighlight lang="raku" line># simulation setup
my \cities = 100; # number of cities
my \cities = 100; # number of cities
my \kmax = 1e6; # iterations to run
my \kmax = 1e6; # iterations to run
Line 2,622: Line 2,622:


say "\nE(s_final): " ~ E-min-global.fmt('%.1f');
say "\nE(s_final): " ~ E-min-global.fmt('%.1f');
say "Path:\n" ~ s».fmt('%2d').rotor(20,:partial).join: "\n";</lang>
say "Path:\n" ~ s».fmt('%2d').rotor(20,:partial).join: "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>k: 0 T: 1.0 Es: 522.0
<pre>k: 0 T: 1.0 Es: 522.0
Line 2,651: Line 2,651:




<lang ratfor>#
<syntaxhighlight lang="ratfor">#
# The Rosetta Code simulated annealing task, in Ratfor 77.
# The Rosetta Code simulated annealing task, in Ratfor 77.
#
#
Line 2,992: Line 2,992:
write (*, 60) pthlen (path)
write (*, 60) pthlen (path)
write (*, 10)
write (*, 10)
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 3,046: Line 3,046:




<lang scheme>(cond-expand
<syntaxhighlight lang="scheme">(cond-expand
(r7rs)
(r7rs)
(chicken (import r7rs)))
(chicken (import r7rs)))
Line 3,247: Line 3,247:
(display (path-length s-final))
(display (path-length s-final))
(newline)))
(newline)))
(newline)</lang>
(newline)</syntaxhighlight>


{{out}}
{{out}}
Line 3,298: Line 3,298:




<lang scheme>(cond-expand
<syntaxhighlight lang="scheme">(cond-expand
(r7rs)
(r7rs)
(chicken (import r7rs)))
(chicken (import r7rs)))
Line 3,505: Line 3,505:
(format #t "Final E(s): ~,5F~%" (E_s s-final))
(format #t "Final E(s): ~,5F~%" (E_s s-final))
(format #t "Final path length: ~,5F~%" (path-length s-final))
(format #t "Final path length: ~,5F~%" (path-length s-final))
(newline)</lang>
(newline)</syntaxhighlight>




Line 3,628: Line 3,628:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Julia}}
{{trans|Julia}}
<lang ruby>module TravelingSalesman {
<syntaxhighlight lang="ruby">module TravelingSalesman {


# Eₛ: length(path)
# Eₛ: length(path)
Line 3,720: Line 3,720:
}.map(1..100)
}.map(1..100)


TravelingSalesman::findpath(citydist, 1e6, 1)</lang>
TravelingSalesman::findpath(citydist, 1e6, 1)</syntaxhighlight>


{{out}}
{{out}}
Line 3,768: Line 3,768:
{{libheader|Wren-math}}
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "random" for Random
<syntaxhighlight lang="ecmascript">import "random" for Random
import "/math" for Math
import "/math" for Math
import "/fmt" for Fmt
import "/fmt" for Fmt
Line 3,866: Line 3,866:
}
}


sa.call(1e6, 1)</lang>
sa.call(1e6, 1)</syntaxhighlight>


{{out}}
{{out}}
Line 3,903: Line 3,903:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|EchoLisp}}
{{trans|EchoLisp}}
<lang zkl>var [const] _dists=(0d10_000).pump(List,fcn(abcd){ // two points (a,b) & (c,d), calc distance
<syntaxhighlight lang="zkl">var [const] _dists=(0d10_000).pump(List,fcn(abcd){ // two points (a,b) & (c,d), calc distance
ab,cd,a,b,c,d:=abcd/100, abcd%100, ab/10,ab%10, cd/10,cd%10;
ab,cd,a,b,c,d:=abcd/100, abcd%100, ab/10,ab%10, cd/10,cd%10;
(a-c).toFloat().hypot(b-d)
(a-c).toFloat().hypot(b-d)
Line 3,961: Line 3,961:
println("E(s_final) %f".fmt(Emin));
println("E(s_final) %f".fmt(Emin));
println("Path: ",s.toString(*));
println("Path: ",s.toString(*));
}</lang>
}</syntaxhighlight>
<lang zkl>sa(0d1_000_000,1);</lang>
<syntaxhighlight lang="zkl">sa(0d1_000_000,1);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>