Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

9.4 Constrained Optimization

In Section 9.3 we saw that the gradients of a surface can be used to find its maxima or minima. Since we allowed any inputs to the surface, the corresponding problems were unconstrained. An unconstrained optimization problem is a problem of the form:

Find: x∗=arg max⁡all x{f(x)}.\textbf{Find: } x_* = \argmax_{\text{all } x}\{f(x)\}.

A constrained optimization problem restricts the range of possible inputs, xx.

Constraint Functions

The set X\mathcal{X} could be defined explicitly, by listing all of its elements. In practice, we usually define the set implicitly via list of criteria that all its elements must satisfy. These are the constraints. We say that X\mathcal{X} is defined by a set of equality constraints if:

For the remainder of this class we will assume that all constrained optimization problems are defined exclusively by a set of equality constraints. To learn about constrained problems that incorporate inequality constraints, take an optimization class, or read on the Karush-Kuhn-Tucker (KKT) conditions.

The explicit and implicit definitions of X\mathcal{X} have different advantages and disadvantages:

  • When X\mathcal{X} is defined explicitly, it is easy to list the elements of a set, but it takes some work to identify a rule that defines the set. In the example above, we started with an explicit description, “X\mathcal{X} is a circle, of radius 3, centered at the origin” and converted it into an implicit definition defined by an equality constraint, “X\mathcal{X} is the set of all xx such that g(x)=9g(x) = 9.”

  • When X\mathcal{X} is defined implicitly, we can use the constraints to check whether an input belongs to X\mathcal{X}. Just check whether g(x)=0g(x) = 0 (or the desired cc) for each equality constraint. If all of the constraints are satisfied then x∈Xx \in \mathcal{X}. If not, x∉Xx \notin \mathcal{X}. While it is easy to check whether any particular input xx satisfies the constraints, it may be hard to find inputs xx will satisfy the constraints. So, it may be hard to recover an explicit representation from an implicit representation. The second animation displayed above works in this direction. It starts from an equality constraint, and recovers an explicit representation of the associated set.

The relation between these two definitions is exactly the relation between surfaces, levels, and level sets that we studied in Section 8.2.

While it can be hard to identify the exact form of a level set for arbitrary functions, a variety of standard sets may be recovered as the level sets of familiar functions. For example:

  1. Every linear set may be described by setting ax1+bx2+c=0a x_1 + b x_2 + c = 0 for some aa, bb, and cc.

  2. Every circular set may be described by setting x12+x22=r2x_1^2 + x_2^2 = r^2 for some radius r>0r > 0.

  3. Every elliptical set may be described by setting ax12+bx22=r2a x_1^2 + b x_2^2 = r^2 for some a>0 a > 0, b>0b > 0, and r>0r > 0.

The same formulas extend naturally to higher dimensions. For example, every planar set may be expressed by setting a1x1+a2x2+a3x3+c=a⋅x+c=0a_1 x_1 + a_2 x_2 + a_3 x_3 + c = a \cdot x + c = 0 for some a=[a1,a2,a3]a = [a_1,a_2,a_3] and some cc. In this case the vector aa is the “normal” vector to the plane (it is perpendicular to the plane) so the choice of aa controls the orientation of the plane. The choice of cc controls the specific position of the plane.

Finding Optima

Consider the constrained problem:

Maximize: f(x)Given: g(x)=0\begin{aligned} & \textbf{Maximize: } f(x) \\ & \textbf{Given: } g(x) = 0 \end{aligned}

for some objective function ff and constraint function gg. How can we find the best xx?

When the set X={all x such that g(x)=0}\mathcal{X} = \{\text{all } x \text{ such that } g(x) = 0\} is low dimensional, and easy to parameterize, then we can find the best xx by evaluating ff at every x∈Xx \in \mathcal{X}. The animation below visualizes this approach for the example introduced at the start of the chapter. Here f(x)f(x) is a bowl with elliptical level sets, and X\mathcal{X} is a circle, of radius 3, centered at the origin.

