-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheuristics-solver.jl
More file actions
120 lines (95 loc) · 3.16 KB
/
heuristics-solver.jl
File metadata and controls
120 lines (95 loc) · 3.16 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Classical TSP Heuristics wrapper
# Uses the TravelingSalesmanHeuristics.jl package
import TravelingSalesmanHeuristics as TSH
"""
solve_nearest_neighbor(tsp, timeout)
Solves a TSPLIB instance using the Nearest Neighbor heuristic.
Starting from an arbitrary city, repeatedly visits the nearest unvisited city
until all cities are visited. Simple and fast O(n^2), but typically produces
tours longer than optimal.
# Arguments
- `tsp`: the TSPLIB instance
- `timeout`: solver timeout in seconds
"""
function solve_nearest_neighbor(tsp, timeout::Int=60)
dist_matrix = Float64.(tsp.weights)
t_start = time()
try
tour, cost = TSH.nearest_neighbor(dist_matrix)
elapsed = time() - t_start
return cost, elapsed
catch e
println("Nearest Neighbor error: $e")
return nothing, nothing
end
end
"""
solve_2opt(tsp, timeout)
Solves a TSPLIB instance using 2-opt local search.
The 2-opt algorithm iteratively removes two edges and reconnects the tour in
the only other possible way, accepting improvements until no beneficial swap
remains. Starting from a Nearest Neighbor tour, this typically reduces tour
length by 5-10%.
# Arguments
- `tsp`: the TSPLIB instance
- `timeout`: solver timeout in seconds
"""
function solve_2opt(tsp, timeout::Int=60)
dist_matrix = Float64.(tsp.weights)
t_start = time()
try
# Initialize with nearest neighbor, then improve
init_tour, _ = TSH.nearest_neighbor(dist_matrix)
tour, cost = TSH.two_opt(dist_matrix, init_tour)
elapsed = time() - t_start
return cost, elapsed
catch e
println("2-opt error: $e")
return nothing, nothing
end
end
"""
solve_simulated_annealing(tsp, timeout)
Solves a TSPLIB instance using Simulated Annealing.
Simulated Annealing is a probabilistic metaheuristic that allows uphill moves
with decreasing probability as the "temperature" cools. This helps escape local
optima that trap greedy methods like 2-opt.
# Arguments
- `tsp`: the TSPLIB instance
- `timeout`: solver timeout in seconds
"""
function solve_simulated_annealing(tsp, timeout::Int=60)
dist_matrix = Float64.(tsp.weights)
t_start = time()
try
# Simulated annealing takes only the distance matrix
tour, cost = TSH.simulated_annealing(dist_matrix)
elapsed = time() - t_start
return cost, elapsed
catch e
println("Simulated Annealing error: $e")
return nothing, nothing
end
end
"""
solve_cheapest_insertion(tsp, timeout)
Solves a TSPLIB instance using the Cheapest Insertion heuristic.
Builds a tour incrementally by repeatedly inserting the city that increases
tour length the least. Starts with a small triangle and grows until all cities
are included.
# Arguments
- `tsp`: the TSPLIB instance
- `timeout`: solver timeout in seconds
"""
function solve_cheapest_insertion(tsp, timeout::Int=60)
dist_matrix = Float64.(tsp.weights)
t_start = time()
try
tour, cost = TSH.cheapest_insertion(dist_matrix)
elapsed = time() - t_start
return cost, elapsed
catch e
println("Cheapest Insertion error: $e")
return nothing, nothing
end
end