Optimization and Calculus for Machine Learning

Constrained Optimization

Feasible sets, active constraints, and Lagrange multipliers as a geometric method for optimizing under restrictions.

Lesson
33

Goal

Learn how to minimize an objective when not every parameter choice is allowed.

Unconstrained optimization asks:

Which point makes the objective smallest?

Constrained optimization adds a second question:

Which of those points are legal?

The answer is often found where the objective would like to move in one direction, while a constraint prevents that movement.

Motivation

Many machine-learning problems have restrictions:

Without constraints, gradient descent can move anywhere in parameter space. With constraints, a downhill step may leave the allowed region.

Constrained optimization separates three objects:

  1. the objective, which says what “better” means;
  2. the constraints, which say what is allowed;
  3. the feasible set, which contains every allowed point.

Notation / Symbol Table

SymbolPlain-English nameMeaning
xxdecision variableThe point or parameter vector being chosen.
f(x)f(x)objectiveThe scalar quantity to minimize.
g(x)=0g(x)=0equality constraintA rule that must hold exactly.
h(x)0h(x)\le 0inequality constraintA rule that sets an allowed side of a boundary.
CCfeasible setAll points satisfying every constraint.
λ\lambdaequality multiplierWeight attached to an equality constraint.
μ\muinequality multiplierNonnegative weight attached to an inequality constraint.
L\mathcal{L}LagrangianObjective plus multiplier-weighted constraint terms.

The Feasible Set

Suppose we want to minimize f(x)f(x) subject to constraints. The feasible set is:

C={x:gi(x)=0, hj(x)0}C=\{x: g_i(x)=0,\ h_j(x)\le0\}

A point is feasible when it belongs to CC. Otherwise it is infeasible.

For example, consider:

x+y=1,x0,y0x+y=1, \qquad x\ge0, \qquad y\ge0

The feasible set is the line segment from (1,0)(1,0) to (0,1)(0,1). The equality restricts us to a line, and the inequalities cut that line down to a segment.

This is the same geometry as a probability vector with two entries.

Equality Versus Inequality Constraints

An equality constraint such as:

g(x)=0g(x)=0

usually restricts motion to a lower-dimensional surface.

An inequality constraint such as:

h(x)0h(x)\le0

usually permits a region on one side of a boundary.

For example:

x2+y2=1x^2+y^2=1

describes only the boundary of the unit circle, while:

x2+y21x^2+y^2\le1

describes the filled unit disk.

The distinction matters because the set of allowed directions is different at an interior point and at a boundary point.

Why The Unconstrained Gradient Condition Is Not Enough

For an unconstrained differentiable problem, a candidate minimum often satisfies:

f(x)=0\nabla f(x^*)=0

But a constrained minimum may have a nonzero gradient.

Imagine minimizing height while walking along a contour drawn on a hillside. The hillside may continue downward away from the contour, but that direction is forbidden. You are finished when there is no downhill direction along the allowed path.

The constraint can balance a nonzero objective gradient.

Geometry Of A Constrained Optimum

Consider one equality constraint:

g(x)=0g(x)=0

At a smooth point, g(x)\nabla g(x) is normal to the constraint surface. Feasible infinitesimal motion must be tangent to that surface.

At a constrained optimum, the component of f(x)\nabla f(x) along every feasible tangent direction must vanish. Therefore f(x)\nabla f(x) must also point in a normal direction.

With one equality constraint, the two gradients are parallel:

f(x)=λg(x)\nabla f(x^*)=-\lambda\nabla g(x^*)

Equivalently:

f(x)+λg(x)=0\nabla f(x^*)+\lambda\nabla g(x^*)=0

The multiplier λ\lambda supplies the scale needed to balance the two gradients.

The Lagrangian

For the problem:

minxf(x)subject tog(x)=0\min_x f(x) \quad \text{subject to} \quad g(x)=0

define the Lagrangian:

L(x,λ)=f(x)+λg(x)\mathcal{L}(x,\lambda)=f(x)+\lambda g(x)

Then solve the stationarity equations:

xL(x,λ)=0\nabla_x\mathcal{L}(x,\lambda)=0

and:

Lλ=g(x)=0\frac{\partial\mathcal{L}}{\partial\lambda}=g(x)=0

