-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from chkwon/macos
using binary executable to solve TSP
- Loading branch information
Showing
7 changed files
with
173 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
# Concorde | ||
# Concorde.jl | ||
|
||
|
||
[![Build Status](https://github.com/chkwon/Concorde.jl/workflows/CI/badge.svg?branch=master)](https://github.com/chkwon/Concorde.jl/actions?query=workflow%3ACI) | ||
[![codecov](https://codecov.io/gh/chkwon/Concorde.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/chkwon/Concorde.jl) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,83 @@ | ||
module Concorde | ||
|
||
using Random | ||
|
||
include("../deps/deps.jl") | ||
# Write your package code here. | ||
|
||
function read_solution(filepath) | ||
sol = readlines(filepath) | ||
n_nodes = sol[1] | ||
tour = parse.(Int, split(join(sol[2:end]))) .+ 1 | ||
return tour | ||
end | ||
|
||
function tour_length(tour, M) | ||
n_nodes = length(tour) | ||
len = 0 | ||
for i in 1:n_nodes | ||
j = i + 1 | ||
if i == n_nodes | ||
j = 1 | ||
end | ||
|
||
len += M[tour[i], tour[j]] | ||
end | ||
return len | ||
end | ||
|
||
function solve_tsp(dist_mtx::Matrix{Int}) | ||
n_nodes = size(dist_mtx, 1) | ||
name = randstring(10) | ||
filepath = joinpath(pwd(), name * ".tsp") | ||
lower_diag_row = Int[] | ||
for i in 1:n_nodes | ||
for j in 1:i | ||
push!(lower_diag_row, dist_mtx[i, j]) | ||
end | ||
end | ||
buf = 10 | ||
n_rows = length(lower_diag_row) / buf |> ceil |> Int | ||
rows = String[] | ||
for i in 1:n_rows | ||
s = buf * (i-1) + 1 | ||
t = min(buf * i, length(lower_diag_row)) | ||
push!(rows, join(lower_diag_row[s:t], " ")) | ||
end | ||
|
||
open(filepath, "w") do io | ||
write(io, "NAME: $(name)\n") | ||
write(io, "TYPE: TSP\n") | ||
write(io, "COMMENT: $(name)\n") | ||
write(io, "DIMENSION: $(n_nodes)\n") | ||
write(io, "EDGE_WEIGHT_TYPE: EXPLICIT\n") | ||
write(io, "EDGE_WEIGHT_FORMAT: LOWER_DIAG_ROW \n") | ||
write(io, "EDGE_WEIGHT_SECTION\n") | ||
for r in rows | ||
write(io, "$r\n") | ||
end | ||
write(io, "EOF\n") | ||
end | ||
|
||
run(`$(Concorde.CONCORDE_EXECUTABLE) $(filepath)`) | ||
|
||
sol_filepath = joinpath(pwd(), name * ".sol") | ||
opt_tour = read_solution(sol_filepath) | ||
opt_len = tour_length(opt_tour, dist_mtx) | ||
|
||
exts = ["mas", "pul", "sav", "sol", "tsp"] | ||
for ext in exts | ||
file = joinpath(pwd(), "$(name).$(ext)") | ||
rm(file, force=true) | ||
file = joinpath(pwd(), "O$(name).$(ext)") | ||
rm(file, force=true) | ||
end | ||
return opt_tour, opt_len | ||
end | ||
|
||
|
||
|
||
|
||
export solve_tsp | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
NAME: gr17 | ||
TYPE: TSP | ||
COMMENT: 17-city problem (Groetschel) | ||
DIMENSION: 17 | ||
EDGE_WEIGHT_TYPE: EXPLICIT | ||
EDGE_WEIGHT_FORMAT: LOWER_DIAG_ROW | ||
EDGE_WEIGHT_SECTION | ||
0 633 0 257 390 0 91 661 228 0 412 227 | ||
169 383 0 150 488 112 120 267 0 80 572 196 | ||
77 351 63 0 134 530 154 105 309 34 29 0 | ||
259 555 372 175 338 264 232 249 0 505 289 262 | ||
476 196 360 444 402 495 0 353 282 110 324 61 | ||
208 292 250 352 154 0 324 638 437 240 421 329 | ||
297 314 95 578 435 0 70 567 191 27 346 83 | ||
47 68 189 439 287 254 0 211 466 74 182 243 | ||
105 150 108 326 336 184 391 145 0 268 420 53 | ||
239 199 123 207 165 383 240 140 448 202 57 0 | ||
246 745 472 237 528 364 332 349 202 685 542 157 | ||
289 426 483 0 121 518 142 84 297 35 29 36 | ||
236 390 238 301 55 96 153 336 0 | ||
EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,24 @@ | ||
using Concorde | ||
using Test | ||
|
||
# @testset "Concorde.jl" begin | ||
# A = Cint.([4, 2, 5, 7, 2, 1, 3, 5]) | ||
# ccall((:CCutil_int_array_quicksort, Concorde.LIB_CONCORDE), Cvoid, (Ref{Cint}, Cint), A, length(A)) | ||
# @test A[1] == minimum(A) | ||
# @test A[end] == maximum(A) | ||
# end | ||
|
||
|
||
@testset "Concorde.jl" begin | ||
A = Cint.([4, 2, 5, 7, 2, 1, 3, 5]) | ||
ccall((:CCutil_int_array_quicksort, Concorde.LIB_CONCORDE), Cvoid, (Ref{Cint}, Cint), A, length(A)) | ||
@test A[1] == minimum(A) | ||
@test A[end] == maximum(A) | ||
M = [ | ||
0 16 7 14 | ||
16 0 3 5 | ||
7 3 0 16 | ||
14 5 16 0 | ||
] | ||
opt_tour, opt_len = solve_tsp(M) | ||
@show opt_tour, opt_len | ||
# display(M) | ||
|
||
@test opt_len == 29 | ||
end |