-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Greedy merge and better size reduction #43
Open
nzy1997
wants to merge
12
commits into
OptimalBranching:main
Choose a base branch
from
nzy1997:greedymerge
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e338ca1
greedymerge
nzy1997 82d019d
size_reduction
nzy1997 b385d22
size_reduction
nzy1997 7198b18
spaces
nzy1997 129efa5
sapces
nzy1997 f2e54d1
add test on coverd_by
nzy1997 7231cea
fix test
nzy1997 31f9a7a
fix test
nzy1997 771ae11
update
GiggleLiu a0fb2fd
update-greedy-implementation
GiggleLiu 63ae467
merge and fix bugs
nzy1997 88ae792
fix doc
nzy1997 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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,43 @@ | ||
struct GreedyMerge <: AbstractSetCoverSolver end | ||
function optimal_branching_rule(table::BranchingTable, variables::Vector, problem::AbstractProblem, m::AbstractMeasure, solver::GreedyMerge) | ||
candidates = bit_clauses(table) | ||
return greedymerge(candidates, problem, variables, m) | ||
end | ||
|
||
function bit_clauses(tbl::BranchingTable{INT}) where {INT} | ||
n, bss = tbl.bit_length, tbl.table | ||
temp_clauses = [[Clause(bmask(INT, 1:n), bs) for bs in bss1] for bss1 in bss] | ||
return temp_clauses | ||
end | ||
|
||
function greedymerge(cls::Vector{Vector{Clause{INT}}}, problem::AbstractProblem, variables::Vector, m::AbstractMeasure) where {INT} | ||
cls = copy(cls) | ||
size_reductions = [size_reduction(problem, m, first(candidate), variables) for candidate in cls] | ||
local γ | ||
while true | ||
γ = complexity_bv(size_reductions) | ||
minval = zero(γ) | ||
minidx = (-1, -1, -1, -1) | ||
local minclause | ||
local minred | ||
for i ∈ 1:length(cls), j ∈ i+1:length(cls) | ||
for ii in 1:length(cls[i]), jj in 1:length(cls[j]) | ||
cl12 = gather2(length(variables), cls[i][ii], cls[j][jj]) | ||
if cl12.mask == 0 | ||
continue | ||
end | ||
reduction = size_reduction(problem, m, cl12, variables) | ||
val = γ^(-reduction) - γ^(-size_reductions[i]) - γ^(-size_reductions[j]) | ||
if val < minval | ||
minval, minidx, minclause, minred = val, (i, j, ii, jj), cl12, reduction | ||
end | ||
end | ||
end | ||
minidx == (-1, -1, -1, -1) && break # no more merging | ||
deleteat!(cls, minidx[1:2]) | ||
deleteat!(size_reductions, minidx[1:2]) | ||
push!(cls, [minclause]) | ||
push!(size_reductions, minred) | ||
end | ||
return OptimalBranchingResult(DNF([cl[1] for cl in cls]), size_reductions, γ) | ||
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
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,15 @@ | ||
using Test | ||
using OptimalBranchingCore | ||
using OptimalBranchingCore: bit_clauses | ||
using OptimalBranchingCore.BitBasis | ||
using GenericTensorNetworks | ||
|
||
@testset "bit_clauses" begin | ||
tbl = BranchingTable(5, [ | ||
[StaticElementVector(2, [0, 0, 1, 0, 0]), StaticElementVector(2, [0, 1, 0, 0, 0])], | ||
[StaticElementVector(2, [1, 0, 0, 1, 0])], | ||
[StaticElementVector(2, [0, 0, 1, 0, 1])], | ||
]) | ||
|
||
bc = bit_clauses(tbl) | ||
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 |
---|---|---|
@@ -1,37 +1,37 @@ | ||
""" | ||
mis_size(g::AbstractGraph; bs::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure=D3Measure()), reducer::AbstractReducer = MISReducer()) | ||
mis_size(g::AbstractGraph; branching_strategy::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure=D3Measure()), reducer::AbstractReducer = MISReducer()) | ||
|
||
Calculate the size of the Maximum Independent Set (MIS) for a given graph. | ||
|
||
### Arguments | ||
- `g::AbstractGraph`: The graph for which the MIS size is to be calculated. | ||
- `bs::BranchingStrategy`: (optional) The branching strategy to be used. Defaults to a strategy using `table_solver=TensorNetworkSolver`, `selector=MinBoundaryHighDegreeSelector(2, 6, 0)`, and `measure=D3Measure`. | ||
- `branching_strategy::BranchingStrategy`: (optional) The branching strategy to be used. Defaults to a strategy using `table_solver=TensorNetworkSolver`, `selector=MinBoundaryHighDegreeSelector(2, 6, 0)`, and `measure=D3Measure`. | ||
- `reducer::AbstractReducer`: (optional) The reducer to be applied. Defaults to `MISReducer`. | ||
|
||
### Returns | ||
- An integer representing the size of the Maximum Independent Set for the given graph. | ||
""" | ||
function mis_size(g::AbstractGraph; bs::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure=D3Measure()), reducer=MISReducer()) | ||
function mis_size(g::AbstractGraph; branching_strategy::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure = D3Measure()), reducer = MISReducer()) | ||
p = MISProblem(g) | ||
res = branch_and_reduce(p, bs, reducer, MaxSize) | ||
res = branch_and_reduce(p, branching_strategy, reducer, MaxSize) | ||
return res.size | ||
end | ||
|
||
""" | ||
mis_branch_count(g::AbstractGraph; bs::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure=D3Measure()), reducer=MISReducer()) | ||
mis_branch_count(g::AbstractGraph; branching_strategy::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure=D3Measure()), reducer=MISReducer()) | ||
|
||
Calculate the size and the number of branches of the Maximum Independent Set (MIS) for a given graph. | ||
|
||
### Arguments | ||
- `g::AbstractGraph`: The graph for which the MIS size and the number of branches are to be calculated. | ||
- `bs::BranchingStrategy`: (optional) The branching strategy to be used. Defaults to a strategy using `table_solver=TensorNetworkSolver`, `selector=MinBoundaryHighDegreeSelector(2, 6, 0)`, and `measure=D3Measure`. | ||
- `branching_strategy::BranchingStrategy`: (optional) The branching strategy to be used. Defaults to a strategy using `table_solver=TensorNetworkSolver`, `selector=MinBoundaryHighDegreeSelector(2, 6, 0)`, and `measure=D3Measure`. | ||
- `reducer::AbstractReducer`: (optional) The reducer to be applied. Defaults to `MISReducer`. | ||
|
||
### Returns | ||
- A tuple `(size, count)` where `size` is the size of the Maximum Independent Set and `count` is the number of branches. | ||
""" | ||
function mis_branch_count(g::AbstractGraph; branching_strategy::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure=D3Measure()), reducer=MISReducer()) | ||
function mis_branch_count(g::AbstractGraph; branching_strategy::BranchingStrategy = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure = D3Measure()), reducer = MISReducer()) | ||
p = MISProblem(g) | ||
res = branch_and_reduce(p, branching_strategy, reducer, MaxSizeBranchCount) | ||
return (res.size, res.count) | ||
end | ||
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,43 @@ | ||
using OptimalBranchingMIS | ||
using OptimalBranchingMIS.EliminateGraphs.Graphs | ||
using Test | ||
using Random | ||
using OptimalBranchingCore | ||
using OptimalBranchingCore.BitBasis | ||
using GenericTensorNetworks | ||
using OptimalBranchingCore: bit_clauses | ||
Random.seed!(1234) | ||
|
||
# Example from arXiv:2412.07685 Fig. 1 | ||
@testset "GreedyMerge" begin | ||
edges = [(1, 4), (1, 5), (3, 4), (2, 5), (4, 5), (1, 6), (2, 7), (3, 8)] | ||
example_g = SimpleGraph(Graphs.SimpleEdge.(edges)) | ||
p = MISProblem(example_g) | ||
tbl = BranchingTable(5, [ | ||
[StaticElementVector(2, [0, 0, 0, 0, 1]), StaticElementVector(2, [0, 0, 0, 1, 0])], | ||
[StaticElementVector(2, [0, 0, 1, 0, 1])], | ||
[StaticElementVector(2, [0, 1, 0, 1, 0])], | ||
[StaticElementVector(2, [1, 1, 1, 0, 0])], | ||
]) | ||
cls = bit_clauses(tbl) | ||
res = OptimalBranchingCore.greedymerge(cls, p, [1, 2, 3, 4, 5], NumOfVertices()) | ||
clsf = res.optimal_rule.clauses | ||
@test clsf[1].mask == cls[3][1].mask | ||
@test clsf[1].val == cls[3][1].val | ||
@test clsf[2].mask == cls[4][1].mask | ||
@test clsf[2].val == cls[4][1].val | ||
@test clsf[3].mask == 27 | ||
@test clsf[3].val == 16 | ||
end | ||
|
||
@testset "GreedyMerge" begin | ||
g = random_regular_graph(20, 3) | ||
mis_num, count2 = mis_branch_count(g) | ||
for reducer in [NoReducer(), MISReducer()] | ||
for measure in [D3Measure(), NumOfVertices()] | ||
bs = BranchingStrategy(table_solver = TensorNetworkSolver(), selector = MinBoundaryHighDegreeSelector(2, 6, 0), measure = measure, set_cover_solver = OptimalBranchingCore.GreedyMerge()) | ||
mis1, count1 = mis_branch_count(g; branching_strategy = bs, reducer) | ||
@test mis1 == mis_num | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, the other measures will fail to work. I think we should keep the previous one as a fallback if no speacial
size_reduction
is defined.Oh I noticed that this function is defined in another file, please move it here.