# Access variables in vector using Symbols

**URL:** <https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [August 7, 2022, 8:11am UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432 "2022-08-07T08:11:17Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 7, 2022, 8:11am UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/1 "2022-08-07T08:11:17Z")

</div>

I have a model with variables defined as `a` is below, I need to be able to access the models variables in a model agnostic way. For this I use Symbols as `model[:b]` will get me `b` for any model which has a variable `b`.

How can I do this when the variables are defined as a vector

```nohighlight
model = Model()
@variable(model, 0 ≤ a[1:3] ≤ 1)
@variable(model, 0 ≤ b ≤ 1)
model[:b]
sa = Symbol(a[1])
sb = Symbol(b)
model[sb] # works
model[sa]
ERROR: KeyError: key Symbol("a[1]") not found

```

A example of why I use symbols, maybe there’s a better way

```nohighlight
models = [copy(model) for _ in 1:4]
[MOI.set(models[ii], MOI.Name(), "Model : $ii") for ii in 1:4]
[m[:b] for m in models] # Works
[m[sa] for m in models] # ERROR: KeyError: key Symbol("a[1]") not found

```

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 7, 2022, 9:42am UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/2 "2022-08-07T09:42:57Z")

</div>

While quite ugly, this does work. It could be made tidier by a PR to `JuMP.getindex`, would this be wanted in JuMP?

```nohighlight
function get_var(m::JuMP.AbstractModel, name::Symbol)
    try
        return JuMP.getindex(m,name)
    catch
        var_name=Symbol(match(r"^\w+", String(name)).match)
        var = JuMP.getindex(m,var_name)
        var_num=parse(Int,(match(r"\d+", String(name)).match))
        return var[var_num]
    end
end

```

---

<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:** [August 9, 2022, 2:52am UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/3 "2022-08-09T02:52:12Z")

</div>

You need `model[:a][1]`. `model[:a]` fetches the vector of variables.

See also `variable_by_name`: [Variables · JuMP](https://jump.dev/JuMP.jl/stable/reference/variables/#JuMP.variable_by_name)

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 9, 2022, 7:04am UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/4 "2022-08-09T07:04:19Z")

</div>

I appreciate this, and it is what I am doing here

> [@this\_josh](#):
>
> `var[var_num]`

But given that the symbol has the vector index information I think it would be beneficial if you could access variables in a vector the same way as you can an individual variable.

While the method I wrote works, to use the `m[sa]` syntax it would require the same function signature as `JuMP.getindex` causing an infinite loop.

Would you consider a PR which allowed `JuMP.getindex` to work with `m[sa]`?

---

<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:** [August 9, 2022, 11:26am UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/5 "2022-08-09T11:26:00Z")

</div>

Did you see `variable_by_name`?

We wont be adding a method for the Symbol lookup. I think you might want the String one instead.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 9, 2022, 1:05pm UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/6 "2022-08-09T13:05:09Z")

</div>

I did, but I was hoping for a method using the concise and consistent `[]`. Although it isn’t a big deal, I can just make a new dispatch for `function String(v::VariableRef)`

```nohighlight
variable_by_name(model,String(Symbol(model[:a][5])))

```

Thank you.

---

<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:** [August 9, 2022, 4:25pm UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/7 "2022-08-09T16:25:12Z")

</div>

The syntax `model[key]` is a short-cut for `object_dictionary(model)[key]`, which has a very particular meaning. In particular, it’s _not_ a mechanism for looking up particular variables and constraints, but also containers of variables and constraints.

We also have to think about more general uses of JuMP. For example if someone did `@variable(model, x[[:a, :b]])`, then they might also expect `model[Symbol("x[:a]")]` to work, but that gets tricky to implement.

---

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [August 10, 2022, 9:17am UTC](https://discourse.julialang.org/t/access-variables-in-vector-using-symbols/85432/8 "2022-08-10T09:17:44Z")

</div>

Thank you for your answer and explaining why this wouldn’t be practical to implement. I’ve adopted the `variable_by_name` methodology.
