# Linear Programming: “at most k out of n variables nonzero” constraint

I have a Mixed-Integer Program that contains (among other things) $n$ variables $v_1, \dots v_n$ (continuous or integer doesn't matter, in $[0, M)$ for some $M$).

I want to formulate the constraint that at most $k < n$ of these variables may become nonzero. The obvious solution would be to introduce a set of binary indicator variables $w_1, \dots w_n$, a big-M constraint per pair and a sum constraint on the indicators:

$$w_i \cdot M \geq v_i$$ $$\sum w_i \leq k$$

However, that's a lot of big-M's and probably wreaks havoc on the relaxed solution. I'm wondering if there are standard techniques for coping with a setting like this.

What I've thought of: Introduce a SOS-Type 1 - Constraint per $v_i$ / $w_i$ pair. This would get rid of the big-M and force $w_i$ to become zero if $v_i$ is nonzero. (Obviously, the second constraint above must be changed to $\sum (1 - w_i) \leq k$). Is that the way to go? It feels like I'm using SOS-constraints for something that they weren't meant to (it's not an ordered set…)

Any help is appreciated! Thanks, Lukas

• Which solver are you using? – xxx--- Feb 11 '19 at 14:03
• I currently use Gurobi 8.0.1, but I'd also be able to use any version CPLEX. – Lukas Barth Feb 11 '19 at 14:28

Internally, a solver is probably going to treat each of your SOS1 constraints a lot like a binary variable -- basically, branch on $w_i$ and, in the $w_i = 1$ branch, force $v_i = 0$. The good news is that you avoid $M$; the bad news is that I don't think the solver has much information with which to assess which SOS1 constraint to branch on at a given node.
If the variables have natural (and not horribly large) upper bounds, the big $M$ approach may not to be too bad. If you don't know those bounds, you might relax integrality conditions and maximize each variable in turn to get bounds.
Another possibility is combinatorial Benders decomposition (Codato and Fischetti, Operations Research 54 (2006), 756-766). Rather than use big $M$ constraints, you have a master problem containing the $w_i$ and one continuous variable (a surrogate for the objective contribution of the $v_i$), and a subproblem that optimizes the original objective function over the $v_i$ subject to the original constraints. For a given solution to the master problem, you simply omit $v_i$ from the subproblem if $w_i = 0$. If the subproblem is infeasible, you add a "feasibility cut" to the master. If the subproblem has an optimal solution but its objective value is worse than the value of the surrogate variable in the master solution, you add a constraint ("optimality cut") to the master problem tying the value of the surrogate to the values of the $w_i$.
I would only use combinatorial Benders if the $M$ formulation was either seriously slow or had numerical stability problems (meaning, in either case, that the "big $M$" coefficients really were big). Also, I'd be hesitant to do it if some or all of the $v_i$ are integer valued, since (a) you'd be repeatedly solving a MIP subproblem and (b) the optimality and feasibility cuts you got might be weaker than usual.