# In an integer program, how can I “activate” a constraint only if a decision variable has a certain value?

Suppose we have the constraint $$a_1x_1 + \cdots + a_nx_n \gtreqless b,$$ where $$a_i$$ and $$b$$ are constants and $$x_i$$ are decision variables. Suppose also that we want the constraint to hold if $$y=1$$ (where $$y$$ is a binary decision variable), and we don’t care whether it holds if $$y=0$$.

How can we accomplish this?

• Here's another question that I keep answering on other SE sites. May 31 '19 at 15:33
• You could just refer them to or.stackexchange.com/questions/57/… .And then only answer beyond that if they need additional help. Despite taking a while to formulate, I saved a lot of time answering questions at the CVX Forum with my thread ask.cvxr.com/t/… May 31 '19 at 17:12

These are know as "indicator constraints" or "on/off" constraints. The best formulation is the convex-hull one, it includes the optimal big-M value plus additional non-redundant constraints, here's a note characterizing this formulation. There's also a generalization for convex nonlinear "on/off" constraints here and recent extensions here.

• Lots of times, "casual" OR users (students taking OR classes, people building small models for a business setting, etc.) want an approach that is easy to code, even if it's not the most efficient computationally. OTOH, people writing production code, solving large problems, writing academic papers, etc. want an approach that is the most efficient computationally, even if it's not the easiest to code. Is it fair to say that the approach you outline here might be most appropriate for the latter type, while the approach I outline in my answer might be most appropriate for the former? Jun 3 '19 at 13:10
• Agreed. Thanks! Jun 6 '19 at 21:51

Let $$M$$ be a new parameter (constant) that equals a large number.

Greater-than-or-equal-to constraints:

The constraint is $$a_1x_1 + \cdots + a_nx_n \ge b$$. Rewrite it as $$a_1x_1 + \cdots + a_nx_n \ge b - M(1-y).$$ Then, if $$y = 1$$, the constraint is active, and if $$y=0$$, it has no effect since the right-hand side is very negative.

(If all of the $$a_i$$ are nonnegative, you can instead use $$a_1x_1 + \cdots + a_nx_n \ge b(1-y),$$ which is tighter.)

Less-than-or-equal-to constraints:

The constraint is $$a_1x_1 + \cdots + a_nx_n \le b$$. Rewrite it as $$a_1x_1 + \cdots + a_nx_n \le b + M(1-y).$$ Then, if $$y=1$$, the constraint is active, and if $$y=0$$, it has no effect since the RHS is very large.

Equality constraints:

The constraint is $$a_1x_1 + \cdots + a_nx_n = b$$.. Rewrite it as \begin{align*} a_1x_1 + \cdots + a_nx_n & \le b + M(1-y) \\ a_1x_1 + \cdots + a_nx_n & \ge b - M(1-y) \end{align*} Then, if $$y=1$$, the equality constraint is active, and if $$y=0$$, the constraints have no effect.

Note: If your model is relatively large, i.e., it takes a non-negligible amount of time to solve, then you need to be careful with big-$$M$$-type formulations. In particular, you want $$M$$ to be as small as possible while still enforcing the logic of the constraints above.

• Even if your model is small, you have to be careful if M is too large relative to the solver integrality tolerance. That point is addressed at the end of my answer in the mega-downvoted thread or.stackexchange.com/questions/57/… . May 31 '19 at 17:10
• @MarkL.Stone I think it would be very helpful to have a standalone Q&A about big-M formulations. Will you write one? I know you prefer the FAQ-type approach, but since it seems the preference of the community is to have multiple, narrower questions, it would be good to have one on big-M. (Plus then you can just link directly to it without having to say "at the end of my answer in..." :) ) May 31 '19 at 19:27
• I will leave the writing to someone else. Mine would probably just get downvoted into oblivion. Someone can take that part of my post and expand and spruce it up. May 31 '19 at 20:05