Skip to content

Commit

Permalink
New: Reduction from Matching to Set Packing (#129)
Browse files Browse the repository at this point in the history
* save

* New:Reduction from Matching to Set Packing
  • Loading branch information
c-allergic authored Dec 27, 2024
1 parent 525be7e commit aa8ae93
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ProblemReductions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export ReductionSATToIndependentSet, ReductionSATToDominatingSet
export ReductionIndependentSetToSetPacking, ReductionSetPackingToIndependentSet
export ReductionSATToCircuit
export ReductionIndependentSetToVertexCovering
export ReductionMatchingToSetPacking

# reduction path
export ReductionGraph, reduction_graph, reduction_paths, ConcatenatedReduction
Expand Down
31 changes: 31 additions & 0 deletions src/rules/matching_setpacking.jl
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
3 changes: 2 additions & 1 deletion src/rules/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ include("sat_independentset.jl")
include("sat_dominatingset.jl")
include("independentset_setpacking.jl")
include("circuit_sat.jl")
include("vertexcovering_independentset.jl")
include("vertexcovering_independentset.jl")
include("matching_setpacking.jl")
29 changes: 29 additions & 0 deletions test/rules/matching_setpacking.jl
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
6 changes: 6 additions & 0 deletions test/rules/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ end
include("vertexcovering_independentset.jl")
end

@testset "matching_setpacking" begin
include("matching_setpacking.jl")
end

@testset "rules" begin
circuit = CircuitSAT(@circuit begin
x = a ¬b
Expand All @@ -65,6 +69,7 @@ end
is = IndependentSet(graph)
is2 = IndependentSet(graph2)
setpacking = SetPacking([[1, 2, 5], [1, 3], [2, 4], [3, 6], [2, 3, 6]])
matching = Matching(graph)
for (source, target_type) in [
# please add more tests here
circuit => SpinGlass{<:SimpleGraph},
Expand All @@ -83,6 +88,7 @@ end
is2 => SetPacking,
setpacking => IndependentSet{<:SimpleGraph},
is => VertexCovering,
matching => SetPacking
]
@info "Testing reduction from $(typeof(source)) to $(target_type)"
# directly solve
Expand Down

0 comments on commit aa8ae93

Please sign in to comment.