Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 11: Constraints: Projections and Penalties
Optimization
01Week 1: Optimization for Learning
02Week 2: Convex Sets and Functions
03Week 3: Gradient Descent Theory
04Week 4: Least Squares and Conditioning
05Week 5: Classification Losses
06Week 6: Stochastic Gradient Descent
07Week 7: Momentum and Acceleration
08Week 8: Adaptive First-Order Methods
09Week 9: Practical Training Dynamics
10Week 10: Nonsmooth and Composite Objectives
11Week 11: Constraints: Projections and Penalties
12Week 12: Duality for Practitioners
13Week 13: Nonconvex Deep Learning
14Week 14: Capstone — Theory Meets Training
Week 11· Optimization· Structure22 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
  • 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.

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.

Exercise · Fill in the blank

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

Question 1 of 4

Projected GD replaces $x_{k+1} = x_k - \eta\nabla f(x_k)$ with $x_{k+1} =$ ___ (use P_C notation).

Browser lab#

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]
    css = np.cumsum(u)
    rho = np.searchsorted((css - 1.0) / (np.arange(len(v)) + 1.0) < u, True) - 1
    r = rho + 1 if rho >= 0 else 0
    theta = (css[r-1] - 1.0) / r if r > 0 else 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}")
← 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
  • Browser lab