# How to Formulate a Constraint Which Ensures All Variables Have the Same Sign

I'm trying to include a constraint in my problem (to be solved by any convex optimization solver). Let {a,b,c,d ...} be a finite set of continuous variables. How to formulate a constraint which ensure all of these variables, in the same time interval, take either "positive or zero" or "negative or zero" values.

sincerely,

Mohan

• A simple $\mathrm(sign)$ function would be suffice right? – Maxtron Sep 21 '18 at 6:12
• Thanks for the reply Maxtron but that doesn't answer my question. Here's what I was looking for orinanobworld.blogspot.com/2018/09/… – Mohan Lal Sep 24 '18 at 5:02
• The way it is done in the blog you posted means the problem becomes Non Convex. So I'm not sure it would work for you. – Royi Jan 24 '19 at 13:13
• @MohanLal, Could you please mark my answer or write what is missing? – Royi Jul 30 '19 at 14:06

The solution from the blog you linked goes as following (Coordinating Variable Signs by Paul Rubin):

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}, {x}_{2}, \ldots, > {x}_{n}$$ and that they are all bounded, say $$\forall \; 1 \leq i \leq n, \; {l}_{i} \leq {x}_{i} \leq {u}_{i}$$. 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 \in \left\{ 0, 1 \right\}$$ and for each $$i$$ the constraints become:

$$\forall \; 1 \leq i \leq n, \; {l}_{i} \left( 1 - y \right) \leq {x}_{i} \leq {u}_{i} y$$

If $$y = 0$$ every original variable must be between its lower bound and 0, nonpositive. If $$y = 1$$ every original variable must be between 0 and its upper bound, 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).

The problem with above approach is that it will make a Convex Optimization Problem into Non Convex Optimization Problem because of the multiplication between variables.

For many solvers I guess it will make no difference. Yet if you use Convex Optimization solver or even Disciplined Convex Programming (DCP) based solver (Like CVX) it won't work.

What I suggest doing, in this case, is take advantage that you have only 2 cases. One where all variables are Non Negative and the other when all of them are Non Positive.

Then all you need is to solve the problem with 2 different sets of constraints (Forming 2 different problems). When the constraints are:

• $$\forall \; 1 \leq i \leq n, \; {l}_{i} \leq {x}_{i} \leq 0$$
• $$\forall \; 1 \leq i \leq n, \; 0 \leq {x}_{i} \leq {u}_{i}$$

Then chose the solution with the lowest objective value.