TExcelTAD
Home › Optimization › Inequality Constraints and the KKT Conditions

Inequality Constraints and the KKT Conditions

Inequality constraints add a twist to Lagrange multipliers: a constraint either matters (it is active and pushes back) or it does not (it is inactive and its multiplier is zero). The Karush-Kuhn-Tucker (KKT) conditions capture both cases in one system and are the optimality conditions that every constrained solver checks.

The KKT conditions

For min⁡f(x)\min f(\mathbf{x}) s.t. gi(x)≤0g_i(\mathbf{x}) \le 0, hj(x)=0h_j(\mathbf{x}) = 0, with Lagrangian L=f+∑iλigi+∑jνjhj\mathcal{L} = f + \sum_i\lambda_ig_i + \sum_j\nu_jh_j, a point x∗\mathbf{x}^* with multipliers (λ∗,ν∗)(\boldsymbol{\lambda}^*, \boldsymbol{\nu}^*) satisfies KKT if

∇f(x∗)+∑iλi∗∇gi(x∗)+∑jνj∗∇hj(x∗)=0stationaritygi(x∗)≤0,hj(x∗)=0primal feasibilityλi∗≥0dual feasibilityλi∗ gi(x∗)=0complementary slackness\begin{aligned} \nabla f(\mathbf{x}^*) + \sum_i\lambda_i^*\nabla g_i(\mathbf{x}^*) + \sum_j\nu_j^*\nabla h_j(\mathbf{x}^*) &= \mathbf{0} && \text{stationarity} \\ g_i(\mathbf{x}^*) \le 0, \quad h_j(\mathbf{x}^*) &= 0 && \text{primal feasibility} \\ \lambda_i^* &\ge 0 && \text{dual feasibility} \\ \lambda_i^*\,g_i(\mathbf{x}^*) &= 0 && \text{complementary slackness} \end{aligned}

Complementary slackness says: for each inequality, either it is active (gi=0g_i = 0) or its multiplier is zero. Dual feasibility λi≥0\lambda_i \ge 0 says the constraint can only push, not pull: −∇f-\nabla f must point out of the feasible set, into the region the constraint forbids.

Problem type KKT conditions are…
any problem, with a constraint qualification (LICQ, Slater, …) necessary for a local minimum
convex problem (ff, gig_i convex, hjh_j affine) sufficient for a global minimum
convex problem with Slater's condition necessary and sufficient

Checking KKT by hand and in code

Minimize (x−3)2+(y−2)2(x - 3)^2 + (y - 2)^2 subject to x+y≤4x + y \le 4 and x≥0x \ge 0 (written −x≤0-x \le 0). From the previous lesson the solution is (2.5,1.5)(2.5, 1.5), with only the first constraint active.

python · try it yourself
import numpy as np

x = np.array([2.5, 1.5])
grad_f = 2 * (x - np.array([3.0, 2.0]))
g = np.array([x[0] + x[1] - 4, -x[0]])                  # constraint values
grad_g = np.array([[1.0, 1.0], [-1.0, 0.0]])            # rows: gradients of g1, g2
lam2 = 0.0                                               # inactive -> multiplier 0
lam1 = -grad_f[0] / grad_g[0, 0]                         # from stationarity, first component
lam = np.array([lam1, lam2])
print("multipliers:", lam)
print("stationarity residual:", grad_f + grad_g.T @ lam)
print("primal feasible:", bool(np.all(g <= 1e-12)), " dual feasible:", bool(np.all(lam >= 0)))
print("complementary slackness lambda_i * g_i:", lam * g + 0)
multipliers: [1. 0.]
stationarity residual: [0. 0.]
primal feasible: True  dual feasible: True
complementary slackness lambda_i * g_i: [0. 0.]

Solving KKT by trying active sets

For small problems you can solve the KKT system by guessing which constraints are active, solving the resulting equalities, and checking the signs:

python · try it yourself
import numpy as np
from itertools import combinations

# minimize x^2 + y^2 - 4x - 6y subject to x + y <= 3, x - y <= 1, -x <= 0
Q = 2 * np.eye(2); c = np.array([-4.0, -6.0])
A = np.array([[1.0, 1.0], [1.0, -1.0], [-1.0, 0.0]]); b = np.array([3.0, 1.0, 0.0])
for k in range(0, 3):
    for S in combinations(range(3), k):
        S = list(S); m = len(S)
        K = np.block([[Q, A[S].T], [A[S], np.zeros((m, m))]]) if m else Q
        sol = np.linalg.solve(K, np.concatenate([-c, b[S]]))
        x, lam = sol[:2], sol[2:]
        ok = np.all(A @ x <= b + 1e-9) and np.all(lam >= -1e-9)
        if ok:
            print("active set", S, " x =", np.round(x, 4), " multipliers =", np.round(lam, 4))
