-
Notifications
You must be signed in to change notification settings - Fork 92
Supports add constrained variable #1101
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
296fcd9
69de9a0
2096dc5
2a1abc7
1dec6d2
b81392d
fe5ceae
5a0ff1a
48dad09
3011d05
56e4855
48d6fd8
2ab04d9
cb2fde3
778e029
c16c776
32eb355
edf8e48
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 |
---|---|---|
|
@@ -157,3 +157,97 @@ end | |
@test dest.added_constrained[idxmap[vi].value] | ||
end | ||
end | ||
|
||
|
||
abstract type AbstractConstrainedVariablesModel <: MOI.ModelLike end | ||
mutable struct OrderConstrainedVariablesModel <: AbstractConstrainedVariablesModel | ||
constraintIndices ::Array{MOI.ConstraintIndex} | ||
inner ::MOIU.Model{Float64} | ||
OrderConstrainedVariablesModel() = new(MOI.ConstraintIndex[], MOIU.Model{Float64}()) | ||
end | ||
mutable struct ReverseOrderConstrainedVariablesModel <: AbstractConstrainedVariablesModel | ||
constraintIndices ::Array{MOI.ConstraintIndex} | ||
inner ::MOIU.Model{Float64} | ||
ReverseOrderConstrainedVariablesModel() = new(MOI.ConstraintIndex[], MOIU.Model{Float64}()) | ||
end | ||
|
||
|
||
|
||
MOI.add_variables(model::AbstractConstrainedVariablesModel, n) = MOI.add_variables(model.inner, n) | ||
MOI.add_variable(model::AbstractConstrainedVariablesModel) = MOI.add_variable(model.inner) | ||
|
||
function MOI.add_constraint(model::AbstractConstrainedVariablesModel, f::F, s::S) where {F<:MOI.AbstractFunction, S<:MOI.AbstractSet} | ||
ci = MOI.add_constraint(model.inner, f, s) | ||
push!(model.constraintIndices, ci) | ||
return ci | ||
end | ||
|
||
function MOI.copy_to(dest::AbstractConstrainedVariablesModel, src::MOI.ModelLike; kws...) | ||
MOIU.automatic_copy_to(dest, src; kws...) | ||
end | ||
|
||
MOIU.supports_default_copy_to(model::AbstractConstrainedVariablesModel, ::Bool) = true | ||
|
||
function MOI.empty!(model::AbstractConstrainedVariablesModel) | ||
model.constraintIndices = MOI.ConstraintIndex[] | ||
MOI.empty!(model.inner) | ||
end | ||
|
||
|
||
MOI.supports_constraint(::OrderConstrainedVariablesModel, ::Type{MOI.VectorOfVariables}, ::Type{MOI.Nonnegatives}) = false | ||
MOI.supports_add_constrained_variables(::OrderConstrainedVariablesModel, ::Type{MOI.Nonnegatives}) = true | ||
MOI.supports_constraint(::OrderConstrainedVariablesModel, ::Type{MOI.VectorOfVariables}, ::Type{MOI.Nonnegatives}) = true | ||
MOI.supports_add_constrained_variables(::OrderConstrainedVariablesModel, ::Type{MOI.Nonpositives}) = false | ||
|
||
MOI.supports_constraint(::ReverseOrderConstrainedVariablesModel, ::Type{MOI.VectorOfVariables}, ::Type{MOI.Nonnegatives}) = true | ||
MOI.supports_add_constrained_variables(::ReverseOrderConstrainedVariablesModel, ::Type{MOI.Nonnegatives}) = false | ||
MOI.supports_constraint(::ReverseOrderConstrainedVariablesModel, ::Type{MOI.VectorOfVariables}, ::Type{MOI.Nonnegatives}) = false | ||
MOI.supports_add_constrained_variables(::ReverseOrderConstrainedVariablesModel, ::Type{MOI.Nonpositives}) = true | ||
|
||
|
||
MOI.supports_constraint(::OrderConstrainedVariablesModel, ::Type{MOI.SingleVariable}, ::Type{<:MOI.GreaterThan}) = true | ||
MOI.supports_add_constrained_variable(::OrderConstrainedVariablesModel, ::Type{<:MOI.GreaterThan}) = false | ||
MOI.supports_constraint(::OrderConstrainedVariablesModel, ::Type{MOI.SingleVariable}, ::Type{<:MOI.LessThan}) = false | ||
MOI.supports_add_constrained_variable(::OrderConstrainedVariablesModel, ::Type{<:MOI.LessThan}) = true | ||
|
||
MOI.supports_constraint(::ReverseOrderConstrainedVariablesModel, ::Type{MOI.SingleVariable}, ::Type{<:MOI.GreaterThan}) = false | ||
MOI.supports_add_constrained_variable(::ReverseOrderConstrainedVariablesModel, ::Type{<:MOI.GreaterThan}) = true | ||
MOI.supports_constraint(::ReverseOrderConstrainedVariablesModel, ::Type{MOI.SingleVariable}, ::Type{<:MOI.LessThan}) = true | ||
MOI.supports_add_constrained_variable(::ReverseOrderConstrainedVariablesModel, ::Type{<:MOI.LessThan}) = false | ||
|
||
|
||
@testset "Create variables using supports_add_constrained_variable(s) (#987)" begin | ||
# With vectors | ||
src = MOIU.Model{Float64}() | ||
a, c1 = MOI.add_constrained_variables(src, MOI.Nonpositives(3)) | ||
c2 = MOI.add_constraint(src, a, MOI.Nonnegatives(3)) | ||
|
||
|
||
dest = OrderConstrainedVariablesModel() | ||
index_map = MOI.copy_to(dest, src) | ||
@test typeof(c1) == typeof(dest.constraintIndices[2]) | ||
@test typeof(c2) == typeof(dest.constraintIndices[1]) | ||
|
||
dest = ReverseOrderConstrainedVariablesModel() | ||
index_map = MOI.copy_to(dest, src) | ||
@test typeof(c1) == typeof(dest.constraintIndices[1]) | ||
@test typeof(c2) == typeof(dest.constraintIndices[2]) | ||
|
||
|
||
|
||
# With single variables | ||
src = MOIU.Model{Float64}() | ||
a, c1 = MOI.add_constrained_variable(src, MOI.GreaterThan{Float64}(5.0)) | ||
c2 = MOI.add_constraint(src, a, MOI.LessThan{Float64}(1.0)) | ||
|
||
|
||
dest = OrderConstrainedVariablesModel() | ||
index_map = MOI.copy_to(dest, src) | ||
@test typeof(c1) == typeof(dest.constraintIndices[2]) | ||
@test typeof(c2) == typeof(dest.constraintIndices[1]) | ||
|
||
dest = ReverseOrderConstrainedVariablesModel() | ||
index_map = MOI.copy_to(dest, src) | ||
@test typeof(c1) == typeof(dest.constraintIndices[1]) | ||
@test typeof(c2) == typeof(dest.constraintIndices[2]) | ||
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. Now, if you run the test with 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. I'm not sure what you meant here. Could you maybe write the corresponding test and I'd try to make it work? 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. I mean something like dest = OrderConstrainedVariablesModel()
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
index_map = MOI.copy_to(bridged_dest, src)
@test typeof(c1) == typeof(dest.constraintIndices[2])
@test typeof(c2) == typeof(dest.constraintIndices[1])
dest = ReverseOrderConstrainedVariablesModel()
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
index_map = MOI.copy_to(bridged_dest, src)
@test typeof(c1) == typeof(dest.constraintIndices[1])
@test typeof(c2) == typeof(dest.constraintIndices[2]) 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. By trying the following lines: src = MOIU.Model{Float64}()
a, c1 = MOI.add_constrained_variables(src, MOI.Nonpositives(3))
c2 = MOI.add_constraint(src, a, MOI.Nonnegatives(3))
dest = OrderConstrainedVariablesModel()
bridged_dest = MOI.Bridges.full_bridge_optimizer(dest, Float64)
index_map = MOI.copy_to(bridged_dest, src) I get an error stating I must say I did not really grasp how the bridge types work, and I'm not sure how I should go from here. Maybe that is why the issue was not tagged as a good first issue 😅 I tried implementing the attributes with: MOI.get(b::LazyBridgeOptimizer, c::MOI.VariableBridgingCost{S}) where S<:MOI.AbstractScalarSet = MOI.get(b.model, c)
MOI.get(b::LazyBridgeOptimizer, c::MOI.VariableBridgingCost{S}) where S<:MOI.AbstractVectorSet = MOI.get(b.model, c)
function MOI.get(b::LazyBridgeOptimizer, c::MOI.ConstraintBridgingCost{F,S}) where {S <: MOI.AbstractSet, F <: MOI.AbstractFunction}
MOI.get(b.model, c)
end But this did not work better, I think this shows how I don't actually understand how all of this works. |
||
end |
Uh oh!
There was an error while loading. Please reload this page.