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:
A constrained optimization problem restricts the range of possible inputs, .
Constraint Functions¶
The set 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 is defined by a set of equality constraints if:
Inequality Constraints
We can also define sets using a collection of inequality constraints. An inequality constraint is a constraint of the form:
for some constraint function . Note, we could use for any , but, this is the same as requiring or where . Similarly, we could use , but this is the same as requiring , or where .
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 have different advantages and disadvantages:
When 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, “ is a circle, of radius 3, centered at the origin” and converted it into an implicit definition defined by an equality constraint, “ is the set of all such that .”
When is defined implicitly, we can use the constraints to check whether an input belongs to . Just check whether (or the desired ) for each equality constraint. If all of the constraints are satisfied then . If not, . While it is easy to check whether any particular input satisfies the constraints, it may be hard to find inputs 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:
Every linear set may be described by setting for some , , and .
Every circular set may be described by setting for some radius .
Every elliptical set may be described by setting for some , , and .
The same formulas extend naturally to higher dimensions. For example, every planar set may be expressed by setting for some and some . In this case the vector is the “normal” vector to the plane (it is perpendicular to the plane) so the choice of controls the orientation of the plane. The choice of controls the specific position of the plane.
Multiple Constraints
To add additional constraints, simply append more equality constraints to your list of “given” statements. For example, if and , and we wanted to maximize over all that lie on the unit sphere, and on the plane , then we would aim to:
Notice that the constraints are joined by an “and” statement. It follows that the set may be expressed as an intersection of the sets defined by each individual constraint:
When we use multiple constraints, the set may be visualized as the intersection of a series of level sets, one per constraint. For the rest of this chapter we will assume that there is only one equality constraint.
Finding Optima¶
Consider the constrained problem:
for some objective function and constraint function . How can we find the best ?
When the set is low dimensional, and easy to parameterize, then we can find the best by evaluating at every . The animation below visualizes this approach for the example introduced at the start of the chapter. Here is a bowl with elliptical level sets, and is a circle, of radius 3, centered at the origin.
In this case, we can parameterize 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 . To maximize over inputs on the circle, we set then sweep over . This process identifes the maxima .
The objective, , restricted to the set satisfying the equality constraint, . Points in may be uniquely identified by fixing an angle . By sweeping over we can identify the points, in , that maximize . Right click on the video to access controls.
This approach, while intuitive, does not work well when is hard to parameterize, and, in absence of a parameterization, requires checking every point in an explicit representation of . What we need is a rule that can identify possible optimizers without an explicit parameterization of .
The next section derives the appropriate rule. It generalizes the rule used in unconstrained univariate optimization, and the 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 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 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 is parallel to the dashed white line representing a level set of 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 (dashed white boundaries), and the set (orange circle), are related. The level set of , passing through the optimal points on , is tangent to at the optimum!
This is a sensible observation. If wasn’t tangent to a level set of at a point , then, it would be possible to increase the value of , by moving along in a direction that crossed the level set from below. In other words, it would be possible to find a direction, tangent to , along which is increasing. No point can be maximal if there exists a direction we could move in, that stays in , but increases . So, to identify optima, we will look for locations where the level sets of are tangent to the set . This is usually done via the method of Lagrange multipliers which converts the statement “check whether the level set of is tangent to ” into a statement regarding the gradients of and .
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 is smooth, and is unconstrained, then cannot be an extrema of unless . In other words, when is unconstrained, all finite extrema of smooth correspond to locations where the surface is flat. If we add constraints, then may be optimized at a location where is not flat, provided that there is no way to move uphill or downhill on , from , while staying inside .
To express this idea formally, let denote a set defined by a collection of equality constraints, let denote a point in , and let denote the collection of tangent directions to the set leaving . The set contains all of the vectors such that the line is tangent to at . It represents all of the directions in which we could move away from while remaining inside of .
A point may be a solution to a constrained optimization problem if it:
Satisfies the constraint
There is no direction along which is increasing or decreasing.
In the optimization literature the first constraint is called primal feasibility and the second is called dual feasibility. A point that satisfies 1 and 2 is “feasible” in the sense that it could be an optima. If 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 is flat at along every path passing through . This is entirely analogous to the observation that, in an unconstrained problem, cannot be an extrema unless , so a point is only feasible if .
Recall that a directional derivative, evaluates the instanteous rate of change (slope) of the surface along a path oriented in the direction leaving the point . Then, the second constraint may be expressed:
Dual feasibility is the generalization of the feasibility statement we derived in Section 9.3, “ cannot be an extrema of unless ”, to constrained problems. Both generalize the familiar one dimensional rule, “set the derivative of 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 (see Section 9.2):
where is the unit vector pointing in the same direction as . So, dual feasibility may be expressed:
for all .
This is a bit easier to think about since is fixed by and . It separates the term associated with the slope of from the term associated with admissible directions along which we could move the input .
The animation shown below illustrates the relationship between , the set , tangent directions , the partial derivative of along directions , and the gradient .
To find a directional derivative of , along a direction, , tangent to , evaluate where is the unit vector parallel to . The left panel in the animation above shows as a heatmap with overlaid contours, and the tangent to at a particular input, a direction parallel to the tangent, and the gradient of at the input point. The right hand panel shows . The slope of is determined by the partial derivative of along directions tangent to . 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 . Right click on the video to access controls.
If , then . So, if is a location where is flat, then is feasible for both the unconstrained and the constrained problem. This makes sense. If is a local maximizer or minimizer of , it will remain a local maximizer or minimizer of if we restrict the allowed ways in which we could perturb the input away from . So, any point that is in , where is zero, is feasible.
The dual feasibility constraint allows other solutions. Suppose that . If is perpendicular to then . So, if is perpendicular to for all , then for all .
The converse also holds. Two nonzero vectors are perpendicular if and only if their inner product equals zero. Therefore, if , then if and only if is perpendicular to .
Now we can write the dual feasibility constraint geometrically:
In other words, all tangent lines to the set , passing through , must be perpendicular to the gradient of at .
The animation shown below illustrates this idea. The left hand panel shows the gradient of and the tangent set , as we move from a non-optimal point in 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 , along any direction tangent to , equals zero. This corresponds to a location, in the right hand plot, where the function is flat.
Follow the gradient and tangent directions, as we move our input around from a non-optimal point to an optimal point. At the optimal point, the partial derivative of , with respect to any direction in the tangent, is equal to zero because the gradient is perpendicular to the tangent . Since is tangent to , the gradient of is perpendicular to the set at the optimum. Conversely, at every non-optimal point the gradient is not perpendicular to , so there exists a direction , along which we could perturb , while remaining in , and increasing . 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 at a large collection of inputs. The resulting collection of gradient vectors are all perpendicular to the level sets of (shown in white). Then, the animation evaluates the gradient of at inputs restricted to the set . Then, it checks whether the gradient is perpendicular to the level set by evaluating the angle between and for . The gradient is perpendicular to the level set at points in that maximize, or minimize, . Where the gradient is not perpendicular to the level set, is neither maximized nor minimized.
To check dual feasibility, evaluate the gradient of at inputs in . 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 . 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, , is perpendicular to the level set of at . This observation provides an alternate description for the set of directions tangent to a level set. Given a smooth function , and an input , every direction such that is tangent to the level set of passing through .
We can use this conclusion to express the tangent set, algebraicly.
By assumption, was defined by a single equality constraint, . So, is a level set of the constraint function . It follows that:
If , then if and only if is perpendicular to . Therefore:
Now, dual feasibility requires that:
is perpendicular to every that is perpendicular to .
In other words, and must both be perpendicular to the same collection of tangent directions .
The tangent set, is the collection of all vectors that satisfy the equation . This is one linear equation. If is dimensional, it defines a dimensional collection of vectors. For example, if , then the tangent set is one-dimensional, and corresponds to a line. If , 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 and are both perpendicular to the same collection of tangent directions , 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 and are parallel if and only if the level sets of and at tangent at . So, we can view the Lagrange multiplier equation as one version of four possible feasibility statements:
is feasible if is perpendicular to the level set of at
is feasible if is parallel to
is feasible if the level set of at is tangent to the level set of at
is feasible if the level set of is perpendicular to .
All four statements compare a gradient or level set of the objective to a gradient or level set of . 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 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 is maximized and a non-extremal point. It first checks whether the gradient of is perpendicular to the level set of . Then, it checks whether the level set of is tangent to the level set of . Finally, it checks whether the gradient of is parallel to the gradient of . 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:
Write down the constraint
Solve for and .
Write down the system of equations
where and are free variables.
Solve for and by enforcing primal and dual feasibility.
If there are multiple feasible points, evaluate at each and pick the feasible point that maximizes (or minimizes) .
Example¶
Let’s solve the constrained optimization problem we established at the start of the chapter using Lagrange multipliers.
Working in order:
Primal feasibility demands:
The gradients are:
and:
So, dual feasibility demands:
Or, as a systems of equations:
The first equation requires that or that . The second requires that or . Since cannot equal both and at the same time, the system can only admit solutions where:
In either case, there is only one remaining unknown. It is dtermined by enforcing primal feasibility. Recall that
Therefore, if , then , and if , then .
So, we are left with four feasible solutions:
Now that we’ve identified all the feasible points, we need only evaluate at each to find the maximizers:
Since , the points are the maximizers. The remining feasible points minimize .
Interactive Visualization¶
To check your conceptual understanding, run the code cell below. It will generate example constrained optimization problems. You can vary the objective , constraint function , and level . You can then choose to display the level sets, or gradient fields, for and . Based on the displayed information, you can then propose possible optima and check if they are feasible.
To identify feasible points, look for inputs where:
the gradient of is parallel to the gradient of ,
the gradient of is perpendicular to the level set of , or, the level set of is perpendicular to the gradient of ,
the level set of is parallel to the level set of .
After checking whether a point is feasible, you can check whether it is maximal.
from utils_constrained_optimization import show_constrained_optimization
demo = show_constrained_optimization()