-
Notifications
You must be signed in to change notification settings - Fork 0
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 #7 from ZIB-IOL/matchinglmo
Matching LMO
- Loading branch information
Showing
6 changed files
with
139 additions
and
49 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 was deleted.
Oops, something went wrong.
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,88 @@ | ||
|
||
""" | ||
MatchingLMO{G}(g::Graphs) | ||
Return a vector v corresponding to edges(g), where if v[i] = 1, | ||
the edge i is in the maximum weight matching, and if v[i] = 0, the edge i is not in the matching. | ||
""" | ||
struct MatchingLMO{G} <: FrankWolfe.LinearMinimizationOracle | ||
graph::G | ||
end | ||
|
||
function FrankWolfe.compute_extreme_point( | ||
lmo::MatchingLMO, | ||
direction::M; | ||
v=nothing, | ||
kwargs..., | ||
) where {M} | ||
N = length(direction) | ||
v = spzeros(N) | ||
iter = collect(edges(lmo.graph)) | ||
g = SimpleGraphFromIterator(iter) | ||
l = nv(g) | ||
add_vertices!(g, l) | ||
w = Dict{typeof(iter[1]),typeof(direction[1])}() | ||
for i in 1:N | ||
add_edge!(g, src(iter[i]) + l, dst(iter[i]) + l) | ||
w[iter[i]] = -direction[i] | ||
w[Edge(src(iter[i]) + l, dst(iter[i]) + l)] = -direction[i] | ||
end | ||
|
||
for i in 1:l | ||
add_edge!(g, i, i + l) | ||
w[Edge(i, i + l)] = 0 | ||
end | ||
|
||
match = GraphsMatching.minimum_weight_perfect_matching(g, 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) | ||
v[j] = 1 | ||
end | ||
end | ||
end | ||
return v | ||
end | ||
|
||
|
||
""" | ||
PerfectMatchingLMO{G}(g::Graphs) | ||
Return a vector v corresponding to edges(g), where if v[i] = 1, | ||
the edge i is in the matching, and if v[i] = 0, the edge i is not in the matching. | ||
If there is not possible perfect matching, all elements of v are set to 0. | ||
""" | ||
struct PerfectMatchingLMO{G} <: FrankWolfe.LinearMinimizationOracle | ||
graph::G | ||
end | ||
|
||
function FrankWolfe.compute_extreme_point( | ||
lmo::PerfectMatchingLMO, | ||
direction::M; | ||
v=nothing, | ||
kwargs..., | ||
) where {M} | ||
N = length(direction) | ||
v = spzeros(N) | ||
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) | ||
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) | ||
v[j] = 1 | ||
end | ||
end | ||
end | ||
return v | ||
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
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