# JuMP Read model from file: ERROR: LoadError: UndefVarError

**URL:** https://discourse.julialang.org/t/jump-read-model-from-file-error-loaderror-undefvarerror/102368
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [August 1, 2023, 7:14pm UTC](https://discourse.julialang.org/t/jump-read-model-from-file-error-loaderror-undefvarerror/102368 "2023-08-01T19:14:25Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Stuart\_Rogers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stuart_rogers/32/10694_2.png) [@Stuart\_Rogers](https://discourse.julialang.org/u/Stuart_Rogers)
#### Post date: [August 1, 2023, 7:14pm UTC](https://discourse.julialang.org/t/jump-read-model-from-file-error-loaderror-undefvarerror/102368/1 "2023-08-01T19:14:26Z")

</div>

I use JuMP to solve a simple MILP read in from the input file test.mps. After solving the model, JuMP generates an undefined variable error when I try to access the value of a variable in the MILP.

test.mps:

NAME  
ROWS  
N OBJ  
G c1  
G c2  
COLUMNS  
x c1 6  
x c2 7  
x OBJ 12  
MARKER ‘MARKER’ ‘INTORG’  
y c1 8  
y c2 12  
y OBJ 20  
RHS  
rhs c1 100  
rhs c2 120  
RANGES  
BOUNDS  
LO bounds x 0  
PL bounds x  
LO bounds y 0  
UP bounds y 3  
ENDATA

Code in Wotan\_read.jl:

using JuMP  
using COPT  
#using HiGHS

model = read\_from\_file(“test.mps”)

opt = COPT.Optimizer  
#opt = HiGHS.Optimizer

#relax\_integrality(model)  
set\_optimizer(model,opt)

print(model)

optimize!(model)  
@show termination\_status(model)  
@show primal\_status(model)  
@show dual\_status(model)  
@show objective\_value(model)  
@show value(x)  
@show value(y)  
@show shadow\_price(c1)  
@show shadow\_price(c2

Output:

$ julia Wotan\_read.jl  
Cardinal Optimizer v6.5.7. Build date Jul 28 2023  
Copyright Cardinal Operations 2023. All Rights Reserved  
Setting parameter ‘LogToConsole’ to 1  
Min 12 x + 20 y  
Subject to  
c1 : 6 x + 8 y ≥ 100  
c2 : 7 x + 12 y ≥ 120  
x ≥ 0  
y ∈ [0, 3]  
y integer  
Model fingerprint: ed27c8a2

Using Cardinal Optimizer v6.5.7 on Linux  
Hardware has 24 cores and 48 threads. Using instruction set X86\_NATIVE (1)  
Minimizing a MIP problem

The original problem has:  
2 rows, 2 columns and 4 non-zero elements  
1 integers

Presolving the problem

The presolved problem has:  
2 rows, 2 columns and 4 non-zero elements  
1 integers

Starting the MIP solver with 32 threads and 64 tasks

```
 Nodes Active LPit/n IntInf BestBound BestSolution Gap Time
     0 1 -- 0 1.520000e+02 -- Inf 0.00s

```

H 0 1 – 0 1.520000e+02 2.657143e+02 42.8% 0.00s  
H 0 1 – 0 1.520000e+02 2.051429e+02 25.9% 0.01s  
0 1 – 1 2.050000e+02 2.051429e+02 0.07% 0.01s  
1 0 0.0 1 2.051429e+02 2.051429e+02 0.00% 0.01s  
1 0 0.0 1 2.051429e+02 2.051429e+02 0.00% 0.01s

Best solution : 205.142857143  
Best bound : 205.142857143  
Best gap : 0.0000%  
Solve time : 0.01  
Solve node : 1  
MIP status : solved  
Solution status : integer optimal (relative gap limit 0.0001)

Violations : absolute relative  
bounds : 0 0  
rows : 0 0  
integrality : 0  
termination\_status(model) = MathOptInterface.OPTIMAL  
primal\_status(model) = MathOptInterface.FEASIBLE\_POINT  
dual\_status(model) = MathOptInterface.NO\_SOLUTION  
objective\_value(model) = 205.14285714285714  
ERROR: LoadError: UndefVarError: `x` not defined  
Stacktrace:  
[1] top-level scope  
@ show.jl:1128  
in expression starting at /home/stuart/julia\_code/Wotan/Wotan\_read.jl:21

---

<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 1, 2023, 9:50pm UTC](https://discourse.julialang.org/t/jump-read-model-from-file-error-loaderror-undefvarerror/102368/2 "2023-08-01T21:50:24Z")

</div>

This is expected.

Here, `value(x)` refers to the Julia binding `x`, not the “x” variable inside the model. There is no binding `x` in the current scope, so you get the `UndefVarError`.

You need something like `x = variable_by_name(model, "x")`. See the docs [JuMP · JuMP](https://jump.dev/JuMP.jl/stable/api/JuMP/#variable_by_name)

See also the note in [Models · JuMP](https://jump.dev/JuMP.jl/stable/manual/models/#Read-a-model-from-file)
