Skip to content
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

Implement starting values for Bridges.Constraint.ScalarizeBridge #937

Merged
merged 1 commit into from
Oct 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/Bridges/Constraint/scalarize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,32 @@ function MOI.get(::MOI.ModelLike, ::MOI.ConstraintSet,
return MOIU.vector_set_type(S)(length(bridge.scalar_constraints))
end

function MOI.get(model::MOI.ModelLike, attr::MOI.ConstraintPrimal,
function MOI.supports(
::MOI.ModelLike,
::Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart},
::Type{<:ScalarizeBridge})

return true
end
function MOI.get(model::MOI.ModelLike,
attr::Union{MOI.ConstraintPrimal, MOI.ConstraintPrimalStart},
bridge::ScalarizeBridge)
return MOI.get.(model, attr, bridge.scalar_constraints) .+ bridge.constants
end
function MOI.get(model::MOI.ModelLike, attr::MOI.ConstraintDual,
function MOI.set(model::MOI.ModelLike, attr::MOI.ConstraintPrimalStart,
bridge::ScalarizeBridge, value)
# TODO do no add constant if the primal status is a ray like in Vectorize
return MOI.set.(model, attr, bridge.scalar_constraints, value .- bridge.constants)
end
function MOI.get(model::MOI.ModelLike,
attr::Union{MOI.ConstraintDual, MOI.ConstraintDualStart},
bridge::ScalarizeBridge)
return MOI.get.(model, attr, bridge.scalar_constraints)
end
function MOI.set(model::MOI.ModelLike, attr::MOI.ConstraintDualStart,
bridge::ScalarizeBridge, value)
return MOI.set.(model, attr, bridge.scalar_constraints, value)
end
function MOI.modify(model::MOI.ModelLike, bridge::ScalarizeBridge{T,F,S},
change::MOI.VectorConstantChange{T}) where {T,F,S}
bridge.constants = change.new_constant
Expand Down
28 changes: 27 additions & 1 deletion test/Bridges/Constraint/scalarize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const MOIB = MathOptInterface.Bridges

include("../utilities.jl")

mock = MOIU.MockOptimizer(MOIU.Model{Float64}())
mock = MOIU.MockOptimizer(MOIU.UniversalFallback(MOIU.Model{Float64}()))
config = MOIT.TestConfig()

@testset "Scalarize" begin
Expand Down Expand Up @@ -36,6 +36,14 @@ config = MOIT.TestConfig()
new_func = MOI.VectorOfVariables(func.variables[[1, 3]])
@test MOI.get(bridged_mock, MOI.ConstraintFunction(), ci) == new_func
@test MOI.get(bridged_mock, MOI.ConstraintSet(), ci) == MOI.Nonnegatives(2)

@testset "$attr" for attr in [MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart()]
@test MOI.supports(bridged_mock, attr, typeof(ci))
values = [1.0, 2.0]
MOI.set(bridged_mock, attr, ci, values)
@test MOI.get(bridged_mock, attr, ci) == values
end

test_delete_bridge(bridged_mock, ci, 2,
((MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0),
(MOI.SingleVariable, MOI.GreaterThan{Float64}, 0)))
Expand All @@ -46,11 +54,29 @@ config = MOIT.TestConfig()
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}) => [0, 2, 0],
(MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}) => [-3, -1])
MOIT.lin1ftest(bridged_mock, config)

ci = first(MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.VectorAffineFunction{Float64}, MOI.Zeros}()))

@testset "$attr" for attr in [MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart()]
@test MOI.supports(bridged_mock, attr, typeof(ci))
values = [1.0, -2.0]
MOI.set(bridged_mock, attr, ci, values)
@test MOI.get(bridged_mock, attr, ci) == values
end

test_delete_bridge(bridged_mock, ci, 3,
((MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}, 0)))

ci = first(MOI.get(bridged_mock, MOI.ListOfConstraintIndices{MOI.VectorAffineFunction{Float64}, MOI.Nonnegatives}()))

@testset "$attr" for attr in [MOI.ConstraintPrimalStart(), MOI.ConstraintDualStart()]
@test MOI.supports(bridged_mock, attr, typeof(ci))
values = [1.0, -2.0, 3.0]
MOI.set(bridged_mock, attr, ci, values)
@test MOI.get(bridged_mock, attr, ci) == values
end

test_delete_bridge(bridged_mock, ci, 3,
((MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}, 0),
(MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}, 0)))
Expand Down