# Create empty constraint with JuMP

**URL:** <https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [June 20, 2020, 3:05pm UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777 "2020-06-20T15:05:08Z")\
**Posts on this page:** 12\
**Page:** 1

<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:** [June 20, 2020, 3:05pm UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/1 "2020-06-20T15:05:08Z")

</div>

I have a part of my code in which it is simpler (far less `ifs` and edge cases) if I add empty constraints first and then, knowing all constraints I need are present in the model, change the coefficient of some variables inside newly added and old constraints.

I have searched the Discourse but the topics I found are from 2017 (before the great change in JuMP I think), and none really addresses the question.

There is a way to do this? I will need to rework more code if I cannot do it.

---

<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 20, 2020, 4:05pm UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/2 "2020-06-20T16:05:05Z")

</div>

You’re looking for [Constraints · JuMP](https://jump.dev/JuMP.jl/v0.21.1/constraints/#Modifying-a-variable-coefficient-1)

```nohighlight
julia> model = Model()
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> @variable(model, x)
x

julia> c = @constraint(model, 0 <= 1)
0 ≤ 1.0

julia> set_normalized_coefficient(c, x, 1)

julia> c
x ≤ 1.0

```

---

<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:** [June 20, 2020, 5:26pm UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/3 "2020-06-20T17:26:50Z")

</div>

Ah, it was my incompetence. I checked that section, but I thought I had no expression I could pass to the `@constraint` to express an empty constraint. I was with C++ and other languages in my head at the moment, and thought that if I did no pass at least one variable, then the `<=` operator would not be the one _overloaded_ for `VariableRef`, and so `0.0 <= 0.0` would be the same thing as passing a `Bool` as second parameter. I forgot how macros work, and that no type wrapping would be necessary for an empty constraint. My fault.

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [June 20, 2020, 6:48pm UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/4 "2020-06-20T18:48:57Z")

</div>

FYI @Henrique_Becker, be aware that setting coefficients one by one will be considerably slow.

---

<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:** [June 21, 2020, 12:10am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/5 "2020-06-21T00:10:42Z")

</div>

Thanks for the hint, but as I see it, there is no batch method for `set_normalized_coefficient`. Even if I start the constraints with some coefficients I know at time of creation, I need to be able to set coefficients in existing constraints. What should I do then?

P.S.: I have an enormous reply to give in that other post where I asked help about resetting the LP basis. However, every time I change something I get deeper into the problem, I will probably reply it when I am 100% sure of what the problem was, just so it can help others with similar problems.

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [June 21, 2020, 12:20am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/6 "2020-06-21T00:20:30Z")

</div>

`set_normalized_coefficient` is the way to go. My previous reply was just a side-comment.

There’s no real remedy to the performance hit, because it is related to solvers’ internal problem representation.  
In the LP case, think of the constraint matrix as being stored in SparseCSC form: adding columns is fast, but setting individual coefficients (even if done in a batch) is bound to be slower, because you have to shift things in memory.

If you’re using JuMP in automatic mode, this may be less of an issue, depending on how MOI’s cache is implemented.  
For instance, if you keep a dictionnary of `(row, column) => coeff` pairs, then modifying a batch of individual coefficients should be as cheap as adding whole rows of columns.

---

<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:** [June 21, 2020, 12:23am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/7 "2020-06-21T00:23:23Z")

</div>

I use direct mode because the last time I tried to use MOI cache it took more time than I used to solve the problem when I tried to delete variables from it.

The solvers or the solver wrappers in Julia?

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [June 21, 2020, 12:26am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/8 "2020-06-21T00:26:36Z")

</div>

> The solvers or the solver wrappers in Julia?

The solvers.

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [June 21, 2020, 12:52am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/9 "2020-06-21T00:52:48Z")

</div>

I ran some local tests, and it appears I was wrong w.r.t slowdown for setting individual coefficients.  
I’ll post a complete script soon.

Sorry for the fuss 😕

---

<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 21, 2020, 1:43am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/10 "2020-06-21T01:43:58Z")

</div>

Yes, we removed special syntax for column generation in lieu of proper benchmarks showing the benefit. It’s not worth the extra code complexity for marginal gain.

Moreover, no one has asked for it in 18 months, so that’s something.

---

<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:** [June 21, 2020, 1:48am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/11 "2020-06-21T01:48:23Z")

</div>

😅

It is kinda awkward that you are apologizing when you are up to run local tests just to be sure you gave me the correct information. I am grateful for your help (not only in this case but in general), no need to overexert yourself.

---

<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:** [June 21, 2020, 1:55am UTC](https://discourse.julialang.org/t/create-empty-constraint-with-jump/41777/12 "2020-06-21T01:55:57Z")

</div>

If I understand correctly, an optimized implementation was not much faster, then? Well, I will finish implementing this “kinda column generation” tomorrow (or, more probably, monday), and if the changes take less time than solving the changed LP I will be already happy enough. The paper I am reproducing did explain very little this process (specially the technical details related to how the “growing” of the model was done using the solver API) and there is a limit on how much more I am willing to elucubrate.