The derivative with respect to λ\lambda restores the original constraint. The derivatives with respect to xx express the gradient-balance condition.

The Lagrangian does not erase the constraint. It packages the objective, constraint, and balancing condition into one system of equations.

Worked Example: Closest Point On A Line

Minimize:

f(x,y)=x2+y2f(x,y)=x^2+y^2

subject to:

x+y=1x+y=1

The objective is squared distance from the origin. The constraint restricts us to a line.

Write the constraint in zero form:

g(x,y)=x+y1=0g(x,y)=x+y-1=0

The Lagrangian is:

L(x,y,λ)=x2+y2+λ(x+y1)\mathcal{L}(x,y,\lambda)=x^2+y^2+\lambda(x+y-1)

Differentiate:

Lx=2x+λ=0\frac{\partial\mathcal{L}}{\partial x}=2x+\lambda=0 Ly=2y+λ=0\frac{\partial\mathcal{L}}{\partial y}=2y+\lambda=0 Lλ=x+y1=0\frac{\partial\mathcal{L}}{\partial\lambda}=x+y-1=0

The first two equations imply:

x=yx=y

Substitute into the constraint:

2x=12x=1

so:

x=y=12x=y=\frac12

The constrained optimum is (12,12)(\frac12,\frac12). The unconstrained minimum (0,0)(0,0) is lower, but it is not feasible.

What The Multiplier Means

The multiplier is more than an algebraic helper.

Suppose the constraint depends on a budget bb:

g(x)=bg(x)=b

Under suitable regularity conditions, the optimal multiplier measures how much the best objective value changes when the budget changes slightly.

This is often called a shadow price.

Intuitively:

Always interpret the sign together with the exact form used in the Lagrangian.

Inequality Constraints And Active Boundaries

Now consider:

h(x)0h(x)\le0

At a candidate solution, the constraint is:

An inactive constraint has slack: the point is safely inside the allowed region. An active constraint touches the boundary and may block further improvement.

For an inequality multiplier μ\mu, the Karush-Kuhn-Tucker conditions include:

μ0\mu\ge0

and complementary slackness:

μh(x)=0\mu h(x)=0

This product says:

Only a binding constraint exerts multiplier pressure.

KKT Conditions: The Full Checklist

For:

minxf(x)\min_x f(x)

subject to:

gi(x)=0,hj(x)0g_i(x)=0, \qquad h_j(x)\le0

define:

L(x,λ,μ)=f(x)+iλigi(x)+jμjhj(x)\mathcal{L}(x,\lambda,\mu) =f(x)+\sum_i\lambda_i g_i(x)+\sum_j\mu_j h_j(x)

The KKT conditions are:

  1. Stationarity

    xL(x,λ,μ)=0\nabla_x\mathcal{L}(x,\lambda,\mu)=0
  2. Primal feasibility

    gi(x)=0,hj(x)0g_i(x)=0, \qquad h_j(x)\le0
  3. Dual feasibility

    μj0\mu_j\ge0
  4. Complementary slackness

    μjhj(x)=0\mu_jh_j(x)=0

These are candidate conditions in general. For convex problems under appropriate regularity assumptions, they can certify global optimality.

That connection is why Lesson 32 matters: convexity turns local-looking KKT equations into powerful global certificates.

Hard Constraints Versus Penalties

A hard-constrained problem has the form:

minxf(x)subject tog(x)=0\min_x f(x) \quad \text{subject to} \quad g(x)=0

A penalty method instead solves something like:

minxf(x)+ρg(x)2\min_x f(x)+\rho\,g(x)^2

where ρ>0\rho>0 controls how expensive violation is.

The difference is important:

Regularization often resembles a penalty, but its purpose may be to express a preference rather than approximate an inviolable rule.

Projected Gradient Descent

Another strategy is to take an ordinary gradient step and project back onto the feasible set:

zt+1=xtηf(xt)z_{t+1}=x_t-\eta\nabla f(x_t) xt+1=ΠC(zt+1)x_{t+1}=\Pi_C(z_{t+1})

Here ΠC\Pi_C means the closest feasible point in CC under the chosen distance.

This connects directly to earlier projection lessons:

Optimize in the ambient space, then use projection to restore feasibility.

When CC is convex, the Euclidean projection is uniquely defined. For complicated sets, computing the projection may itself be an optimization problem.

