Skip to main content
© 2026 ePowerAI — free, open courseware. No login required.
CoursesContact
ePOWERAI
CoursesContact
Week 11: Constraints: Projections and Penalties
Optimization

Foundations

  1. 01Week 1: Optimization for Learning
  2. 02Week 2: Convex Sets and Functions
  3. 03Week 3: Gradient Descent Theory

Model problems

  1. 04Week 4: Least Squares and Conditioning
  2. 05Week 5: Classification Losses

First-order stack

  1. 06Week 6: Stochastic Gradient Descent
  2. 07Week 7: Momentum and Acceleration
  3. 08Week 8: Adaptive First-Order Methods
  4. 09Week 9: Practical Training Dynamics

Structure

  1. 10Week 10: Nonsmooth and Composite Objectives
  2. 11Week 11: Constraints: Projections and Penalties
  3. 12Week 12: Duality for Practitioners

Nonconvex practice

  1. 13Week 13: Nonconvex Deep Learning
  2. 14Week 14: Capstone — Theory Meets Training
Week 11· Optimization· Structure8 min read

Week 11: Constraints: Projections and Penalties

Learning Outcomes
  • Apply projected gradient descent: after each GD step, project onto the feasible set
  • Compute the Euclidean projection onto an ℓ2\ell_2ℓ2​ ball and onto a box
  • Implement a soft penalty and contrast with hard projection
  • Interpret a Lagrange multiplier as the marginal cost of tightening a constraint
Prerequisites

Background: Weeks 1–3 (GD, convexity) and Week 10 (proximal operators: the proximal of an indicator function is exactly a projection). The Lagrange multiplier intuition here sets up the duality content of Week 12.

Later weeks: Advanced Optimization takes this constraint and duality material to Boyd depth (KKT, cones, IPMs).

Why constraints appear#

In many optimisation problems, parameters must satisfy hard constraints:

  • Budget limits. ∑iwi≤B\sum_i w_i \leq B∑i​wi​≤B: total allocation cannot exceed a budget
  • Physics. ∥u∥2≤umax⁡\|u\|_2 \leq u_{\max}∥u∥2​≤umax​: a robot's control input is bounded (developed in Robot Learning Week 3: Control Fundamentals)
  • Probability. wi≥0,∑iwi=1w_i \geq 0, \sum_i w_i = 1wi​≥0,∑i​wi​=1: weights must form a valid probability distribution (simplex)
  • Box constraints. w∈[a,b]w \in [a, b]w∈[a,b]: each parameter must lie in an interval
  • Robustness. ∥w∥2≤R\|w\|_2 \leq R∥w∥2​≤R: the model's parameter norm must be bounded

The constrained optimisation problem is:

min⁡x  f(x)subject to   x∈C\min_x \; f(x) \quad \text{subject to } \; x \in Cminx​f(x)subject to x∈C

where CCC is the feasible set (assumed convex for this week). GD alone ignores CCC and may step outside it. We need mechanisms that respect the constraint.

Projected gradient descent#

The simplest approach: after each GD step, project the result back onto the feasible set:

xk+1=PC(xk−η∇f(xk))x_{k+1} = P_C\big(x_k - \eta \nabla f(x_k)\big)xk+1​=PC​(xk​−η∇f(xk​))

where PC(v)=arg⁡min⁡x∈C∥x−v∥2P_C(v) = \arg\min_{x \in C} \|x - v\|_2PC​(v)=argminx∈C​∥x−v∥2​ is the Euclidean projection onto CCC.

For convex fff and convex CCC, projected GD with η=1/L\eta = 1/Lη=1/L converges at the same O(1/K)\mathcal{O}(1/K)O(1/K) rate as unconstrained GD. The projection step adds no asymptotic cost. Its per-iteration cost depends on how fast we can project onto CCC.

projection returns the nearest feasible point feasible set C v P_C(v)

Projected GD takes the usual gradient step and then maps the result back into CCC. The projection PC(v)P_C(v)PC​(v) is the closest feasible point. For a ball or a box it has a closed form and costs O(d)O(d)O(d).

When projection is cheap: A projection is fast if CCC has a simple closed form. We need PC(v)P_C(v)PC​(v) to be computable in O(d)\mathcal{O}(d)O(d) or O(dlog⁡d)\mathcal{O}(d \log d)O(dlogd) time.

Simple projections#

ℓ2\ell_2ℓ2​ ball of radius RRR: C={x∣∥x∥2≤R}C = \{x \mid \|x\|_2 \leq R\}C={x∣∥x∥2​≤R}

PC(v)=v⋅min⁡ ⁣(1,R∥v∥2)P_C(v) = v \cdot \min\!\left(1, \frac{R}{\|v\|_2}\right)PC​(v)=v⋅min(1,∥v∥2​R​)

