Search code examples
linear-programmingcplexampl

How to convert this into a set of linear constraints?


Given a 1-dimensional array of binary variables, for example

x = [0,1,0,0,1] 

I would like to create a new variable y such that y <= max(x). In other words

y = 0 only if sum(x) = 0.

y = 1 only if sum(x) > 0.

How do I convert this into a set of linear constraints?

I know this must be possible because IBM CP Optimizer Suite can handle this automatically, but I don't have access to it.


Solution

  • Try something simple like y <= sum(x) which will force y to zero if all the x are zero.

    Then for forcing y to 1 you have several choices. You could simply add a constraint that y >= x for every variable in x, or use a big M constraint like My >= sum(x) where M is some constant which is the maximum number of variables in x that can be simultaneously equal to 1. Adding the separate constraints might give a tighter linear relaxation, especially if there are many x variables.