Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/chkwon/TSPSolvers.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
chkwon committed Apr 26, 2023
2 parents 9b49b7a + 89c6cda commit 8c5276c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: CI
on:
push:
branches: [master]
pull_request:
types: [opened, synchronize, reopened]
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version: ['1'] # Test against LTS and current minor release
os: [ubuntu-latest, macOS-latest, windows-latest] # macOS-latest, windows-latest
arch: [x64]
steps:
- uses: actions/checkout@v2
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
# - name: Install dependencies
# run: julia --project=. -e 'using Pkg; Pkg.add(url="""https://github.com/matago/TSPLIB.jl"""); Pkg.instantiate()'
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
19 changes: 17 additions & 2 deletions src/main.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const supported_algorithms = [
]


function solve_tsp(dist_mtx::Matrix{Int}; algorithm="LKH", firstcity=1, kwargs...)
function solve_tsp(dist_mtx::Matrix{Int}; algorithm="LKH", firstcity=1, init_tour::Vector{Int}=Int[], kwargs...)
n = size(dist_mtx, 1)

S = dist_mtx
Expand Down Expand Up @@ -66,7 +66,22 @@ function solve_tsp(dist_mtx::Matrix{Int}; algorithm="LKH", firstcity=1, kwargs..
return tour, cost

elseif algorithm == "TwoOpt"
init_tour = rand_tour_for_heuristics(size(S, 1); firstcity=firstcity)
if isempty(init_tour)
init_tour = rand_tour_for_heuristics(size(S, 1); firstcity=firstcity)
end

if length(init_tour) == n
init_tour = shift_tour!(init_tour, firstcity)
push!(init_tour, firstcity)
end

@assert length(init_tour) == n + 1
@assert init_tour[1] == init_tour[end]

if init_tour[1] != firstcity
@warn("The first city of the initial tour is not $firstcity. It will be ignored.")
end

_tour, cost = TravelingSalesmanHeuristics.two_opt(S, init_tour; kwargs...)
tour = _tour[1:end-1]
return tour, cost
Expand Down
3 changes: 2 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ end
function shift_tour!(tour::Vector{Int}, firstcity)
if tour[1] != firstcity
idx = findfirst(x -> x==firstcity, tour)
circshift!(tour, idx - 1)
circshift!(tour, length(tour) - idx + 1)
end
end

function rand_tour_for_heuristics(n::Int; firstcity=1)
tour = shuffle(1:n)
shift_tour!(tour, firstcity)
push!(tour, firstcity) # TravelingSalesmanHeuristics requires the first node at the end of the tour again.

return tour
end

3 changes: 2 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ using Test

@testset "TSPSolvers.jl" begin
# Write your tests here.
include("simple.jl")
include("test_shift_tour.jl")
include("test_simple_TSP.jl")
end
17 changes: 0 additions & 17 deletions test/simple.jl

This file was deleted.

14 changes: 14 additions & 0 deletions test/test_shift_tour.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using TSPSolvers
using Test
using LinearAlgebra
using Random

@testset "Test Shift Tour" begin
for _ in 1:10
n = rand(1:100)
tour = shuffle(1:n)
first_city = rand(1:n)
TSPSolvers.shift_tour!(tour, first_city)
@test tour[1] == first_city
end
end

0 comments on commit 8c5276c

Please sign in to comment.