In this case, we can parameterize X\mathcal{X} by expressing every point on the circle in polar coordinates. In polar coordinates, every point is specified by a radius and an angle. All points on the circle have radius 3. So, every point on the circle is uniquely specified by an angle θ\theta. To maximize ff over inputs on the circle, we set [x1(θ),x2(θ)]=[3cos⁡(θ),3sin⁡(θ)][x_1(\theta),x_2(\theta)] = [3 \cos(\theta), 3 \sin(\theta)] then sweep over θ\theta. This process identifes the maxima [±3,0][\pm 3, 0].

The objective, f(x)f(x), restricted to the set X\mathcal{X} satisfying the equality constraint, g(x)=9g(x) = 9. Points in X\mathcal{X} may be uniquely identified by fixing an angle θ\theta. By sweeping over θ\theta we can identify the points, in X\mathcal{X}, that maximize ff. Right click on the video to access controls.

This approach, while intuitive, does not work well when X\mathcal{X} is hard to parameterize, and, in absence of a parameterization, requires checking every point in an explicit representation of X\mathcal{X}. What we need is a rule that can identify possible optimizers without an explicit parameterization of X\mathcal{X}.

The next section derives the appropriate rule. It generalizes the ddxf(x)=0\frac{d}{dx} f(x) = 0 rule used in unconstrained univariate optimization, and the ∇f(x)=0\nabla f(x) = 0 rule used in multivariate unconstrained optimization. Like those rules, the procedure derived below can be used to identify possible (e.g. feasible) optima. Then, to actually identify an optima, check the value of ff at all feasible points.

To anticipate the necessary rule, let’s simplify our picture. To simplify, we’ll view the problem “from above”, via a contour plot. The animation below adds the level sets of ff to the heatmap in the left hand panel, then drops the three-dimensional view. Make sure you are comfortable with this picture before progressing

Conversion to a contour plot. Notice, the red circle representing X\mathcal{X} is parallel to the dashed white line representing a level set of ff passing through the gold points representing optima! Right click on the video to access controls.

Notice, at the optimal points (gold dots), the contours of ff (dashed white boundaries), and the set X\mathcal{X} (orange circle), are related. The level set of ff, passing through the optimal points on X\mathcal{X}, is tangent to X\mathcal{X} at the optimum!

This is a sensible observation. If X\mathcal{X} wasn’t tangent to a level set of ff at a point xx, then, it would be possible to increase the value of ff, by moving along X\mathcal{X} in a direction that crossed the level set from below. In other words, it would be possible to find a direction, tangent to X\mathcal{X}, along which ff is increasing. No point can be maximal if there exists a direction we could move in, that stays in X\mathcal{X}, but increases ff. So, to identify optima, we will look for locations where the level sets of ff are tangent to the set X\mathcal{X}. This is usually done via the method of Lagrange multipliers which converts the statement “check whether the level set of ff is tangent to X\mathcal{X}” into a statement regarding the gradients of ff and gg.

Lagrange Multipliers Identify Feasible Points

Introducing constraints on the inputs both restricts the range of possible input values we need to consider, and relaxes the requirements optima must satisfy. If ff is smooth, and xx is unconstrained, then x∗x_* cannot be an extrema of ff unless ∇f(x∗)=0\nabla f(x_*) = 0. In other words, when xx is unconstrained, all finite extrema of smooth ff correspond to locations where the surface is flat. If we add constraints, then ff may be optimized at a location x∗∈Xx_* \in \mathcal{X} where ff is not flat, provided that there is no way to move uphill or downhill on ff, from x∗x_*, while staying inside X\mathcal{X}.

To express this idea formally, let X\mathcal{X} denote a set defined by a collection of equality constraints, let x∗∈Xx_* \in \mathcal{X} denote a point in X\mathcal{X}, and let T(x∗)\mathcal{T}(x_*) denote the collection of tangent directions to the set X\mathcal{X} leaving x∗x_*. The set T(x∗)\mathcal{T}(x_*) contains all of the vectors vv such that the line x(t)=x∗+tv^x(t) = x_* + t \hat{v} is tangent to X\mathcal{X} at x∗x_*. It represents all of the directions in which we could move away from x∗x_* while remaining inside of X\mathcal{X}.

A point x∗x_* may be a solution to a constrained optimization problem if it:

  1. Satisfies the constraint g(x∗)=0g(x_*) = 0

  2. There is no direction v∈T(x∗)v \in \mathcal{T}(x_*) along which f(x+tv^)f(x + t \hat{v}) is increasing or decreasing.