Connection To Probability

A categorical probability vector pp with nn entries satisfies:

pi0p_i\ge0

and:

i=1npi=1\sum_{i=1}^n p_i=1

These constraints define the probability simplex, a convex feasible set.

Softmax handles these constraints through parameterization: unconstrained logits are mapped automatically to positive values that sum to one.

This reveals a fourth strategy beyond Lagrangians, penalties, and projection:

Reparameterize the problem so every parameter value automatically produces a feasible solution.

Connection To Machine Learning

Constrained optimization appears when we:

Different constraints call for different tools:

StrategyCore ideaMain tradeoff
LagrangianBalance objective and constraints with multipliers.Requires solving a coupled system.
PenaltyCharge the objective for violation.Finite penalties may remain infeasible.
ProjectionReturn each step to the feasible set.Projection may be expensive.
ReparameterizationMake infeasible states impossible.Can change geometry or be hard to design.

Connection To Programming

For a scalar equality constraint, a Lagrangian helper can keep the roles explicit:

type ScalarField = (x: readonly number[]) => number;

function lagrangian(
  objective: ScalarField,
  constraint: ScalarField,
  x: readonly number[],
  lambda: number,
): number {
  return objective(x) + lambda * constraint(x);
}

For the worked example:

const objective: ScalarField = ([x, y]) => x ** 2 + y ** 2;
const constraint: ScalarField = ([x, y]) => x + y - 1;

Notice the types:

Keeping those types separate prevents the coordinatewise mistake of treating a scalar-valued objective as though it returned one value per coordinate.

Common Mistakes

Forgetting zero form

Before building a Lagrangian, rewrite an equality as:

g(x)=0g(x)=0

Finding a stationary point but not checking feasibility

A solution to the gradient equations is irrelevant if it violates the original constraints.

Treating every inequality as active

An inequality may have slack. Complementary slackness determines whether its multiplier can be nonzero.

Assuming KKT always proves global optimality

KKT conditions need assumptions. Convexity and regularity conditions are what turn them into a global certificate.

Confusing a penalty with a hard constraint

A large penalty discourages violation. It does not automatically make violation impossible.

Summary

Constrained optimization minimizes an objective over a feasible set:

minxCf(x)\min_{x\in C}f(x)

Equality constraints define surfaces, while inequalities define allowed sides of boundaries.

At a smooth equality-constrained optimum, the objective gradient is balanced by constraint gradients:

f(x)+λg(x)=0\nabla f(x^*)+\lambda\nabla g(x^*)=0

The Lagrangian packages this balance:

L(x,λ)=f(x)+λg(x)\mathcal{L}(x,\lambda)=f(x)+\lambda g(x)

For inequalities, KKT conditions add nonnegative multipliers and complementary slackness.

Convexity is the bridge from candidate conditions to global guarantees.

Exercises

  1. In your own words, distinguish an objective function, a constraint, and a feasible set.
  2. Describe the feasible set defined by x+y=1x+y=1, x0x\ge0, and y0y\ge0. Is it convex?
  3. For f(x,y)=x2+y2f(x,y)=x^2+y^2 subject to x+y=2x+y=2, write the Lagrangian and solve for the constrained minimum.
  4. Explain geometrically why f\nabla f and g\nabla g are parallel at a smooth equality-constrained optimum.
  5. For the constraint h(x)=x30h(x)=x-3\le0, classify it as active or inactive at x=2x=2 and at x=3x=3.
  6. If h(x)<0h(x)<0, use complementary slackness to determine the corresponding multiplier μ\mu.
  7. Compare a hard constraint g(x)=0g(x)=0 with the penalty term ρg(x)2\rho g(x)^2. What can a finite penalty fail to guarantee?
  8. A probability vector p=(p1,p2,p3)p=(p_1,p_2,p_3) must have nonnegative entries summing to one. State its equality and inequality constraints.
  9. Explain why projecting onto a convex feasible set after a gradient step can restore feasibility.
  10. Write TypeScript-style pseudocode for one projected-gradient step given a gradient function and a projection function.

Looking Ahead

Lagrange multipliers rely on gradients of functions with several inputs.

The next lessons will slow down and formalize that machinery: partial derivatives, gradients as local linear maps, and Jacobians for vector-valued functions.