I have a variable t, and I would like to have the following constraints:
a <= t <= b
and
(c <= t <= d) or (e <= t <= f)
Here is the code I used in Julia:
using JuMP, Cbc, StatsBase
import Random
model = Model(with_optimizer(Cbc.Optimizer));
@variable(model, T, Int);
@constraint(model, 5 <= T <= 1000);
@constraint(model, (100 <= T <= 200) | (1100 <= T <= 1300) );
# which fails with error:
# ERROR: LoadError: In `@constraint(model, (100 <= T[i] <= 200) | (1100 <= T[i] <= 1300))`: Unrecognized sense |
Is there a way to linearize these constraints? Or, alternatively, is there a non-linear solver that can deal with these constraints?
The special case:
5 ≤ T ≤ 1000
and
(100 ≤ T ≤ 200) or (1100 ≤ T ≤ 1300)
is easy. The result is just:
100 ≤ T ≤ 200
In general:
a ≤ t ≤ b
and
(c ≤ t ≤ d) or (e ≤ t ≤ f)
(where a,b,c,d,e,f are constants) can be linearized using binary variables:
a ≤ t ≤ b
c + (a-c)δ ≤ t ≤ d + (b-d)δ
e + (a-e)(1-δ) ≤ t ≤ f + (b-f)(1-δ)
δ ∈ {0,1}
Now the bonus question: how to do
a ≤ t ≤ b
and
(c ≤ t ≤ d) or (e ≤ t ≤ f) or (g ≤ t ≤ h)
Again, the only variable here is t. All other quantities are constants. Below is a direct extension of what we did before:
a ≤ t ≤ b
c + (a-c)δ1 ≤ t ≤ d + (b-d)δ1
e + (a-e)δ2 ≤ t ≤ f + (b-f)δ2
g + (a-g)δ3 ≤ t ≤ h + (b-h)δ3
δ1+δ2+δ3 = 2
δ1,δ2,δ3 ∈ {0,1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With