# JuMP constraints independent of a particular model

**URL:** <https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [April 27, 2020, 4:28pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314 "2020-04-27T16:28:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomas\_Uribe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_uribe/32/14402_2.png) [@Tomas\_Uribe](https://discourse.julialang.org/u/Tomas_Uribe)\
**Post date:** [April 27, 2020, 4:28pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/1 "2020-04-27T16:28:44Z")

</div>

Is there a way to represent a JuMP constraint independently of a specific model (even if it is purely syntactic)? The macros for creating variables and constraints require a model, of course… What would be a convenient model-independent representation of constraints?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 27, 2020, 7:37pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/2 "2020-04-27T19:37:07Z")

</div>

The manual has a section on [constructing constraints without adding them to the model](http://www.juliaopt.org/JuMP.jl/stable/constraints/#Constructing-constraints-without-adding-them-to-the-model-1).

---

<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:** [April 27, 2020, 7:57pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/3 "2020-04-27T19:57:39Z")

</div>

No. You cannot create a JuMP constraint independently of a model.

p.s. You should use the Optimization domain for JuMP-related questions: [Optimization (Mathematical) - JuliaLang](https://discourse.julialang.org/c/domain/opt/13)

---

<div class="post-metadata">

**Author:** ![Tomas\_Uribe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_uribe/32/14402_2.png) [@Tomas\_Uribe](https://discourse.julialang.org/u/Tomas_Uribe)\
**Post date:** [April 27, 2020, 7:58pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/4 "2020-04-27T19:58:27Z")

</div>

Thanks, that gets me closer to what I want. Two follow-up questions:

(1) The variables in the constraint are still tied to the model, though; any way to avoid this? (In the worst case, I could represent the constraint with a string, but don’t know how to parse it… Was also thinking of a Julia Expr.)

(2) How do I add one of these constraints to a model?

---

<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:** [April 27, 2020, 8:00pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/5 "2020-04-27T20:00:34Z")

</div>

A better question is: why do you want to do this? `@build_constraint` is mainly used for callbacks ([Callbacks · JuMP](https://www.juliaopt.org/JuMP.jl/stable/callbacks/)).

---

<div class="post-metadata">

**Author:** ![Tomas\_Uribe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_uribe/32/14402_2.png) [@Tomas\_Uribe](https://discourse.julialang.org/u/Tomas_Uribe)\
**Post date:** [April 27, 2020, 8:00pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/6 "2020-04-27T20:00:52Z")

</div>

Sorry, thought the “jump” tag would be enough (first time using discourse). Any way to move a question over?

---

<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:** [April 27, 2020, 8:05pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/7 "2020-04-27T20:05:46Z")

</div>

If you click the pencil by the title, there is an option to choose the category. I just moved it for you 🙂

---

<div class="post-metadata">

**Author:** ![Tomas\_Uribe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_uribe/32/14402_2.png) [@Tomas\_Uribe](https://discourse.julialang.org/u/Tomas_Uribe)\
**Post date:** [April 27, 2020, 8:06pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/8 "2020-04-27T20:06:49Z")

</div>

I want to pick and choose which constraints go into a model.

---

<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:** [April 27, 2020, 8:09pm UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/9 "2020-04-27T20:09:01Z")

</div>

One way to achieve that is as follows:

```nohighlight
function build_model(flag::Bool)
    model = Model()
    @variable(model, x)
    if flag
        @constraint(model, x >= 0)
    end
    @objective(model, Min, x)
    return model
end

```

Projects like PowerModels.jl very successfully use this approach to automatically build JuMP models that depend on the context.

---

<div class="post-metadata">

**Author:** ![GerhardTotschnig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gerhardtotschnig/32/215516_2.png) [@GerhardTotschnig](https://discourse.julialang.org/u/GerhardTotschnig)\
**Post date:** [August 12, 2020, 9:46am UTC](https://discourse.julialang.org/t/jump-constraints-independent-of-a-particular-model/38314/10 "2020-08-12T09:46:19Z")

</div>

One other option could be to use expressions:  
One can define different expressions  
e.g. ex = @expression(model, 2x + y - 1)  
and then generate the constraint from one of these expressions.  
@constraint(m, ex + y \>= 5)
