AI Knowledge Tree

The KKT conditions

State the KKT conditions and use them to solve inequality-constrained problems.

⏱ 32 minPrerequisites:Lagrangian duality

The Karush-Kuhn-Tucker (KKT) conditions are the inequality-constrained generalization of "set the gradient to zero". They are necessary at any optimum of a constrained problem with strong duality, and for convex problems they are also sufficient, turning optimization into solving a system of equations and inequalities. They characterize the solution of every SVM and convex program.

The four conditions

For the problem min⁡xf(x)\min_x f(x) subject to gi(x)≤0g_i(x) \le 0 and hj(x)=0h_j(x) = 0, a point x⋆x^\star with multipliers (λ⋆,ν⋆)(\lambda^\star, \nu^\star) satisfies the KKT conditions if:

  1. Stationarity. The Lagrangian gradient vanishes: ∇f(x⋆)+∑iλi⋆∇gi(x⋆)+∑jνj⋆∇hj(x⋆)=0.\nabla f(x^\star) + \sum_i \lambda_i^\star \nabla g_i(x^\star) + \sum_j \nu_j^\star \nabla h_j(x^\star) = 0.
  2. Primal feasibility. gi(x⋆)≤0g_i(x^\star) \le 0 and hj(x⋆)=0h_j(x^\star) = 0.
  3. Dual feasibility. λi⋆≥0\lambda_i^\star \ge 0 for all inequality multipliers.
  4. Complementary slackness. λi⋆ gi(x⋆)=0\lambda_i^\star\, g_i(x^\star) = 0 for each ii[1].

Complementary slackness

The fourth condition is the new ingredient. For each inequality, either the constraint is active (gi(x⋆)=0g_i(x^\star) = 0, the optimum sits on its boundary) or its multiplier is zero (λi⋆=0\lambda_i^\star = 0, the constraint does not push). A constraint cannot simultaneously be slack (gi<0g_i < 0) and exert force (λi>0\lambda_i > 0).

Why it holds (under strong duality). At the optimum f(x⋆)=D(λ⋆,ν⋆)f(x^\star) = \mathcal{D}(\lambda^\star, \nu^\star). Tracing the weak-duality proof with equalities, the slack terms ∑iλi⋆gi(x⋆)\sum_i \lambda_i^\star g_i(x^\star) must vanish; since each term is ≤0\le 0 (product of λi≥0\lambda_i \ge 0 and gi≤0g_i \le 0), each must be exactly zero. The active set, the constraints with gi=0g_i = 0, is what determines the solution.

Sufficiency for convex problems

For a convex problem (convex ff and gig_i, affine hjh_j), the KKT conditions are not only necessary but sufficient: any point satisfying all four is a global optimum[2].

Why. Stationarity makes x⋆x^\star a minimizer of the convex Lagrangian L(⋅,λ⋆,ν⋆)\mathcal{L}(\cdot, \lambda^\star, \nu^\star). Then for any feasible xx, f(x)≥L(x,λ⋆,ν⋆)≥L(x⋆,λ⋆,ν⋆)=f(x⋆)f(x) \ge \mathcal{L}(x, \lambda^\star, \nu^\star) \ge \mathcal{L}(x^\star, \lambda^\star, \nu^\star) = f(x^\star), where the last equality uses complementary slackness. So solving the KKT system is solving the problem.

Worked example

Minimize f(x)=12∥x∥2f(x) = \tfrac{1}{2}\lVert x \rVert^2 subject to a⊤x≥1a^\top x \ge 1, written g(x)=1−a⊤x≤0g(x) = 1 - a^\top x \le 0. Stationarity: x−λa=0x - \lambda a = 0, so x=λax = \lambda a. If the constraint is active, a⊤x=1a^\top x = 1 gives λ∥a∥2=1\lambda \lVert a\rVert^2 = 1, so λ=1/∥a∥2≥0\lambda = 1/\lVert a\rVert^2 \ge 0 (dual feasible) and x⋆=a/∥a∥2x^\star = a/\lVert a\rVert^2. Complementary slackness holds since g=0g = 0. All four conditions check out, certifying the global optimum.

At a constrained optimum the objective gradient aligns with the active constraint's gradient (KKT stationarity), so the contour is tangent to the boundary.
At a constrained optimum the objective gradient aligns with the active constraint's gradient (KKT stationarity), so the contour is tangent to the boundary.

Check it on arrays

python · numpy
import numpy as np

# min 0.5 ||x||^2 s.t. a^T x >= 1  ->  g(x) = 1 - a^T x <= 0
a = np.array([1.0, 2.0, 2.0])
norm2 = a @ a

lam = 1.0 / norm2                                # candidate multiplier
x = lam * a                                      # from stationarity x = lam a

# Verify all four KKT conditions
stationarity = np.allclose(x - lam * a, 0)
primal_feasible = (1 - a @ x) <= 1e-9
dual_feasible = lam >= 0
slackness = np.isclose(lam * (1 - a @ x), 0)
print("stationarity:        ", stationarity)
print("primal feasibility:  ", primal_feasible)
print("dual feasibility:    ", dual_feasible)
print("compl. slackness:    ", slackness)
print("x* =", np.round(x, 4), " f* =", round(0.5 * x @ x, 4))

# Inactive-constraint case: a^T x >= -1 is already satisfied at x=0, so lam=0.
# Then x* = 0 and the constraint is slack (g < 0) with zero multiplier.
print("\ninactive constraint: x*=0, lam=0, slackness:",
      np.isclose(0.0 * (-1 - a @ np.zeros(3)), 0))

All four KKT conditions hold at the certified optimum, and the inactive-constraint case shows complementary slackness with a zero multiplier.

Exercises

Exercise 1. Solve min⁡x12∥x∥2\min_x \tfrac12\lVert x\rVert^2 subject to a⊤x≥1a^\top x \ge 1 with a=(3,4)a = (3, 4) by hand, using the KKT conditions. Report x⋆x^\star, λ⋆\lambda^\star, and f(x⋆)f(x^\star).

Solution

Step 1. Write g(x)=1−a⊤x≤0g(x) = 1 - a^\top x \le 0. Stationarity of L=12∥x∥2+λ(1−a⊤x)\mathcal{L} = \tfrac12\lVert x\rVert^2 + \lambda(1 - a^\top x) gives x−λa=0x - \lambda a = 0, so x=λax = \lambda a.

Step 2. Guess the constraint is active, a⊤x=1a^\top x = 1. Substituting x=λax = \lambda a gives λ∥a∥2=1\lambda \lVert a\rVert^2 = 1, so λ⋆=1/∥a∥2\lambda^\star = 1/\lVert a\rVert^2. With ∥a∥2=9+16=25\lVert a\rVert^2 = 9 + 16 = 25, λ⋆=1/25=0.04≥0\lambda^\star = 1/25 = 0.04 \ge 0, so dual feasibility holds.

Step 3. Then x⋆=λ⋆a=(3/25,4/25)=(0.12,0.16)x^\star = \lambda^\star a = (3/25, 4/25) = (0.12, 0.16). Complementary slackness holds since g(x⋆)=0g(x^\star) = 0, and primal feasibility holds (a⊤x⋆=1a^\top x^\star = 1). All four conditions check out.

Step 4. The objective value is

f(x⋆)=12∥x⋆∥2=12∥a∥2=150=0.02.f(x^\star) = \tfrac12 \lVert x^\star\rVert^2 = \tfrac{1}{2\lVert a\rVert^2} = \tfrac{1}{50} = 0.02.

Exercise 2. Using complementary slackness, prove that if the constraint g(x)≤0g(x) \le 0 is strictly slack at the optimum (g(x⋆)<0g(x^\star) < 0), then its multiplier must be zero, and conversely a nonzero multiplier forces the constraint active.

Solution

Step 1. Complementary slackness states λ⋆g(x⋆)=0\lambda^\star g(x^\star) = 0. This is a product of two numbers equal to zero, so at least one factor is zero.

Step 2. Suppose g(x⋆)<0g(x^\star) < 0 (strictly slack). Then g(x⋆)≠0g(x^\star) \neq 0, so the product can be zero only if λ⋆=0\lambda^\star = 0. A constraint that does not bind exerts no force.

Step 3. Conversely, suppose λ⋆>0\lambda^\star > 0. Then λ⋆≠0\lambda^\star \neq 0, so the product forces g(x⋆)=0g(x^\star) = 0: the constraint is active. A constraint that pushes must sit on its boundary. The two cases are the active set and the inactive set, with no point both slack and forceful.

Exercise 3 (code). A box-constrained least squares: minimize f(x)=12∥x−c∥2f(x) = \tfrac12\lVert x - c\rVert^2 subject to x≤ux \le u componentwise, with c=(2,−1,3)c = (2, -1, 3) and u=(1,1,1)u = (1, 1, 1). The KKT-optimal solution clips each coordinate at its upper bound, xi⋆=min⁡(ci,ui)x^\star_i = \min(c_i, u_i). Verify the four KKT conditions hold at this clipped point.

Solution
python · numpy
import numpy as np

c = np.array([2.0, -1.0, 3.0])
u = np.array([1.0, 1.0, 1.0])

# Constraints g_i(x) = x_i - u_i <= 0. Gradient of f is x - c.
# KKT solution clips at the bound where c_i > u_i.
x = np.minimum(c, u)
lam = np.maximum(c - u, 0.0)     # multiplier active where the bound binds

g = x - u
stationarity = np.allclose((x - c) + lam, 0)   # grad f + sum lam_i grad g_i = 0
primal_feasible = np.all(g <= 1e-12)
dual_feasible = np.all(lam >= 0)
slackness = np.allclose(lam * g, 0)

assert stationarity and primal_feasible and dual_feasible and slackness
print("x* =", x, " lambda =", lam)
print("all four KKT conditions hold:",
      stationarity and primal_feasible and dual_feasible and slackness)

Each coordinate either sits below its bound with a zero multiplier or clips to the bound with a positive multiplier; complementary slackness holds coordinatewise, certifying the clipped point as the global optimum.

The next lesson returns to unconstrained methods but uses curvature: Newton's method.


References

  1. [1]Boyd, S., & Vandenberghe, L. (2004). Convex Optimization, §5.5. Cambridge University Press. The KKT conditions and complementary slackness.↩
  2. [2]Bertsekas, D. P. (1999). Nonlinear Programming (2nd ed.), §3.3. Athena Scientific. Necessity and, for convex problems, sufficiency of the KKT conditions.↩