# Calculate the number of values greater than a certain value

**URL:** <https://discourse.julialang.org/t/calculate-the-number-of-values-greater-than-a-certain-value/84578>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [July 21, 2022, 5:39am UTC](https://discourse.julialang.org/t/calculate-the-number-of-values-greater-than-a-certain-value/84578 "2022-07-21T05:39:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chenr86](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chenr86/32/36785_2.png) [@chenr86](https://discourse.julialang.org/u/chenr86)\
**Post date:** [July 21, 2022, 5:39am UTC](https://discourse.julialang.org/t/calculate-the-number-of-values-greater-than-a-certain-value/84578/1 "2022-07-21T05:39:42Z")

</div>

Hello,

I’m solving a MINLP problem. In my case, there is an objective function `max(sum(time60[time_slice]))` indicating the number of time slices in which the expression `Q[time_slice]` is greater than 60. That is to say:  
Q[i] \>=60 ==\> time60[i] = 1, else time60[i] = 0

Q is between [0, 100]

I have tried to formulate the problem as below, but according to the optimal point, it fails, the result cannot satisfy the formulation I want. I always got time60 = 1, even though Q \<60

```julia
@variable(model, time60[1:numtime], Bin)
@expression(model, Q[i = 1:numtime], "formula of Q")
@constraint(model, [i = 1:numtime], -60 * time60[i] <= Q[i] - 60)
@constraint(model, [i = 1:numtime], Q[i] - 60 <= (100-60) * time60[i])

```

What’s wrong with my formulation?

---

<div class="post-metadata">

**Author:** ![blob](https://avatars.discourse-cdn.com/v4/letter/b/ebca7d/32.png) [@blob](https://discourse.julialang.org/u/blob)\
**Post date:** [July 21, 2022, 7:34am UTC](https://discourse.julialang.org/t/calculate-the-number-of-values-greater-than-a-certain-value/84578/2 "2022-07-21T07:34:12Z")

</div>

It seems that your constraint

`@constraint(model, [i = 1:numtime], -60 * time60[i] <= Q[i] - 60)`

forces `time60[i]` to be 1 if `Q[i] < 60`. Let’s say that `Q[i]=40`. Then we have:

`-60*time60[i]<=-20`

If `time60[i]` is zero, then we have `0<=-20` which is false, so `time60[i]` must be one. Maybe this link would help: [Conditional constraint if else in JuMP - #12 by odow](https://discourse.julialang.org/t/conditional-constraint-if-else-in-jump/6795/12)? Your constraint could be something like:

```julia
for i in numtime
    if Q[i] >= 0
        @constraint(model,time60[i] == 1)
    end
end

```

If you really want to play with reformulations, how about (not sure if this covers all the cases, so take with a pinch of salt):

`@constraint(model, [i = 1:numtime], -60 * time60[i] <= 60 - Q[i] )`

Now if `Q[i]<=60`, then the right-hand side is non-negative and `time60[i]` can be either zero or one. If `Q[i]>60` the right-hand side is negative and `time60[i]` must be one. We still need to make sure that for `Q[i]<60` you get `time60[i]=0`. We can redo the second constraint as:

`@constraint(model, [i = 1:numtime], Q[i] -60>= -60 * (1-time60[i]))`

Now, if `Q[i]>=60`, the left-hand side is positive, so `time60[i]` can be either zero or one. But if `Q[i] <60`, then the left-hand side is negative. And for the constraint to be satisfied we need to push the right-hand side to its minimal value that can be attained only when `time60[i]=0`.

---

<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 21, 2022, 7:57am UTC](https://discourse.julialang.org/t/calculate-the-number-of-values-greater-than-a-certain-value/84578/3 "2022-07-21T07:57:36Z")

</div>

> [@blob](#):
>
> ```julia
> for i in numtime
> if Q[i] >= 0
> @constraint(model,time60[i] == 1)
> end
> end
> 
> ```

You cannot write JuMP constraints like this.

The reformulations is usually something like this, where `M` is a large upper bound on `x - 60`:

```julia
M = 1_000
model = Model()
@variable(model, x)
@variable(model, z, Bin)

@constraint(model, x - 60 <= M * z)
# if x > 60, then x - 60 > 0, so z must be 1
# if x < 60, then x - 60 < 0, so z might be 0 or 1

@constraint(model, 60 - x <= M * (1 - z))
# if x > 60, then 60 - x < 0, so (1 - z) might be 0 or 1
# if x < 60, then 60 - x > 0, so (1 - z) must be 0

```
