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.

Nonlinear Programming

Unless specific assumptions are made on the dynamics and cost structure, a DOCP is, in its most general form, a nonlinear mathematical program (commonly referred to as an NLP, not to be confused with Natural Language Processing). An NLP can be formulated as follows:

minimize f(x)subject to g(x)≤0h(x)=0\begin{aligned} \text{minimize } & f(\mathbf{x}) \\ \text{subject to } & \mathbf{g}(\mathbf{x}) \leq \mathbf{0} \\ & \mathbf{h}(\mathbf{x}) = \mathbf{0} \end{aligned}

Here x\mathbf x denotes a generic decision vector. When applying this appendix to a trajectory problem, it is the stacked vector z\mathbf z, not just one physical state. The objective ff and constraint maps g,h\mathbf g,\mathbf h then correspond to F,G,HF,G,H in the control chapters.

Where:

Unlike unconstrained optimization commonly used in deep learning, the optimality of a solution in constrained optimization must consider both the objective value and constraint feasibility. To illustrate this, consider the following problem, which includes both equality and inequality constraints:

Minimizef(x1,x2)=(x1−1)2+(x2−2.5)2subject tog(x1,x2)=(x1−1)2+(x2−1)2−1.5≤0,h(x1,x2)=x2−(0.5sin⁡(2πx1)+1.5)=0.\begin{align*} \text{Minimize} \quad & f(x_1, x_2) = (x_1 - 1)^2 + (x_2 - 2.5)^2 \\ \text{subject to} \quad & g(x_1, x_2) = (x_1 - 1)^2 + (x_2 - 1)^2 - 1.5 \leq 0, \\ & h(x_1, x_2) = x_2 - \left(0.5 \sin(2 \pi x_1) + 1.5\right) = 0. \end{align*}

In this example, the objective function f(x1,x2)f(x_1, x_2) is quadratic, the inequality constraint g(x1,x2)g(x_1, x_2) defines a circular feasible region centered at (1,1)(1, 1) with a radius of 1.5\sqrt{1.5} and the equality constraint h(x1,x2)h(x_1, x_2) requires x2x_2 to lie on a sine wave function. The following code demonstrates the difference between the unconstrained, and constrained solutions to this problem.

Source
<Figure size 800x600 with 1 Axes>

The Lagrangian, Duality, and Optimality Conditions

A constrained optimizer must reduce the objective while respecting the constraints. The Lagrangian combines these two requirements in one function:

L(x,λ,μ)=f(x)+∑i=1mμigi(x)+∑j=1ℓλjhj(x),μ≥0,λ∈Rℓ.\mathcal L(\mathbf x,\boldsymbol\lambda,\boldsymbol\mu) =f(\mathbf x)+\sum_{i=1}^m\mu_i g_i(\mathbf x) +\sum_{j=1}^\ell\lambda_j h_j(\mathbf x), \qquad \boldsymbol\mu\ge\mathbf0, \quad \boldsymbol\lambda\in\mathbb R^\ell.

The Lagrange multipliers assign weights to the constraint residuals. Inequality multipliers μi\mu_i are nonnegative because a positive residual gi(x)>0g_i(\mathbf x)>0 is a violation that should increase the quantity being minimized. Equality multipliers λj\lambda_j can have either sign because hj(x)h_j(\mathbf x) can violate its constraint in either direction. These signs follow the convention g(x)≤0\mathbf g(\mathbf x)\le\mathbf0 used here.

The primal problem as a min–max problem

Suppose one player chooses x\mathbf x to minimize the Lagrangian and a second player chooses the multipliers to maximize it. If the multiplier player can respond after seeing x\mathbf x, every infeasible choice can be ruled out. For a violated inequality gi(x)>0g_i(\mathbf x)>0, sending μi\mu_i to infinity makes the Lagrangian arbitrarily large. For a violated equality, choosing λj\lambda_j with the same sign as hj(x)h_j(\mathbf x) and increasing its magnitude has the same effect.

For a feasible x\mathbf x, all equality terms vanish and every inequality term is nonpositive. The multiplier player can attain f(x)f(\mathbf x) by setting μ=0\boldsymbol\mu=\mathbf0, and cannot obtain a larger value. Thus

