Simulated annealing: Difference between revisions

m
→‎{{header|Phix}}: pp_StrFmt tweaks, removed b_a_exp(), and exp(x,-inf) attempt
m (→‎{{header|Perl}}: Fix link: Perl 6 --> Raku)
m (→‎{{header|Phix}}: pp_StrFmt tweaks, removed b_a_exp(), and exp(x,-inf) attempt)
Line 749:
=={{header|Phix}}==
{{trans|zkl}}
Note that the standard builtin exp() suffered occasional overflows, so this uses b_a_exp() from bigatom.e, but
it does make it much slower.
<lang Phix>function hypot(atom a,b) return sqrt(a*a+b*b) end function
 
Line 778 ⟶ 776:
function T(integer k, kmax, kT) return (1-k/kmax)*kT end function
 
include bigatom.e -- (just for b_a_exp())
-- deltaE = Es_new - Es_old > 0
-- probability to move if deltaE > 0, -->0 when T --> 0 (frozen state)
function P(atom deltaE, integer k, kmax, kT) return b_a_expexp(-deltaE/T(k,kmax,kT)) end function
-- deltaE from path ( .. a u b .. c v d ..) to (.. a v b ... c u d ..)
Line 812 ⟶ 808:
if mod(k,kmax/10)=0 then
printf(1,"k:%,10d T: %8.4f Es: %8.4f\n",{k,T(k,kmax,kT),Es(s)})
if k=kmax then exit end if -- avoid exp(x,-inf)
end if
integer u = rand(99), -- city index 1 99
Line 828 ⟶ 825:
printf(1,"E(s_final) %f\n",Emin)
printf(1,"Path:\n")
pp(s,{pp_IntFmt,"%2d",pp_StrFmtpp_IntCh,-2false})
end procedure
sa(1_000_000,1)</lang>
7,803

edits