-
Notifications
You must be signed in to change notification settings - Fork 94
When copying a model, propose to only keep conflict constraints. #1135
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
Changes from all commits
2e16c43
ae3ea4a
82baad7
8192220
1342f9c
aa7c311
d8c4160
27d3de7
2fa662c
b002a4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,3 +256,62 @@ MOI.supports_add_constrained_variable(::ReverseOrderConstrainedVariablesModel, : | |
@test typeof(c1) == typeof(dest.constraintIndices[1]) | ||
@test typeof(c2) == typeof(dest.constraintIndices[2]) | ||
end | ||
|
||
@testset "Filtering copy: check based on index" begin | ||
# Create a basic model. | ||
src = MOIU.Model{Float64}() | ||
x = MOI.add_variable(src) | ||
c1 = MOI.add_constraint(src, x, MOI.LessThan{Float64}(1.0)) | ||
c2 = MOI.add_constraint(src, x, MOI.GreaterThan{Float64}(0.0)) | ||
|
||
# Filtering function: the default case where this function always returns | ||
# true is already well-tested by the above cases. | ||
# Only keep the constraint c1. | ||
f = (cidx) -> cidx == c1 | ||
|
||
# Perform the copy. | ||
dst = OrderConstrainedVariablesModel() | ||
index_map = MOI.copy_to(dst, src, filter_constraints=f) | ||
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. Could you also add a test where the filter filters out all constraints of a given type and the output model does not support the type of constraints that is filtered out ? If they are all filtered out, it shouldn't throw 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. Something like the last commit? |
||
|
||
@test typeof(c1) == typeof(dst.constraintIndices[1]) | ||
@test length(dst.constraintIndices) == 1 | ||
end | ||
|
||
mutable struct BoundModel <: MOI.ModelLike | ||
# Type of model that only supports ≤ bound constraints. In particular, it does not support integrality constraints. | ||
inner::MOIU.Model{Float64} | ||
BoundModel() = new(MOIU.Model{Float64}()) | ||
end | ||
|
||
MOI.add_variable(model::BoundModel) = MOI.add_variable(model.inner) | ||
MOI.add_constraint(model::BoundModel, f::MOI.AbstractFunction, s::MOI.LessThan{Float64}) = MOI.add_constraint(model.inner, f, s) | ||
MOI.supports_constraint(::BoundModel, ::Type{MOI.SingleVariable}, ::MOI.LessThan{Float64}) = true | ||
|
||
MOIU.supports_default_copy_to(::BoundModel, ::Bool) = true | ||
MOI.copy_to(dest::BoundModel, src::MOI.ModelLike; kws...) = MOIU.automatic_copy_to(dest, src; kws...) | ||
MOI.empty!(model::BoundModel) = MOI.empty!(model.inner) | ||
|
||
MOI.supports(::BoundModel, ::Type{MOI.NumberOfConstraints}) = true | ||
MOI.get(model::BoundModel, attr::MOI.NumberOfConstraints) = MOI.get(model.inner, attr) | ||
|
||
@testset "Filtering copy: check based on constraint type" begin | ||
# Create a basic model. | ||
src = MOIU.Model{Float64}() | ||
x = MOI.add_variable(src) | ||
c1 = MOI.add_constraint(src, x, MOI.LessThan{Float64}(10.0)) | ||
c2 = MOI.add_constraint(src, x, MOI.Integer()) | ||
|
||
# Filtering function: get rid of integrality constraint. | ||
f = (cidx) -> MOI.get(src, MOI.ConstraintSet(), cidx) != MOI.Integer() | ||
|
||
# Perform the unfiltered copy. This should throw an error (i.e. the implementation of BoundModel | ||
# should be correct). | ||
dst = BoundModel() | ||
@test_throws MOI.UnsupportedConstraint{MOI.SingleVariable, MOI.Integer} MOI.copy_to(dst, src) | ||
|
||
# Perform the filtered copy. This should not throw an error. | ||
dst = BoundModel() | ||
MOI.copy_to(dst, src, filter_constraints=f) | ||
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. Can you also add a |
||
@test MOI.get(dst, MOI.NumberOfConstraints{MOI.SingleVariable, MOI.LessThan{Float64}}()) == 1 | ||
@test MOI.get(dst, MOI.NumberOfConstraints{MOI.SingleVariable, MOI.Integer}()) == 0 | ||
end |
Uh oh!
There was an error while loading. Please reload this page.