sup⁡μ≥0, λ∈RℓL(x,λ,μ)={f(x),x is feasible,+∞,x is infeasible.\sup_{\boldsymbol\mu\ge\mathbf0,\,\boldsymbol\lambda\in\mathbb R^\ell} \mathcal L(\mathbf x,\boldsymbol\lambda,\boldsymbol\mu) = \begin{cases} f(\mathbf x),&\mathbf x\text{ is feasible},\\ +\infty,&\mathbf x\text{ is infeasible}. \end{cases}

Minimizing this function gives exactly the original constrained problem:

p⋆:=inf⁡x: g(x)≤0, h(x)=0f(x)=inf⁡xsup⁡μ≥0, λ∈RℓL(x,λ,μ).p^\star :=\inf_{\mathbf x:\,\mathbf g(\mathbf x)\le\mathbf0,\,\mathbf h(\mathbf x)=\mathbf0} f(\mathbf x) =\inf_{\mathbf x}\sup_{\boldsymbol\mu\ge\mathbf0,\,\boldsymbol\lambda\in\mathbb R^\ell} \mathcal L(\mathbf x,\boldsymbol\lambda,\boldsymbol\mu).

This representation requires no convexity or differentiability. The notation inf⁡\inf and sup⁡\sup allows values that are approached without being attained; in particular, no finite multiplier attains +∞+\infty at an infeasible point. The representation also uses unbounded multiplier sets. Capping the multipliers would instead give a finite penalty for constraint violations.

The dual problem and lower bounds

Reversing the order changes what the multiplier player can enforce. It must now choose one set of weights before the minimizing player chooses x\mathbf x. For fixed multipliers, define the dual function

q(λ,μ)=inf⁡xL(x,λ,μ).q(\boldsymbol\lambda,\boldsymbol\mu) =\inf_{\mathbf x}\mathcal L(\mathbf x,\boldsymbol\lambda,\boldsymbol\mu).

The inner infimum ranges over all x\mathbf x, including infeasible choices. For any feasible xˉ\bar{\mathbf x} and any μ≥0\boldsymbol\mu\ge\mathbf0,

q(λ,μ)≤L(xˉ,λ,μ)≤f(xˉ).q(\boldsymbol\lambda,\boldsymbol\mu) \le\mathcal L(\bar{\mathbf x},\boldsymbol\lambda,\boldsymbol\mu) \le f(\bar{\mathbf x}).

Consequently, each multiplier choice supplies a lower bound on the constrained optimal value. The dual problem searches for the largest such bound:

d⋆:=sup⁡μ≥0, λ∈Rℓq(λ,μ)≤p⋆.d^\star :=\sup_{\boldsymbol\mu\ge\mathbf0,\,\boldsymbol\lambda\in\mathbb R^\ell} q(\boldsymbol\lambda,\boldsymbol\mu) \le p^\star.

This inequality is weak duality. It holds for nonconvex problems as well. When d⋆=p⋆d^\star=p^\star, strong duality holds: choosing the multipliers first yields the same value as letting them respond to x\mathbf x. Equality of these values does not by itself say that either optimum is attained.

Saddle points and fixed multipliers

A Lagrangian saddle point is one feasible point x⋆\mathbf x^\star and one multiplier pair (μ⋆,λ⋆)(\boldsymbol\mu^\star,\boldsymbol\lambda^\star), with μ⋆≥0\boldsymbol\mu^\star\ge\mathbf0, satisfying

L(x⋆,λ,μ)≤L(x⋆,λ⋆,μ⋆)≤L(x,λ⋆,μ⋆)\mathcal L(\mathbf x^\star,\boldsymbol\lambda,\boldsymbol\mu) \le \mathcal L(\mathbf x^\star,\boldsymbol\lambda^\star,\boldsymbol\mu^\star) \le \mathcal L(\mathbf x,\boldsymbol\lambda^\star,\boldsymbol\mu^\star)

for every x\mathbf x, μ≥0\boldsymbol\mu\ge\mathbf0, and λ∈Rℓ\boldsymbol\lambda\in\mathbb R^\ell. The left inequality says that the multiplier player cannot increase the value with x⋆\mathbf x^\star held fixed. The right inequality says that the minimizing player cannot decrease it with these multipliers held fixed. This is an equilibrium of the two-player game.

The right inequality is stronger than solving the primal min–max problem. It requires x⋆\mathbf x^\star to minimize the Lagrangian over all x\mathbf x against one fixed multiplier pair, even when the competing x\mathbf x is infeasible. A saddle point therefore certifies global primal and dual optimality with equal values. Conversely, if the primal and dual optima are both attained and their finite values agree, their optimizers form a saddle point. Convexity is one route to these properties, but is not part of the saddle-point definition.

Karush-Kuhn-Tucker conditions

For a smooth problem, a global minimum of the Lagrangian in x\mathbf x must have zero gradient. The multiplier side of the saddle inequalities also requires feasibility and complementary slackness. These requirements give the Karush-Kuhn-Tucker (KKT) conditions:

Complementary slackness describes the multiplier player’s response to a feasible point. If gi(x⋆)<0g_i(\mathbf x^\star)<0, a positive multiplier would reduce the Lagrangian, so maximizing requires μi⋆=0\mu_i^\star=0. If gi(x⋆)=0g_i(\mathbf x^\star)=0, any nonnegative multiplier gives the same contribution at that point. A tight constraint can therefore carry a positive weight, although tightness does not require its weight to be positive.

KKT conditions also arise at local constrained minima even when no saddle point exists. If the objective and constraints are continuously differentiable and a constraint qualification holds at a local minimizer, then multipliers satisfying KKT exist. One sufficient qualification is LICQ, the linear independence of the equality-constraint gradients and the gradients of inequalities active at that point. It ensures that the linearized constraints have enough regularity for the multiplier theorem to apply.

Here, first-order means that the conditions use function values and first derivatives at the candidate point. Stationarity requires

∇f(x⋆)+∑iμi⋆∇gi(x⋆)+∑jλj⋆∇hj(x⋆)=0.\nabla f(\mathbf x^\star) +\sum_i\mu_i^\star\nabla g_i(\mathbf x^\star) +\sum_j\lambda_j^\star\nabla h_j(\mathbf x^\star)=\mathbf0.

This balances the objective gradient against a weighted sum of constraint gradients. It does not compare Lagrangian values at other points or determine its curvature. Thus KKT alone need not imply that x⋆\mathbf x^\star minimizes the Lagrangian, even locally. Describing KKT as equations and inequalities at one point should not be confused with the global comparisons in the saddle inequalities.

For example, consider

min⁡x∈R−x2subject to x=0,L(x,λ)=−x2+λx.\min_{x\in\mathbb R}-x^2\qquad\text{subject to }x=0, \qquad \mathcal L(x,\lambda)=-x^2+\lambda x.

The unique feasible point is x⋆=0x^\star=0, so p⋆=0p^\star=0. The exact primal representation still works: at x=0x=0 the inner supremum is zero, and at any x≠0x\ne0 it is +∞+\infty. The equality gradient is 1, so LICQ holds, and stationarity gives −2x⋆+λ⋆=0-2x^\star+\lambda^\star=0. Thus (0,0)(0,0) satisfies KKT. Yet L(x,0)=−x2\mathcal L(x,0)=-x^2 has a strict maximum at zero. For every fixed λ\lambda, it is also unbounded below as ∣x∣|x| grows. Hence q(λ)=−∞q(\lambda)=-\infty for every λ\lambda, d⋆=−∞d^\star=-\infty, and no saddle point exists. Even a global constrained optimizer satisfying KKT need not minimize the Lagrangian.

Convexity and sufficient conditions

Suppose ff and each gig_i are differentiable convex functions on Rn\mathbb R^n, and the equality functions hjh_j are affine. For any μ≥0\boldsymbol\mu\ge\mathbf0, the Lagrangian is then convex in x\mathbf x. Stationarity becomes sufficient for a global minimum because the convexity inequality gives

L(x,λ⋆,μ⋆)≥L(x⋆,λ⋆,μ⋆)+∇xL(x⋆,λ⋆,μ⋆)⊤(x−x⋆)=L(x⋆,λ⋆,μ⋆).\begin{aligned} \mathcal L(\mathbf x,\boldsymbol\lambda^\star,\boldsymbol\mu^\star) &\ge \mathcal L(\mathbf x^\star,\boldsymbol\lambda^\star,\boldsymbol\mu^\star)\\ &\quad+\nabla_{\mathbf x}\mathcal L(\mathbf x^\star,\boldsymbol\lambda^\star,\boldsymbol\mu^\star)^\top (\mathbf x-\mathbf x^\star)\\ &=\mathcal L(\mathbf x^\star,\boldsymbol\lambda^\star,\boldsymbol\mu^\star). \end{aligned}

This supplies the right saddle inequality. Primal feasibility and nonnegative multipliers give L(x⋆,λ,μ)≤f(x⋆)\mathcal L(\mathbf x^\star,\boldsymbol\lambda,\boldsymbol\mu)\le f(\mathbf x^\star), while complementary slackness gives L(x⋆,λ⋆,μ⋆)=f(x⋆)\mathcal L(\mathbf x^\star,\boldsymbol\lambda^\star,\boldsymbol\mu^\star)=f(\mathbf x^\star). Together they supply the left saddle inequality. Thus any KKT point of this convex problem is a global saddle point; no additional constraint qualification is needed for this sufficiency direction.

A constraint qualification is needed to guarantee the existence of KKT multipliers at an optimizer. For the convex problem just specified, Slater’s condition requires a point satisfying all equalities and every inequality strictly. If Slater’s condition holds and the primal optimal value is finite, strong duality holds and the dual optimum is attained. If the primal optimum is attained as well, an optimizer and suitable multipliers satisfy KKT and form a saddle point. These convex duality results are developed in Chapter 5 of Boyd and Vandenberghe’s Convex Optimization.

For nonconvex trajectory problems, KKT supplies necessary conditions under regularity, and numerical methods seek points satisfying those conditions. Global saddle inequalities require a separate argument. The exact primal min–max representation remains valid in either case.

Multipliers in the constrained example

Let’s now solve our example problem above, this time using Ipopt via the Pyomo interface so that we can access the Lagrange multipliers found by the solver.

x1: 1.2271417639244486
x2: 1.994852000302119
Objective value: 0.3067678825174803

Lagrange multipliers:
ineq_constraint[None]: -5.466075458072094e-09
eq_constraint[None]: -1.0102959885190541

The computed point lies strictly inside the circle, so complementary slackness requires its inequality multiplier to be zero, up to numerical tolerance. The converse inference would be invalid: a zero multiplier does not by itself show that a constraint is inactive. Every equality must hold at a feasible point regardless of its multiplier’s value. Under suitable sensitivity assumptions, the multipliers also describe how the optimal value changes when the constraint right-hand sides are perturbed; the sign depends on the convention used to write those perturbations.

For a vector constraint map, write Jh(x)J_h(\mathbf x) for its Jacobian, whose jjth row is ∇hj(x)⊤\nabla h_j(\mathbf x)^\top; define JgJ_g similarly. Gradients of scalar functions are column vectors.

Lagrange Multiplier Theorem

For equality constraints alone, the KKT necessary conditions reduce to the Lagrange multiplier theorem. They identify stationary candidates; further conditions are needed to establish that a candidate is a local minimum.

Note that both the stationarity and primal feasibility statements are simply saying that the derivative of the Lagrangian in either the primal or dual variables must be zero at an optimal constrained solution. In other words:

∇x,λL(x∗,λ∗)=0\nabla_{\mathbf{x}, \boldsymbol{\lambda}} \mathcal L(\mathbf{x}^*, \boldsymbol{\lambda}^*) = \mathbf{0}

Let F(x,λ)\mathbf F(\mathbf x,\boldsymbol\lambda) denote this combined gradient. Under the theorem’s assumptions, a local minimizer and its multipliers give a zero of F\mathbf F. Newton’s method can seek such zeros, but reaching one alone does not certify a minimum or a Lagrangian saddle point.

Newton’s Method

For a vector residual F\mathbf F, write JFJ_F for its Jacobian, with component gradients as rows. This vector residual is distinct from the scalar objective FF used in trajectory optimization.

Newton’s method is a numerical procedure for solving root-finding problems. These are nonlinear systems of equations of the form:

Find z∗∈Rn\mathbf{z}^* \in \mathbb{R}^n such that F(z∗)=0\mathbf{F}(\mathbf{z}^*) = \mathbf{0}

where F:Rn→Rn\mathbf{F}: \mathbb{R}^n \to \mathbb{R}^n is a continuously differentiable function. Newton’s method then consists in applying the following sequence of iterates:

zk+1=zk−[JF(zk)]−1F(zk)\mathbf{z}^{k+1} = \mathbf{z}^k - [J_F(\mathbf{z}^k)]^{-1} \mathbf{F}(\mathbf{z}^k)

where zk\mathbf{z}^k is the k-th iterate, and JF(zk)J_F(\mathbf{z}^k) is the Jacobian matrix of F\mathbf{F} evaluated at zk\mathbf{z}^k.

Newton’s method exhibits local quadratic convergence: if the initial guess z0\mathbf{z}^0 is sufficiently close to the true solution z∗\mathbf{z}^*, and JF(z∗)J_F(\mathbf{z}^*) is nonsingular, the method converges quadratically to z∗\mathbf{z}^* Ortega & Rheinboldt (1970). However, the method is sensitive to the initial guess; if it’s too far from the desired solution, Newton’s method might fail to converge or converge to a different root. To mitigate this problem, a set of techniques known as numerical continuation methods Allgower & Georg (1990) have been developed. These methods effectively enlarge the basin of attraction of Newton’s method by solving a sequence of related problems, progressing from an easy one to the target problem. This approach is reminiscent of several concepts in machine learning and statistical inference: curriculum learning in machine learning, where models are trained on increasingly complex data; tempering in Markov Chain Monte Carlo (MCMC) samplers, which gradually adjusts the target distribution to improve mixing; and modern diffusion models, which use a similar concept of gradually transforming noise into structured data.

Efficient Implementation of Newton’s Method

Note that each step of Newton’s method involves computing the inverse of a Jacobian matrix. However, a cardinal rule in numerical linear algebra is to avoid computing matrix inverses explicitly: rarely, if ever, should there be a np.linalg.inv in your code. Instead, the numerically stable and computationally efficient approach is to solve a linear system of equations at each step. Given the Newton’s method iterate:

zk+1=zk−[JF(zk)]−1F(zk)\mathbf{z}^{k+1} = \mathbf{z}^k - [J_F(\mathbf{z}^k)]^{-1} \mathbf{F}(\mathbf{z}^k)

We can reformulate this as a two-step procedure:

  1. Solve the linear system: [JF(zk)]⏟AΔzk=−F(zk)\underbrace{[J_F(\mathbf{z}^k)]}_{\mathbf{A}} \Delta \mathbf{z}^k = -\mathbf{F}(\mathbf{z}^k)

  2. Update: zk+1=zk+Δzk\mathbf{z}^{k+1} = \mathbf{z}^k + \Delta \mathbf{z}^k

The structure of the linear system in step 1 often allows for specialized solution methods. In the context of automatic differentiation, matrix-free linear solvers are particularly useful. These solvers can find a solution without explicitly forming the matrix A, requiring only the ability to evaluate matrix-vector or vector-matrix products. Typical examples of such methods include classical matrix-splitting methods (e.g., Richardson iteration) or conjugate gradient methods through sparse.linalg.cg for example. Another useful method is the Generalized Minimal Residual method (GMRES) implemented in SciPy via sparse.linalg.gmres, which is useful when facing non-symmetric and indefinite systems.

By inspecting the structure of matrix A\mathbf{A} in the specific application where the function F\mathbf{F} is the derivative of the Lagrangian, we will also uncover an important structure known as the KKT matrix. This structure will then allow us to derive a Quadratic Programming (QP) sub-problem as part of a larger iterative procedure for solving equality and inequality constrained problems via Sequential Quadratic Programming (SQP).

Solving Equality Constrained Programs with Newton’s Method

To seek stationary candidates for equality-constrained optimization, Newton’s method searches for a zero of the function F(z)=∇x,λL(x,λ)\mathbf{F}(\mathbf{z}) = \nabla_{\mathbf{x}, \boldsymbol{\lambda}} \mathcal L(\mathbf{x}, \boldsymbol{\lambda}). Here, F\mathbf{F} represents the derivative of the Lagrangian function, and z=(x,λ)\mathbf{z} = (\mathbf{x}, \boldsymbol{\lambda}) combines both the primal variables x\mathbf{x} and the dual variables (Lagrange multipliers) λ\boldsymbol{\lambda}. Explicitly, we have:

F(z)=[∇xL(x,λ)h(x)]=[∇f(x)+∑i=1ℓλi∇hi(x)h(x)].\mathbf{F}(\mathbf{z}) = \begin{bmatrix} \nabla_{\mathbf{x}} \mathcal L(\mathbf{x}, \boldsymbol{\lambda}) \\ \mathbf{h}(\mathbf{x}) \end{bmatrix} = \begin{bmatrix} \nabla f(\mathbf{x}) + \sum_{i=1}^\ell \lambda_i \nabla h_i(\mathbf{x}) \\ \mathbf{h}(\mathbf{x}) \end{bmatrix}.

Newton’s method involves linearizing F(z)\mathbf{F}(\mathbf{z}) around the current iterate zk=(xk,λk)\mathbf{z}^k = (\mathbf{x}^k, \boldsymbol{\lambda}^k) and then solving the resulting linear system. At each iteration kk, Newton’s method updates the current estimate by solving the linear system:

zk+1=zk−[JF(zk)]−1F(zk).\mathbf{z}^{k+1} = \mathbf{z}^k - [J_F(\mathbf{z}^k)]^{-1} \mathbf{F}(\mathbf{z}^k).

However, instead of explicitly inverting the Jacobian matrix JF(zk)J_F(\mathbf{z}^k), we solve the linear system:

JF(zk)⏟AΔzk=−F(zk),\underbrace{J_F(\mathbf{z}^k)}_{\mathbf{A}} \Delta \mathbf{z}^k = -\mathbf{F}(\mathbf{z}^k),

where Δzk=(Δxk,Δλk)\Delta \mathbf{z}^k = (\Delta \mathbf{x}^k, \Delta \boldsymbol{\lambda}^k) represents the Newton step for the primal and dual variables. Substituting the expression for F(z)\mathbf{F}(\mathbf{z}) and its Jacobian, the system becomes:

[∇xx2L(xk,λk)Jh(xk)TJh(xk)0][ΔxkΔλk]=−[∇f(xk)+Jh(xk)Tλkh(xk)].\begin{bmatrix} \nabla^2_{\mathbf{x}\mathbf{x}} \mathcal L(\mathbf{x}^k, \boldsymbol{\lambda}^k) & J_h(\mathbf{x}^k)^T \\ J_h(\mathbf{x}^k) & \mathbf{0} \end{bmatrix} \begin{bmatrix} \Delta \mathbf{x}^k \\ \Delta \boldsymbol{\lambda}^k \end{bmatrix} = - \begin{bmatrix} \nabla f(\mathbf{x}^k) + J_h(\mathbf{x}^k)^T \boldsymbol{\lambda}^k \\ \mathbf{h}(\mathbf{x}^k) \end{bmatrix}.

The matrix on the left-hand side is known as the KKT matrix, as it stems from the Karush-Kuhn-Tucker conditions for this optimization problem The solution of this system provides the updates Δxk\Delta \mathbf{x}^k and Δλk\Delta \boldsymbol{\lambda}^k, which are then used to update the primal and dual variables:

xk+1=xk+Δxk,λk+1=λk+Δλk.\mathbf{x}^{k+1} = \mathbf{x}^k + \Delta \mathbf{x}^k, \quad \boldsymbol{\lambda}^{k+1} = \boldsymbol{\lambda}^k + \Delta \boldsymbol{\lambda}^k.
Demonstration

The following code demonstates how we can implement this idea in Jax. In this demonstration, we are minimizing a quadratic objective function subject to a single equality constraint, a problem formally stated as follows:

min⁡x∈R2f(x)=(x1−2)2+(x2−1)2subject toh(x)=x12+x22−1=0\begin{aligned} \min_{x \in \mathbb{R}^2} \quad & f(x) = (x_1 - 2)^2 + (x_2 - 1)^2 \\ \text{subject to} \quad & h(x) = x_1^2 + x_2^2 - 1 = 0 \end{aligned}

Geometrically speaking, the constraint h(x)h(x) describes a unit circle centered at the origin. To solve this problem using the method of Lagrange multipliers, we form the Lagrangian:

L(x,λ)=f(x)+λh(x)=(x1−2)2+(x2−1)2+λ(x12+x22−1)\mathcal L(x, \lambda) = f(x) + \lambda h(x) = (x_1 - 2)^2 + (x_2 - 1)^2 + \lambda(x_1^2 + x_2^2 - 1)

For this particular problem, it happens so that we can also find an analytical without even having to use Newton’s method. From the first-order optimality conditions, we obtain the following linear system of equations:

2(x1−2)+2λx1=02(x2−1)+2λx2=0x12+x22−1=0\begin{align*} 2(x_1 - 2) + 2\lambda x_1 &= 0 \\ 2(x_2 - 1) + 2\lambda x_2 &= 0 \\ x_1^2 + x_2^2 - 1 &= 0\\ \end{align*}

From the first two equations, we then get:

x1=21+λ,x2=11+λx_1 = \frac{2}{1 + \lambda}, \quad x_2 = \frac{1}{1 + \lambda}

which we can substitute these into the 3rd constraint equation to obtain:

(21+λ)2+(11+λ)2=1⇔λ=5−1(\frac{2}{1 + \lambda})^2 + (\frac{1}{1 + \lambda})^2 = 1 \Leftrightarrow \lambda = \sqrt{5} - 1

This value of the Lagrange multiplier can then be backsubstituted into the above equations to obtain x1=25x_1 = \frac{2}{\sqrt{5}} and x2=15x_2 = \frac{1}{\sqrt{5}}. We can verify numerically (and visually on the following graph) that the point (2/5,1/5)(2/\sqrt{5}, 1/\sqrt{5}) is indeed the point on the unit circle closest to (2,1)(2, 1).

Source

Numerical Solution:
Constraint violation: 0.000000
Objective function value: 1.527864

Analytical Solution:
Constraint violation: -0.000000
Objective function value: 1.527864

Comparison:
Difference in x: 5.960464477539063e-08
Difference in lambda: 0.0

Precision Test:
x values are close: True
lambda values are close: True
The numerical solution matches the analytical solution within the specified tolerance.
<Figure size 1200x1000 with 2 Axes>

The SQP Approach: Taylor Expansion and Quadratic Approximation

Sequential Quadratic Programming (SQP) tackles the problem of solving constrained programs by iteratively solving a sequence of simpler subproblems. Specifically, these subproblems are quadratic programs (QPs) that approximate the original problem around the current iterate by using a quadratic model of the objective function and a linear model of the constraints. Suppose we have the following optimization problem with equality constraints:

min⁡xf(x)subject toh(x)=0.\begin{aligned} \min_{\mathbf{x}} \quad & f(\mathbf{x}) \\ \text{subject to} \quad & \mathbf{h}(\mathbf{x}) = \mathbf{0}. \end{aligned}

At each iteration kk, we approximate the objective function f(x)f(\mathbf{x}) using a second-order Taylor expansion around the current iterate xk\mathbf{x}^k. The standard Taylor expansion for ff would be:

f(x)≈f(xk)+∇f(xk)T(x−xk)+12(x−xk)T∇2f(xk)(x−xk).\begin{align*} f(\mathbf{x}) \approx f(\mathbf{x}^k) + \nabla f(\mathbf{x}^k)^T (\mathbf{x} - \mathbf{x}^k) + \frac{1}{2} (\mathbf{x} - \mathbf{x}^k)^T \nabla^2 f(\mathbf{x}^k) (\mathbf{x} - \mathbf{x}^k). \end{align*}

This expansion uses the Hessian of the objective function ∇2f(xk)\nabla^2 f(\mathbf{x}^k) to capture the curvature of ff. However, in the context of constrained optimization, we also need to account for the effect of the constraints on the local behavior of the solution. If we were to use only ∇2f(xk)\nabla^2 f(\mathbf{x}^k), we would not capture the influence of the constraints on the curvature of the feasible region. The resulting subproblem might then lead to steps that violate the constraints or are less effective in achieving convergence. The choice that we make instead is to use the Hessian of the Lagrangian, ∇xx2L(xk,λk)\nabla^2_{\mathbf{x}\mathbf{x}} \mathcal L(\mathbf{x}^k, \boldsymbol{\lambda}^k), leading to the following quadratic model:

f(x)≈f(xk)+∇f(xk)T(x−xk)+12(x−xk)T∇xx2L(xk,λk)(x−xk).f(\mathbf{x}) \approx f(\mathbf{x}^k) + \nabla f(\mathbf{x}^k)^T (\mathbf{x} - \mathbf{x}^k) + \frac{1}{2} (\mathbf{x} - \mathbf{x}^k)^T \nabla^2_{\mathbf{x}\mathbf{x}} \mathcal L(\mathbf{x}^k, \boldsymbol{\lambda}^k) (\mathbf{x} - \mathbf{x}^k).

Similarly, the equality constraints h(x)\mathbf{h}(\mathbf{x}) are linearized around xk\mathbf{x}^k:

h(x)≈h(xk)+Jh(xk)(x−xk).\mathbf{h}(\mathbf{x}) \approx \mathbf{h}(\mathbf{x}^k) + J_h(\mathbf{x}^k) (\mathbf{x} - \mathbf{x}^k).

Combining these approximations, we obtain a Quadratic Programming (QP) subproblem, which approximates our original problem locally at xk\mathbf{x}^k but is easier to solve:

Minimize∇f(xk)TΔx+12ΔxT∇xx2L(xk,λk)Δxsubject toJh(xk)Δx+h(xk)=0,\begin{aligned} \text{Minimize} \quad & \nabla f(\mathbf{x}^k)^T \Delta \mathbf{x} + \frac{1}{2} \Delta \mathbf{x}^T \nabla^2_{\mathbf{x}\mathbf{x}} \mathcal L(\mathbf{x}^k, \boldsymbol{\lambda}^k) \Delta \mathbf{x} \\ \text{subject to} \quad & J_h(\mathbf{x}^k) \Delta \mathbf{x} + \mathbf{h}(\mathbf{x}^k) = \mathbf{0}, \end{aligned}

where Δx=x−xk\Delta \mathbf{x} = \mathbf{x} - \mathbf{x}^k. The QP subproblem solved at each iteration focuses on finding the optimal step direction Δx\Delta \mathbf{x} for the primal variables. While solving this QP, we obtain not only the step Δx\Delta \mathbf{x} but also the associated Lagrange multipliers for the QP subproblem, which correspond to an updated dual variable vector λk+1\boldsymbol{\lambda}^{k+1}. More specifically, after solving the QP, we use Δxk\Delta \mathbf{x}^k to update the primal variables:

xk+1=xk+Δxk.\begin{align*} \mathbf{x}^{k+1} = \mathbf{x}^k + \Delta \mathbf{x}^k. \end{align*}

Simultaneously, the Lagrange multipliers from the QP provide the updated dual variables λk+1\boldsymbol{\lambda}^{k+1}. We summarize the SQP algorithm in the following pseudo-code:

Connection to Newton’s Method in the Equality-Constrained Case

The QP subproblem in SQP is directly related to applying Newton’s method for equality-constrained optimization. To see this, note that the KKT matrix of the QP subproblem is:

[∇xx2L(xk,λk)Jh(xk)TJh(xk)0][ΔxkΔλk]=−[∇f(xk)+Jh(xk)Tλkh(xk)]\begin{align*} \begin{bmatrix} \nabla^2_{\mathbf{x}\mathbf{x}} \mathcal L(\mathbf{x}^k, \boldsymbol{\lambda}^k) & J_h(\mathbf{x}^k)^T \\ J_h(\mathbf{x}^k) & \mathbf{0} \end{bmatrix} \begin{bmatrix} \Delta \mathbf{x}^k \\ \Delta \boldsymbol{\lambda}^k \end{bmatrix} = - \begin{bmatrix} \nabla f(\mathbf{x}^k) + J_h(\mathbf{x}^k)^T \boldsymbol{\lambda}^k \\ \mathbf{h}(\mathbf{x}^k) \end{bmatrix} \end{align*}

This is exactly the same linear system that have to solve when applying Newton’s method to the KKT conditions of the original program! Thus, solving the QP subproblem at each iteration of SQP is equivalent to taking a Newton step on the KKT conditions of the original nonlinear problem.

SQP for Inequality-Constrained Optimization

So far, we’ve applied the ideas behind Sequential Quadratic Programming (SQP) to problems with only equality constraints. Now, let’s extend this framework to handle optimization problems that also include inequality constraints. Consider a general nonlinear optimization problem that includes both equality and inequality constraints:

min⁡xf(x)subject tog(x)≤0,h(x)=0.\begin{align*} \min_{\mathbf{x}} \quad & f(\mathbf{x}) \\ \text{subject to} \quad & \mathbf{g}(\mathbf{x}) \leq \mathbf{0}, \\ & \mathbf{h}(\mathbf{x}) = \mathbf{0}. \end{align*}

As we did earlier, we approximate this problem by constructing a quadratic approximation to the objective and a linearization of the constraints. QP subproblem at each iteration is then formulated as:

Minimize∇f(xk)TΔx+12ΔxT∇xx2L(xk,λk,μk)Δxsubject toJg(xk)Δx+g(xk)≤0,Jh(xk)Δx+h(xk)=0,\begin{align*} \text{Minimize} \quad & \nabla f(\mathbf{x}^k)^T \Delta \mathbf{x} + \frac{1}{2} \Delta \mathbf{x}^T \nabla^2_{\mathbf{x}\mathbf{x}} \mathcal L(\mathbf{x}^k, \boldsymbol{\lambda}^k, \boldsymbol{\mu}^k) \Delta \mathbf{x} \\ \text{subject to} \quad & J_g(\mathbf{x}^k) \Delta \mathbf{x} + \mathbf{g}(\mathbf{x}^k) \leq \mathbf{0}, \\ & J_h(\mathbf{x}^k) \Delta \mathbf{x} + \mathbf{h}(\mathbf{x}^k) = \mathbf{0}, \end{align*}

where Δx=x−xk\Delta \mathbf{x} = \mathbf{x} - \mathbf{x}^k represents the step direction for the primal variables. The following pseudocode outlines the steps involved in applying SQP to a problem with both equality and inequality constraints:

Demonstration with JAX and CVXPy

Consider the following equality and inequality-constrained problem:

min⁡x∈R2f(x)=(x1−2)2+(x2−1)2subject tog(x)=x12−x2≤0h(x)=x12+x22−1=0\begin{align*} \min_{x \in \mathbb{R}^2} \quad & f(x) = (x_1 - 2)^2 + (x_2 - 1)^2 \\ \text{subject to} \quad & g(x) = x_1^2 - x_2 \leq 0 \\ & h(x) = x_1^2 + x_2^2 - 1 = 0 \end{align*}

This example builds on our previous one but adds a parabola-shaped inequality constraint. We require our solution to lie not only on the circle defining our equality constraint but also on or above the parabola. To solve the QP subproblem, we will be using the CVXPY package. While the Lagrangian and derivatives could be computed easily by hand, we use JAX for generality:

Source
Optimal x: [0.78615135 0.618034  ]
Optimal lambda: [1.03215619]
Optimal mu: [0.51188314]
Iterations: 5

Equality constraint violation: -0.000000
Inequality constraint violation: -0.000000
Objective function value: 1.619326
<Figure size 1200x1000 with 2 Axes>

The Arrow-Hurwicz-Uzawa algorithm

The primal min–max representation motivates updates that descend in the primal variables and ascend in the multipliers. The Arrow-Hurwicz-Uzawa method Arrow et al. (1958) uses these first derivatives instead of the quadratic subproblems used by SQP. With the notation of the preceding duality discussion, L(x,λ,μ)=f(x)+μ⊤g(x)+λ⊤h(x)\mathcal L(\mathbf x,\boldsymbol\lambda,\boldsymbol\mu) =f(\mathbf x)+\boldsymbol\mu^\top\mathbf g(\mathbf x) +\boldsymbol\lambda^\top\mathbf h(\mathbf x), where equality multipliers are unrestricted and inequality multipliers are nonnegative.

These updates do not compute the inner supremum of the primal formulation. Each iteration takes a finite step in both players’ variables. The resulting method is called gradient descent-ascent. Its convergence requires a separate analysis, even when the problem has a saddle point. For equality constraints, the alternating updates take the following form:

Now to account for the fact that the Lagrange multiplier needs to be non-negative for inequality constraints, we can use our previous idea from projected gradient descent for bound constraints and consider a projection, or clipping step to ensure that this condition is satisfied throughout. In this case, the algorithm looks like the following:

Here, [⋅]+[\cdot]_+ denotes the projection onto the non-negative orthant, ensuring that μ\boldsymbol{\mu} remains non-negative.

A saddle point need not attract these iterates. For the bilinear Lagrangian L(x,λ)=λx\mathcal L(x,\lambda)=\lambda x, the equality-only updates give xk+1=xk−αλkx^{k+1}=x^k-\alpha\lambda^k and λk+1=λk+βxk+1\lambda^{k+1}=\lambda^k+\beta x^{k+1}. Their update matrix has determinant one, so it cannot contract in all directions. Convexity and saddle-point existence alone therefore do not guarantee convergence of this iteration. Additional assumptions or modifications, such as augmented terms or extragradient steps, are needed for an applicable convergence guarantee.

Source
Final x: [0.7861007  0.61806566]
Final lambda: [1.0322341]
Final mu: [0.51208335]

Equality constraint violation: -0.000041
Inequality constraint violation: -0.000111
Objective function value: 1.619426
<Figure size 1200x1000 with 3 Axes>

Projected Gradient Descent

The Arrow-Hurwicz-Uzawa algorithm provided a way to handle constraints through dual variables and a primal-dual update scheme. Another commonly used approach for constrained optimization is Projected Gradient Descent (PGD). The idea is simple: take a gradient descent step as if the problem were unconstrained, then project the result back onto the feasible set. Formally:

xk+1=PC(xk−α∇f(xk)),\mathbf{x}_{k+1} = \mathcal{P}_C\big(\mathbf{x}_k - \alpha \nabla f(\mathbf{x}_k)\big),

where PC\mathcal{P}_C is the projection onto the feasible set CC, α\alpha is the step size, and f(x)f(\mathbf{x}) is the objective function.

PGD is particularly effective when the projection is computationally cheap. A common example is box constraints (or bound constraints), where the feasible set is a hyperrectangle:

C={x∣xlb≤x≤xub}.C = \{ \mathbf{x} \mid \mathbf{x}_{\mathrm{lb}} \leq \mathbf{x} \leq \mathbf{x}_{\mathrm{ub}} \}.

In this case, the projection reduces to an element-wise clipping operation:

[PC(x)]i=max⁡(min⁡([x]i,[xub]i),[xlb]i).[\mathcal{P}_C(\mathbf{x})]_i = \max\big(\min([\mathbf{x}]_i, [\mathbf{x}_{\mathrm{ub}}]_i), [\mathbf{x}_{\mathrm{lb}}]_i\big).

For bound-constrained problems, PGD is almost as easy to implement as standard gradient descent because the projection step is just a clipping operation. For more general constraints, however, the projection may require solving a separate optimization problem, which can be as hard as the original task. Here is the algorithm for a problem of the form:

min⁡xf(x)subject toxlb≤x≤xub.\begin{aligned} \min_{\mathbf{x}} \quad & f(\mathbf{x}) \\ \text{subject to} \quad & \mathbf{x}_{\mathrm{lb}} \leq \mathbf{x} \leq \mathbf{x}_{\mathrm{ub}}. \end{aligned}

The clipping function is defined as:

clip(x,xlb,xub)=max⁡(min⁡(x,xub),xlb).\text{clip}(x, x_{\mathrm{lb}}, x_{\mathrm{ub}}) = \max\big(\min(x, x_{\mathrm{ub}}), x_{\mathrm{lb}}\big).

Under mild conditions such as Lipschitz continuity of the gradient, PGD converges to a stationary point of the constrained problem. Its simplicity and low cost make it a common choice whenever the projection can be computed efficiently.

References
  1. Ortega, J. M., & Rheinboldt, W. C. (1970). Iterative Solution of Nonlinear Equations in Several Variables. Academic Press.
  2. Allgower, E. L., & Georg, K. (1990). Numerical Continuation Methods: An Introduction (Vol. 13). Springer-Verlag.
  3. Arrow, K. J., Hurwicz, L., & Uzawa, H. (1958). Studies in linear and non-linear programming. Stanford University Press.