Skip to content

Commit

Permalink
readme, format
Browse files Browse the repository at this point in the history
  • Loading branch information
matbesancon committed Jul 10, 2024
1 parent b0e1b18 commit ccf40c7
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 32 deletions.
6 changes: 6 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
margin = 100
import_to_using = false
always_use_return = true
whitespace_in_kwargs = false
annotate_untyped_fields_with_any = true
always_for_in = true
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "CombinatorialLinearOracles"
uuid = "0002e35e-4a6a-41c8-a2f5-6940c7e5949f"
authors = ["Mathieu Besançon <[email protected]> and contributors"]
authors = ["ZIB-IOL and contributors"]
version = "1.0.0-DEV"

[deps]
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@

[![Build Status](https://github.com/ZIB-IOL/CombinatorialLinearOracles.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/ZIB-IOL/CombinatorialLinearOracles.jl/actions/workflows/CI.yml?query=branch%3Amain)
[![Coverage](https://codecov.io/gh/ZIB-IOL/CombinatorialLinearOracles.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ZIB-IOL/CombinatorialLinearOracles.jl)

This package is primarily a companion of [FrankWolfe.jl](https://github.com/ZIB-IOL/FrankWolfe.jl/) and implements several combinatorial linear minimization oracles, for instance for minimizing a linear function over a polytope defined by objects on graphs (spanning trees, matchings, ...).

## Installation

```julia
import Pkg
Pkg.add("https://github.com/ZIB-IOL/CombinatorialLinearOracles.jl")

import CombinatorialLinearOracles
```
15 changes: 5 additions & 10 deletions src/MatchingLinearOracle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,23 @@ struct MatchingLMO{G} <: FrankWolfe.LinearMinimizationOracle
graph::G
end

function compute_extreme_point(
lmo::MatchingLMO,
direction::M;
v=nothing,
kwargs...,
) where {M}
function compute_extreme_point(lmo::MatchingLMO, direction::M; v=nothing, kwargs...) where {M}
N = length(direction)
v = spzeros(N)
if(nv(lmo.graph) % 2 != 0)
if (nv(lmo.graph) % 2 != 0)
return v
end
iter = collect(Graphs.edges(lmo.graph))
w = Dict{typeof(iter[1]),typeof(direction[1])}()
for i in 1:N
w[iter[i]] = direction[i]
end
match = GraphsMatching.minimum_weight_perfect_matching(lmo.graph,w)

match = GraphsMatching.minimum_weight_perfect_matching(lmo.graph, w)
K = length(match.mate)
for i in 1:K
for j in 1:N
if(match.mate[i] == src(iter[j]) && dst(iter[j]) == i)
if (match.mate[i] == src(iter[j]) && dst(iter[j]) == i)
v[j] = 1
end
end
Expand Down
19 changes: 7 additions & 12 deletions src/SpanningTreeLinearOracle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,21 @@ struct SpanningTreeLMO{G} <: FrankWolfe.LinearMinimizationOracle
graph::G
end

function compute_extreme_point(
lmo::SpanningTreeLMO,
direction::M;
v=nothing,
kwargs...,
) where {M}
function compute_extreme_point(lmo::SpanningTreeLMO, direction::M; v=nothing, kwargs...) where {M}
N = length(direction)
iter = collect(Graphs.edges(lmo.graph))
distmx = spzeros(N,N)
distmx = spzeros(N, N)
for idx in 1:N
if(direction[idx] > 0)
distmx[src(iter[idx]),dst(iter[idx])] = direction[idx]
distmx[dst(iter[idx]),src(iter[idx])] = direction[idx]
if (direction[idx] > 0)
distmx[src(iter[idx]), dst(iter[idx])] = direction[idx]
distmx[dst(iter[idx]), src(iter[idx])] = direction[idx]
end
end
span = Graphs.kruskal_mst(lmo.graph,distmx)
span = Graphs.kruskal_mst(lmo.graph, distmx)
v = spzeros(N)
for edge in span
for i in 1:N
if(src(edge) == src(iter[i]) && dst(edge) == dst(iter[i]))
if (src(edge) == src(iter[i]) && dst(edge) == dst(iter[i]))
v[i] = 1
end
end
Expand Down
17 changes: 8 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ using Graphs
M = length(iter)
direction = randn(M)
lmo = CombinatorialLinearOracles.MatchingLMO(g)
v = CombinatorialLinearOracles.compute_extreme_point(lmo,direction)
v = CombinatorialLinearOracles.compute_extreme_point(lmo, direction)
tab = zeros(M)
is_matching = true
for i in 1:M
if(v[i] == 1)
if (v[i] == 1)
is_matching = (tab[src(iter[i])] == 0 && tab[dst(iter[i])] == 0)
if(!is_matching)
if (!is_matching)
break
end
tab[src(iter[i])] = 1
tab[dst(iter[i])] = 1
end
end
end
@test is_matching == true
end
Expand All @@ -35,13 +35,12 @@ end
M = length(iter)
direction = randn(M)
lmo = CombinatorialLinearOracles.SpanningTreeLMO(g)
v = CombinatorialLinearOracles.compute_extreme_point(lmo,direction)
tree = Array{Edge}(undef,(0,))
v = CombinatorialLinearOracles.compute_extreme_point(lmo, direction)
tree = Array{Edge}(undef, (0,))
for i in 1:M
if(v[i] == 1)
push!(tree,iter[i])
if (v[i] == 1)
push!(tree, iter[i])
end
end
@test Graphs.is_tree(SimpleGraphFromIterator(tree)) == true
end

0 comments on commit ccf40c7

Please sign in to comment.