# How to refer to a JuMP variable using a string when the model is defined in a module?

**URL:** <https://discourse.julialang.org/t/how-to-refer-to-a-jump-variable-using-a-string-when-the-model-is-defined-in-a-module/125122>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [January 23, 2025, 12:06pm UTC](https://discourse.julialang.org/t/how-to-refer-to-a-jump-variable-using-a-string-when-the-model-is-defined-in-a-module/125122 "2025-01-23T12:06:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [January 23, 2025, 12:06pm UTC](https://discourse.julialang.org/t/how-to-refer-to-a-jump-variable-using-a-string-when-the-model-is-defined-in-a-module/125122/1 "2025-01-23T12:06:48Z")

</div>

Hello, in a JuMP model I need to fix some variables, and these variables to fix are defined in a dictionary.

This works:

```julia
using JuMP, Ipopt
fvars = Dict{String,Float64}("c" => 5)
m = Model(Ipopt.Optimizer)
# [...]
@variable(m, c) 
for (k,v) in fvars
    fix(eval(Symbol(k)), v; force = true)
end
# [...]

```

However if I wrap the model in a module I got an `UndefVarError`:

```julia
module Foo 
using JuMP, Ipopt
function foo()
    fvars = Dict{String,Float64}("c" => 5)
    m = Model(Ipopt.Optimizer)
    # [...]
    @variable(m, c) 
    for (k,v) in fvars
        fix(eval(Symbol(k)), v; force = true)
    end
    # [...]
end
end

```

`ERROR: UndefVarError: `c`not defined in`Main.Foo``

I guess the “eval” somehow happens too early, is there any workaround ?

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [January 23, 2025, 2:07pm UTC](https://discourse.julialang.org/t/how-to-refer-to-a-jump-variable-using-a-string-when-the-model-is-defined-in-a-module/125122/2 "2025-01-23T14:07:55Z")

</div>

Actually the issue was that the model was in a function, not much that it was embedded in a module…

I solved by using

`global c = @variable(m, c) `

but I guess if there is a better way, as this may kill the performances of my program (not much of the solving the optimization itself)…

---

<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:** [January 23, 2025, 6:31pm UTC](https://discourse.julialang.org/t/how-to-refer-to-a-jump-variable-using-a-string-when-the-model-is-defined-in-a-module/125122/3 "2025-01-23T18:31:20Z")

</div>

First. Never use `eval`. It is always the wrong thing to do.

Either use `variable_by_name`, or, if the variable is registered, convert the string to a symbol and do `model[Symbol(k)]`:

```Julia
using JuMP, Ipopt
function foo()
    fvars = Dict{String,Float64}("c" => 5)
    m = Model(Ipopt.Optimizer)
    @variable(m, c) 
    for (k, v) in fvars
        # Option 1
        x = variable_by_name(m, k)
        fix(x, v; force = true)
        # Option 2
        x = m[Symbol(k)]
        fix(x, v; force = true)
    end
end

```

More generally, consider using some sort of data structure that you control:

```Julia
using JuMP, Ipopt
function foo()
    fvars = Dict{String,Float64}("c" => 5)
    my_variables = Dict{String,VariableRef}()
    m = Model(Ipopt.Optimizer)
    my_variables["c"] = @variable(m, c)
    for (k, v) in fvars
        fix(my_variables[k], v; force = true)
    end
end

```

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [January 24, 2025, 8:23am UTC](https://discourse.julialang.org/t/how-to-refer-to-a-jump-variable-using-a-string-when-the-model-is-defined-in-a-module/125122/4 "2025-01-24T08:23:26Z")

</div>

Thank you.

Just to notice that in my specific model, adding a constraint `@constraint(m, x == v)` is much faster than using `fix`.
