Calculate the number of values greater than a certain value

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

@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?

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? Your constraint could be something like:

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.

You cannot write JuMP constraints like this.

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

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
1 Like