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

Add value for constraints with a custom var_value function #2317

Merged
merged 10 commits into from
Aug 31, 2020
5 changes: 5 additions & 0 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ function value(con_ref::ConstraintRef{Model, <:_MOICON}; result::Int = 1)
return reshape_vector(_constraint_primal(con_ref, result), con_ref.shape)
end

function value(con_ref::ConstraintRef{Model, <:_MOICON}, var_value::Function)
dourouc05 marked this conversation as resolved.
Show resolved Hide resolved
f = jump_function(constraint_object(con_ref))
return reshape_vector(value.(f, Ref(var_value)), con_ref.shape)
dourouc05 marked this conversation as resolved.
Show resolved Hide resolved
end

# Returns the value of MOI.ConstraintPrimal in a type-stable way
function _constraint_primal(
con_ref::ConstraintRef{
Expand Down
4 changes: 4 additions & 0 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,10 @@ function value(v::VariableRef; result::Int = 1)::Float64
return MOI.get(owner_model(v), MOI.VariablePrimal(result), v)
end

function value(v::VariableRef, var_value::Function)
return var_value(v)
end

"""
has_values(model::Model; result::Int = 1)

Expand Down
15 changes: 15 additions & 0 deletions test/constraint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,21 @@ function test_Model_add_to_function_constant_vector(::Any, ::Any)
@test JuMP.moi_set(con) == MOI.Nonnegatives(2)
end

function test_Model_value_constraint_var(ModelType, ::Any)
model = ModelType()
@variable(model, x[1:2])
@constraint(model, c1, x[1] + x[2] <= 3.0)
@constraint(model, c2, x[1]^2 + x[2]^2 <= 3.0)
@constraint(model, c3, [1.0, x[1], x[2]] in SecondOrderCone())

vals = Dict(x[1] => 1.0, x[2] => 2.0)
f = vidx -> vals[vidx]

@test value(c1, f) === 3.0 # Affine expression
@test value(c2, f) === 5.0 # Quadratic expression
@test value(c3, f) == [1.0, 1.0, 2.0] # Vector expression
end

function _test_shadow_price_util(model_string, constraint_dual, constraint_shadow)
model = Model()
MOIU.loadfromstring!(backend(model), model_string)
Expand Down
11 changes: 11 additions & 0 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,17 @@ function test_Model_value(::Any, ::Any)
@test value(JuMP._MA.Zero()) === 0.0
end

function test_Model_value_var(ModelType, ::Any)
model = ModelType()
@variable(model, x[1:2])

vals = Dict(x[1] => 1.0, x[2] => 2.0)
f = vidx -> vals[vidx]

@test value(x[1], f) === 1.0
@test value(x[2], f) === 2.0
end

function test_Model_relax_integrality(::Any, ::Any)
model = Model()
@variable(model, x, Bin)
Expand Down