From 30ea6b91be1f069802708d9e22f74b7eb038b260 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 12 Nov 2023 13:12:07 +0100 Subject: [PATCH] Add tests --- src/constraints.jl | 38 +++++++++++++++++++++++++++----------- test/test_constraint.jl | 9 +++++++++ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/constraints.jl b/src/constraints.jl index e6aa6f16b88..eb74ec33403 100644 --- a/src/constraints.jl +++ b/src/constraints.jl @@ -85,6 +85,27 @@ function dual_start_value( return reshape_vector(_dual_start(con_ref), dual_shape(con_ref.shape)) end +function _set_converted( + attr::MOI.AbstractConstraintAttribute, + con_ref::ConstraintRef{ + <:AbstractModel, + <:MOI.ConstraintIndex{ + F, + }, + }, + value, +) where {F<:MOI.AbstractFunction} + model = owner_model(con_ref) + V = MOI.Utilities.value_type(value_type(typeof(model)), F) + MOI.set( + model, + attr, + con_ref, + _convert_if_something(V, value), + ) + return +end + function _value_type( ::Type{M}, ::Type{F}, @@ -119,13 +140,10 @@ function set_dual_start_value( }, value, ) - model = owner_model(con_ref) - vectorized_value = vectorize(value, dual_shape(con_ref.shape)) - MOI.set( - model, + _set_converted( MOI.ConstraintDualStart(), con_ref, - model_convert(model, vectorized_value), + vectorize(value, dual_shape(con_ref.shape)), ) return end @@ -152,7 +170,7 @@ function set_dual_start_value( }, value, ) - MOI.set(owner_model(con_ref), MOI.ConstraintDualStart(), con_ref, value) + _set_converted(MOI.ConstraintDualStart(), con_ref, value) return end @@ -175,12 +193,10 @@ function set_start_value( }, value, ) - model = owner_model(con_ref) - MOI.set( - model, + _set_converted( MOI.ConstraintPrimalStart(), con_ref, - model_convert(model, vectorize(value, con_ref.shape)), + vectorize(value, con_ref.shape), ) return end @@ -209,7 +225,7 @@ function set_start_value( }, value, ) - MOI.set(owner_model(con_ref), MOI.ConstraintPrimalStart(), con_ref, value) + _set_converted(MOI.ConstraintPrimalStart(), con_ref, value) 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