# Access JuMP variables queried as string

**URL:** <https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [February 19, 2024, 8:41am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393 "2024-02-19T08:41:44Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [February 19, 2024, 8:41am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/1 "2024-02-19T08:41:44Z")

</div>

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:

```julia
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!

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [February 19, 2024, 9:03am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/2 "2024-02-19T09:03:21Z")

</div>

I recommend writing your MWE to not use Gurobi given it is a commerical solver not everyone has access to.

I don’t currently have JuMP installed but I think `model["x"]` should work?  
Its been a while

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [February 19, 2024, 9:23am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/3 "2024-02-19T09:23:05Z")

</div>

> [@oxinabox](#):
>
> I don’t currently have JuMP installed but I think `model["x"]` should work?

Unfortunately, it doesn’t. `model[:x]` works but again my input is a string.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 19, 2024, 9:45am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/4 "2024-02-19T09:45:33Z")

</div>

convert the input to a symbol?

`model[Symbol("x")]`

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [February 19, 2024, 9:48am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/5 "2024-02-19T09:48:36Z")

</div>

This works only if the input is `"x"`. If the user inputs `"x[1,3]"` or `"x[:,2]"`, this would fail.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 19, 2024, 9:51am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/6 "2024-02-19T09:51:29Z")

</div>

In that case, I guess you have to manually parse the string expression and separate any indexing from the variable name. It shouldn’t be too hard?

```julia
julia> Meta.parse("x[1,1]")
:(x[1, 1])

julia> Meta.parse("x[1,1]") |> dump
Expr
  head: Symbol ref
  args: Array{Any}((3,))
    1: Symbol x
    2: Int64 1
    3: Int64 1

```

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [February 19, 2024, 9:56am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/7 "2024-02-19T09:56:59Z")

</div>

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]"`.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 19, 2024, 11:17am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/8 "2024-02-19T11:17:13Z")

</div>

> [@math\_opt](#):
>
> Thanks! I am not super familiar with parsing this way.

Neither am I 🙂

If all you can ever expect to get in the string is `x` or `x[inds...]` you could split it up something like

```julia
ex = parse(input)
if ex isa Symbol
    model[ex]
else
    getindex(model[ex.args[1]], eval.(ex.args[2:end])...)
end

```

(untested)

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [February 19, 2024, 11:34am UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/9 "2024-02-19T11:34:00Z")

</div>

This works. Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 19, 2024, 5:52pm UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/10 "2024-02-19T17:52:17Z")

</div>

_Please_ do not use `eval`. It is unsafe, and it may return incorrect solutions if called from within a function instead of the top-level.

See also [julia - Adding constraints to jump model from dict - Stack Overflow](https://stackoverflow.com/a/77999249/13591160)

The real answer is that using a string is the wrong approach.

Do something like:

```julia
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.

---

<div class="post-metadata">

**Author:** ![math\_opt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/math_opt/32/25317_2.png) [@math\_opt](https://discourse.julialang.org/u/math_opt)\
**Post date:** [February 20, 2024, 12:16pm UTC](https://discourse.julialang.org/t/access-jump-variables-queried-as-string/110393/11 "2024-02-20T12:16:06Z")

</div>

Makes sense. Thanks!
