forked from PhoenixSmaug/TSP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.jl
More file actions
75 lines (59 loc) · 2.29 KB
/
Copy pathmain.jl
File metadata and controls
75 lines (59 loc) · 2.29 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
using TSPLIB
using Statistics
include("integer-programming.jl")
include("branch-and-bound.jl")
include("dynamic-programming.jl")
# header parsing inconsistent and TSP constructor fails for large instances like pla85900.tsp, so simply count vertices by line
function get_instance_size(path::String)
count = 0
open(path) do file
for line in eachline(file)
if occursin(r"^\s*\d", line)
count += 1
end
end
end
return count
end
function get_small_instances(limit::Int=25)
tsp_files = filter(file -> endswith(file, ".tsp"), readdir(TSPLIB.TSPLIB95_path, join=true))
small_files = String[]
for path in tsp_files
size_est = get_instance_size(path)
if size_est > 0 && size_est <= limit
push!(small_files, path)
end
end
return small_files
end
function benchmark()
files = get_small_instances(25)
total = length(files)
timeout = 60 # seconds
println("Found $total small instances")
for (i, path) in enumerate(files)
# Load
tsp = readTSP(path)
name = basename(path)
println("\nRunning instance $name with optimum $(tsp.optimal) ($i/$total):")
# BnB
println("Starting Branch and Bound (timeout $(timeout)s):")
val_bnb, t_bnb = solve_bnb(tsp, timeout)
res_bnb = (val_bnb === nothing) ? "Timeout" : string(round(val_bnb, digits=0))
time_bnb = (t_bnb === nothing) ? "-" : string(round(t_bnb, digits=3))
println("Results: $res_bnb ($(time_bnb)s)")
# DP
println("Starting Dynamic Programming (timeout $(timeout)s):")
val_dp, t_dp = solve_dp(tsp, timeout)
res_dp = (val_dp === nothing) ? "Timeout" : string(round(val_dp, digits=0))
time_dp = (t_dp === nothing) ? "-" : string(round(t_dp, digits=3))
println("Results: $res_dp ($(time_dp)s)")
# ILP
println("Starting Integer Linear Programming (timeout $(timeout)s):")
val_ilp, t_ilp = solve_ilp(tsp, timeout)
res_ilp = (val_ilp === nothing) ? "Timeout" : string(round(val_ilp, digits=0))
time_ilp = (t_ilp === nothing) ? "-" : string(round(t_ilp, digits=3))
println("Results: $res_ilp ($(time_ilp)s)")
end
end
benchmark()