# Adding new constraints

**URL:** <https://discourse.julialang.org/t/adding-new-constraints/114884>\
**Category:** Optimization (Mathematical)\
**Created:** [May 29, 2024, 1:37am UTC](https://discourse.julialang.org/t/adding-new-constraints/114884 "2024-05-29T01:37:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)\
**Post date:** [May 29, 2024, 1:37am UTC](https://discourse.julialang.org/t/adding-new-constraints/114884/1 "2024-05-29T01:37:17Z")

</div>

Dear all,  
I have a MWE where I want to add new constraints into a model inside a function:

```julia
m,n=10,20
A=rand(-10:10,m,n)
b=[sum(A[i,:]) for i in 1:m]/2

function Basic_model(A,b,n)
    modelo = Model(Gurobi.Optimizer)
    @variable(modelo, x[1:n] >=0, Bin)
    @objective(modelo, Min, sum(x[i] for i in 1:n))
    @constraint(modelo,[i in 1:m],
    A*x .>= b
    )
    @constraint(modelo, con, x[3] + x[2] == 1)
    return (modelo)
end
modelo = Basic_model(A,b,n)
@constraint(modelo, x[1] + x[5] ==1)
optimize!(modelo)

```

The function `Basic_model` only build the model to optimize in the future. I want to add new constraints, like this:

```julia
@constraint(modelo, x[1] + x[5] ==1)

```

but this does not work.  
Could be possible I build the main model and add/remove new constraints iteratively, outside the function? What is the wrong in the above code?  
For some reason, I need to add/remove constraints after the construction of the main model.  
O don’t want to rebuild my model several times (because the insertion/remotion of constraints) from initial stage to save computational effort.  
Thanks.

---

<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:** [May 29, 2024, 2:12am UTC](https://discourse.julialang.org/t/adding-new-constraints/114884/2 "2024-05-29T02:12:13Z")

</div>

Your error is:

```Julia
julia> using JuMP, Gurobi

julia> function Basic_model(A, b, n)
           modelo = Model(Gurobi.Optimizer)
           @variable(modelo, x[1:n] >= 0, Bin)
           @objective(modelo, Min, sum(x[i] for i in 1:n))
           @constraint(modelo,[i in 1:m], A * x .>= b)
           @constraint(modelo, con, x[3] + x[2] == 1)
           return modelo
       end
Basic_model (generic function with 1 method)

julia> m, n = 10, 20
(10, 20)

julia> A = rand(-10:10, m, n);

julia> b = [sum(A[i, :]) for i in 1:m] / 2;

julia> modelo = Basic_model(A, b, n);

julia> @constraint(modelo, x[1] + x[5] == 1)
ERROR: UndefVarError: `x` not defined
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/MutableArithmetics/iovKe/src/rewrite.jl:325 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/JuMP/Gwn88/src/macros.jl:257 [inlined]
 [3] macro expansion
   @ ~/.julia/packages/JuMP/Gwn88/src/macros/@constraint.jl:131 [inlined]
 [4] macro expansion
   @ ~/.julia/packages/JuMP/Gwn88/src/macros.jl:393 [inlined]
 [5] top-level scope
   @ REPL[7]:1

```

This is because `x` is defined inside the function, and it is not accessible outside.

To fix, you either need to do:

```julia
julia> x = modelo[:x]
20-element Vector{VariableRef}:
 x[1]
 x[2]
 x[3]
 x[4]
 x[5]
 x[6]
 x[7]
 x[8]
 x[9]
 x[10]
 x[11]
 x[12]
 x[13]
 x[14]
 x[15]
 x[16]
 x[17]
 x[18]
 x[19]
 x[20]

julia> @constraint(modelo, x[1] + x[5] == 1)
x[1] + x[5] = 1

```

Or you need to return `x` from the function:

```Julia
using JuMP, Gurobi
function Basic_model(A, b, n)
    modelo = Model(Gurobi.Optimizer)
    @variable(modelo, x[1:n] >= 0, Bin)
    @objective(modelo, Min, sum(x[i] for i in 1:n))
    @constraint(modelo,[i in 1:m], A * x .>= b)
    @constraint(modelo, con, x[3] + x[2] == 1)
    return modelo, x
end
modelo, x = Basic_model(A, b, n);

```

It may help if you read [Design patterns for larger models · JuMP](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/design_patterns_for_larger_models/)
