# The conflict of variables using @build\_constraint

**URL:** <https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998>\
**Category:** Optimization (Mathematical)\
**Created:** [June 16, 2021, 9:50am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998 "2021-06-16T09:50:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [June 16, 2021, 9:50am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/1 "2021-06-16T09:50:39Z")

</div>

Hi,  
If there are two models in my code

```julia
model1=Model()
@variable(model1,x[1:5])
model2=Model()
@variable(model2,x[1:5])

```

If I use  
`con=@build_constraint(x[1]<=1)`  
the `x[1]` belongs to `model1` or `model2`?  
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:** [June 16, 2021, 11:03pm UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/2 "2021-06-16T23:03:27Z")

</div>

> If I use `con=@build_constraint(x[1]<=1)` the `x[1]` belongs to `model1` or `model2` ?

It depends on when you call it. There is nothing special about `x` things that JuMP macros create; they are regular Julia variables (sometimes called bindings to make clear they are not optimization variables) that can be overwritten.

The [JuMP documentation explains this](https://jump.dev/JuMP.jl/stable/manual/variables/#What-is-a-JuMP-variable?).

In your example:

```Julia
model1=Model()
@variable(model1,x[1:5])

# `x` is a binding to the vector of variables `model1[:x]`

model2=Model()
@variable(model2,x[1:5])

# Now `x` is a binding to the vector of variables `model2[:x]`

```

---

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [June 17, 2021, 1:21am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/3 "2021-06-17T01:21:46Z")

</div>

ok, I have still some confusion. my model is something like this,

```julia
model1=Model()
@variable(model1,x)
@constraint(model1,x<=5)
function lazy_constraints(cb_data)
     model2=Model()
     @variable=(model2,x<=5)
     @optimize!(model2)
     Judge = has_values(model2)
     if Judge == false
           con=build_constraint(x<=2)
     end
end

```

so constraint `x<=2` is added in model1 or model2

---

<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:** [June 17, 2021, 1:26am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/4 "2021-06-17T01:26:40Z")

</div>

Your code is not valid, so I’m not sure what you’re trying to do. Please make a reproducible example:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

In your function `x <= 2` refers to the `x` binding created by `model2`. if you want `x` from `model1`, call the inner x something different so the binding doesn’t get over-written, or use `model1[:x]`.

---

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [June 17, 2021, 1:34am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/5 "2021-06-17T01:34:46Z")

</div>

Thank you @odow. I have understood, just now I have misunderstood the meaning you say.

---

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [June 17, 2021, 1:40am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/6 "2021-06-17T01:40:33Z")

</div>

`MOI.submit(model1, MOI.LazyConstraint(cb_data), con)`, if I use this code, it can also assign con to `model1`?

---

<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:** [June 17, 2021, 1:51am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/7 "2021-06-17T01:51:37Z")

</div>

> it can also assign con to `model1` ?

Only if it refers to variables in `model1`.

I guess you want something like

```Julia
model1 = Model()
@variable(model1, x <= 5)
function lazy_constraints(cb_data)
    model2 = Model()
    @variable(model2, x <= 5)
    optimize!(model2)
    Judge = has_values(model2)
    if Judge == false
        con = @build_constraint(model1[:x] <= 2)
        MOI.submit(model1, MOI.LazyConstraint(cb_data), con)
    end
    return
end

```

---

<div class="post-metadata">

**Author:** ![BYS](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bys/32/25384_2.png) [@BYS](https://discourse.julialang.org/u/BYS)\
**Post date:** [June 17, 2021, 1:58am UTC](https://discourse.julialang.org/t/the-conflict-of-variables-using-build-constraint/62998/8 "2021-06-17T01:58:19Z")

</div>

Thank you for your kindly help, I have understood. It is what I want to express.
