Modeling At-Most-K and Exactly-K Constraints

How to use sums of binary variables to enforce that at most K, at least K, or exactly K items in a set are selected, and how to combine these cardinality constraints on subsets of variables.

Step 1 of 157%

Tutorial

At-Most-K Constraints

Suppose we have binary decision variables x1,x2,,xn{0,1}x_1, x_2, \ldots, x_n \in \{0,1\}, where xi=1x_i = 1 means item ii is selected and xi=0x_i = 0 means it is not.

The at-most-K constraint restricts the number of selected items to at most KK. Since each selected item contributes 11 to the sum and each unselected item contributes 00, the total count of selected items is exactly i=1nxi\sum_{i=1}^n x_i. We therefore write

i=1nxiK,i.e.,x1+x2++xnK.\sum_{i=1}^n x_i \leq K, \quad \text{i.e.,} \quad x_1 + x_2 + \cdots + x_n \leq K.

For example, with five binaries x1,,x5x_1, \ldots, x_5, the requirement "at most 22 of them equal 11" becomes

x1+x2+x3+x4+x52.x_1 + x_2 + x_3 + x_4 + x_5 \leq 2.

The assignment (x1,x2,x3,x4,x5)=(1,0,1,0,0)(x_1, x_2, x_3, x_4, x_5) = (1, 0, 1, 0, 0) has sum 22 and is feasible. The assignment (1,1,1,0,0)(1, 1, 1, 0, 0) has sum 33 and is infeasible.

navigate · Enter open · Esc close · ⌘K/Ctrl K toggle