Skip to content

Commit

Permalink
add errors to bound hints
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquimg committed Apr 20, 2022
1 parent 4c7185b commit 52b1a07
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/jump_constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,56 @@ function empty_info(level, c::JuMP.VectorConstraint{F,S}) where {F,S}
return BilevelConstraintInfo{Vector{Float64}}(level, MOI.dimension(c.set))
end

function _assert_dim(cref, array::Vector, value::Vector)
if length(array) != length(value)
error("For the Vector constraint {$(cref)}, expected a Vector of length = $(length(array)) and got a Vector of length = $(length(value))")
end
return
end
function _assert_dim(cref, array::Vector, value::Number)
error("For the Vector constraint {$(cref)}, expected a Vector (of length = $(length(array))) and got the scalar $value")
return
end
function _assert_dim(cref, array::Number, value::Number)
return
end
function _assert_dim(cref, array::Number, value::Vector)
error("For the Scalar constraint {$(cref)}, expected a Scalar and got the Vector $(value)")
return
end

function JuMP.set_dual_start_value(cref::BilevelConstraintRef, value::T) where T<:Number
_assert_dim(cref, cref.model.ctr_info[cref.index].start, value)
cref.model.ctr_info[cref.index].start = value
end
function JuMP.set_dual_start_value(cref::BilevelConstraintRef, value::T) where T<:Vector{S} where S
array = cref.model.ctr_info[cref.index].start
@assert length(array) == length(value)
_assert_dim(cref, array, value)
copyto!(array, value)
end
function JuMP.dual_start_value(cref::BilevelConstraintRef)
cref.model.ctr_info[cref.index].start
end

function set_dual_upper_bound_hint(cref::BilevelConstraintRef, value::T) where T<:Number
_assert_dim(cref, cref.model.ctr_info[cref.index].upper, value)
cref.model.ctr_info[cref.index].upper = value
end
function set_dual_upper_bound_hint(cref::BilevelConstraintRef, value::T) where T<:Vector{S} where S
array = cref.model.ctr_info[cref.index].upper
@assert length(array) == length(value)
_assert_dim(cref, array, value)
copyto!(array, value)
end
function get_dual_upper_bound_hint(cref::BilevelConstraintRef)
cref.model.ctr_info[cref.index].upper
end
function set_dual_lower_bound_hint(cref::BilevelConstraintRef, value::T) where T<:Number
_assert_dim(cref, cref.model.ctr_info[cref.index].lower, value)
cref.model.ctr_info[cref.index].lower = value
end
function set_dual_lower_bound_hint(cref::BilevelConstraintRef, value::T) where T<:Vector{S} where S
array = cref.model.ctr_info[cref.index].lower
@assert length(array) == length(value)
_assert_dim(cref, array, value)
copyto!(array, value)
end
function get_dual_lower_bound_hint(cref::BilevelConstraintRef)
Expand Down
17 changes: 17 additions & 0 deletions test/jump_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,21 @@ function constraint_dualof()

@test_throws ErrorException DualOf(ctrs)

end

function constraint_hints()

model = BilevelModel()

@variable(Upper(model), x)
@variable(Lower(model), y)

@constraint(Lower(model), lin, y == 0)
@constraint(Lower(model), soc, [y, x] in SecondOrderCone())


@test_throws ErrorException BilevelJuMP.set_dual_lower_bound_hint(lin, [1])
@test_throws ErrorException BilevelJuMP.set_dual_upper_bound_hint(soc, 1)
@test_throws ErrorException BilevelJuMP.set_dual_lower_bound_hint(soc, [1])

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ end
jump_no_cb()
constraint_unit()
constraint_dualof()
constraint_hints()
for solver in solvers_unit
invalid_lower_objective(solver.opt, solver.mode)
jump_display_solver(solver.opt, solver.mode)
Expand Down

0 comments on commit 52b1a07

Please sign in to comment.