-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimAnel.jl
64 lines (59 loc) · 1.55 KB
/
SimAnel.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
include("tsp.jl")
function simAnel!(tsp, t0, alfa, maxit, maxitSub; sol=[], limite=50)
moves=[0.05, 0.8, 0.15]
n = tsp.dimension
T = t0
diff = 0
contad = limite
roleta = cumsum(moves)
if isempty(sol)
sol = copy(randperm(n))
end
dist = tspdist(tsp, sol)
solGlob=copy(sol)
#inicia loop temperatura reduzindo
for i = 1:maxit
#inicia loop temperatura constante
solLoc = zeros(Int64, length(sol))
for j = 1:maxitSub
solLoc .= sol#copia solucao atual
x = rand()
x = findfirst(x .<= roleta)
a = rand(1:length(sol)-1)
b = rand(a+1:length(sol))
if x == 1
swap!(solLoc, a, b)
elseif x == 2
reversao!(solLoc, a, b)
else
insercao!(solLoc, a, b)
end
diff = tspdist(tsp, solLoc) - tspdist(tsp, sol)
#if diff <= 0 || rand() <= exp(-1*(diff/T))
if rand() <= exp(-1*(diff/T))
sol .= solLoc
end
end
#solGlob .= (tspdist(tsp, sol) <= tspdist(tsp, solGlob)) ? sol : solGlob
if tspdist(tsp, sol) <= tspdist(tsp, solGlob)
solGlob .= sol
contad = limite
end
T *= alfa
i += 1
contad-=1
if(contad <= 0)
break
end
tspplot(tsp, sol)
sleep(0.08)
println(tspdist(tsp, sol))
end
sol .=solGlob
tspplot(tsp, sol)
sleep(0.08)
println("Solução encontrada: ", tspdist(tsp, sol), "metros")
println("Solução ótima: ", tsp.optimal, " metros")
println("A diferença é ", tspdist(tsp, sol) - tsp.optimal, " metros, oque representa um erro de ", (tspdist(tsp, sol)- tsp.optimal)*100/tsp.optimal,"%.")
return sol
end