Simulated annealing: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 98:
 
 
<langsyntaxhighlight lang="ada">----------------------------------------------------------------------
--
-- The Rosetta Code simulated annealing task in Ada.
Line 508:
end simanneal;
 
----------------------------------------------------------------------</langsyntaxhighlight>
 
 
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.
 
<langsyntaxhighlight lang="c">#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Line 824:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 866:
=={{header|C++}}==
'''Compiler:''' [[MSVC]] (19.27.29111 for x64)
<langsyntaxhighlight lang="c++">
#include<array>
#include<utility>
Line 1,039:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,067:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'math)
;; distances
Line 1,151:
(printf "E(s_final) %d" Emin)
(writeln 'Path s))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,183:
 
 
<langsyntaxhighlight lang="fortran">module simanneal_support
implicit none
 
Line 1,526:
write (*, 10)
 
end program simanneal</langsyntaxhighlight>
 
 
Line 1,568:
=={{header|Go}}==
{{trans|zkl}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,682:
func main() {
sa(1e6, 1)
}</langsyntaxhighlight>
 
{{out}}
Line 1,721:
 
 
<langsyntaxhighlight lang="icon">link printf
link random
 
Line 1,899:
}
return path
end</langsyntaxhighlight>
 
{{out}}
Line 1,943:
Implementation:
 
<langsyntaxhighlight Jlang="j">dist=: +/&.:*:@:-"1/~10 10#:i.100
 
satsp=:4 :0
Line 1,968:
end.
0,s,0
)</langsyntaxhighlight>
 
Notes:
Line 1,984:
Sample run:
 
<langsyntaxhighlight Jlang="j"> 1e6 satsp dist
0 1 538.409
100000 0.9 174.525
Line 1,996:
900000 0.1 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</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 2,002:
 
'''Module''':
<langsyntaxhighlight lang="julia">module TravelingSalesman
 
using Random, Printf
Line 2,077:
end
 
end # module TravelingSalesman</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight 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)
 
TravelingSalesman.findpath(_citydist, 1_000_000, 1)</langsyntaxhighlight>
 
{{out}}
Line 2,126:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import math, random, sequtils, strformat
 
const
Line 2,208:
echo fmt"path: {s}"
 
main()</langsyntaxhighlight>
 
Compile and run: <pre>nim c -r -d:release --opt:speed travel_sa.nim</pre>
Line 2,230:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use utf8;
use strict;
use warnings;
Line 2,308:
printf "@{['%4d' x 20]}\n", @s[$l*20 .. ($l+1)*20 - 1];
}
printf " 0\n";</langsyntaxhighlight>
{{out}}
<pre>k: 0 T: 1.0 Es: 519.2
Line 2,331:
=={{header|Phix}}==
{{trans|zkl}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
Line 2,412:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,437:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require racket/fixnum)
Line 2,550:
 
(module+ main
(Simulated-annealing))</langsyntaxhighlight>
{{out}}
<pre>T:1 E:552.4249706051347
Line 2,568:
(formerly Perl 6)
{{trans|Go}}
<syntaxhighlight lang="raku" perl6line># simulation setup
my \cities = 100; # number of cities
my \kmax = 1e6; # iterations to run
Line 2,622:
 
say "\nE(s_final): " ~ E-min-global.fmt('%.1f');
say "Path:\n" ~ s».fmt('%2d').rotor(20,:partial).join: "\n";</langsyntaxhighlight>
{{out}}
<pre>k: 0 T: 1.0 Es: 522.0
Line 2,651:
 
 
<langsyntaxhighlight lang="ratfor">#
# The Rosetta Code simulated annealing task, in Ratfor 77.
#
Line 2,992:
write (*, 60) pthlen (path)
write (*, 10)
end</langsyntaxhighlight>
 
{{out}}
Line 3,046:
 
 
<langsyntaxhighlight lang="scheme">(cond-expand
(r7rs)
(chicken (import r7rs)))
Line 3,247:
(display (path-length s-final))
(newline)))
(newline)</langsyntaxhighlight>
 
{{out}}
Line 3,298:
 
 
<langsyntaxhighlight lang="scheme">(cond-expand
(r7rs)
(chicken (import r7rs)))
Line 3,505:
(format #t "Final E(s): ~,5F~%" (E_s s-final))
(format #t "Final path length: ~,5F~%" (path-length s-final))
(newline)</langsyntaxhighlight>
 
 
Line 3,628:
=={{header|Sidef}}==
{{trans|Julia}}
<langsyntaxhighlight lang="ruby">module TravelingSalesman {
 
# Eₛ: length(path)
Line 3,720:
}.map(1..100)
 
TravelingSalesman::findpath(citydist, 1e6, 1)</langsyntaxhighlight>
 
{{out}}
Line 3,768:
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "/math" for Math
import "/fmt" for Fmt
Line 3,866:
}
 
sa.call(1e6, 1)</langsyntaxhighlight>
 
{{out}}
Line 3,903:
=={{header|zkl}}==
{{trans|EchoLisp}}
<langsyntaxhighlight 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;
(a-c).toFloat().hypot(b-d)
Line 3,961:
println("E(s_final) %f".fmt(Emin));
println("Path: ",s.toString(*));
}</langsyntaxhighlight>
<syntaxhighlight lang ="zkl">sa(0d1_000_000,1);</langsyntaxhighlight>
{{out}}
<pre>
10,351

edits