In the optimization literature the first constraint is called primal feasibility and the second is called dual feasibility. A point x∗x_* that satisfies 1 and 2 is “feasible” in the sense that it could be an optima. If x∗x_* does not satisfy 1 and 2, then it cannot solve the constrained optimization problem.

The second constraint is the most interesting. It requires that the surface ff is flat at x∗x_* along every path x(t)∈Xx(t) \in \mathcal{X} passing through x∗x_*. This is entirely analogous to the observation that, in an unconstrained problem, x∗x_* cannot be an extrema unless ∇f(x∗)=0\nabla f(x_*) = 0, so a point x∗x_* is only feasible if ∇f(x∗)=0\nabla f(x_*) = 0.

Recall that a directional derivative, ∂vf(x∗)\partial_v f(x_*) evaluates the instanteous rate of change (slope) of the surface ff along a path oriented in the direction vv leaving the point x∗x_*. Then, the second constraint may be expressed:

Dual feasibility is the generalization of the feasibility statement we derived in Section 9.3, “x∗x_* cannot be an extrema of ff unless ∇f(x∗)=0\nabla f(x_*) = 0”, to constrained problems. Both generalize the familiar one dimensional rule, “set the derivative of ff to zero.” from Section 3.3.

To simplify the dual feasibility statement, recall that every directional derivative may be expressed in terms of the gradient of ff (see Section 9.2):

∂vf(x∗)=∇f(x∗)⋅v^\partial_v f(x_*) = \nabla f(x_*) \cdot \hat{v}

where v^\hat{v} is the unit vector pointing in the same direction as vv. So, dual feasibility may be expressed:

  1. ∇f(x∗)⋅v^=0\nabla f(x_*) \cdot \hat{v} = 0 for all v∈T(x∗)v \in \mathcal{T}(x_*).

This is a bit easier to think about since ∇f(x∗)\nabla f(x_*) is fixed by ff and x∗x_*. It separates the term associated with the slope of ff from the term associated with admissible directions along which we could move the input xx.

The animation shown below illustrates the relationship between ff, the set X\mathcal{X}, tangent directions T\mathcal{T}, the partial derivative of ff along directions v∈Tv \in \mathcal{T}, and the gradient ∇f\nabla f.

To find a directional derivative of ff, along a direction, vv, tangent to X\mathcal{X}, evaluate ∂vf(x)=∇f(x)⋅v^\partial_v f(x) = \nabla f(x) \cdot \hat{v} where v^\hat{v} is the unit vector parallel to vv. The left panel in the animation above shows ff as a heatmap with overlaid contours, X\mathcal{X} and the tangent to X\mathcal{X} at a particular input, a direction vv parallel to the tangent, and the gradient of ff at the input point. The right hand panel shows f(x(θ))f(x(\theta)). The slope of f(x(θ))f(x(\theta)) is determined by the partial derivative of ff along directions tangent to X\mathcal{X}. To find maxima, we will need to identify locations where all of these partial derivatives equal zero. In the animation, these are the inputs where ddθf(x(θ))=0\frac{d}{d\theta} f(x(\theta)) = 0. Right click on the video to access controls.

If ∇f(x∗)=0\nabla f(x_*) = 0, then ∇f(x∗)⋅v^=0⋅v^=0\nabla f(x_*) \cdot \hat{v} = 0 \cdot \hat{v} = 0. So, if x∗x_* is a location where ff is flat, then x∗x_* is feasible for both the unconstrained and the constrained problem. This makes sense. If x∗x_* is a local maximizer or minimizer of ff, it will remain a local maximizer or minimizer of ff if we restrict the allowed ways in which we could perturb the input xx away from x∗x_*. So, any point that is in X\mathcal{X}, where ∇f\nabla f is zero, is feasible.

