# 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:** 1\
**Showing post:** 2

<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`.

---

_[View the full topic](https://discourse.julialang.org/t/calculate-the-number-of-values-greater-than-a-certain-value/84578)._
