Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert constraint starting values #3571

Merged
merged 6 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
odow marked this conversation as resolved.
Show resolved Hide resolved
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
Loading