-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Reduction from Matching to Set Packing (#129)
* save * New:Reduction from Matching to Set Packing
- Loading branch information
1 parent
525be7e
commit aa8ae93
Showing
5 changed files
with
69 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
$TYPEDEF | ||
The reduction result of a vertex matching to a set packing problem. | ||
### Fields | ||
- `SetPacking{WT<:AbstractVector{Int}}`: the target set packing problem | ||
""" | ||
struct ReductionMatchingToSetPacking{ET,T,WT<:AbstractVector{T}} <: AbstractReductionResult | ||
setpacking::SetPacking{ET, T, WT} | ||
end | ||
Base.:(==)(a::ReductionMatchingToSetPacking, b::ReductionMatchingToSetPacking) = a.setpacking == b.setpacking | ||
|
||
target_problem(res::ReductionMatchingToSetPacking) = res.setpacking | ||
|
||
function reduceto(::Type{SetPacking}, s::Matching) | ||
sp = matching2setpacking(s.graph, s.weights) | ||
return ReductionMatchingToSetPacking(sp) | ||
end | ||
|
||
function matching2setpacking(g::SimpleGraph, weights) | ||
subsets = Vector{Vector{Int}}() | ||
# map edges to subset | ||
for edge in edges(g) | ||
push!(subsets,[edge.src, edge.dst]) | ||
end | ||
return SetPacking(subsets, weights) | ||
end | ||
|
||
function extract_solution(res::ReductionMatchingToSetPacking, sol) | ||
return sol | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Test, ProblemReductions, Graphs | ||
|
||
@testset "matching_setpacking" begin | ||
|
||
# UnitWeight Graph | ||
g1 = SimpleGraph(4) | ||
for (i, j) in [(1, 2), (1, 3), (3, 4), (2, 3)] | ||
add_edge!(g1, i, j) | ||
end | ||
Matching1 = Matching(g1) | ||
SP1 = reduceto(SetPacking, Matching1) | ||
@test target_problem(SP1) == SP1.setpacking | ||
@test target_problem(SP1) == SetPacking([[1,2], [1,3],[2,3] ,[3,4]], [1, 1, 1, 1]) | ||
sol = findbest(SP1.setpacking, BruteForce()) | ||
@test extract_solution(SP1, sol) == sol | ||
|
||
# Weighted Graph | ||
g2 = SimpleGraph(4) | ||
for (i, j) in [(1, 2), (1, 3), (3, 4), (2, 3)] | ||
add_edge!(g2, i, j) | ||
end | ||
Matching2 = Matching(g2, [1, 2, 3, 4]) | ||
SP2 = reduceto(SetPacking, Matching2) | ||
@test target_problem(SP2) == SP2.setpacking | ||
@test target_problem(SP2) == SetPacking([[1,2], [1,3], [2,3], [3,4]], [1, 2, 3, 4]) | ||
sol1 = findbest(SP2.setpacking, BruteForce()) | ||
sol2 = findbest(Matching2,BruteForce()) | ||
@test extract_solution(SP2, sol1) == sol2 | ||
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