# if-else in linear programming

 0 Hello, I am trying to model a system with following constraints using linear programming in MATLAB: if P(i) > 0 limit1 <= K(1)*P(1) + K(2)*P(2) + K(3)*P(3) + ... + K(i)*P(i) <= limit2 # K(1),K(2)... are constants, P is power to a battery else if P(i) <= 0 limit1 <= C(1)*P(1) + C(2)*P(2)+ C(3)*P(3) + ... + C(i)*P(i) <= limit2 # C(1),C(2)... are constants different from K(1)...  I modified it in following form: (e.g. for i=1) P(1)<=b*U # b is binary & U is a big number; limit1<= (1-b)*P(1)*C(1) + b*P(1)*K(1)<=limit2  Is this formulation correct? My doubt is that at b=0, I get P(1)<=0 and the second condition, which is perfect. But when b=1, I get P(1)<=U, and first condition. But then the program can also return P(1) less than zero for b=1. Is there something wrong here or I did not understand correctly? Thank you so much for any help :) PS: I know it is not linear right now. But I want to confirm if what I did so far is right or not. cheers, Aastha asked 30 Apr '14, 12:03 aastha 13●4 accept rate: 0% fbahr ♦ 4.6k●7●16

 2 if P(i) > 0 then limit1 <= sum {j in 1..i} K(j) * P(j) <= limit2   not ( P(i) > 0 ) or ( limit1 <= sum {j in 1..i} K(j) * P(j) <= limit2 ) <=> ( P(i) <= 0 ) or ( limit1 <= sum {j in 1..i} K(j) * P(j) <= limit2 )  and disjunctions (or-relations) can be expressed using [a] (binary) indicator variable[s] b (to modify the bounds imposed on the linear expression). Hence, "we" get P(i) <= U_P(i) * b(i) limit1 * b(i) + L_SKP(i) * ( 1 - b(i) ) <= sum {j in 1..i} K(j) * P(j) limit2 * b(i) + U_SKP(i) * ( 1 - b(i) ) >= sum {j in 1..i} K(j) * P(j)  for the 'if-branch', and P(i) >= .000001 + L_P(i) * ( 1 - b(i) ) limit1 * ( 1 - b(i) ) + L_SCP(i) * b(i) <= sum {j in 1..i} C(j) * P(j) limit2 * ( 1 - b(i) ) + U_SCP(i) * b(i) >= sum {j in 1..i} C(j) * P(j)  for the 'else-branch' (with L_P, U_P as lower/upper bounds on P, and L_SKP, U_SKP, L_SCP, U_SCP as bounds for the respective sum expressions). answered 30 Apr '14, 15:50 fbahr ♦ 4.6k●7●16 accept rate: 13% 1 This is perfect. And here I also don't need further manipulations to linearize (unlike my previous solution). Thank you so much. :) One tiny correction. in else branch, I think it should be P(i) >= .000001 + L_P(i) * ( 1 - b(i) ) but otherwise its perfect :) (02 May '14, 04:14) aastha I think it should be P(i) >= .000001 + L_P(i) * ( 1 - b(i) ) Well spotted! (02 May '14, 04:49) fbahr ♦ One more comment, although your answer is perfect for what I asked, in my case, I might still have to use following system (for time i): P(i) <= U_P(i) * b(i) P(i) >= .000001 + L_P(i) * ( 1 - b(i) ) limit1 <= sum {j in 1..i}(1-b(j))*P(j)*C(j) + sum {j in 1..i}b(j)*P(j)*K(j) <= limit2  because whether I use constant K(1) or C(1) depends on sign of P(1) and not on the time interval. I think I should have been more detailed in my question. So this is what I am using right now. Do you know a more elegant way to do this? (I am sorry for spamming your inbox) (02 May '14, 05:09) aastha If you want limit1 <= sum {j in 1..i} (if P(j) <= 0 then C(j) else K(j)) * P(j) <= limit2, then limit1 <= sum {j in 1..i} (b(j)*K(j) + (1-b(j))*C(j))*P(j) <= limit2 (or limit1 <= sum {j in 1..i} K(j)*P(j) + sum {j in 1..i} (C(j)-K(j))*P(j)*b(j) <= limit2) is one way to go – you still need to linearize each quadratic term P(j)*b(j) as described here, though. Alternatively, ... (04 May '14, 13:45) fbahr ♦ ... (if P(j) <= 0 then C(j) else K(j)) * P(j) can be expressed as piecewise linear function {{C(j)*P(j), L_P <= P(j) <= 0}, {K(j)*P(j), 0 <= P(j) <= U_P}}}. To do so, we write limit1 <= sum {j in 1..i} (C(j)*L_P*x(j,1) + 0*x(j,2) + K(j)*U_P*x(j,3)) <= limit2 (w/ [non-neg. continuous] SOS2 variables x(j,l), l=1,..,3) and add L_P*x(i,1) + 0*x(i,2) + U_P*x(i,3) == P(i) ∀ i; x(i,1) + x(i,2) + x(i,3) == 1 ∀ i as additional constraints [which has the charm that you don't need to introduce aux. binary variables]. (04 May '14, 14:26) fbahr ♦
 toggle preview community wiki

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "Title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Tags: