Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: matago/TSPLIB.jl
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.1.2
Choose a base ref
...
head repository: matago/TSPLIB.jl
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 1 commit
  • 17 files changed
  • 1 contributor

Commits on Jul 2, 2022

  1. Fixes GEO instances. (#19)

    * Fixes GEO instances.
    
    * New tests and some minor changes.
    rafaelmartinelli authored Jul 2, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    6f77574 View commit details
7 changes: 0 additions & 7 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -5,15 +5,8 @@ version = "0.1.2"
[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Match = "7eb4fadd-790c-5f42-8a69-bfa0b872bfbf"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
julia = "1"
DataStructures = "0.18, 0.19"
Match = "1.1"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -20,19 +20,21 @@ end
```

To install:

```julia
] add TSPLIB
```

Some TSP instances in the [TSPLIB](http://elib.zib.de/pub/mp-testdata/tsp/tsplib/tsplib.html) library are preloaded. See the [list](https://github.com/matago/TSPLIB.jl/tree/master/data/TSPLIB95/tsp).

For example, to load `a280.tsp`, you can do:

```julia
tsp = readTSPLIB(:a280)
```

For custom TSP files, you can load:

```julia
tsp = readTSP(path_to_tsp_file)
```

10 changes: 10 additions & 0 deletions src/TSPLIB.jl
Original file line number Diff line number Diff line change
@@ -16,6 +16,16 @@ module TSPLIB
optimal::Float64
end

function Base.show(io::IO, data::TSP)
print(io, "TSP data $(data.name)")
print(io, " ($(data.dimension) nodes,")
if data.optimal != -1
print(io, " opt = $(data.optimal))")
else
print(io, " no optimal)")
end
end

const TSPLIB95_path = joinpath(pkgdir(TSPLIB), "data", "TSPLIB95", "tsp")

const tsp_keys = ["NAME",
4 changes: 2 additions & 2 deletions src/distances.jl
Original file line number Diff line number Diff line change
@@ -81,12 +81,12 @@ function geo(x::Vector{T},y::Vector{T}) where T<:Real
lon = coords[:,2]

for i in 1:nsz, j in 1:nsz
if i<=j
if i<j
q1 = cos(lon[i]-lon[j])
q2 = cos(lat[i]-lat[j])
q3 = cos(lat[i]+lat[j])
dij = RRR.*acos(0.5.*((1.0.+q1).*q2.-(1.0.-q1).*q3)).+1.0
dist[i,j] = dist[j,i] = floor(dij)
dist[i,j] = dist[j,i] = trunc(dij)
end
end
return dist
13 changes: 8 additions & 5 deletions src/reader.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@

function readTSP(path::AbstractString)
if !isfile(path)
println("File $path not found!")
return nothing
end
raw = read(path, String)
checkEOF(raw)
return _generateTSP(raw)
@@ -50,7 +54,7 @@ function keyextract(raw::T,ks::Array{T}) where T<:AbstractString
vals = Dict{T,T}()
for k in ks
idx = findfirst(k,raw)
idx != nothing && enqueue!(pq,k,extrema(idx))
idx !== nothing && enqueue!(pq,k,extrema(idx))
end
while length(pq) > 1
s_key, s_pts = peek(pq)
@@ -62,15 +66,14 @@ function keyextract(raw::T,ks::Array{T}) where T<:AbstractString
return vals
end


function explicit_weights(key::AbstractString,data::Vector{Float64})
w = @match key begin
"UPPER_DIAG_ROW" => vec2UDTbyRow(data)
"LOWER_DIAG_ROW" => vec2LDTbyRow(data)
"UPPER_DIAG_COL" => vec2UDTbyCol(data)
"LOWER_DIAG_COL" => vec2LDTbyCol(data)
"UPPER_ROW" => vec2UTbyRow(data)
"LOWER_ROW" => vec2LTbyRow(data)
"LOWER_ROW" => vec2LTbyRow(data)
"FULL_MATRIX" => vec2FMbyRow(data)
end
if !in(key,["FULL_MATRIX"])
@@ -95,8 +98,8 @@ end

function checkEOF(raw::AbstractString)
n = findlast("EOF",raw)
if n == nothing
throw("EOF not found")
if n === nothing
error("EOF not found")
end
return
end
2 changes: 2 additions & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
14 changes: 14 additions & 0 deletions test/data/dummy.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NAME: dummy
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 5
EDGE_WEIGHT_TYPE: GEO
EDGE_WEIGHT_FORMAT: FUNCTION
DISPLAY_DATA_TYPE: COORD_DISPLAY
NODE_COORD_SECTION
1 16.47 96.10
2 16.47 94.44
3 20.09 92.54
4 22.39 93.37
5 25.23 97.24
EOF
13 changes: 13 additions & 0 deletions test/data/full_matrix.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME: full_matrix
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 4
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: FULL_MATRIX
DISPLAY_DATA_TYPE: TWOD_DISPLAY
EDGE_WEIGHT_SECTION
0 107 241 190
107 0 148 137
241 148 0 374
190 137 374 0
EOF
13 changes: 13 additions & 0 deletions test/data/lower_diag_col.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME: lower_diag_col
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 4
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: LOWER_DIAG_COL
DISPLAY_DATA_TYPE: TWOD_DISPLAY
EDGE_WEIGHT_SECTION
0 107 241 190
0 148 137
0 374
0
EOF
13 changes: 13 additions & 0 deletions test/data/lower_diag_row.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME: lower_diag_row
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 4
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: LOWER_DIAG_ROW
DISPLAY_DATA_TYPE: TWOD_DISPLAY
EDGE_WEIGHT_SECTION
0
107 0
241 148 0
190 137 374 0
EOF
12 changes: 12 additions & 0 deletions test/data/lower_row.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NAME: lower_row
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 4
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: LOWER_ROW
DISPLAY_DATA_TYPE: TWOD_DISPLAY
EDGE_WEIGHT_SECTION
107
241 148
190 137 374
EOF
13 changes: 13 additions & 0 deletions test/data/no_eof.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME: no_eof
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 5
EDGE_WEIGHT_TYPE: GEO
EDGE_WEIGHT_FORMAT: FUNCTION
DISPLAY_DATA_TYPE: COORD_DISPLAY
NODE_COORD_SECTION
1 16.47 96.10
2 16.47 94.44
3 20.09 92.54
4 22.39 93.37
5 25.23 97.24
13 changes: 13 additions & 0 deletions test/data/upper_diag_col.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME: upper_diag_col
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 4
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: UPPER_DIAG_COL
DISPLAY_DATA_TYPE: TWOD_DISPLAY
EDGE_WEIGHT_SECTION
0
107 0
241 148 0
190 137 374 0
EOF
13 changes: 13 additions & 0 deletions test/data/upper_diag_row.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
NAME: upper_diag_row
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 4
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: UPPER_DIAG_ROW
DISPLAY_DATA_TYPE: TWOD_DISPLAY
EDGE_WEIGHT_SECTION
0 107 241 190
0 148 137
0 374
0
EOF
12 changes: 12 additions & 0 deletions test/data/upper_row.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
NAME: upper_row
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 4
EDGE_WEIGHT_TYPE: EXPLICIT
EDGE_WEIGHT_FORMAT: UPPER_ROW
DISPLAY_DATA_TYPE: TWOD_DISPLAY
EDGE_WEIGHT_SECTION
107 241 190
148 137
374
EOF
14 changes: 14 additions & 0 deletions test/data/wrong_weight.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
NAME: wrong_weight
TYPE: TSP
COMMENT: Dummy instance for testing
DIMENSION: 5
EDGE_WEIGHT_TYPE: BLAH
EDGE_WEIGHT_FORMAT: FUNCTION
DISPLAY_DATA_TYPE: COORD_DISPLAY
NODE_COORD_SECTION
1 16.47 96.10
2 16.47 94.44
3 20.09 92.54
4 22.39 93.37
5 25.23 97.24
EOF
113 changes: 95 additions & 18 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,99 @@
using TSPLIB
using Test

@testset "TSPLIB.jl" begin
exclude_list = [
:rl11849
:usa13509
:brd14051
:d15112
:d18512
:pla33810
:pla85900
]
for instance in keys(Optimals)
if !(instance in exclude_list)
@test readTSPLIB(instance).optimal == Optimals[instance]
end
end

tsp = readTSP(joinpath(@__DIR__, "../data/TSPLIB95/tsp/a280.tsp"))
@test tsp.name == "a280"
@testset "LoadSymbol" begin
data = readTSPLIB(:a280)
@test data.name == "a280"
@test data.dimension == 280
@test data.weight_type == "EUC_2D"
@test data.weights[3, 3] == 0.0
@test data.weights[1, 2] == 20.0
@test data.nodes[4, 2] == 141.0
@test data.Dnodes == false
@test data.optimal == 2579.0
@test_nowarn println(data)
end

@testset "ErrorSymbol" begin
data = readTSPLIB(:notaninstance)
@test data === nothing
end

@testset "LoadString" begin
data = readTSP(joinpath(pkgdir(TSPLIB), "test/data/dummy.tsp"))
@test data.name == "dummy"
@test data.dimension == 5
@test data.weight_type == "GEO"
@test data.weights[3, 3] == 0.0
@test data.weights[1, 2] == 153.0
@test data.nodes[4, 2] == 93.37
@test data.Dnodes == false
@test data.optimal == -1
@test_nowarn println(data)
end

@testset "ErrorString" begin
data = readTSP("notaninstance")
@test data === nothing
end

@testset "LoadATT" begin
data = readTSPLIB(:att48)
@test data.name == "att48"
@test data.dimension == 48
@test data.weight_type == "ATT"
@test data.weights[3, 3] == 0.0
@test data.weights[1, 2] == 1495.0
@test data.nodes[4, 2] == 841.0
@test data.Dnodes == false
@test data.optimal == 10628.0
@test_nowarn println(data)
end

@testset "LoadCEIL2D" begin
data = readTSPLIB(:dsj1000)
@test data.name == "dsj1000"
@test data.dimension == 1000
@test data.weight_type == "CEIL_2D"
@test data.weights[3, 3] == 0.0
@test data.weights[1, 2] == 709145.0
@test data.nodes[4, 2] == -96645.0
@test data.Dnodes == false
@test data.optimal == 18659688.0
@test_nowarn println(data)
end

@testset "WrongWeightType" begin
file_name = joinpath(pkgdir(TSPLIB), "test/data/wrong_weight.tsp")
expected_error = "Distance function type BLAH is not supported."
@test_throws ErrorException(expected_error) readTSP(file_name)
end

@testset "NoEOF" begin
file_name = joinpath(pkgdir(TSPLIB), "test/data/no_eof.tsp")
expected_error = "EOF not found"
@test_throws ErrorException(expected_error) readTSP(file_name)
end

@testset "FindTSP" begin
@test_nowarn findTSP(joinpath(pkgdir(TSPLIB), "test/data"))
expected_error = "Not a valid directory"
@test_throws ErrorException(expected_error) findTSP("BLAH")
end

@testset "Explicit" begin
full_matrix = readTSP(joinpath(pkgdir(TSPLIB), "test/data/full_matrix.tsp"))
lower_row = readTSP(joinpath(pkgdir(TSPLIB), "test/data/lower_row.tsp"))
upper_row = readTSP(joinpath(pkgdir(TSPLIB), "test/data/upper_row.tsp"))
lower_diag_row = readTSP(joinpath(pkgdir(TSPLIB), "test/data/lower_diag_row.tsp"))
upper_diag_row = readTSP(joinpath(pkgdir(TSPLIB), "test/data/upper_diag_row.tsp"))
lower_diag_col = readTSP(joinpath(pkgdir(TSPLIB), "test/data/lower_diag_col.tsp"))
upper_diag_col = readTSP(joinpath(pkgdir(TSPLIB), "test/data/upper_diag_col.tsp"))

@test full_matrix.weights == lower_row.weights
@test full_matrix.weights == upper_row.weights
@test full_matrix.weights == lower_diag_row.weights
@test full_matrix.weights == upper_diag_row.weights
@test full_matrix.weights == lower_diag_col.weights
@test full_matrix.weights == upper_diag_col.weights
end