# UndefVarError in JuMP constraint

**URL:** <https://discourse.julialang.org/t/undefvarerror-in-jump-constraint/15701>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, debugging\
**Created:** [September 30, 2018, 4:45pm UTC](https://discourse.julialang.org/t/undefvarerror-in-jump-constraint/15701 "2018-09-30T16:45:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chisquared](https://avatars.discourse-cdn.com/v4/letter/c/87869e/32.png) [@chisquared](https://discourse.julialang.org/u/chisquared)\
**Post date:** [September 30, 2018, 4:45pm UTC](https://discourse.julialang.org/t/undefvarerror-in-jump-constraint/15701/1 "2018-09-30T16:45:40Z")

</div>

I’m using the dev version of JuMP (primarily for compatibility with Julia 1.0) with GLPK, and I encountered this error which I can’t figure out:

```julia
ERROR: LoadError: UndefVarError: ##366 not defined
Stacktrace:
 [1] macro expansion at /Users/chisquared/.julia/dev/JuMP/src/parseexpr.jl:354 [inlined]
 [2] top-level scope at /Users/chisquared/.julia/dev/JuMP/src/macros.jl:286
 [3] include_string(::Module, ::String, ::String) at ./loading.jl:1002
 [4] (::getfield(Atom, Symbol("##128#133")){String,String,Module})() at /Users/chisquared/.julia/packages/Atom/Fha1N/src/eval.jl:120
 [5] withpath(::getfield(Atom, Symbol("##128#133")){String,String,Module}, ::String) at /Users/chisquared/.julia/packages/CodeTools/hB4Hy/src/utils.jl:30
 [6] withpath at /Users/chisquared/.julia/packages/Atom/Fha1N/src/eval.jl:46 [inlined]
 [7] #127 at /Users/chisquared/.julia/packages/Atom/Fha1N/src/eval.jl:117 [inlined]
 [8] hideprompt(::getfield(Atom, Symbol("##127#132")){String,String,Module}) at /Users/chisquared/.julia/packages/Atom/Fha1N/src/repl.jl:84
 [9] macro expansion at /Users/chisquared/.julia/packages/Atom/Fha1N/src/eval.jl:116 [inlined]
 [10] (::getfield(Atom, Symbol("##126#131")){Dict{String,Any}})() at ./task.jl:85
in expression starting at /Users/chisquared/Dropbox/Julia/jumperr.jl:16

```

This is the code that generates the error:

```julia
using JuMP, GLPK

pH = 1/2
pL = 1 - pH
vH = 2/3
vL = 1/3

sigL = [1/3 1/3 1/3]
sigH = [1/3 1/3 1/3]
model = Model(with_optimizer(GLPK.Optimizer))
@variable(model,0 <= xH <= 1)
@variable(model,0 <= tH <= 1)
@variable(model,xL[1:length(sigL)],Bin)
@variable(model,tL[1:length(sigL)],Bin)

@constraint(model,ICH,vH*xH - tH >= sum(sigH[i]*(vH * xL[i] - tL[i]) for i in 1:length(sigH)))

```

The error occurs exclusively in the last line. If instead, I replace the last line with

```julia
@constraint(model,IRL,sum(sigL[i]*(vL*xL[i] - tL[i]) for i in 1:length(sigL)) >= 0)

```

I get a very similar, but different error:

```julia
ERROR: LoadError: UndefVarError: q not defined

```

Could someone explain what’s going on? I already have a workaround using `dot`, but I’d like to understand what is happening here. Thanks.

---

<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:** [October 1, 2018, 10:36am UTC](https://discourse.julialang.org/t/undefvarerror-in-jump-constraint/15701/2 "2018-10-01T10:36:55Z")

</div>

This was fixed in [https://github.com/JuliaOpt/JuMP.jl/pull/1497](https://github.com/JuliaOpt/JuMP.jl/pull/1497) a few days ago but the PR is not merged yet.  
You can do

```julia
] add JuMP#bl/hygiene

```

to use the fix.

---

<div class="post-metadata">

**Author:** ![chisquared](https://avatars.discourse-cdn.com/v4/letter/c/87869e/32.png) [@chisquared](https://discourse.julialang.org/u/chisquared)\
**Post date:** [October 2, 2018, 9:17pm UTC](https://discourse.julialang.org/t/undefvarerror-in-jump-constraint/15701/3 "2018-10-02T21:17:43Z")

</div>

This works. Thanks!

---

<div class="post-metadata">

**Author:** ![chisquared](https://avatars.discourse-cdn.com/v4/letter/c/87869e/32.png) [@chisquared](https://discourse.julialang.org/u/chisquared)\
**Post date:** [October 2, 2018, 9:35pm UTC](https://discourse.julialang.org/t/undefvarerror-in-jump-constraint/15701/4 "2018-10-02T21:35:08Z")

</div>

By the way, while your PR fixes the problems in my original post, but there still seems to be a similar error from the following line:

```julia
@objective(model,Max,pH * tH + pL * sum(sigL[i]*tL[i] for i in 1:length(sigL)))

```

Appending this line to the code in my original post gives the following error message:

```julia
ERROR: UndefVarError: ##385 not defined
Stacktrace:
 [1] macro expansion at /Users/chisquared/.julia/packages/JuMP/ETBj9/src/parseexpr.jl:354 [inlined]
 [2] top-level scope at /Users/chisquared/.julia/packages/JuMP/ETBj9/src/macros.jl:876

```

---

<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:** [October 3, 2018, 9:20am UTC](https://discourse.julialang.org/t/undefvarerror-in-jump-constraint/15701/5 "2018-10-03T09:20:22Z")

</div>

Should be fixed in [https://github.com/JuliaOpt/JuMP.jl/pull/1517](https://github.com/JuliaOpt/JuMP.jl/pull/1517)
