Skip to content

Commit 19f68bc

Browse files
authored
Merge pull request #22 from ZIB-IOL/matching-fixed
matching conditions
2 parents 5a2ad66 + 0c99970 commit 19f68bc

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/matchings.jl

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"""
33
MatchingLMO{G}(g::Graphs)
44
5-
Return a vector v corresponding to edges(g), where if v[i] = 1,
6-
the edge i is in the maximum weight matching, and if v[i] = 0, the edge i is not in the matching.
5+
Return an incidence vector v of the edges of `g`, ordered as `edges(g)` for a minimum-weight matching.
76
"""
87
struct MatchingLMO{G} <: FrankWolfe.LinearMinimizationOracle
98
graph::G
@@ -16,16 +15,20 @@ function FrankWolfe.compute_extreme_point(
1615
kwargs...,
1716
) where {M}
1817
N = length(direction)
19-
v = spzeros(N)
18+
if v === nothing
19+
v = spzeros(N)
20+
else
21+
v .= 0
22+
end
2023
iter = collect(edges(lmo.graph))
2124
g = SimpleGraphFromIterator(iter)
2225
l = nv(g)
2326
add_vertices!(g, l)
2427
w = Dict{typeof(iter[1]),typeof(direction[1])}()
2528
for i in 1:N
2629
add_edge!(g, src(iter[i]) + l, dst(iter[i]) + l)
27-
w[iter[i]] = -direction[i]
28-
w[Edge(src(iter[i]) + l, dst(iter[i]) + l)] = -direction[i]
30+
w[iter[i]] = direction[i]
31+
w[Edge(src(iter[i]) + l, dst(iter[i]) + l)] = direction[i]
2932
end
3033

3134
for i in 1:l
@@ -50,24 +53,29 @@ end
5053
"""
5154
PerfectMatchingLMO{G}(g::Graphs)
5255
53-
Return a vector v corresponding to edges(g), where if v[i] = 1,
54-
the edge i is in the matching, and if v[i] = 0, the edge i is not in the matching.
55-
If there is not possible perfect matching, all elements of v are set to 0.
56+
Return the incidence vector of a minimum-weight perfect matching, `v` is ordered as `edges(g)`.
57+
The constructor verifies that the graph admits a perfect matching.
5658
"""
5759
struct PerfectMatchingLMO{G} <: FrankWolfe.LinearMinimizationOracle
5860
graph::G
5961
end
6062

63+
function PerfectMatchingLMO(graph::G) where {G}
64+
@assert nv(graph) % 2 == 0
65+
return PerfectMatchingLMO{G}(graph)
66+
end
67+
6168
function FrankWolfe.compute_extreme_point(
6269
lmo::PerfectMatchingLMO,
6370
direction::M;
6471
v=nothing,
6572
kwargs...,
6673
) where {M}
6774
N = length(direction)
68-
v = spzeros(N)
69-
if (nv(lmo.graph) % 2 != 0)
70-
return v
75+
if v === nothing
76+
v = spzeros(N)
77+
else
78+
v .= 0
7179
end
7280
iter = collect(Graphs.edges(lmo.graph))
7381
w = Dict{typeof(iter[1]),typeof(direction[1])}()

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ end
5050
for i in 1:M
5151
adj_mat[src(iter[i]), dst(iter[i])] = direction[i]
5252
end
53-
match_result = GraphsMatching.maximum_weight_matching(g, HiGHS.Optimizer, adj_mat)
53+
match_result = GraphsMatching.maximum_weight_matching(g, HiGHS.Optimizer, -adj_mat)
5454
v_sol = spzeros(M)
5555
K = length(match_result.mate)
5656
for i in 1:K

0 commit comments

Comments
 (0)