if multi_constraints then
AnsweredHi~
I have three continuous variable a,b,c and three binary P, N, and Z.
I want to set three constraints :
1. if a>0 and b>0 and c>0, then P==1 #when abc are all positive, P==1
2. if a<0 and b<0 and c<0, then N==1 #when abc are all negative, N==1
3. will it be possible if (a>0 and b>0 and c>0) or (a<0 and b<0 and c<0) then Z==1?
-
Hi Carol,
It will be easier to model this using contrapositive statements because we want the if-clause to be based on a binary variable. This gives the following equivalent constraints:
1. If P == 0 then a <= 0 or b <= 0 or c <= 0
2. if N == 0 then a >= 0 or b >= 0 or c >= 0
3. if Z == 0 then (a <= 0 or b <= 0 or c <= 0) and (a >= 0 or b >= 0 or c >= 0), or
if Z == 0 then P == 0 or N == 0a_neg = m.addVar(vtype="B")
b_neg = m.addVar(vtype="B")
c_neg = m.addVar(vtype="B")
a_pos = m.addVar(vtype="B")
b_pos = m.addVar(vtype="B")
c_pos = m.addVar(vtype="B")
m.addConstr((a_neg=1) >> (a <= 0))
m.addConstr((b_neg=1) >> (b <= 0))
m.addConstr((c_neg=1) >> (c <= 0))
m.addConstr((a_pos=1) >> (a >= 0))
m.addConstr((b_pos=1) >> (b >= 0))
m.addConstr((c_pos=1) >> (c >= 0))
# condition 1
m.addConstr((P == 0) >> (a_neg + b_neg + c_neg >= 1))
# condition 2
m.addConstr((N == 0) >> (a_pos + b_pos + c_pos >= 1))
# condition 3
m.addConstr((Z == 0) >> (P + N <= 1))Note that due to tolerances there are solutions where a, b, and c take on tiny non-zero values yet P = N = 0.
Also, be careful not to assume that P == 1 implies a>0 and b>0 and c>0. The above constraints do not model this. P = 1, a = b = c = 0 is a valid solution.- Riley
0
Please sign in to leave a comment.
Comments
1 comment