# Constraint on count of variables \>0 in JuMP?

**URL:** https://discourse.julialang.org/t/constraint-on-count-of-variables-0-in-jump/117072
**Category:** Optimization (Mathematical)
**Created:** [July 15, 2024, 8:43pm UTC](https://discourse.julialang.org/t/constraint-on-count-of-variables-0-in-jump/117072 "2024-07-15T20:43:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)
#### Post date: [July 15, 2024, 8:43pm UTC](https://discourse.julialang.org/t/constraint-on-count-of-variables-0-in-jump/117072/1 "2024-07-15T20:43:21Z")

</div>

I have a model with a control variable as follows:

```Julia
@variable(model, 0 ≤ u[1:(T+1)] ≤ u_max)

```

I want to constrain the number of elements in `u` greater than 0 to be less than a threshold, `u_nonzero_max`. How can I do this in JuMP?

---

<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: [July 15, 2024, 10:56pm UTC](https://discourse.julialang.org/t/constraint-on-count-of-variables-0-in-jump/117072/2 "2024-07-15T22:56:54Z")

</div>

Here you go:

```julia
T, u_max, u_non_zero_max = 3, 4.0, 2
model = Model()
@variable(model, 0 <= u[1:(T+1)] <= u_max)
@variable(model, z[1:(T+1)], Bin)
@constraint(model, [t in 1:T+1], u[t] <= u_max * z[t])
@constraint(model, sum(z) <= u_non_zero_max)

```

---

<div class="post-metadata">

### Author: ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)
#### Post date: [July 18, 2024, 7:00pm UTC](https://discourse.julialang.org/t/constraint-on-count-of-variables-0-in-jump/117072/3 "2024-07-18T19:00:24Z")

</div>

Thanks @odow! Unfortunately, my MINLP problem is way slower to fit than my NLP problem. Is there a workaround to do this to avoid binary variables? My searches to find an alternative haven’t amounted to much.

---

<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: [July 18, 2024, 11:46pm UTC](https://discourse.julialang.org/t/constraint-on-count-of-variables-0-in-jump/117072/4 "2024-07-18T23:46:06Z")

</div>

> Unfortunately, my MINLP problem is way slower to fit than my NLP problem

This is expected. The M-choose-N-non-zero is quite a nasty constraint.

> Is there a workaround to do this to avoid binary variables?

No good ones. Consider the case when `T = 1` and `u_non_zero_max = 1`. There are two decisions variables, and the feasible region is the L formed by the x- and y-axes from `[0, u_max]`. This is the same as a classical complementarity constraint, and you’ve made it harder by increasing the dimension 😄
