# \[sparse modelling\] Does JuMP automatically remove "uninfluent" variables from constraints/objective?

**URL:** https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366
**Category:** Optimization (Mathematical)
**Created:** [May 20, 2022, 1:25pm UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366 "2022-05-20T13:25:09Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [May 20, 2022, 1:25pm UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366/1 "2022-05-20T13:25:09Z")

</div>

Hello, I am designing a non-linear model where some quantities depend from many variables, but only a few of them actually have a parameter that allow the dependent variable to play.  
The actual implementation is:

`quantity_i = prod(price[p]^e[p] for p in products)`

Where a few `e[p]` are actually different than zero.  
Is JuMP smart enough to “remove” there variables from the constraint or should I write each constrain separately as to remove the dependency from the uninfluent variables ?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 20, 2022, 1:48pm UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366/2 "2022-05-20T13:48:04Z")

</div>

Do you use a sparse vector to store `e`?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [May 20, 2022, 1:55pm UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366/3 "2022-05-20T13:55:30Z")

</div>

no. that’s the way to go ?

---

<div class="post-metadata">

### Author: ![joaquimg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joaquimg/32/223_2.png) [@joaquimg](https://discourse.julialang.org/u/joaquimg)
#### Post date: [May 20, 2022, 2:11pm UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366/4 "2022-05-20T14:11:40Z")

</div>

JuMP will not do presolving.  
If some variable does not matter for a constraint such as yours you should write them without the variables that do not matter.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [May 20, 2022, 2:39pm UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366/5 "2022-05-20T14:39:38Z")

</div>

if the variables that "don’t matter’ is defined in the data (trough a parameter, `e` in my case) rather than in the code is there a way to still keep the structure of the constraint homogeneous ?  
My constraints are currently :

```julia
@NLconstraint(m, demand[p in 1:np, r in 1:nr], d[p,r] == const_d[p,r] * prod(pd[p,r]^ e_d[p,r,in_p,in_r] for in_p in 1:np, in_r in 1:nr ) )
@NLconstraint(m, supply[p in 1:np, r in 1:nr], s[p,r] == const_s[p,r] * prod(ps[p,r]^ e_s[p,r,in_p,in_r] for in_p in 1:np, in_r in 1:nr ) )

```

I could add a “logic” to test for `e_d`/`e_d` being equal to zero, but this would be “passed” to the solver I think.  
Otherwise I could write many unidimensional constraints, all with a different structure (with the removal of irrelevant variables logic outside the constraint) but how will I get them? by name ?

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [May 20, 2022, 3:44pm UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366/6 "2022-05-20T15:44:36Z")

</div>

Doesn’t it work when the coefficients are sparse though?

---

<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 22, 2022, 12:13am UTC](https://discourse.julialang.org/t/sparse-modelling-does-jump-automatically-remove-uninfluent-variables-from-constraints-objective/81366/7 "2022-05-22T00:13:02Z")

</div>

Does something like this work? (I haven’t tested, so there might be typos, etc.)

```nohighlight
sets = Dict(
    (p, r) => [
        (inp, inr) for inp in 1:np for inr in 1:nr if !iszero(e_d[p,r,in_p,in_r])
    ] for p in 1:np, r in 1:nr
)
@NLconstraint(
    m, 
    demand[p in 1:np, r in 1:nr],
    d[p,r] == const_d[p,r] * prod(pd[p,r]^e_d[p,r,in_p,in_r] for (in_p, in_r) in sets[(p, r)])
)

```
