juliajulia-jump

How to use variable references to evaluate expressions at a fixed point?


I am pretty new to julia and JuMP and I am stuck how to efficiently evaluate the objective or the expressions in a model at a fixed point.

Googling brought me to this solution in the julia discourse, where a dictionary is used to hold the fixed variable values:

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)

value(i -> get(inp, i, 0.0), ex) 

However I have my fixed values saved in several arrays, one for each variable:

last_x = Containers.DenseAxisArray(vcat(1:4), vcat(1:4))
last_y = Containers.DenseAxisArray([5,6], vcat(1:2))
last_z = Containers.DenseAxisArray([7], 1)

How can I evaluate the expression ex of the model such that each variable reference of the model is pointed to the corresponding value in the arrays? I was thinking about taking the indices from the variables and then looping through the arrays might be a way, but I can only get the variable reference as a whole:

model[:x] 
for key in eachindex(model[:x]) # will give only the variable reference
    println((model[:x][key]))
end

I could construct a dictionary as in the linked solution but was searching for a way to avoid this intermediate step. Thanks for any suggestions!


Solution

  • Just make the dictionary.

    Note that the @NL syntax is now legacy. You can remove in favor of @expression and @constraint.