Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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:
include:
- version: '1'
os: ubuntu-latest
arch: x64
- version: '1.6'
os: ubuntu-latest
arch: x64
- version: '1'
os: macOS-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 }}-
- 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
1 change: 1 addition & 0 deletions src/CombinatorialLinearOracles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ using GraphsMatching

include("matchings.jl")
include("spanning_tree.jl")
include("shortest_path.jl")

end
30 changes: 30 additions & 0 deletions src/shortest_path.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

struct ShortestPathLMO{T,G} <: FrankWolfe.LinearMinimizationOracle
graph::G
src::Int
dst::Int
dist_matrix::SparseArrays.SparseMatrixCSC{T,Int}
end

function ShortestPathLMO(graph, src_node, dst_node)
@assert !Graphs.is_cyclic(graph)
@assert Graphs.has_path(graph, src_node, dst_node)
dist_matrix = spzeros(nv(graph), nv(graph))
return ShortestPathLMO{eltype(dist_matrix), typeof(graph)}(graph, src_node, dst_node, dist_matrix)
end

function FrankWolfe.compute_extreme_point(lmo::ShortestPathLMO, direction)
for (idx, edge) in enumerate(edges(lmo.graph))
lmo.dist_matrix[src(edge), dst(edge)] = direction[idx]
end
shortest_path_result = Set(Graphs.a_star(lmo.graph, lmo.src, lmo.dst, lmo.dist_matrix))
# resulting vertex as a BitVector
indicence_vector = falses(ne(lmo.graph))
for (idx, edge) in enumerate(edges(lmo.graph))
if edge in shortest_path_result
indicence_vector[idx] = 1
delete!(shortest_path_result, edge)
end
end
return indicence_vector
end
20 changes: 20 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,23 @@ end
end
@test Graphs.is_tree(SimpleGraphFromIterator(tree))
end

@testset "Shortest path" begin
n = 200
Random.seed!(42)
for _ in 1:10
g = Graphs.random_orientation_dag(Graphs.complete_graph(n))
src_node = 1
dst_node = nv(g)
while !has_path(g, src_node, dst_node) || src_node == dst_node
src_node = rand(1:nv(g))
dst_node = rand(1:nv(g))
end
lmo = CombinatorialLinearOracles.ShortestPathLMO(g, src_node, dst_node)
for _ in 1:10
direction = randn(ne(g))
v = FrankWolfe.compute_extreme_point(lmo, direction)
@test sum(v) >= 1
end
end
end