Constrained optimization and Lagrange multipliers
Turn an equality-constrained problem into stationarity of a Lagrangian.
Real problems come with constraints: minimize loss subject to a budget, a norm bound, or a physical law. Lagrange multipliers convert a constrained problem into an unconstrained stationarity condition by encoding each constraint with its own variable. The geometry behind it, gradients lining up at the optimum, is the seed of duality and the KKT conditions.
The problem
An equality-constrained problem is
The feasible set is the intersection of the constraint surfaces . A solution must minimize while staying on this set; the unconstrained condition no longer applies, because moving off the surface is not allowed.
The geometric condition
At a constrained minimum , you cannot decrease by any feasible move. A feasible move is one tangent to the constraint surface, that is, orthogonal to every constraint gradient . For no such move to decrease , the objective gradient must have no component along the surface: must lie entirely in the span of the constraint gradients.
[1]for some scalars , the Lagrange multipliers. With one constraint, this says and are parallel: the level set of is tangent to the constraint surface at the optimum. If they were not parallel, you could slide along the surface to a lower level set.
The Lagrangian
Package the condition into a single function, the Lagrangian:
Its stationarity conditions reproduce the problem exactly. Setting gives the gradient-alignment condition , and setting gives back the constraint . A constrained problem in variables becomes an unconstrained stationarity problem in variables .
What the multiplier means
The multiplier is the sensitivity of the optimal value to the constraint. If the constraint is relaxed to , then
[2]the shadow price: how much the optimum improves per unit of loosening. A zero multiplier means the constraint is not binding the objective. This economic reading carries directly into the dual variables of the next lesson.
Worked example
Minimize subject to . The Lagrangian is . Stationarity: , , . The first two give , the constraint gives , and . The closest point on the line to the origin is its foot of perpendicular, confirming the gradient-alignment geometry.
Check it on arrays
import numpy as np
# min x^2 + y^2 s.t. x + y = 1, solved via the KKT linear system.
# Stationarity + constraint: [2 0 -1][x] [0]
# [0 2 -1][y] = [0]
# [1 1 0][lam] [1]
M = np.array([[2.0, 0.0, -1.0],
[0.0, 2.0, -1.0],
[1.0, 1.0, 0.0]])
rhs = np.array([0.0, 0.0, 1.0])
x, y, lam = np.linalg.solve(M, rhs)
print(f"x={x:.4f}, y={y:.4f}, lambda={lam:.4f}") # 0.5, 0.5, 1.0
# Shadow price: relax constraint to x + y = 1 + c, optimum f* = (1+c)^2 / 2
# df*/dc at c=0 should equal lambda
c = 1e-5
f_star = lambda cc: (1 + cc)**2 / 2
print("df*/dc:", round((f_star(c) - f_star(-c)) / (2 * c), 4), " lambda:", round(lam, 4))
# Confirm gradient alignment: grad f = lambda * grad h at the optimum
grad_f = np.array([2 * x, 2 * y])
grad_h = np.array([1.0, 1.0])
print("grad f == lambda grad h:", np.allclose(grad_f, lam * grad_h))Solving the Lagrange system lands on with multiplier 1, the shadow price matches the multiplier, and the objective and constraint gradients align.
Exercises
Exercise 1. Minimize subject to using a Lagrangian. Find , and the multiplier .
Solution
Step 1. Form . Stationarity in and gives and , so and .
Step 2. Impose the constraint :
Step 3. Back-substitute: and . Check the constraint: . The optimum is with , and .
Exercise 2. Verify the shadow-price interpretation for Exercise 1 directly. Relax the constraint to , find the optimal value as a function of , and confirm .
Solution
Step 1. With the right-hand side , Steps 1 and 2 of Exercise 1 repeat with replaced by : the constraint becomes , so , and , .
Step 2. Then and . The optimal value is
Step 3. Differentiate: , and at this is . The multiplier is the marginal change in the optimum per unit relaxation, as claimed.
Exercise 3 (code). Solve Exercise 1 by assembling and solving the KKT linear system, then verify the shadow price numerically with a central difference. Every result must match the hand answers (, , ).
Solution
import numpy as np
# min x^2 + 4y^2 s.t. x + y = 3, KKT system:
# 2x - lam = 0
# 8y - lam = 0
# x + y = 3
M = np.array([[2.0, 0.0, -1.0],
[0.0, 8.0, -1.0],
[1.0, 1.0, 0.0]])
rhs = np.array([0.0, 0.0, 3.0])
x, y, lam = np.linalg.solve(M, rhs)
assert np.allclose([x, y, lam], [2.4, 0.6, 4.8])
# Shadow price by central difference of f*(c) = 4(3+c)^2 / 5
f_star = lambda c: 4 * (3 + c)**2 / 5
c = 1e-5
slope = (f_star(c) - f_star(-c)) / (2 * c)
assert np.isclose(slope, lam, atol=1e-4)
print(f"x={x}, y={y}, lambda={lam}, df*/dc={slope:.4f}")The KKT solve recovers with , and the numerical slope of the optimal value matches the multiplier.
The next lesson generalizes the Lagrangian into a dual problem with its own optimization.
References
- [1]Bertsekas, D. P. (1999). Nonlinear Programming (2nd ed.), §3.1. Athena Scientific. The Lagrange multiplier theorem for equality-constrained problems.↩
- [2]Boyd, S., & Vandenberghe, L. (2004). Convex Optimization, §5.6. Cambridge University Press. The sensitivity (shadow-price) interpretation of the multipliers.↩