# Error: JuMP model variable not defined

**URL:** https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670
**Category:** Optimization (Mathematical)
**Tags:** question, jump, error
**Created:** [November 24, 2020, 5:15am UTC](https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670 "2020-11-24T05:15:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)
#### Post date: [November 24, 2020, 5:15am UTC](https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670/1 "2020-11-24T05:15:07Z")

</div>

I am trying to use the package [Dualization.jl](https://github.com/jump-dev/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`.

```julia
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.

---

<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: [November 24, 2020, 9:24am UTC](https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670/2 "2020-11-24T09:24:33Z")

</div>

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`](https://jump.dev/JuMP.jl/v0.21.5/variables/#JuMP.variable_by_name), e.g.

```julia
y_con1_1 = variable_by_name(dual_model, "y_con1_1")

```

---

<div class="post-metadata">

### Author: ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)
#### Post date: [November 24, 2020, 10:04am UTC](https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670/3 "2020-11-24T10:04:06Z")

</div>

This works! Thank you.

---

<div class="post-metadata">

### Author: ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)
#### Post date: [November 24, 2020, 11:48am UTC](https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670/4 "2020-11-24T11:48:20Z")

</div>

@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.

---

<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: [November 24, 2020, 12:13pm UTC](https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670/5 "2020-11-24T12:13:43Z")

</div>

You can assign `y_con` to a vector:

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

```

---

<div class="post-metadata">

### Author: ![trathi](https://avatars.discourse-cdn.com/v4/letter/t/7c8e57/32.png) [@trathi](https://discourse.julialang.org/u/trathi)
#### Post date: [November 24, 2020, 12:22pm UTC](https://discourse.julialang.org/t/error-jump-model-variable-not-defined/50670/6 "2020-11-24T12:22:24Z")

</div>

Thank you!
