Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Nov 12, 2023
1 parent c42a1b3 commit 30ea6b9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
38 changes: 27 additions & 11 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

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 30ea6b9

Please sign in to comment.