-
Notifications
You must be signed in to change notification settings - Fork 3
Integer programming solver #143
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1b92bea
update
GiggleLiu 618343f
hypercube
nzy1997 b958648
min max
nzy1997 5962ccd
point slice
nzy1997 56cf027
fix factoring to sat conversion
GiggleLiu 6c8d2e8
save
nzy1997 e91925a
Merge remote-tracking branch 'origin/jg/fix-factoring-sat-convert' in…
nzy1997 38dbad8
update factoring
GiggleLiu 787ee94
Merge remote-tracking branch 'origin/jg/fix-factoring-sat-convert' in…
nzy1997 b2ba950
up test
nzy1997 dada6db
Merge branch 'main' into nzy/PointSlicer
nzy1997 54ba718
fix tests
nzy1997 ec38b9b
add test
nzy1997 c335deb
set covering
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 hidden or 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 hidden or 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,106 @@ | ||
| module IPSolverExt | ||
|
|
||
| import JuMP | ||
| using ProblemReductions | ||
| using LinearAlgebra | ||
|
|
||
| function Base.findmin(problem::AbstractProblem, solver::IPSolver) | ||
| return _find(problem, solver,true) | ||
| end | ||
| function Base.findmax(problem::AbstractProblem, solver::IPSolver) | ||
| return _find(problem, solver,false) | ||
| end | ||
|
|
||
| # tag: true for min, false for max | ||
| function _find(problem::AbstractProblem, solver::IPSolver,tag::Bool) | ||
| @assert num_flavors(problem) == 2 "IPSolver only supports boolean variables" | ||
| cons = constraints(problem) | ||
| nsc = ProblemReductions.num_variables(problem) | ||
| maxN = maximum([length(c.variables) for c in cons]) | ||
| combs = [ProblemReductions.combinations(2,i) for i in 1:maxN] | ||
|
|
||
| objs = objectives(problem) | ||
| @assert all(length(obj.variables) <= 1 for obj in objs) "IPSolver only supports objectives with at most 1 variables" | ||
|
|
||
| # IP by JuMP | ||
| model = JuMP.Model(solver.optimizer) | ||
| !solver.verbose && JuMP.set_silent(model) | ||
|
|
||
| JuMP.@variable(model, 0 <= x[i = 1:nsc] <= 1, Int) | ||
|
|
||
| for con in cons | ||
| f_vec = findall(!,con.specification) | ||
| num_vars = length(con.variables) | ||
| for f in f_vec | ||
| JuMP.@constraint(model, sum(j-> iszero(combs[num_vars][f][j]) ? (1 - x[con.variables[j]]) : x[con.variables[j]], 1:num_vars) <= num_vars -1) | ||
| end | ||
| end | ||
| if isempty(objs) | ||
| JuMP.@objective(model, Min, 0) | ||
| else | ||
| obj_sum = sum(objs) do obj | ||
| (1-x[obj.variables[1]])*obj.specification[1] + x[obj.variables[1]]*obj.specification[2] | ||
| end | ||
| tag ? JuMP.@objective(model, Min, obj_sum) : JuMP.@objective(model, Max, obj_sum) | ||
| end | ||
|
|
||
| JuMP.optimize!(model) | ||
| @assert JuMP.is_solved_and_feasible(model) "The problem is infeasible" | ||
| return round.(Int, JuMP.value.(x)) | ||
| end | ||
|
|
||
| """ | ||
| minimal_set_cover(coverset::Vector{Int}, subsets::Vector{Vector{Int}}, optimizer) | ||
|
|
||
| Solve the set cover problem: all elements in the coverset must be covered by the subsets. | ||
| The objective is to minimize the number of subsets used. | ||
|
|
||
| # Arguments | ||
| - `coverset::Vector{Int}`: The set of all elements to be covered. | ||
| - `subsets::Vector{Vector{Int}}`: The set of subsets to choose from. | ||
| - `optimizer`: The optimizer to use, e.g. SCIP.Optimizer or HiGHS.Optimizer | ||
|
|
||
| # Returns | ||
| - `Vector{Int}`: The indices of the subsets to choose. | ||
| """ | ||
| function minimal_set_cover(coverset::Vector{Int}, subsets::Vector{Vector{Int}}, weights::AbstractVector, optimizer, verbose::Bool=false) | ||
| # Remove subsets that cover not existing elements in the coverset | ||
| @assert all(set -> issubset(set, coverset), subsets) "subsets ($subsets) must not cover any elements absent in the coverset ($coverset)" | ||
|
|
||
| # Create a JuMP model for exact set cover | ||
| model = JuMP.Model(optimizer) | ||
| !verbose && JuMP.set_silent(model) | ||
|
|
||
| # Define binary variables for each subset (1 if selected, 0 if not) | ||
| n = length(subsets) | ||
| JuMP.@variable(model, x[1:n], Bin) | ||
|
|
||
| # Each element in the coverset must be covered exactly once | ||
| for element in coverset | ||
| # Find all subsets containing this element | ||
| covering_subsets = [i for i in 1:n if element in subsets[i]] | ||
|
|
||
| # Each element must be covered exactly once | ||
| JuMP.@constraint(model, sum(x[i] for i in covering_subsets) >= 1) | ||
| end | ||
|
|
||
| # Minimize the number of subsets used (optional objective) | ||
| JuMP.@objective(model, Min, sum(weights[i]*x[i] for i in 1:n)) | ||
| # Solve the model | ||
| JuMP.optimize!(model) | ||
|
|
||
| # Return the solution if feasible | ||
| @assert JuMP.is_solved_and_feasible(model) "The problem is infeasible" | ||
| return [i for i in 1:n if JuMP.value(x[i]) > 0.5] | ||
| end | ||
| minimal_set_cover(coverset::Vector{Int}, subsets::Vector{Vector{Int}}, optimizer, verbose::Bool=false) = minimal_set_cover(coverset, subsets, UnitWeight(length(subsets)), optimizer, verbose) | ||
|
|
||
| function Base.findmin(problem::SetCovering, solver::IPSolver) | ||
| return minimal_set_cover(problem.elements, problem.sets, problem.weights, solver.optimizer, solver.verbose) | ||
| end | ||
|
|
||
| function Base.findmax(problem::SetCovering, solver::IPSolver) | ||
| return minimal_set_cover(problem.elements, problem.sets, - problem.weights, solver.optimizer, solver.verbose) | ||
| end | ||
|
|
||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| using Test | ||
| using JuMP | ||
| using ProblemReductions | ||
| using SCIP | ||
| using Graphs | ||
|
|
||
| @testset "IPSolverExt" begin | ||
| # Test exact_set_cover with HiGHS optimizer | ||
| optimizer = SCIP.Optimizer | ||
| nflavor = 5 | ||
| subsets = [[1, 2], [2, 3], [3, 4], [4, 5]] | ||
| coverset = [1, 2, 3, 4, 5] | ||
|
|
||
| # Test exact_set_cover with HiGHS optimizer | ||
| Ext = Base.get_extension(ProblemReductions, :IPSolverExt) | ||
| result = Ext.minimal_set_cover(coverset, subsets, optimizer) | ||
| @test result == [1, 2, 4] || result == [1, 3, 4] | ||
| end | ||
|
|
||
| @testset "SetCovering" begin | ||
| problem = SetCovering([[1, 2], [2, 3], [3, 4], [4, 5]], [1, 2, 3, 4]) | ||
| @test findmin(problem, IPSolver(SCIP.Optimizer,20,false)) == [1, 2, 4] | ||
| @test findmax(problem, IPSolver(SCIP.Optimizer,20,false)) == [1, 2, 3, 4] | ||
| end | ||
|
|
||
| @testset "IPSolver" begin | ||
| graph = smallgraph(:petersen) | ||
| problem = MaximalIS(graph) | ||
| @test findmin(problem, IPSolver(SCIP.Optimizer,20,false)) ∈ findmin(problem, BruteForce()) | ||
|
|
||
| problem = IndependentSet(graph) | ||
| @test findmax(problem, IPSolver(SCIP.Optimizer,20,false)) ∈ findmax(problem, BruteForce()) | ||
|
|
||
| fact3 = Factoring(2, 1, 3) | ||
| res3 = reduceto(CircuitSAT, fact3) | ||
| problem = CircuitSAT(res3.circuit.circuit; use_constraints=true) | ||
| @test findmin(problem, IPSolver(SCIP.Optimizer,20,false)) ∈ findmin(problem, BruteForce()) | ||
| best_config3 = findmin(problem, IPSolver(SCIP.Optimizer,20,false)) | ||
| assignment3 = Dict(zip(res3.circuit.symbols, best_config3)) | ||
| @test (2* assignment3[:p2]+ assignment3[:p1]) * assignment3[:q1] == 3 | ||
|
|
||
| m1 = Matching(graph) | ||
| @test findmax(m1, IPSolver(SCIP.Optimizer,20,false)) ∈ findbest(m1, BruteForce()) | ||
| end | ||
|
|
||
| @testset "Factoring" begin | ||
| function factoring(m,n,N,solver) | ||
| fact3 = Factoring(m, n, N) | ||
| res3 = reduceto(CircuitSAT, fact3) | ||
| problem = CircuitSAT(res3.circuit.circuit; use_constraints=true) | ||
| vals = findmin(problem, IPSolver(solver,20,true)) | ||
| return ProblemReductions.read_solution(fact3, [vals[res3.p]...,vals[res3.q]...]) | ||
| end | ||
| a,b = factoring(5,5,899,SCIP.Optimizer) | ||
| @test a*b == 899 | ||
| end |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -6,6 +6,10 @@ using Documenter | |
| include("bitvector.jl") | ||
| end | ||
|
|
||
| @testset "solvers" begin | ||
| include("solvers.jl") | ||
| end | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IPSolverExt.jl should be included. |
||
| @testset "models" begin | ||
| include("models/models.jl") | ||
| end | ||
|
|
@@ -30,5 +34,8 @@ end | |
| include("deprecated.jl") | ||
| end | ||
|
|
||
| @testset "IPSolverExt" begin | ||
| include("IPSolverExt.jl") | ||
| end | ||
| DocMeta.setdocmeta!(ProblemReductions, :DocTestSetup, :(using ProblemReductions); recursive=true) | ||
| Documenter.doctest(ProblemReductions; manual=false, fix=false) | ||
This file contains hidden or 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,12 @@ | ||
| using Test, ProblemReductions, Graphs | ||
|
|
||
| @testset "BruteForce" begin | ||
| graph = smallgraph(:petersen) | ||
| problem = IndependentSet(graph) | ||
| solver = BruteForce() | ||
| res = findbest(problem, solver) | ||
| @test res == [[0, 0, 1, 0, 1, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0, 0, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0, 1, 1]] | ||
| solver = BruteForce() | ||
| res = findbest(problem, solver) | ||
| @test res == [[0, 0, 1, 0, 1, 1, 1, 0, 0, 0], [1, 0, 0, 1, 0, 0, 1, 1, 0, 0], [0, 1, 0, 0, 1, 0, 0, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0, 0, 0, 1, 1]] | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.