# Best practices for passing JuMP decision variables and data to a function for adding constraints

**URL:** https://discourse.julialang.org/t/best-practices-for-passing-jump-decision-variables-and-data-to-a-function-for-adding-constraints/122853
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [November 20, 2024, 3:12pm UTC](https://discourse.julialang.org/t/best-practices-for-passing-jump-decision-variables-and-data-to-a-function-for-adding-constraints/122853 "2024-11-20T15:12:44Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)
#### Post date: [November 20, 2024, 3:12pm UTC](https://discourse.julialang.org/t/best-practices-for-passing-jump-decision-variables-and-data-to-a-function-for-adding-constraints/122853/1 "2024-11-20T15:12:45Z")

</div>

I have an integer optimization problem of the form:

 {\text{minimize}}\_{x\_{1},\ldots,x\_{r}} \quad c\_{1}^{\top}x\_{1}+c\_{2}^{\top}x\_{2}+\ldots+c\_{r}^{\top}x\_{r}\\​\text{subject to} \quad B\_{1}x\_{1}+B\_{2}x\_{2}+\ldots+B\_{r}x\_{r}\leq d\\​\quad \quad\quad\;\; A\_{i}x\_{i}\leq b\_{i},\quad i\in\{1,2,\ldots,r\}\\​\quad \quad\quad\;\; x\_{i}\in\mathbb{Z}^{d\_{i}},\quad i\in\{1,2,\ldots,r\}.

I would like to model each the uncoupled constraints A\_{i}x\_{i}\leq b\_{i} by passing the data \{A\_{i},b\_{i}\} and decision variable \{x\_{i}\} to a function, say `add_A_b_constraint`and then implementing these constraints by calling the function `add_A_b_constraint` r times in a loop. In the JuMP documentation I see an example at [https://jump.dev/JuMP.jl/stable/tutorials/getting\_started/design\_patterns\_for\_larger\_models/#Generalize-constraints-and-objectives](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/design_patterns_for_larger_models/#Generalize-constraints-and-objectives) where we can access each registered variable by passing the JuMP model, say `model` itself to the function `add_A_b_constraint` and then accessing the registered variable in the function by calling syntax such as `model[:x_i]`, but then this does not work for all x\_i uniformly.

My question is in such a situation what would be the best practice to pass the data \{A\_{i},b\_{i}\} and decision variable \{x\_{i}\} to a JuMP function to model A\_{i}x\_{i}\leq b\_{i}? Thanks in advance.

---

<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: [November 20, 2024, 7:39pm UTC](https://discourse.julialang.org/t/best-practices-for-passing-jump-decision-variables-and-data-to-a-function-for-adding-constraints/122853/2 "2024-11-20T19:39:44Z")

</div>

Just do something like this? You don’t have to follow the one-true-way to use JuMP. Just do whatever is most convenient.

```julia
function add_A_b_constraint(model, A, b, x)
    @constraint(model, A * x <= b)
end
A = [rand(2, 3) for _ in 1:2]
b = [rand(2) for _ in 1:2]
model = Model()
@variable(model, x[1:3, 1:2])
for i in 1:2
    add_A_b_constraint(model, A[i], b[i], x[:, i])
end

```

---

<div class="post-metadata">

### Author: ![Shuvomoy\_Das\_Gupta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuvomoy_das_gupta/32/10069_2.png) [@Shuvomoy\_Das\_Gupta](https://discourse.julialang.org/u/Shuvomoy_Das_Gupta)
#### Post date: [November 20, 2024, 8:15pm UTC](https://discourse.julialang.org/t/best-practices-for-passing-jump-decision-variables-and-data-to-a-function-for-adding-constraints/122853/3 "2024-11-20T20:15:31Z")

</div>

Okay sounds good, thanks Oscar!
