# \`UndefVarError: x not defined\` but x is in the model

**URL:** https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [September 30, 2019, 2:23pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326 "2019-09-30T14:23:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ariel\_Cruz\_Cruz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ariel_cruz_cruz/32/10377_2.png) [@Ariel\_Cruz\_Cruz](https://discourse.julialang.org/u/Ariel_Cruz_Cruz)
#### Post date: [September 30, 2019, 2:23pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326/1 "2019-09-30T14:23:06Z")

</div>

Hi, I’m having the next problem:

I want to solve a given optimization problem using a **JuMP** model. In the algorithm that I am using I need to pass the model itself to another function having just the variables and I will construct the model (objective function and constraints) in this function. When I do that in the function I have the model but when I try to access to any variable I get the next Error:

```julia
>>@show m
m = A JuMP Model
Feasibility problem with:
Variables: 4
`VariableRef`-in-`MathOptInterface.EqualTo{Float64}`: 1 constraint
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
Names registered in the model: l, x, y

>>@show x
UndefVarError: x not defined

```

Can you help me?

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [September 30, 2019, 3:44pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326/2 "2019-09-30T15:44:43Z")

</div>

You can do

```julia
x = m[:x]

```

See [the doc](https://www.juliaopt.org/JuMP.jl/v0.20.0/variables/#What-is-a-JuMP-variable?-1) for more details.

---

<div class="post-metadata">

### Author: ![Ariel\_Cruz\_Cruz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ariel_cruz_cruz/32/10377_2.png) [@Ariel\_Cruz\_Cruz](https://discourse.julialang.org/u/Ariel_Cruz_Cruz)
#### Post date: [September 30, 2019, 3:59pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326/3 "2019-09-30T15:59:04Z")

</div>

Thank you for your help, but I noticed right now that my problems isn’t that, but when I use Expressions and I want to use the `eval()` of this expressions as my objective function or constraints, like this:

```julia
m = Model()
@variable(m, x[1:2])
f = :(x[1])
@objective(m, Min, eval(f))

```

That works, but if I do exactly the same inside a function I get the next Error:

```julia
function fun()
    m = Model()
    @variable(m, x[1:2])
    f = :(x[1])
    @objective(m, Min, eval(f))
end

fun()
ERROR: VariableNotOwned{VariableRef}(x[1])

```

I want to keep using the expressions, it’s an important part of my algorithm. Please do you know why is this happening?

---

<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: [September 30, 2019, 4:03pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326/4 "2019-09-30T16:03:55Z")

</div>

Using `eval` is generally a sign that you shouldn’t be doing what you are trying to do. In this case, `eval` is `eval`ing things in the global-scope, not function scope.

What are you trying to do? And why/how are you building up `f` as an expression?

---

<div class="post-metadata">

### Author: ![Ariel\_Cruz\_Cruz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ariel_cruz_cruz/32/10377_2.png) [@Ariel\_Cruz\_Cruz](https://discourse.julialang.org/u/Ariel_Cruz_Cruz)
#### Post date: [September 30, 2019, 4:09pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326/5 "2019-09-30T16:09:45Z")

</div>

Ok, first at all thank you for your answer.

I’m trying to create a function that solve a class of problem doing an specific algorithm. This function must solve any problem of this class and isn’t just build the model and solve, so the idea is that I must pass to this function all the elements to build the model inside. My idea was passing the objective function and the constraints as expressions. Do you have any suggestion?

---

<div class="post-metadata">

### Author: ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)
#### Post date: [September 30, 2019, 4:20pm UTC](https://discourse.julialang.org/t/undefvarerror-x-not-defined-but-x-is-in-the-model/29326/6 "2019-09-30T16:20:58Z")

</div>

> My idea was passing the objective function and the constraints as expressions. Do you have any suggestion?

I would pass a function that takes as argument the model and variables and calls the macro inside, e.g. instead of

```julia
function fun(f)
    m = Model()
    @variable(m, x[1:2])
    @objective(m, Min, eval(f))
end
fun(:(x[1]))

```

do

```julia
function fun(add_obj)
    m = Model()
    @variable(m, x[1:2])
    add_obj(m, x)
end
fun((m, x) -> @objective(m, Min, x[1]))

```