The dual feasibility constraint allows other solutions. Suppose that ∇f(x∗)≠0\nabla f(x_*) \neq 0. If vv is perpendicular to ∇f(x∗)\nabla f(x_*) then ∂vf(x∗)=∇f(x∗)⋅v^=0\partial_v f(x_*) = \nabla f(x_*) \cdot \hat{v} = 0. So, if ∇f(x∗)\nabla f(x_*) is perpendicular to vv for all v∈T(x∗)v \in \mathcal{T}(x_*), then ∂vf(x∗)=∇f(x∗)⋅v^=0\partial_v f(x_*) = \nabla f(x_*) \cdot \hat{v} = 0 for all v∈T(x∗)v \in \mathcal{T}(x_*).

The converse also holds. Two nonzero vectors are perpendicular if and only if their inner product equals zero. Therefore, if ∇f(x∗)≠0\nabla f(x_*) \neq 0, then ∇f(x∗)⋅v^=0\nabla f(x_*) \cdot \hat{v} = 0 if and only if vv is perpendicular to ∇f(x∗)\nabla f(x_*).

Now we can write the dual feasibility constraint geometrically:

In other words, all tangent lines to the set X\mathcal{X}, passing through x∗x_*, must be perpendicular to the gradient of ff at x∗x_*.

The animation shown below illustrates this idea. The left hand panel shows the gradient of ff and the tangent set T\mathcal{T}, as we move from a non-optimal point in X\mathcal{X} towards an optimal point. As the input approaches the optimum, the angle between the gradient and tangent approach ninety degrees. Once the gradient and tangent are perpendicular, the partial derivative of ff, along any direction vv tangent to X\mathcal{X}, equals zero. This corresponds to a location, in the right hand plot, where the function f(x(θ))f(x(\theta)) is flat.

Follow the gradient and tangent directions, as we move our input around X\mathcal{X} from a non-optimal point to an optimal point. At the optimal point, the partial derivative of ff, with respect to any direction vv in the tangent, is equal to zero because the gradient is perpendicular to the tangent T\mathcal{T}. Since T\mathcal{T} is tangent to X\mathcal{X}, the gradient of ff is perpendicular to the set X\mathcal{X} at the optimum. Conversely, at every non-optimal point the gradient is not perpendicular to T\mathcal{T}, so there exists a direction vv, along which we could perturb xx, while remaining in X\mathcal{X}, and increasing ff. Right click on the video to access controls.

We can visualize this condition at multiple points at once. The animation shown below first shows the gradient of ff at a large collection of inputs. The resulting collection of gradient vectors are all perpendicular to the level sets of ff (shown in white). Then, the animation evaluates the gradient of ff at inputs restricted to the set X\mathcal{X}. Then, it checks whether the gradient is perpendicular to the level set by evaluating the angle between ∇f(x)\nabla f(x) and T(x)\mathcal{T}(x) for x∈Xx \in \mathcal{X}. The gradient is perpendicular to the level set at points in X\mathcal{X} that maximize, or minimize, ff. Where the gradient is not perpendicular to the level set, ff is neither maximized nor minimized.

To check dual feasibility, evaluate the gradient of ff at inputs in X\mathcal{X}. These gradients are shown as purple arrows. Then, check whether each gradient is perpendicular to the level set where it is evaluated. If it is not perpendicular, then the corresponding point cannot maximize or minimize ff. Right click on the video to access controls.

To solve for possible optima, we need to convert our geometric constraint into a system of algebraic equations that we can manipulate. Once again, we can rely on ideas we introduced earlier in the course.

Recall that (see Section 9.2), the gradient of some surface, ∇h(x)\nabla h(x), is perpendicular to the level set of hh at xx. This observation provides an alternate description for the set of directions tangent to a level set. Given a smooth function hh, and an input xx, every direction vv such that ∂vh(x)=0\partial_v h(x) = 0 is tangent to the level set of hh passing through xx.

We can use this conclusion to express the tangent set, T(x∗)\mathcal{T}(x_*) algebraicly.

By assumption, X\mathcal{X} was defined by a single equality constraint, g(x)=0g(x) = 0. So, X\mathcal{X} is a level set of the constraint function gg. It follows that:

T(x∗)={v such that ∂vg(x∗)=0}={v such that ∇g(x∗)⋅v^=0}.\mathcal{T}(x_*) = \{v \text{ such that } \partial_v g(x_*) = 0 \} = \{v \text{ such that } \nabla g(x_*) \cdot \hat{v} = 0 \}.

