Evaluating constraints at fixed points (and extracting their variables) in JuMP

Buried in ? value, there is

help?> value
search: value values setvalue getvalue set_value fix_value has_values start_value callback_value
─────────────────────────────────────────────────────────────────────────────────────────────────

# ... lots of other stuff ...

─────────────────────────────────────────────────────────────────────────────────────────────────

  value(ex::NonlinearExpression, var_value::Function)

  Evaluate ex using var_value(v) as the value for each variable v.

So:

using JuMP
model = Model()
@variables(model, begin
    -1 <= x[1:5] <= 1
    -5 <= y[1:3] <= 8
    -30 <= z <= 5
end)
ex = @NLexpression(model, sum(x[i] for i=1:4) - y[1] * y[2] + z)
@NLconstraint(model, ex <= 10)

inp = Dict(
    x[1] => 1, x[2] => 2, x[3] => 3, x[4] => 4, y[1] => 5, y[2] => 6, z => 7
)

# `get(dict, key, default)` is used to avoid specifying unused variables.
value(ex, i -> get(inp, i, 0.0))  # -13.0
3 Likes