# Error: JuMP model variable not defined

I am trying to use the package Dualization.jl, to reformulate a primal problem and solving the dual model separately. However, after solving I am not able to access the solution (the variable values) as they don’t seem to be attached to the JuMP model generated by the function `dualize`.

``````model = Model()
@variables(model, begin
x1>=0
x2
x3
x4<=0
end)

@constraint(model, con1, x1 - 2*x2 + 3*x3 + 4*x4 <= 3)
@constraint(model, con2, x2 + 3*x3 <= -5)
@constraint(model, con3, 2*x1 - 3*x2 - 7*x3 - 4*x4 == -2)

@objective(model, Min, 3*x1 + 2*x2 - 3*x3 + 4*x4)

dual_model = dualize(model; dual_names = DualNames("y_", "y_"))

set_optimizer(dual_model, Gurobi.Optimizer)
optimize!(dual_model)

sol = (value.(y_con1_1), value.(y_con2_1), value.(y_con3_1))
println(sol)

``````

On running this script, the `dual_model` is solved correctly, but then on trying to access the solution it throws an error: `UndefVarError: y_con1_1 not defined`.

I am probably missing out on something trivial here. Can someone point it out? Thank you.

The Julia variable `y_con1_1` is not assigned to anything even if there is a variable with name `y_con1_1` in the model `dual_model`. You can get a variable by its name with `variable_by_name`, e.g.

``````y_con1_1 = variable_by_name(dual_model, "y_con1_1")
``````
3 Likes

This works! Thank you.

@blegat I have a follow up question: In actual, I have a large primal problem with JuMP constraint defined as an array (say `con[1:100])`. Now, the `dual_model` variables form an array. Can I still use `variable_by_name` somehow to assign them all at once to a Julia array after the `dual_model is solved`. Since `variable_by_name` is taking variable name as a string as the parameter, I am not sure how to assign them all at once.

You can assign `y_con` to a vector:

``````y_con = [variable_by_name(dual_model, "y_con[\$i]") for i in 1:100]
``````
2 Likes

Thank you!

1 Like