If ∇g(x∗)≠0\nabla g(x_*) \neq 0, then ∇g(x∗)⋅v^=0\nabla g(x_*) \cdot \hat{v} = 0 if and only if vv is perpendicular to ∇g(x∗)\nabla g(x_*). Therefore:

T(x∗)={v perpendicular to ∇g(x∗)}.\mathcal{T}(x_*) = \{v \text{ perpendicular to } \nabla g(x_*) \}.

Now, dual feasibility requires that:

  1. ∇f(x∗)\nabla f(x_*) is perpendicular to every vv that is perpendicular to ∇g(x∗)\nabla g(x_*).

In other words, ∇f(x∗)\nabla f(x_*) and ∇g(x∗)\nabla g(x_*) must both be perpendicular to the same collection of tangent directions T(x∗)\mathcal{T}(x_*).

The tangent set, T(x∗)\mathcal{T}(x_*) is the collection of all vectors that satisfy the equation ∇g(x∗)⋅v=0\nabla g(x_*) \cdot v = 0. This is one linear equation. If xx is dd dimensional, it defines a d−1d - 1 dimensional collection of vectors. For example, if d=2d = 2, then the tangent set is one-dimensional, and corresponds to a line. If d=3d = 3, then the tangent set is two-dimensional, and corresponds to a plane. In each case, there is only one direction perpendicular to the tangent set.

So, if ∇f(x∗)\nabla f(x_*) and ∇g(x∗)\nabla g(x_*) are both perpendicular to the same collection of tangent directions T(x∗)\mathcal{T}(x_*), then they must be parallel vectors!

This is, at last, a systems of equations we could manipulate to solve for candidate optima!

Note: it is also true that ∇f(x∗)\nabla f(x_*) and ∇g(x∗)\nabla g(x_*) are parallel if and only if the level sets of ff and gg at tangent at x∗x_*. So, we can view the Lagrange multiplier equation as one version of four possible feasibility statements:

  1. x∗∈Xx_* \in \mathcal{X} is feasible if ∇f(x∗)\nabla f(x_*) is perpendicular to the level set of gg at x∗x_*

  2. x∗∈Xx_* \in \mathcal{X} is feasible if ∇f(x∗)\nabla f(x_*) is parallel to ∇g(x∗)\nabla g(x_*)

  3. x∗∈Xx_* \in \mathcal{X} is feasible if the level set of ff at x∗x_* is tangent to the level set of gg at x∗x_*

  4. x∗∈Xx_* \in \mathcal{X} is feasible if the level set of ff is perpendicular to ∇g(x∗)\nabla g(x_*).

All four statements compare a gradient or level set of the objective to a gradient or level set of ff. The second comparison produces the Lagrange multiplier equation. We derived it using the first statement. The third matches the observation we drew when we first visualized ff using a contour plot. The second is usually adopted since it produces a systems of equations that can be solved to identify feasible points, however, all four statements are valid.

The animation shown below checks the first three statements at an extremal point where ff is maximized and a non-extremal point. It first checks whether the gradient of ff is perpendicular to the level set of gg. Then, it checks whether the level set of ff is tangent to the level set of gg. Finally, it checks whether the gradient of ff is parallel to the gradient of gg. The last check produces the Lagrange multiplier equation.

Checking dual feasibility at an extremal, and non-extremal, point. Right click on the video to access controls.

We can now write down the feasibility constraints as a system of equations:

So, to identify candidate optima for a constrained optimization problem:

  1. Write down the constraint g(x∗)=0g(x_*) = 0

  2. Solve for ∇f(x)\nabla f(x) and ∇g(x)\nabla g(x).

  3. Write down the system of equations

    ∇f(x∗)=λ∇g(x∗)\nabla f(x_*) = \lambda \nabla g(x_*)

    where x∗x_* and λ\lambda are free variables.

  4. Solve for x∗x_* and λ\lambda by enforcing primal and dual feasibility.

  5. If there are multiple feasible points, evaluate ff at each and pick the feasible point that maximizes (or minimizes) ff.

Example

Let’s solve the constrained optimization problem we established at the start of the chapter using Lagrange multipliers.