If vvv is already inside the ball (∥v∥≤R\|v\| \leq R∥v∥≤R), nothing changes. Otherwise, rescale vvv to lie exactly on the sphere of radius RRR. Cost: O(d)\mathcal{O}(d)O(d).

Box constraints [a,b][a, b][a,b]: C=[a1,b1]×⋯×[ad,bd]C = [a_1, b_1] \times \cdots \times [a_d, b_d]C=[a1​,b1​]×⋯×[ad​,bd​]

PC(v)i=clamp(vi,ai,bi)=max⁡(ai,min⁡(vi,bi))P_C(v)_i = \text{clamp}(v_i, a_i, b_i) = \max(a_i, \min(v_i, b_i))PC​(v)i​=clamp(vi​,ai​,bi​)=max(ai​,min(vi​,bi​))

Each coordinate is independently clamped to its interval. Cost: O(d)\mathcal{O}(d)O(d).

Probability simplex: C={x∣∑ixi=1,  xi≥0}C = \{x \mid \sum_i x_i = 1,\; x_i \geq 0\}C={x∣∑i​xi​=1,xi​≥0}

The projection onto the simplex can be done in O(dlog⁡d)\mathcal{O}(d \log d)O(dlogd) via sorting (details in the browser lab code). This is more expensive than ball/box projections but still practical.

Worked example: ball projection.

Project v=(5,0)v = (5, 0)v=(5,0) onto the ℓ2\ell_2ℓ2​ ball of radius R=3R = 3R=3:

∥v∥2=5\|v\|_2 = 5∥v∥2​=5, so rescale: PC(v)=35⋅(5,0)=(3,0)P_C(v) = \frac{3}{5} \cdot (5, 0) = (3, 0)PC​(v)=53​⋅(5,0)=(3,0). The projected point lies on the boundary of the ball.

Soft penalties#

An alternative to hard projection is to add a penalty term to the objective:

min⁡x  f(x)+μ⋅p(x)\min_x \; f(x) + \mu \cdot p(x)minx​f(x)+μ⋅p(x)

where p(x)p(x)p(x) measures how much xxx violates the constraints. For example:

  • p(x)=max⁡(0,∥x∥2−R)2p(x) = \max(0, \|x\|_2 - R)^2p(x)=max(0,∥x∥2​−R)2: penalises being outside the ℓ2\ell_2ℓ2​ ball
  • p(x)=∑imax⁡(0,bi−xi)2+max⁡(0,xi−ai)2p(x) = \sum_i \max(0, b_i - x_i)^2 + \max(0, x_i - a_i)^2p(x)=∑i​max(0,bi​−xi​)2+max(0,xi​−ai​)2: penalises box violations

As μ→∞\mu \to \inftyμ→∞, the penalty solution approaches the hard-constrained solution. Finite μ\muμ allows some violation, a "soft" constraint.

Tradeoff:

  • Hard projection: exactly feasible at every step, but the projection itself can be expensive
  • Soft penalty: simple GD on the augmented objective (no projection), but constraints are only approximately satisfied
  • In practice: use hard projection for simple constraints (ball, box); soft penalty or augmented Lagrangian for complex constraints

Lagrange multiplier intuition#

For the problem min⁡xf(x)\min_{x} f(x)minx​f(x) subject to h(x)≤0h(x) \leq 0h(x)≤0, the Lagrangian is:

L(x,λ)=f(x)+λh(x),λ≥0L(x, \lambda) = f(x) + \lambda h(x), \quad \lambda \geq 0L(x,λ)=f(x)+λh(x),λ≥0

The Lagrange multiplier λ\lambdaλ acts as a "price" for violating the constraint. If λ\lambdaλ is large, the penalty for h(x)>0h(x) > 0h(x)>0 is severe and the solver stays well inside the feasible region. If λ\lambdaλ is small, violation is cheap.

At the optimum x∗x^*x∗, complementary slackness holds: λ∗h(x∗)=0\lambda^* h(x^*) = 0λ∗h(x∗)=0. Either the constraint is active (h(x∗)=0h(x^*) = 0h(x∗)=0) or the multiplier is zero (λ∗=0\lambda^* = 0λ∗=0, the constraint costs nothing).

The optimal λ∗\lambda^*λ∗ also has a sensitivity interpretation: if we tighten the constraint by a small amount ε\varepsilonε (change h(x)≤0h(x) \leq 0h(x)≤0 to h(x)≤−εh(x) \leq -\varepsilonh(x)≤−ε), the optimal objective value changes by approximately λ∗ε\lambda^* \varepsilonλ∗ε. The multiplier tells you how much you'd gain by relaxing the constraint.