active set [0]  x = [1. 2.]  multipliers = [2.]

Exactly one guess passes all the checks. Trying every subset is exponential in the number of constraints; active-set methods walk through the subsets intelligently, and interior-point methods avoid guessing altogether.

A worked example: water filling

Allocate power pi≥0p_i \ge 0 to channels with noise levels nin_i to maximize capacity ∑ilog⁡(ni+pi)\sum_i\log(n_i + p_i) subject to ∑ipi=P\sum_ip_i = P. KKT gives pi=max⁡(0,w−ni)p_i = \max(0, w - n_i) for a "water level" ww chosen so the powers sum to PP: pour water over an uneven floor.

python · try it yourself
import numpy as np

noise = np.array([0.5, 1.0, 2.0, 4.0]); P = 3.0
lo, hi = 0.0, noise.max() + P
for _ in range(100):                                    # bisection on the water level
    w = (lo + hi) / 2
    lo, hi = (w, hi) if np.maximum(0, w - noise).sum() < P else (lo, w)
p = np.maximum(0, w - noise)
print("water level:", round(w, 6), " powers:", np.round(p, 4), " sum:", round(p.sum(), 6))
water level: 2.166667  powers: [1.6667 1.1667 0.1667 0.    ]  sum: 3.0

The noisiest channel gets no power: its constraint p4≥0p_4 \ge 0 is active.

In software

  • Solvers report the KKT residuals (stationarity, feasibility, complementarity) as their stopping criteria; IPOPT's log shows inf_pr, inf_du and mu per iteration.
  • scipy.optimize.minimize(method="trust-constr") returns multipliers in res.v; CVXPY exposes them as constraint.dual_value.

Exercises

Exercise 1: One variable

Minimize (x−2)2(x - 2)^2 subject to x≤1x \le 1. Write the KKT conditions and find λ\lambda.

Answer

x∗=1x^* = 1, λ∗=2\lambda^* = 2.

Solution

Stationarity: 2(x−2)+λ=02(x - 2) + \lambda = 0. The unconstrained minimum x=2x = 2 is infeasible, so the constraint is active: x=1x = 1, giving λ=2>0\lambda = 2 > 0. Dual feasibility holds.

Exercise 2: Inactive constraint

Minimize (x−2)2(x - 2)^2 subject to x≤5x \le 5. What is λ∗\lambda^*?

Answer

λ∗=0\lambda^* = 0, x∗=2x^* = 2.

Solution

The unconstrained minimum is feasible with g=2−5<0g = 2 - 5 < 0; complementary slackness forces λ=0\lambda = 0, and stationarity 2(x−2)=02(x - 2) = 0 holds.

Exercise 3: Wrong sign (code)

For min⁡(x−2)2\min (x - 2)^2 s.t. x≤1x \le 1, someone claims x=0.5x = 0.5 is optimal with constraint inactive. Which KKT condition fails?

Answer

Stationarity: with λ=0\lambda = 0 the gradient is −3≠0-3 \ne 0.

Solution
python · try it yourself
x, lam = 0.5, 0.0
print("stationarity:", 2 * (x - 2) + lam, " feasibility:", x - 1 <= 0, " complementarity:", lam * (x - 1) + 0)
stationarity: -3.0  feasibility: True  complementarity: 0.0

x=0.5x = 0.5 is feasible and complementarity holds trivially, but ff still decreases toward x=1x = 1.

Test yourself

Complementary slackness λigi(x∗)=0\lambda_ig_i(\mathbf{x}^*) = 0 means…

  1. all constraints are active
  2. an inactive constraint has a zero multiplier
  3. all multipliers are zero
  4. the objective is zero

For a convex problem, a point satisfying the KKT conditions is…

  1. a global minimum
  2. possibly a saddle point
  3. always infeasible
  4. a local maximum

The multipliers of inequality constraints gi≤0g_i \le 0 must be…

  1. negative
  2. non-negative
  3. zero
  4. equal to 1