Repository navigation
Stale lazy constraint removal #40
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
5 commits
Select commit
Hold shift + click to select a range
ff8129d
stale lazy constraint removal
fef867b
prune in reverse order
2a8e647
oscar review: attributes -> kwargs; fixed tolerance; no manual prune
36b0e41
update needs to happen before new rows added; update docstring
50bb068
remove old prune entrypoint
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| Manifest.toml | ||
| .DS_Store |
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 |
|---|---|---|
|
|
@@ -90,6 +90,7 @@ struct _LazyData{F<:MOI.AbstractScalarFunction,S<:MOI.AbstractScalarSet} | |
| data::Vector{Tuple{F,S}} | ||
| status::Vector{_LAZY_CONSTRAINT_STATUS} | ||
| index::Vector{MOI.ConstraintIndex{F,S}} | ||
| stale::Vector{Int} | ||
|
|
||
| function _LazyData{ | ||
| F, | ||
|
|
@@ -99,6 +100,7 @@ struct _LazyData{F<:MOI.AbstractScalarFunction,S<:MOI.AbstractScalarSet} | |
| Tuple{F,S}[], | ||
| _LAZY_CONSTRAINT_STATUS[], | ||
| MOI.ConstraintIndex{F,S}[], | ||
| Int[], | ||
| ) | ||
| end | ||
| end | ||
|
|
@@ -122,7 +124,7 @@ struct Algorithm <: MOI.AbstractOptimizerAttribute end | |
| abstract type AbstractAlgorithm end | ||
|
|
||
| """ | ||
| Iterative() | ||
| Iterative(; stale_solve_limit::Int = typemax(Int), prune_batch_size::Int = 1) | ||
|
|
||
| This algorithm iteratively solves a sequence of problems that iteratively add | ||
| violated lazy constraints to the main problem. | ||
|
|
@@ -145,8 +147,30 @@ warm-start from the previous basis after new constraints are added, whereas | |
| every iteration of the second phase solves a mixed-integer program from | ||
| scratch. The second phase is still needed because a lazy constraint can be | ||
| violated by an integer solution even if the relaxation satisfies it. | ||
|
|
||
| ## Stale constraints | ||
|
|
||
| An active lazy constraint is stale if it has been non-binding for at least | ||
| `stale_solve_limit` consecutive solves (including internal iterative solves). | ||
| Stale constraints are pruned before solving if there are at least | ||
| `prune_batch_size` stale constraints. | ||
| """ | ||
| struct Iterative <: AbstractAlgorithm end | ||
| struct Iterative <: AbstractAlgorithm | ||
| stale_solve_limit::Int | ||
| prune_batch_size::Int | ||
|
|
||
| function Iterative(; | ||
| stale_solve_limit::Int = typemax(Int), | ||
| prune_batch_size::Int = typemax(Int), | ||
| ) | ||
| if stale_solve_limit < 1 | ||
| throw(ArgumentError("stale_solve_limit must be at least 1.")) | ||
| elseif prune_batch_size < 1 | ||
| throw(ArgumentError("prune_batch_size must be at least 1.")) | ||
| end | ||
| return new(stale_solve_limit, prune_batch_size) | ||
| end | ||
| end | ||
|
|
||
| """ | ||
| Callback() | ||
|
|
@@ -489,6 +513,7 @@ function MOI.add_constraint( | |
| push!(data.data, (f, s.set)) | ||
| push!(data.status, _kLAZY_CONSTRAINT_INACTIVE) | ||
| push!(data.index, MOI.ConstraintIndex{F,S}(0)) | ||
| push!(data.stale, 0) | ||
| return MOI.ConstraintIndex{F,LazyScalarSet{S}}(length(data.data)) | ||
| end | ||
|
|
||
|
|
@@ -655,6 +680,87 @@ function MOI.get( | |
| return count(==(_kLAZY_CONSTRAINT_ACTIVE), _data(model, F, S).status) | ||
| end | ||
|
|
||
| ### MathOptLazy.prune_stale_constraints! | ||
|
|
||
| function _is_stale(algorithm::Iterative, data::_LazyData, i::Int) | ||
| return data.status[i] == _kLAZY_CONSTRAINT_ACTIVE && | ||
| data.stale[i] >= algorithm.stale_solve_limit | ||
| end | ||
|
|
||
| function _number_stale(algorithm::Iterative, data::_LazyData) | ||
| return count(i -> _is_stale(algorithm, data, i), eachindex(data.status)) | ||
| end | ||
|
|
||
| function _prune_stale!( | ||
| model::Optimizer, | ||
| algorithm::Iterative, | ||
| data::_LazyData{F,S}, | ||
| ) where {F,S} | ||
| constraints_pruned = 0 | ||
| for i in reverse(eachindex(data.status)) | ||
| if _is_stale(algorithm, data, i) | ||
| MOI.delete(model.inner, data.index[i]) | ||
| data.index[i] = MOI.ConstraintIndex{F,S}(0) | ||
| data.status[i] = _kLAZY_CONSTRAINT_INACTIVE | ||
| data.stale[i] = 0 | ||
| constraints_pruned += 1 | ||
| end | ||
| end | ||
| return constraints_pruned | ||
| end | ||
|
|
||
| function _prune_stale!(model::Optimizer, algorithm::Iterative) | ||
| return sum( | ||
| d -> _prune_stale!(model, algorithm, d), | ||
| values(model.lazy); | ||
| init = 0, | ||
| ) | ||
| end | ||
|
|
||
| function _maybe_prune_stale!(model::Optimizer, algorithm::Iterative) | ||
| n = sum(d -> _number_stale(algorithm, d), values(model.lazy); init = 0) | ||
| if n < algorithm.prune_batch_size | ||
| return | ||
| end | ||
| _prune_stale!(model, algorithm) | ||
| if !model.silent | ||
| println("[MathOptLazy] pruned $(n) stale constraints") | ||
| end | ||
| return | ||
| end | ||
|
|
||
| _is_binding(y, s::MOI.LessThan) = y >= s.upper - 1e-2 | ||
| _is_binding(y, s::MOI.GreaterThan) = y <= s.lower + 1e-2 | ||
| _is_binding(y, s::MOI.Interval) = y <= s.lower + 1e-2 || y >= s.upper - 1e-2 | ||
|
|
||
| # For sets whose slackness we can't measure, assume they bind and don't prune. | ||
| _is_binding(y, ::MOI.AbstractScalarSet) = true | ||
|
|
||
| function _update_stale!(model::Optimizer, data::_LazyData) | ||
| for (i, (_, s)) in enumerate(data.data) | ||
| if data.status[i] != _kLAZY_CONSTRAINT_ACTIVE | ||
| continue | ||
| end | ||
| y = MOI.get(model.inner, MOI.ConstraintPrimal(), data.index[i]) | ||
| if _is_binding(y, s) | ||
| data.stale[i] = 0 | ||
| else | ||
| data.stale[i] += 1 | ||
| end | ||
| end | ||
| return | ||
| end | ||
|
|
||
| function _update_stale!(model::Optimizer) | ||
| if MOI.get(model, MOI.PrimalStatus()) != MOI.FEASIBLE_POINT | ||
| return | ||
| end | ||
| for data in values(model.lazy) | ||
| _update_stale!(model, data) | ||
| end | ||
| return | ||
| end | ||
|
|
||
| ### MOI.optimize! | ||
|
|
||
| function _try_get(model::Optimizer, attr::MOI.AbstractModelAttribute, default) | ||
|
|
@@ -690,7 +796,7 @@ end | |
|
|
||
| ### MathOptLazy.Iterative | ||
|
|
||
| function _optimize!(model::Optimizer, ::Iterative) | ||
| function _optimize!(model::Optimizer, algorithm::Iterative) | ||
| if (undo = _relax_integrality(model.inner)) !== nothing | ||
| if !model.silent | ||
| println("[MathOptLazy] relaxing binary and integer variables") | ||
|
|
@@ -701,6 +807,7 @@ function _optimize!(model::Optimizer, ::Iterative) | |
| end | ||
| undo() | ||
| end | ||
| _maybe_prune_stale!(model, algorithm) | ||
|
Member
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. Interesting question whether this should go at the entry to |
||
| _iterate(model; start = true) | ||
| return | ||
| end | ||
|
|
@@ -722,6 +829,7 @@ function _iterate(model::Optimizer; start::Bool) | |
| println("[MathOptLazy] solving current subproblem\n") | ||
| end | ||
| _optimize_inner!(model) | ||
| _update_stale!(model) | ||
| if MOI.get(model, MOI.TerminationStatus()) == MOI.DUAL_INFEASIBLE | ||
| # The problem is unbounded, but it might not be if we add more | ||
| # constraints. | ||
|
|
||
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
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.
We're better to cache the vector of indices that need to be pruned, and then call one
MOI.delete(model.inner, list_to_be_deleted), otherwise we might callMOI.deletea lot of times