I have the variable Si. How to express a variable Di in LP that satisfies:

  • Di=100*Si if Si>=0
  • Di=-200*Si if Si<0

The objective function would be Min{Sum(Di)}


I think it's cleaner to define two variables that represent the positive and negative parts of Si. Call them Si1 (= positive part) and Si2 (= negative part).

Si1 - Si2 = Si
Si1, Si2 >= 0

Probably you will also need constraints that say that if Si < 0 then Si1 must equal 0, and if Si > 0 then Si2 must equal 0. Introduce an additional binary variable y that equals 1 if Si > 0, equals 0 if Si < 0, and could equal either if Si = 0:

-M * (1 - y) <= Si
 M * y >= Si

where M is a large constant. Then require Si1 = 0 if y = 0 and Si2 = 0 if y = 1:

Si1 <= M * y
Si2 <= M * (1 - y)

Then set your objective function to minimize 100 * Si1 + 200 * Si2.

Note: If both Si1 and Si2 have positive coefficients in the objective function, and the objective is a minimization, then you do not need the stuff after the divider line. The two constraints before the line are sufficient, because we will always want to set Si1 and Si2 as small as possible, which means setting one of them to 0.

  • $\begingroup$ Can you please explain the conditions with M? How do they make sure that f Si <= 0 then Si1 must equal 0, and if Si >= 0 then Si2 must equal 0. Say Si=10. We want Si1=10 and Si2=0. So the first condition brings 9M<=10? That cant happen. $\endgroup$ – Kristjan Kica Jun 19 at 15:45
  • $\begingroup$ If Si < 0 then the LHS of the first constraint must also be < 0; setting Si1 = 1 would make it =0, so we must set Si1 = 0, which makes the constraint satisfied trivially. Similarly for the other constraint. My solution used <= and >= but they should be strict inequalities, I will edit. $\endgroup$ – LarrySnyder610 Jun 19 at 15:48
  • $\begingroup$ Si is a real number, and so should the Si1 abd Si2 be. Not binary? $\endgroup$ – Kristjan Kica Jun 19 at 15:51
  • $\begingroup$ Ack. sorry. I am not concentrating enough. I will edit. $\endgroup$ – LarrySnyder610 Jun 19 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.