Maximize: 14x2+125y2Given: x2+y2−9=0.\begin{aligned} & \textbf{Maximize: } \frac{1}{4} x^2 + \frac{1}{25} y^2 \\ & \textbf{Given: } x^2 + y^2 - 9 = 0. \end{aligned}

Working in order:

  1. Primal feasibility demands:

    x∗2+y∗2−9=0x_*^2 + y_*^2 - 9 = 0
  2. The gradients are:

    ∇f(x∗,y∗)=[24x∗,225y∗]\nabla f(x_*,y_*) = \left[\frac{2}{4} x_*, \frac{2}{25} y_* \right]

    and:

    ∇g(x∗,y∗)=[2x∗,2y∗]\nabla g(x_*,y_*) = [2 x_*, 2 y_*]
  3. So, dual feasibility demands:

    [24x∗225y∗]=λ[2x∗2y∗]\left[\begin{array}{c} \frac{2}{4} x_* \\ \frac{2}{25} y_* \end{array} \right] = \lambda \left[ \begin{array}{c} 2 x_* \\ 2 y_* \end{array} \right]

    Or, as a systems of equations:

    14x∗=λx∗⇒(λ−14)x∗=0125y∗=λy∗⇒(λ−125)y∗=0.\begin{aligned} & \frac{1}{4} x_* = \lambda x_* \Rightarrow \left(\lambda - \frac{1}{4} \right) x_* = 0 \\ & \frac{1}{25} y_* = \lambda y_* \Rightarrow \left(\lambda - \frac{1}{25} \right) y_* = 0. \end{aligned}

    The first equation requires that x∗=0x_* = 0 or that λ=1/4\lambda = 1/4. The second requires that y∗=0y_* = 0 or λ=1/25\lambda = 1/25. Since λ\lambda cannot equal both 1/41/4 and 1/251/25 at the same time, the system can only admit solutions where:

    (x∗=0,y∗=?,λ=1/25) or (x∗=?,y∗=0,λ=1/4)(x_* = 0, y_* = ?, \lambda = 1/25) \text{ or } (x_* = ?, y_* = 0, \lambda = 1/4)

    In either case, there is only one remaining unknown. It is dtermined by enforcing primal feasibility. Recall that

    x∗2+y∗2=9.x_*^2 + y_*^2 = 9.

    Therefore, if x∗=0x_* = 0, then y∗=±3y_* = \pm 3, and if y∗=0y_* = 0, then x∗=±3x_* = \pm 3.

    So, we are left with four feasible solutions:

    (x∗=0,y∗=±3) or (x∗=±3,y∗=0).(x_* = 0, y_* = \pm 3) \text{ or } (x_* = \pm 3, y_* = 0).
  4. Now that we’ve identified all the feasible points, we need only evaluate ff at each to find the maximizers:

    (x∗=0,y∗=3):f(0,3)=925(x∗=0,y∗=−3):f(0,−3)=925(x∗=3,y∗=0):f(3,0)=94(x∗=−3,y∗=0):f(−3,0)=94\begin{aligned} & (x_* = 0, y_* = 3): & f(0,3) = \frac{9}{25} \\ & (x_* = 0, y_* = -3): & f(0,-3) = \frac{9}{25} \\ & (x_* = 3, y_* = 0): & f(3,0) = \frac{9}{4} \\ & (x_* = -3, y_* = 0): & f(-3,0) = \frac{9}{4} \\ \end{aligned}

    Since 9/4>9/259/4 > 9/25, the points (x∗=±3,y∗=0)(x_* = \pm 3, y_* = 0) are the maximizers. The remining feasible points minimize ff.

Interactive Visualization

To check your conceptual understanding, run the code cell below. It will generate example constrained optimization problems. You can vary the objective ff, constraint function gg, and level cc. You can then choose to display the level sets, or gradient fields, for ff and gg. Based on the displayed information, you can then propose possible optima and check if they are feasible.

To identify feasible points, look for inputs xx where:

  1. the gradient of ff is parallel to the gradient of gg,

  2. the gradient of ff is perpendicular to the level set of gg, or, the level set of gg is perpendicular to the gradient of ff,

  3. the level set of ff is parallel to the level set of gg.

After checking whether a point is feasible, you can check whether it is maximal.