This intuition is the gateway to duality (Week 12), where we solve the dual problem max⁡λ≥0min⁡xL(x,λ)\max_{\lambda \geq 0} \min_x L(x, \lambda)maxλ≥0​minx​L(x,λ) instead.

Knowledge check#

Exercise · Fill in the blank

Project v = (5, 0) onto the ℓ₂ ball of radius 3. What is the x-coordinate of the projection?

Question 1 of 4

Projected GD replaces xₖ₊₁ = xₖ − η∇f(xₖ) with xₖ₊₁ = ___ (use P_C notation).

Browser lab: projected GD on the simplex#

Run projected GD to minimise f(x)=∥x−x0∥22f(x) = \|x - x_0\|_2^2f(x)=∥x−x0​∥22​ subject to xxx being on the probability simplex. Plot the trajectory and observe the projection at each step.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

def project_simplex(v):
    u = np.sort(v)[::-1]
    cssv = np.cumsum(u)
    rho = np.nonzero(u * np.arange(1, len(v) + 1) > (cssv - 1))[0][-1]
    theta = (cssv[rho] - 1.0) / (rho + 1.0)
    return np.maximum(v - theta, 0)

x0 = np.array([2.0, -1.0, 0.5])
def f(x): return 0.5 * np.sum((x - x0)**2)
def grad(x): return x - x0

w = np.array([0.4, 0.3, 0.3])
eta = 0.5
K = 30
path = [w.copy()]; losses = [f(w)]

for _ in range(K):
    w = project_simplex(w - eta * grad(w))
    path.append(w.copy()); losses.append(f(w))

path = np.array(path)

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 4.5))
ax1.plot(path[:, 0], path[:, 1], "o-", markersize=3, label="Projected GD")
ax1.set_xlabel("x1"); ax1.set_ylabel("x2")
w_opt = project_simplex(x0)
ax1.plot(w_opt[0], w_opt[1], "kx", markersize=10, label="optimum on simplex")
ax1.plot(x0[0], x0[1], "rx", markersize=8, label="unconstrained target x0")
ax1.set_title("Iterates on the Simplex (x1–x2 plane)")
ax1.legend()
ax2.plot(losses)
ax2.set_xlabel("Iteration"); ax2.set_ylabel("f(x)")
ax2.set_title("Loss vs Iteration")
ax2.grid(True, alpha=0.3)
plt.tight_layout(); plt.show()

print(f"Final point on simplex: {np.round(path[-1], 4)}, sum={path[-1].sum():.6f}")
print("Notice: every iterate stays on the simplex — the projection enforces the constraint exactly at each step.")

Key takeaways#

  • Projected gradient descent takes the usual step and projects back onto the feasible set, and it matches unconstrained GD's rate when fff and CCC are both convex.
  • The projection has to be cheap to be useful: the ℓ2\ell_2ℓ2​ ball rescales, the box clamps, and the simplex sorts, all O(d)\mathcal{O}(d)O(d) or O(dlog⁡d)\mathcal{O}(d \log d)O(dlogd).
  • Soft penalties replace the hard constraint with a penalty in the objective; finite μ\muμ permits some violation, and μ→∞\mu \to \inftyμ→∞ recovers the constrained solution.
  • The Lagrange multiplier is the shadow price of the constraint: λ∗\lambda^*λ∗ is the marginal change in the optimal objective when the constraint is tightened.
  • Complementary slackness λihi(x)=0\lambda_i h_i(x) = 0λi​hi​(x)=0 says an inactive constraint has zero price, while a binding one can have a positive price.
  • Hard projection for simple sets, penalties or augmented Lagrangians for complex ones. The multiplier intuition is the doorway to Week 12.

Further reading#

  • Stephen Boyd & Lieven Vandenberghe, Convex Optimization (Cambridge University Press, 2004). §4.2 develops projection onto convex sets and gives the ball, box, and simplex formulas.
  • Dimitri P. Bertsekas, Nonlinear Programming (3rd ed., Athena Scientific, 2016). Chapter 3 covers projection and penalty methods for constrained problems.
  • Neal Parikh & Stephen Boyd, Proximal Algorithms (Foundations and Trends in Optimization, 2014). Section 6 treats projection as the proximal operator of an indicator function.

Reading is free. PDF and ePub unlock with the newsletter. Join once and every lesson opens in this browser.

← Previous
Week 10: Nonsmooth and Composite Objectives
Next →
Week 12: Duality for Practitioners
On this page
  • Why constraints appear
  • Projected gradient descent
  • Simple projections
  • Soft penalties
  • Lagrange multiplier intuition
  • Knowledge check
  • Browser lab: projected GD on the simplex
  • Key takeaways
  • Further reading