diff --git a/src/constraints.jl b/src/constraints.jl index 64c6e74655d..7c6d9309b7b 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -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}}, @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/test/test_constraint.jl b/test/test_constraint.jl index 59c734d4865..20b43dab372 100644 --- a/test/test_constraint.jl +++ b/test/test_constraint.jl @@ -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 @@ -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 @@ -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 @@ -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