Skip to content

Commit

Permalink
Convert constraint starting values (#3571)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Nov 12, 2023
1 parent f3319b4 commit 3015564
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ end

_value_type(::Any, ::Any) = Any

function _value_type(::ConstraintRef{M,<:MOI.ConstraintIndex{F}}) where {M,F}
return _value_type(M, F)
end

# Returns the value of MOI.ConstraintDualStart in a type-stable way
function _dual_start(
con_ref::ConstraintRef{M,MOI.ConstraintIndex{F,S}},
Expand Down Expand Up @@ -124,7 +128,7 @@ function set_dual_start_value(
owner_model(con_ref),
MOI.ConstraintDualStart(),
con_ref,
vectorized_value,
_convert_if_something(_value_type(con_ref), vectorized_value),
)
return
end
Expand All @@ -151,7 +155,8 @@ function set_dual_start_value(
},
value,
)
MOI.set(owner_model(con_ref), MOI.ConstraintDualStart(), con_ref, value)
v = _convert_if_something(_value_type(con_ref), value)
MOI.set(owner_model(con_ref), MOI.ConstraintDualStart(), con_ref, v)
return
end

Expand All @@ -174,11 +179,12 @@ function set_start_value(
},
value,
)
vectorized_value = vectorize(value, con_ref.shape)
MOI.set(
owner_model(con_ref),
MOI.ConstraintPrimalStart(),
con_ref,
vectorize(value, con_ref.shape),
_convert_if_something(_value_type(con_ref), vectorized_value),
)
return
end
Expand Down Expand Up @@ -207,7 +213,8 @@ function set_start_value(
},
value,
)
MOI.set(owner_model(con_ref), MOI.ConstraintPrimalStart(), con_ref, value)
v = _convert_if_something(_value_type(con_ref), value)
MOI.set(owner_model(con_ref), MOI.ConstraintPrimalStart(), con_ref, v)
return
end

Expand Down
9 changes: 9 additions & 0 deletions test/test_constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,7 @@ function test_dual_start()
con = @constraint(model, 2x <= 1)
@test dual_start_value(con) === nothing
set_dual_start_value(con, 2)
@test MOI.get(model, MOI.ConstraintDualStart(), con) isa Float64
@test dual_start_value(con) == 2.0
set_dual_start_value(con, nothing)
@test dual_start_value(con) === nothing
Expand All @@ -925,6 +926,9 @@ function test_dual_start_vector()
@test dual_start_value(con_vec) === nothing
set_dual_start_value(con_vec, [1.0, 3.0])
@test dual_start_value(con_vec) == [1.0, 3.0]
set_dual_start_value(con_vec, [1, 3])
@test MOI.get(model, MOI.ConstraintDualStart(), con_vec) isa Vector{Float64}
@test dual_start_value(con_vec) == [1, 3]
set_dual_start_value(con_vec, nothing)
@test dual_start_value(con_vec) === nothing
return
Expand All @@ -936,6 +940,7 @@ function test_primal_start()
con = @constraint(model, 2x <= 1)
@test start_value(con) === nothing
set_start_value(con, 2)
@test MOI.get(model, MOI.ConstraintPrimalStart(), con) isa Float64
@test start_value(con) == 2.0
set_start_value(con, nothing)
@test start_value(con) === nothing
Expand All @@ -949,6 +954,10 @@ function test_primal_start_vector()
@test start_value(con_vec) === nothing
set_start_value(con_vec, [1.0, 3.0])
@test start_value(con_vec) == [1.0, 3.0]
set_start_value(con_vec, [1, 3])
attr = MOI.ConstraintPrimalStart()
@test MOI.get(model, attr, con_vec) isa Vector{Float64}
@test start_value(con_vec) == [1, 3]
set_start_value(con_vec, nothing)
@test start_value(con_vec) === nothing
return
Expand Down

0 comments on commit 3015564

Please sign in to comment.