# Accessing constraints from JuMP model created in function

**URL:** <https://discourse.julialang.org/t/accessing-constraints-from-jump-model-created-in-function/45044>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [August 16, 2020, 1:05pm UTC](https://discourse.julialang.org/t/accessing-constraints-from-jump-model-created-in-function/45044 "2020-08-16T13:05:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)\
**Post date:** [August 16, 2020, 1:05pm UTC](https://discourse.julialang.org/t/accessing-constraints-from-jump-model-created-in-function/45044/1 "2020-08-16T13:05:57Z")

</div>

Hello,  
I have specified a function which creates a JuMP model as below. I can call the function to create the model, and subsequently can optimize! it. I wish to access the shadow prices/duals of the constraint created within the function. However, these constraints are not within the global scope and so when I try to access it, it does not work. How would i access the duals of the constraints created within the function?

Any help would be greatly appreciated. Thank you!!

```julia

function make_model(model=Model())
    @variable(model,x)
    @objective(model,Min,x^2)
    cons = []
    for a = 1:4
        c = @constraint(model, x >= a)
        push!(cons, c)
    end
    return model
end

model = Model()
set_optimizer(model,Cbc.Optimizer)
pm = make_model(model)
result = optimize!(model)
has_duals(model)
dual(cons)

```

---

<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:** [August 16, 2020, 1:17pm UTC](https://discourse.julialang.org/t/accessing-constraints-from-jump-model-created-in-function/45044/2 "2020-08-16T13:17:08Z")

</div>

Do `@constraint(model, c[a=1:4], x >= a)` and then access the vector of constraint references from the model with `model[:c] `.

---

<div class="post-metadata">

**Author:** ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)\
**Post date:** [August 17, 2020, 3:57am UTC](https://discourse.julialang.org/t/accessing-constraints-from-jump-model-created-in-function/45044/3 "2020-08-17T03:57:06Z")

</div>

Thank you though, I would like to nest the constrain inside a for loop as there are a few precalculation steps to actually creating the constraint. Is there a way to use the naming convention you outlined within a for loop?

---

<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 17, 2020, 5:04am UTC](https://discourse.julialang.org/t/accessing-constraints-from-jump-model-created-in-function/45044/4 "2020-08-17T05:04:13Z")

</div>

`cons` is a local variable to `make_model`. You need to return it to the outer scope:

```nohighlight
function make_model(model=Model())
    @variable(model,x)
    @objective(model,Min,x^2)
    cons = []
    for a = 1:4
        c = @constraint(model, x >= a)
        push!(cons, c)
    end
    return model, cons
end

model = Model()
set_optimizer(model,Cbc.Optimizer)
pm, cons = make_model(model)
result = optimize!(model)
has_duals(model)
dual.(cons)

```

---

<div class="post-metadata">

**Author:** ![fnbillimoria](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fnbillimoria/32/16518_2.png) [@fnbillimoria](https://discourse.julialang.org/u/fnbillimoria)\
**Post date:** [August 17, 2020, 6:07am UTC](https://discourse.julialang.org/t/accessing-constraints-from-jump-model-created-in-function/45044/5 "2020-08-17T06:07:16Z")

</div>

Thank you ! That works great!
