JuMP Read model from file: ERROR: LoadError: UndefVarError

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

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

See also the note in Models · JuMP