Written by: Paul Rubin

Primary Source: OR in an OB World. 9/21/2018.

Someone asked me today (or yesterday, depending on whose time zone you go by) how to force a group of variables in an optimization model to take the same sign (all nonpositive or all nonnegative). Assuming that all the variables are bounded, you just need one new binary variable and a few constraints.

Assume that the variables in question are \(x_1,\dots,x_n\) and that they are all bounded, say \(L_i \le x_i \le U_i\) for \(i=1,\dots,n\). If we are going to allow variables to be either positive or negative, then clearly we need \(L_i < 0 < U_i\). We introduce a new binary variable \(y\) and, for each \(i\), the constraints

\(\displaystyle L_i (1-y) \le x_i \le U_i y.\)

If \(y\) takes the value 0, every original variable must be between its lower bound and 0 (so nonpositive). If \(y\) takes the value 1, every original variable must be between 0 and its upper bound (so nonnegative).

Note that trying to enforce strictly positive or strictly negative rather than nonnegative or nonpositive is problematic, since optimization models abhor strict inequalities. The only work around I know is to change “strictly positive” to “greater than or equal to \(\epsilon\)” for some strictly positive \(\epsilon\), which creates holes in the domains of the variables (making values between 0 and \(\epsilon\) infeasible).

#### Paul Rubin

#### Latest posts by Paul Rubin (see all)

- Using Java Collections with CPLEX - July 22, 2019
- Evaluating Expressions in CPLEX - June 27, 2019
- R v. Python - June 17, 2019