If a user wants to know the variable values for a JuMP model that is not exposed to the user directly, what are the options? For example:
using JuMP
using HiGHS
#some arbitrary JuMP model
model = Model(HiGHS.Optimizer)
@variable(model, x[1:3, 1:5] >= 0)
@constraint(model, sum(x[i,i+2] for i in 1:3) >= 5)
@objective(model, Min, sum(sum(x[i,j] for i in 1:3) for j in 1:5))
optimize!(model)
#Just assume that the above model is not exposed to the user and one cannot use `value` function directly. The only way user can query a variable is through the following input command.
variable_of_interest = readline() #x or x[1,:] or x[2,4], etc. #basically a string
#Then, how to make the following function work?
function val_return(m::Model, variable_of_interest)
# convert variable_of_interest to form where I can actually use it
return variable_value
end
# if variable_of_interest = "x", then we return the entire matrix of values
# if variable_of_interest = "x[:,1]", then we return the vector of values
# if variable_of_interest = "x[1,3]", then we return the scalar value
How to make the val_return function work if the input argument is a string? I see that variable_by_name function in JuMP might be useful but it only acts on a single element, i.e variable_by_name(model, "x[1,3]") works but variable_by_name(model, "x") doesn’t. Any help is appreciated. Thanks!
This looks useful. Thanks! I am not super familiar with parsing this way. Any links where I can read more about it? Especially for cases like "x[:,2]".
The real answer is that using a string is the wrong approach.
Do something like:
function val_return(model::Model, variable_of_interest::String)
return value.(model[Symbol(variable_of_interest)])
end
which returns a Matrix{Float64} to the user. Then they can decide whether to call x[:, 1] or x[1, 3]. Don’t make them write Julia code in a string and try to evaluate it.