Stanford AA274A Principles of Robotic Autonomy | Autumn 2019 | Advanced Trajectory Optimization

Stanford AA274A Principles of Robotic Autonomy | Autumn 2019 | Advanced Trajectory Optimization — Stanford Online

Stanford Online

AI-assisted summary of Stanford AA274A Principles of Robotic Autonomy | Autumn 2019 | Advanced Trajectory Optimization — original source. Check the original for full context.

Source published:

Key ideas

  • Continuous-time optimal control operates over infinite-dimensional function spaces because the decision variable is an uncountable control trajectory u(t), dividing computational solutions into indirect ('first optimize, then discretize') and direct ('first discretize, then optimize') frameworks. (4:04)
  • Indirect optimal control transforms dynamic optimization into a two-point boundary value problem by constructing a Hamiltonian and introducing continuous costate functions p(t), directly extending static Lagrange multiplier theory to differential equality constraints. (32:46)
  • Free final time optimal control problems can be adapted to off-the-shelf numerical BVP solvers via a dimensionless time normalization tau = t / tf on [0, 1], augmenting the state space with a constant parameter state r = tf governed by zero dynamics. (57:13)
  • Direct and indirect trajectory optimization are theoretically consistent: as the temporal discretization step size approaches zero, discrete Karush-Kuhn-Tucker (KKT) conditions converge exactly to the continuous Euler-Lagrange and Hamiltonian boundary value conditions. (74:31)

Detailed notes

## Background Published on YouTube by Stanford Online on October 9, 2026, this video features Lecture 5 of the Stanford AA274A 'Principles of Robotic Autonomy' course taught by Prof. Marco Pavone. According to the page description, this 2019 lecture covers advanced methods for trajectory optimization and serves as a companion to the open-access textbook Principles of Robot Autonomy.

## TL;DR In this lecture, Prof. Marco Pavone explores indirect methods for continuous-time trajectory optimization, contrasting the 'first optimize, then discretize' philosophy with direct nonlinear programming methods. Starting from finite-dimensional Lagrange multipliers and a geometric circle example, he introduces the continuous-time Hamiltonian, costates, and the resulting two-point boundary value problems (TPBVPs). Pavone demonstrates how to resolve boundary conditions and handle free final times via a normalized time-rescaling trick on a double integrator benchmark. He concludes by proving the structural consistency of direct and indirect methods, showing that Karush-Kuhn-Tucker conditions converge to continuous necessary conditions in the limit.

## Atmosphere & style The lecture carries an engaging, mathematically rigorous yet relaxed academic atmosphere typical of an elite Stanford graduate seminar. Pavone commands the room with infectious enthusiasm and casual humor, taking playful historical detours to claim Lagrange for Turin, playfully teasing students about MATLAB versus Python, and warning that TPBVPs represent calculus 'difficulty on steroids.' Watching the session feels like sitting in an accessible masterclass that steadily demystifies intimidating variational calculus by anchoring abstract theory in clear whiteboard geometry and functional code.

## Bulletin - [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) Continuous-time optimal control operates over infinite-dimensional function spaces because the decision variable is an uncountable control trajectory u(t), dividing computational solutions into indirect ('first optimize, then discretize') and direct ('first discretize, then optimize') frameworks. - [32:46](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1966s) Indirect optimal control transforms dynamic optimization into a two-point boundary value problem by constructing a Hamiltonian and introducing continuous costate functions p(t), directly extending static Lagrange multiplier theory to differential equality constraints. - [57:13](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=3433s) Free final time optimal control problems can be adapted to off-the-shelf numerical BVP solvers via a dimensionless time normalization tau = t / tf on [0, 1], augmenting the state space with a constant parameter state r = tf governed by zero dynamics. - [01:14:31](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4471s) Direct and indirect trajectory optimization are theoretically consistent: as the temporal discretization step size approaches zero, discrete Karush-Kuhn-Tucker (KKT) conditions converge exactly to the continuous Euler-Lagrange and Hamiltonian boundary value conditions.

## Key takeaways - [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) Continuous optimal control is inherently infinite-dimensional because the decision variable is the continuous time history u(t), containing an uncountable number of decision variables across the time horizon. - [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) Open-loop optimal controls derived solely from initial conditions are vulnerable to disturbances and dynamic mismatch, requiring physical robotic systems to pair planned trajectories with feedback tracking controllers. - [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) Computational optimal control divides into indirect methods ('first optimize, then discretize') that solve analytical necessary boundary conditions, and direct methods ('first discretize, then optimize') that transcribe dynamics into finite nonlinear programs. - [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) Differential flatness belongs to neither standard category because it solves a dynamic feasibility mapping rather than numerically optimizing an arbitrary cost functional over time. - [14:00](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=840s) Finite-dimensional equality-constrained optimization serves as the conceptual stepping stone for optimal control, using Lagrange multipliers to convert constrained problems into unconstrained Lagrangian stationarity conditions. - [32:46](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1966s) In continuous optimal control, the Hamiltonian acts as the continuous analogue of the Lagrangian, adjoining system differential equations of motion to the running cost via time-varying costate functions p(t). - [37:08](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2228s) The continuous indirect formulation yields an exact balance of unknowns and equations: an n-state, m-control system generates 2n ODEs and m algebraic conditions to solve for 2n + m unknown functions of time. - [42:09](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2529s) Constants of integration from the 2n coupled first-order differential equations are resolved by n initial state conditions alongside terminal conditions dictated by the calculus-of-variations master boundary equation. - [44:42](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2682s) When the final arrival time tf is unconstrained, admissibility of final-time variations enforces the scalar transversality condition H(tf) + dh/dtf = 0, supplying the required (2n+1)-th equation to resolve the optimal duration. - [49:08](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2948s) Optimal control yields two-point boundary value problems (TPBVPs) where boundary conditions are split across opposite temporal ends, requiring numerical solvers to drive residual functions to zero. - [57:13](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=3433s) A dimensionless time rescaling tau = t / tf maps unknown integration intervals [0, tf] onto a fixed interval [0, 1], allowing standard BVP routines to solve free-final-time problems by treating tf as a static state. - [01:14:31](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4471s) Direct nonlinear programming multipliers correspond directly to continuous costate variables, proving that discrete NLP solutions converge to continuous indirect solutions as grid spacing approaches zero.

## Detailed walkthrough ### [00:05](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=5s) Optimal Control Formulation for Trajectory Optimization

Professor Marco Pavone opens the lecture by establishing that this is the final session focused purely on trajectory optimization before the class transitions into motion planning. In the vocabulary of this course, motion planning is simply trajectory optimization equipped with obstacle constraints. In previous lectures on differential flatness, the primary objective was merely finding a dynamically feasible solution that possessed smooth, desirable properties—such as minimizing snap or jerk—rather than strictly optimizing an arbitrary performance index. In many practical robotics settings, however, finding just any feasible trajectory is not enough; the robot must actively minimize a cost function such as transit time, control effort, or a weighted blend of both.

```mermaid flowchart TD accTitle: Indirect vs Direct Methods in Optimal Control accDescr: Workflow comparison showing indirect methods optimizing before discretizing versus direct methods discretizing before optimizing.

subgraph Indirect["Indirect Methods ('First Optimize, Then Discretize')"] I1["Continuous Optimal Control Problem"] -->|"Derive necessary conditions"| I2["Boundary Value Problem"] I2 -->|"Discretize & numerically solve"| I3["Optimal Trajectory"] end

subgraph Direct["Direct Methods ('First Discretize, Then Optimize')"] D1["Continuous Optimal Control Problem"] -->|"Discretize states and inputs"| D2["Finite-Dimensional NLP"] D2 -->|"Solve via nonlinear solver"| D3["Optimal Trajectory"] end ```

This optimization perspective places trajectory planning squarely within the domain of optimal control theory, a topic Pavone teaches as a full dedicated course in the spring. For this lecture, the focus centers on giving students actionable insights into indirect methods, while concluding with the theoretical bridge connecting indirect and direct approaches. As a primary reference, Pavone recommends the textbook *Optimal Control Theory: An Introduction* by Donald E. Kirk, noting that while the text is somewhat dated, it succeeds at making an inherently sophisticated, mathematically dense subject accessible without sacrificing analytical rigor.

Mathematically, the trajectory planning problem is formalized around the system's differential equations of motion:

$$\dot{x}(t) = f(x(t), u(t))$$

where $x$ denotes the state vector and $u$ represents the control input (describing models such as a unicycle, differential drive robot, or simple car). The control input is restricted by actuator authority limits, such as saturation bounds on speed, linear acceleration, and angular velocity. While obstacle avoidance constraints are deferred to the following week—meaning the state space is currently treated as the unconstrained Euclidean space $\mathbb{R}^n$—the objective is governed by an additive cost functional consisting of a terminal penalty $\Phi$ and an integral running stage cost $L$:

$$J(u) = \Phi(x(t_f), t_f) + \int_{t_0}^{t_f} L(x(t), u(t), t) \, dt$$

The terminal term penalizes deviations from the target state at the final time $t_f$, while the integral running term accumulates costs incurred along the path, such as expended energy or elapsed time.

- **Dynamics**: Differential constraints $\dot{x} = f(x, u)$ representing continuous robotic kinematics and dynamics. - **Actuation limits**: Hard upper and lower bounds on control authority, velocities, and accelerations. - **Objective functional**: Additive combination of terminal cost $\Phi(x(t_f), t_f)$ and running cost $\int_{t_0}^{t_f} L(x, u, t) \, dt$.

---

[04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) Infinite-Dimensional Spaces, Indirect vs. Direct Methods, and Flatness

The fundamental difficulty in optimal control stems from optimizing over a function space rather than a conventional finite set of scalar decision variables. In typical parameter optimization, one solves for a finite vector like $[x_1, x_2, x_3]^T$. In optimal control, the decision variable is the continuous time history $u(t)$. Because time $t$ is continuous, the problem contains an uncountable number of decision variables—one for every single instant $t$. This classifies optimal control as an infinite-dimensional optimization problem, rendering it substantially more intricate than standard finite-dimensional calculus.

In standard practice, trajectory generation solves for an open-loop control trajectory $u(t)$ parameterized solely by time and the initial state $x(t_0)$. Pavone reiterates the intrinsic vulnerability of open-loop control: if the robot operates under purely nominal conditions, open loop works as designed; however, if unmodeled dynamics, external disturbances, or state drift intervene, an open-loop controller blindly continues executing its preplanned control action, "which might indeed result in disaster." Consequently, open-loop optimal trajectories must ultimately be paired with feedback tracking controllers in real physical systems.

When addressing infinite-dimensional optimal control computationally, two foundational philosophies exist:

1. **Indirect Methods**: Summarized by the maxim "first optimize, then discretize." One derives the analytical, necessary first-order optimality conditions directly in continuous, infinite-dimensional space, and then numerically discretizes the resulting boundary value equations to solve them. 2. **Direct Methods**: Summarized by the maxim "first discretize, then optimize." One transcribes the continuous infinite-dimensional problem into a finite-dimensional nonlinear programming (NLP) problem via time discretization and parameterization, and subsequently delegates the optimization to numerical solvers.

A student in the lecture asks where the previously studied method of differential flatness fits within these two paradigms. Pavone clarifies that differential flatness strictly belongs to neither class. Philosophically, flatness leans closer to direct methods because it entirely bypasses the derivation of necessary optimality conditions, instead converting dynamics into algebraic relations or linear systems of equations. Crucially, differential flatness solves a feasibility problem rather than an optimal control problem: it aims to find any valid input trajectory $u(t)$ that satisfies system dynamics and boundary conditions. Although one can project cost criteria into the flat output space (e.g., minimizing snap or jerk), it remains fundamentally a feasibility mapping technique rather than a rigorous numerical optimizer of an arbitrary cost functional.

---

[08:47](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=527s) Foundation of Indirect Methods: From Unconstrained to Constrained Optimization

To build intuition for indirect methods without becoming bogged down in dense functional analysis proofs, Pavone draws an analogy to familiar finite-dimensional optimization. In unconstrained finite-dimensional optimization, one seeks the global minimizer of a differentiable scalar function $f(x)$ over $x \in \mathbb{R}^n$. The established necessary condition of optimality is setting the gradient to zero:

$$\nabla f(x) = 0$$

Solving this system yields stationary candidate points, which are evaluated to determine the lowest cost. Indirect optimal control operates under the exact same procedural philosophy: write down the first-order necessary optimality conditions, and then solve the resulting system of equations. However, because optimal control is defined over continuous function spaces subject to complex dynamic constraints, formulating these conditions is vastly more intricate than taking an elementary gradient.

To bridge the gap between elementary calculus and infinite-dimensional optimal control, Pavone introduces constrained finite-dimensional optimization. Specifically, minimizing a cost function $f(x)$ subject to an $m$-dimensional system of equality constraints:

$$h(x) = 0$$

Before unveiling the necessary conditions for this constrained setting, Pavone poses a conceptual question to the class: why is this constrained formulation the true finite-dimensional analogue of an optimal control problem? He prompts the room to identify the core equality constraint that is the universal "hallmark of any optimal control problem"—namely, the governing differential equation of motion $\dot{x} - f(x, u) = 0$, which must hold true across the entire continuous trajectory.

[11:54](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=714s) Optimal Control and Equality-Constrained Optimization

To understand how indirect methods solve optimal control problems, it is instructive to look first at finite-dimensional nonlinear optimization. In any standard optimal control problem, one must find control inputs and corresponding state trajectories that satisfy the differential equations governing the system's dynamics. Although these equations of motion are differential rather than simple algebraic relations, mathematically they remain equality constraints. Consequently, equality-constrained nonlinear optimization is the finite-dimensional problem class that shares the closest mathematical kinship with continuous-time optimal control. By studying the necessary optimality conditions for equality-constrained problems, one can draw a direct conceptual parallel to the necessary conditions used in indirect optimal control.

```mermaid flowchart LR accTitle: Analogy between Finite Optimization and Optimal Control accDescr: Parallel between equality-constrained nonlinear programming and continuous-time optimal control. subgraph FiniteOpt["Finite-Dimensional Optimization"] A["Decision Variables x"] --> B["Algebraic Equality Constraints h(x) = 0"] B --> C["Lagrangian Multipliers & NOC"] end subgraph OptControl["Optimal Control"] D["States x(t) & Controls u(t)"] --> E["Differential Equations of Motion dx/dt = f(x,u)"] E --> F["Costates & Pontryagin Conditions"] end ```

During the discussion, a student asks about handling constraints that are not strict equalities—such as upper bounds on velocity. Prof. Pavone notes that general nonlinear programming certainly accommodates both equality and inequality constraints, which yield their own necessary optimality conditions. However, treating inequality constraints rigorously is beyond the scope of this course; the focus on equality constraints here is pedagogical rather than a complete survey of general nonlinear programming. He adds that handling more sophisticated constraints leads directly to richer optimality conditions, which students are welcome to explore further in his advanced spring course.

[14:00](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=840s) The Lagrangian and Necessary Optimality Conditions

In unconstrained optimization, locating a candidate local minimum requires checking the familiar first-order necessary condition that the gradient vanishes: $\nabla f(x) = 0$. When equality constraints of the form $h_i(x) = 0$ for $i \in \{1, \dots, m\}$ are present, direct optimization over the feasible manifold becomes substantially more complicated. The classical approach to overcoming this difficulty is Lagrange theory, which transforms the constrained problem into an unconstrained problem by defining an augmented cost function known as the Lagrangian:

$$L(x, \lambda) = f(x) + \sum_{i=1}^m \lambda_i h_i(x)$$

Here, the original decision variables $x$ are augmented with an additional vector of decision variables $\lambda = [\lambda_1, \dots, \lambda_m]^T$, known as Lagrange multipliers. Rather than forcing the search to strictly satisfy the constraints at every step, the constraints are moved directly into the objective, where violations are penalized via $\lambda$. The fundamental result of equality-constrained optimization is that a Necessary Optimality Condition (abbreviated on the board as NOC) for a regular point $x^*$ to be a local minimum is that the Lagrangian must achieve stationarity at $(x^*, \lambda^*)$:

$$\nabla L(x^*, \lambda^*) = 0 \iff \frac{\partial L}{\partial x}(x^*, \lambda^*) = 0 \quad \text{and} \quad \frac{\partial L}{\partial \lambda}(x^*, \lambda^*) = 0$$

- **NOC Definition**: In response to a clarifying question from the class, Prof. Pavone confirms that "NOC" stands for Necessary Optimality Condition. - **Unconstrained Equivalency**: Moving constraints to the objective allows practitioners to leverage the simple first-order stationary condition ($\text{gradient} = 0$) established in unconstrained calculus.

[18:21](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1101s) Historical Aside on Lagrange and the Mechanics of Multipliers

Before diving deeper into the calculus, Prof. Pavone takes a humorous historical detour to correct a widespread misconception about Joseph-Louis Lagrange. While most students assume Lagrange was French, he was born Giuseppe Lodovico Lagrangia in Turin (Torino), Italy—the very same city Prof. Pavone is from. Lagrange lived in Italy until around age 40, when he moved to France; the French adapted his name to "Lagrange," which sounded more French, leading posterity to misidentify his nationality. "You didn't know that one, eh? You thought he was French," Pavone teases the laughing lecture hall, taking pride in this mathematical "legacy of Italy, courtesy of Italy."

Returning to the mathematics, Pavone asks the students what result is obtained when taking the partial derivative of the Lagrangian with respect to the $i$-th multiplier, $\frac{\partial L}{\partial \lambda_i}$. When a student guesses zero, Pavone corrects them: the derivative evaluates to $h_i(x)$. Setting this derivative to zero precisely recovers $h_i(x) = 0$, which guarantees that any stationary point satisfies primal feasibility. Meanwhile, setting $\frac{\partial L}{\partial x} = 0$ ensures stationarity with respect to the original decision variables.

In practice, the procedure is straightforward: 1. Construct the Lagrangian $L(x, \lambda)$. 2. Compute the stationarity conditions $\nabla_x L = 0$ and $\nabla_\lambda L = 0$. 3. Solve the resulting system of algebraic equations simultaneously for both $x$ and $\lambda$. 4. Discard the multipliers $\lambda$, which merely served as auxiliary tools to solve the problem, leaving $x^*$ as candidate optimal solutions.

Pavone mentions a mild technical requirement that he is largely "hiding under the rug": the candidate point $x^*$ must be *regular*, meaning the constraint gradients $\nabla h_i(x^*)$ must be linearly independent.

[21:05](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1265s) Geometric Example: Minimizing Linear Cost on a Circle

To ground the theoretical framework, Prof. Pavone introduces a concrete two-dimensional constrained optimization problem:

$$\min_{x_1, x_2} \; (x_1 + x_2) \quad \text{subject to} \quad x_1^2 + x_2^2 = 2$$

Geometrically, the constraint $x_1^2 + x_2^2 = 2$ defines the set of all feasible solutions as a circle of radius $\sqrt{2}$ centered at the origin on the $x_1$-$x_2$ plane. Any acceptable solution must lie on the boundary of this circle. The cost function $f(x_1, x_2) = x_1 + x_2$ can be visualized through its level curves, $x_1 + x_2 = c$, which form parallel straight lines inclined at a 45-degree angle (having a slope of $-1$). For positive values of $c$, the lines sit further to the upper right; as $c$ decreases through zero toward negative numbers, the lines shift downward and to the left.

To minimize the objective geometrically, one slides the level curve line as far down and to the left as possible while maintaining at least one intersection point with the circle. Moving past the circle entirely would yield no feasible solutions. By symmetry, the extreme level curve achieves tangency in the third quadrant at the coordinate $(-1, -1)$. At this point, the constraint is satisfied because $(-1)^2 + (-1)^2 = 2$, producing the minimum cost value of $-2$.

Prof. Pavone then turns back to the board to demonstrate how the Lagrange multiplier method analytically recovers this geometric solution. Setting up the formal notation, the objective is defined as $f(x) = x_1 + x_2$, and the single equality constraint is written in standard zero-equality form as $h(x) = x_1^2 + x_2^2 - 2 = 0$.

[25:25](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1525s) Solving the Constrained Example with Lagrange Multipliers

Continuing with the toy equality-constrained problem, the goal is to minimize $f(x_1, x_2) = x_1 + x_2$ subject to the single equality constraint $x_1^2 + x_2^2 - 2 = 0$. Because there is exactly one equality constraint, only one scalar Lagrange multiplier $\lambda \in \mathbb{R}$ is needed. The augmented objective function, the Lagrangian $\mathcal{L}(x_1, x_2, \lambda)$, is formed by appending $\lambda(x_1^2 + x_2^2 - 2)$ to the original cost. To find candidate stationary points, one sets the gradient of this Lagrangian with respect to all decision variables—both the original coordinates and the multiplier—equal to zero.

Taking the partial derivatives yields three necessary optimality conditions: $$\frac{\partial \mathcal{L}}{\partial x_1} = 1 + 2\lambda x_1 = 0$$ $$\frac{\partial \mathcal{L}}{\partial x_2} = 1 + 2\lambda x_2 = 0$$ $$\frac{\partial \mathcal{L}}{\partial \lambda} = x_1^2 + x_2^2 - 2 = 0$$

Differentiating with respect to the multiplier $\lambda$ directly recovers the original equality constraint. This enforces that any stationary point of the augmented function is strictly feasible for the constrained problem, establishing the mathematical link between the unconstrained formulation and the constrained reality.

Solving these three simultaneous equations for the three unknowns ($x_1, x_2, \lambda$) begins by isolating the spatial coordinates: $x_1 = -\frac{1}{2\lambda}$ and $x_2 = -\frac{1}{2\lambda}$. Substituting these expressions back into the constraint yields: $$\frac{1}{4\lambda^2} + \frac{1}{4\lambda^2} = 2 \implies \frac{1}{2\lambda^2} = 2 \implies \lambda^2 = \frac{1}{4} \implies \lambda = \pm \frac{1}{2}$$

Plugging each possible value of $\lambda$ back into the coordinate formulas reveals two candidate points: - For $\lambda = \frac{1}{2}$, $(x_1, x_2) = (-1, -1)$ - For $\lambda = -\frac{1}{2}$, $(x_1, x_2) = (1, 1)$

Once the candidates are found, the multiplier has served its purpose and can be discarded. While multipliers carry valuable sensitivity information in advanced optimization, for our purposes here, $\lambda$ is merely an auxiliary device to find candidates for local optimality. Evaluating the original objective function $f(x) = x_1 + x_2$ at both points yields $f(-1, -1) = -2$ and $f(1, 1) = 2$. Because $-2 < 2$, the point $(-1, -1)$ is identified as the minimizer.

```mermaid flowchart LR accTitle: Solving Equality Constrained Optimization accDescr: Pipeline showing setup of Lagrangian, finding stationary points, and identifying the global minimum. subgraph Formulation["1. Formulation"] L["Form Lagrangian<br/>L = f(x) + &lambda; h(x)"] end subgraph Conditions["2. Stationarity"] Grad["Set &nabla;L = 0<br/>&part;L/&part;x = 0<br/>&part;L/&part;&lambda; = 0"] end subgraph Solve["3. Candidates"] Pts["Solve for x, &lambda;<br/>Candidates: (-1,-1), (1,1)"] end subgraph Selection["4. Optimal Solution"] Eval["Evaluate f(x)<br/>f(-1,-1) = -2 (Min)<br/>f(1,1) = 2 (Max)"] end L --> Grad Grad --> Pts Pts --> Eval ```

[29:45](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1785s) Intuition, Feasibility, and Boundary Nuances

When asked what happens if one evaluates the full Lagrangian $\mathcal{L}$ at these candidates rather than just $f(x)$, the intuition boils down to constraint penalization. The Lagrangian acts as an unconstrained, penalized surrogate for the original problem. Because every stationary point inherently satisfies $\partial \mathcal{L} / \partial \lambda = 0$, the constraint term evaluates to zero at feasibility ($x_1^2 + x_2^2 - 2 = 0$). Consequently, at these candidate points, the numerical value of the Lagrangian equals the value of the original cost function.

Addressing whether checking these stationary points proves global optimality, the answer in this specific instance is yes. The linear objective function is continuous, and the constraint domain (a circle of radius $\sqrt{2}$) is compact; by the extreme value theorem, both a global minimum and maximum must exist, and here they are fully captured by the stationary points.

However, this does not hold universally. In unconstrained or inequality-constrained optimization, the condition $\nabla f = 0$ is strictly a necessary condition for local optimality when the extremum lies in the *interior* of the feasible set. If a domain has boundaries, a true global minimum may occur on a boundary where the gradient does not vanish. While these theoretical nuances are central to nonlinear programming, the primary takeaway for this robotics course is the core structural paradigm: constructing an augmented function, setting gradients to zero, and solving the resulting equations to obtain candidate optimal trajectories.

[32:46](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1966s) From Static Multipliers to Indirect Optimal Control: The Hamiltonian

The foundational logic of Lagrange multipliers transitions directly into *indirect methods* for optimal control. In continuous-time trajectory optimization, instead of optimizing over a finite-dimensional vector of static coordinates, the decision variables are dynamic functions: state trajectories $x(t)$ and control inputs $u(t)$. In complete analogy to the static Lagrangian, optimal control introduces an augmented function termed the **Hamiltonian**, denoted $H$: $$H(x, u, p, t) = g(x, u, t) + p^T(t) a(x, u, t)$$

Here, $g(x, u, t)$ represents the running stage cost (analogous to the objective $f(x)$), while $a(x, u, t)$ represents the system equations of motion $\dot{x} = a(x, u, t)$, which serve as the equality constraints. The vector $p(t)$ represents the **costate variables**. These costates are the exact continuous-time counterpart of Lagrange multipliers. Because the decision variables $x(t)$ and $u(t)$ are time-varying functions, the Lagrange multipliers $p(t)$ must also be time-varying functions of time.

Writing down the necessary optimality conditions in optimal control yields a set of coupled ordinary differential equations (ODEs) rather than simple nonlinear algebraic equations: - The state dynamics emerge directly from differentiating with respect to the multiplier: $\dot{x} = \frac{\partial H}{\partial p} = a(x, u, t)$, mirroring how $\partial \mathcal{L} / \partial \lambda = 0$ recovered the equality constraints in the static case. - The costate dynamics are governed by differential equations driven by $\frac{\partial H}{\partial x}$. - The optimal control input is determined by stationarity with respect to control: $\frac{\partial H}{\partial u} = 0$.

Although solving coupled ODEs is computationally more challenging than solving static algebraic roots, the operational philosophy is unchanged: solve the coupled differential-algebraic boundary value problem for the states, controls, and costates, discard the costates $p(t)$, and keep the resulting trajectory $(x(t), u(t))$ as the candidate optimal motion profile.

[37:08](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2228s) Matching Variables and Equations: Dimension Counting

A balance between the number of equations and unknowns ensures that these optimality conditions are mathematically well-posed. Comparing the finite-dimensional optimization problem directly against the continuous-time optimal control problem demonstrates a consistent structural match.

In the finite-dimensional setting: - **Decision variables:** State vector $x \in \mathbb{R}^n$ ($n$ variables) plus Lagrange multipliers $\lambda \in \mathbb{R}^m$ for $m$ equality constraints ($m$ variables), giving $n + m$ total unknowns. - **Equations:** Differentiating with respect to $x$ gives $n$ equations ($\nabla_x \mathcal{L} = 0$), and differentiating with respect to $\lambda$ recovers the $m$ constraints ($\nabla_\lambda \mathcal{L} = 0$), providing $n + m$ algebraic equations to solve for $n + m$ unknowns.

In the optimal control formulation: - **State dimension:** The physical system state $x(t)$ is an $n$-dimensional vector function ($n$ functions). - **Costate dimension:** Because the vector differential equation of motion $\dot{x} = a(x, u, t)$ consists of $n$ equality constraints (one per state derivative), there are $n$ costate functions $p(t) \in \mathbb{R}^n$. - **Control dimension:** There are $m$ control inputs $u(t) \in \mathbb{R}^m$. - **Total unknown functions:** $n \text{ (states)} + n \text{ (costates)} + m \text{ (controls)} = 2n + m$ functions of time.

Matching these unknowns on the equation side: - $\dot{x} = \frac{\partial H}{\partial p}$ provides $n$ ODEs for the states. - $\dot{p} = -\frac{\partial H}{\partial x}$ provides $n$ ODEs for the costates. - $\frac{\partial H}{\partial u} = 0$ provides $m$ algebraic conditions for the controls.

This yields exactly $2n$ ordinary differential equations and $m$ algebraic equations—totaling $2n + m$ equations to determine the $2n + m$ unknown functions.

| Attribute | Static Constrained Optimization | Optimal Control (Indirect Method) | |:--- |:--- |:--- | | **Augmented Function** | Lagrangian $\mathcal{L}(x, \lambda) = f(x) + \lambda^T h(x)$ | Hamiltonian $H(x, u, p, t) = g(x, u, t) + p(t)^T a(x, u, t)$ | | **Auxiliary Variables** | Lagrange Multipliers $\lambda \in \mathbb{R}^m$ | Costate Functions $p(t) \in \mathbb{R}^n$ | | **System Constraints** | $h(x) = 0$ | Differential Equations $\dot{x} = a(x, u, t)$ | | **Feasibility Recovery** | $\frac{\partial \mathcal{L}}{\partial \lambda} = 0 \implies h(x) = 0$ | $\frac{\partial H}{\partial p} = \dot{x} \implies \dot{x} = a(x, u, t)$ | | **Total Unknowns** | $n + m$ scalar variables | $2n + m$ functions of time | | **Equation Structure** | $n + m$ nonlinear algebraic equations | $2n$ coupled ODEs $+ m$ algebraic equations |

[39:58](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2398s) Boundary Conditions and Constants of Integration

The necessary optimality conditions provide enough equations to pinpoint all the decision variables and Lagrange multipliers (costates). However, because these necessary conditions are formulated as differential equations, solving them requires boundary conditions to determine the constants of integration. In general, each first-order differential equation introduces one constant of integration. Professor Pavone playfully quizzes the room, threatening to call out an ICME (Institute for Computational and Mathematical Engineering) student to answer fundamental calculus questions.

To illustrate, consider the simple scalar equation $\dot{x} = x$, whose general solution is $x(t) = C e^t$, where $C$ is a constant determined by the initial condition. For a system with $n$ state variables and $n$ costate variables, there are $2n$ first-order differential equations in total, which means exactly $2n$ constants of integration must be resolved. The initial boundary condition at time $t_0$, specified as $x(t_0) = x_0$, supplies the first $n$ equations. The remaining conditions must be determined from the terminal constraints and the variations allowed at the final time.

[42:09](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2529s) The Master Boundary Equation and Admissible Variations

To resolve the remaining $n$ constants of integration, optimal control theory relies on a transversality condition derived from the calculus of variations. While the expression can appear convoluted at first glance, Pavone advises students to treat it as an operational tool rather than overanalyzing its mechanical derivation. The condition is expressed through the master boundary equation:

$$\left( \frac{\partial h}{\partial x} - p \right)^T \delta x_f + \left( H + \frac{\partial h}{\partial t} \right) \delta t_f = 0$$

In this relation, $h$ denotes the terminal cost of the optimal control problem, $p$ is the costate vector evaluated at the terminal time, and $H$ is the Hamiltonian. The quantities $\delta x_f$ and $\delta t_f$ represent the admissible variations of the final state and final time: - $\delta x_f$ captures the admissible variation at the final endpoint—whether the system must reach a fixed, prescribed target state or whether the final state is free to be chosen by the optimizer. - $\delta t_f$ captures the admissible variation in final time—whether the horizon is fixed (such as arriving at a destination in exactly 10 seconds) or free (such as reaching a target as quickly as possible without specifying $t_f$ in advance).

```mermaid flowchart TD accTitle: Boundary Condition Resolution accDescr: Flowchart illustrating how initial conditions and terminal variations together resolve all 2n constants of integration. subgraph Initial["Initial Boundary"] IC["x(t₀) = x₀"] -->|"n conditions"| Total["2n System Constants"] end subgraph Master["Master Equation"] ME["(∂h/∂x - p)ᵀ δx_f + (H + ∂h/∂t) δt_f = 0"] ME -->|"Evaluate δx_f and δt_f"| Term["Terminal Conditions"] Term -->|"n (or n+1) conditions"| Total end ```

[44:42](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2682s) Analyzing Fixed vs. Free Boundary Scenarios

Applying the master boundary equation depends on the physical problem setup:

1. **Fixed Final Time and Fixed Final State ($t_f$ fixed, $x_f$ fixed):** If both the arrival time and destination are specified in advance, no variation is permitted. As a mnemonic rule, $\delta x_f = 0$ and $\delta t_f = 0$, rendering the master equation trivially satisfied without yielding extra algebraic equations. However, because the final state is explicitly fixed, the requirement $x(t_f) = x_f$ immediately supplies $n$ terminal equations. Combined with the $n$ initial conditions $x(t_0) = x_0$, there are exactly $2n$ equations to resolve all $2n$ constants of integration. 2. **Free Final Time and Fixed Final State ($t_f$ free, $x_f$ fixed):** This scenario arises frequently in minimum-time maneuvers where a robot must reach a specific configuration as fast as possible. Since $x_f$ is fixed, $\delta x_f = 0$, extinguishing the first term of the master equation. Conversely, because $t_f$ is unconstrained, $\delta t_f$ can take any arbitrary value. For the product $(H + \partial h / \partial t)\delta t_f = 0$ to hold for all admissible perturbations, the bracketed scalar term must vanish:

$$H + \frac{\partial h}{\partial t} = 0$$

This yields one scalar equation. Together with the $n$ equations from $x(t_0) = x_0$ and the $n$ equations from $x(t_f) = x_f$, there are $2n + 1$ equations in total. The additional equation is precisely what is needed to determine the unknown optimal final time $t_f$ alongside the $2n$ constants of integration.

[49:08](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2948s) Two-Point Boundary Value Problems (TPBVPs) and Numerical Solvers

Most engineering students primarily have experience with initial value problems (IVPs), where all boundary conditions (such as initial position and initial velocity) are prescribed at the starting time $t_0$. In optimal control, boundary conditions are split between the initial time and the final time, giving rise to two-point boundary value problems (TPBVPs). Pavone emphasizes that if standard differential equations seem difficult, solving a TPBVP is "difficulty on steroids," because one must find a continuous trajectory that satisfies the governing dynamic equations while simultaneously hitting constraints anchored at opposite temporal boundaries.

Fortunately, modern numerical solvers can tackle these problems: - In Python (the required language for the course problem sets), students use standard BVP solvers (such as SciPy's `scipy.integrate.solve_bvp`). - In MATLAB, a classic counterpart is `bvp4c`, though Pavone playfully chides the class that they should not be using MATLAB for their coursework.

[51:14](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=3074s) Setting Up a TPBVP Solver: ODEs and Residuals

To demonstrate how numerical BVP solvers operate, Pavone walks through a standalone mathematical example independent of optimal control. Consider the second-order system defined over $t \in [0, 4]$:

$$\dot{z}_1 = z_2$$ $$\dot{z}_2 = -|z_1|$$

subject to the split boundary conditions: $$z_1(0) = 0$$ $$z_1(4) = -2$$

Implementing this problem requires specifying two callback functions: 1. **The ODE Right-Hand Side Function:** Defines the state derivatives. Because Python uses 0-based indexing, state $z_1$ corresponds to `z[0]` and $z_2$ corresponds to `z[1]`. The return vector specifies $\dot{z}_1 = z_2$ as `z[1]` and $\dot{z}_2 = -|z_1|$ as `-np.abs(z[0])`. 2. **The Boundary Residuals Function:** Expresses how far the current candidate solution deviates from the target boundary constraints. Solvers require all boundary conditions to be written in residual form equaling zero ($g(z_a, z_b) = 0$). For this example, moving all terms to the left-hand side yields: - Initial residual at $t = 0$: $z_1(0) - 0 = z_1(0)$ - Terminal residual at $t = 4$: $z_1(4) - (-2) = z_1(4) + 2$

The solver iteratively adjusts the trajectory until these residuals vanish, demonstrating the standard operational pattern used later for optimal control TPBVPs.

[54:40](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=3280s) Setting Up and Solving a Two-Point Boundary Value Problem in Code

Boundary value problem (BVP) solvers rely on residual functions to check whether boundary conditions are met. Rather than accepting an explicit equality, the solver evaluates the residual expression to see if it equals zero. If the residual is nonzero, the solver applies an internal penalty that drives the numerical iterations toward a trajectory satisfying all specified boundary constraints. While this approach can feel counterintuitive at first glance, it is fundamentally just a mathematical device to tell the solver by how much a candidate solution currently violates the required boundaries.

Setting up the problem in code requires specifying a few key components: the total number of differential equations, the number of left-hand boundary conditions (the constraints at the initial time $t=0$), the domain of integration (such as an interval from $t = 0$ to $t = 4$), the ODE callback function encoding the system dynamics, and the residual boundary condition function. Because the solver knows the total number of differential equations, specifying the count of left-hand boundary conditions automatically determines the number of conditions remaining for the right-hand boundary. Users must also supply an initial guess function across the time domain. Typically, a constant function suffices—such as guessing that the first state $z_1(t) = 1$ and the second state $z_2(t) = 0$ everywhere, or simply setting everything to $(0,0)$ if no better intuition is available.

Plotting the solver's output confirms that the simulated states match the underlying physics and constraints. For instance, in a system where the derivative of $z_2$ is strictly negative (due to a negative absolute value term), $z_2$ decreases monotonically over the interval. The derivative of $z_1$ shares the same sign as $z_2$: as long as $z_2$ is positive, $z_1$ increases, and once $z_2$ drops below zero, $z_1$ turns around and decreases. Both boundary conditions—such as $z_1(0) = 0$ and $z_1(4) = -2$—are satisfied at the endpoints, demonstrating that the two-point boundary value problem has been successfully solved.

- The ODE solver setup requires the system order, left boundary condition count, integration domain, ODE callback, and residual callback. - Initial guesses do not need to be exact; simple constant functions like $(1, 0)$ or $(0, 0)$ across the domain give the solver a starting trajectory to deform. - A provided code template serves as the foundation for the corresponding problems on the student problem set.

[57:13](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=3433s) The Free Final Time Challenge and the Time Rescaling Trick

Standard numerical two-point boundary value solvers operate under the strict assumption that the final time of integration, $t_f$, is fixed and known beforehand. In optimal control, however, the final time is very frequently an open decision variable that must be optimized alongside the control inputs. When $t_f$ is free, one can write down the theoretical necessary conditions of optimality via Pontryagin's Minimum Principle, but off-the-shelf BVP solvers cannot be invoked directly because the right endpoint of the integration domain is unknown.

The resolution to this dilemma lies in a standard mathematical technique: time rescaling. A dimensionless time variable $\tau$ is defined by normalizing the physical time $t$ by the final time: $$\tau = \frac{t}{t_f}$$ Under this transformation, as physical time $t$ runs from $0$ to $t_f$, the rescaled time $\tau$ strictly runs from $0$ to $1$. Regardless of what $t_f$ turns out to be, the domain of integration with respect to $\tau$ is completely fixed and fully specified, making it compatible with standard BVP solvers.

Applying the chain rule allows all time derivatives to be expressed in terms of $\tau$. Viewing the state trajectory as $x(t) = x(\tau(t))$: $$\frac{dx}{dt} = \frac{dx}{d\tau} \frac{d\tau}{dt} = \frac{dx}{d\tau} \frac{1}{t_f} \implies \frac{dx}{d\tau} = t_f \frac{dx}{dt}$$ Because the scalar $t_f$ is constant across the entire trajectory but unknown, it is appended to the system as an additional state variable, $r(\tau) \equiv t_f$, governed by the trivial differential equation: $$\frac{dr}{d\tau} = 0$$ The numerical solver optimizes the constant value of $r$ so that the trajectory simultaneously satisfies the state dynamics and the transversality boundary condition derived from the Hamiltonian master equation.

```mermaid flowchart TD accTitle: Time Rescaling for Free Final Time BVPs accDescr: Diagram illustrating the conversion of a free final time problem on domain 0 to tf into a fixed domain 0 to 1 with an augmented state.

subgraph PhysicalDomain["Original Problem (Free Final Time)"] A["Physical Time: t in [0, tf]"] B["State Dynamics: dx/dt = f(x, u)"] C["Unknown domain length tf"] end

subgraph RescalingStep["Time Rescaling Transformation"] D["Define tau = t / tf"] E["Chain Rule: dx/d tau = tf * (dx/dt)"] F["Augment State: r(tau) = tf, r' = 0"] end

subgraph SolverDomain["Rescaled BVP (Standard Form)"] G["Fixed Domain: tau in [0, 1]"] H["Rescaled Dynamics: x' = r * f(x, u)"] I["Transversality BC fixes optimal r"] end

A --> D B --> E C --> F D --> G E --> H F --> I ```

[01:01:10](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=3670s) Motivating Example: Double Integrator with Free Final Time

To illustrate the complete workflow, consider a classic yet insightful optimal control problem: driving a double integrator from an initial displaced state to rest at the origin in minimum time while penalizing control effort. Physically, this represents a particle of unit mass moving along a line under an applied force $u(t)$ with momentum: $$\ddot{x}(t) = u(t)$$ The particle starts at position $x(0) = 10$ with zero initial velocity, $\dot{x}(0) = 0$. The objective is to bring the particle to rest exactly at the origin, meaning $x(t_f) = 0$ and $\dot{x}(t_f) = 0$.

The cost functional balances the total transfer time $t_f$ against control effort: $$J = \int_{0}^{t_f} \left( 1 + \frac{1}{2} b u(t)^2 \right) dt$$ Here, $t_f$ is free and subject to optimization, while the parameter $b > 0$ weights the quadratic control penalty against the passage of time.

To match standard first-order state-space notation, the second-order equation is rewritten as a system of two first-order differential equations by introducing state variables $x_1 = x$ and $x_2 = \dot{x}$: $$\dot{x}_1 = x_2$$ $$\dot{x}_2 = u$$ Because there are two state differential equations, two Lagrange multipliers (costate variables), $p_1$ and $p_2$, are required to construct the Hamiltonian.

- System initial conditions: $x_1(0) = 10$, $x_2(0) = 0$. - Target terminal conditions: $x_1(t_f) = 0$, $x_2(t_f) = 0$. - Cost running integrand: $g(x, u) = 1 + \frac{1}{2} b u^2$. - Control penalty weight: scalar $b$.

[01:03:25](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=3805s) Deriving Optimality Conditions and the Rescaled Two-Point BVP

The Hamiltonian for the system is formed by combining the running cost with the inner product of the costates and system dynamics: $$H(x, p, u) = 1 + \frac{1}{2} b u^2 + p_1 x_2 + p_2 u$$ Applying Pontryagin's Minimum Principle yields the necessary conditions for optimality: 1. State dynamics: $\dot{x}_1 = \frac{\partial H}{\partial p_1} = x_2$ and $\dot{x}_2 = \frac{\partial H}{\partial p_2} = u$. 2. Costate (adjoint) dynamics: $$\dot{p}_1 = -\frac{\partial H}{\partial x_1} = 0$$ $$\dot{p}_2 = -\frac{\partial H}{\partial x_2} = -p_1$$ 3. Control stationarity: $$\frac{\partial H}{\partial u} = b u + p_2 = 0 \implies u^*(t) = -\frac{p_2(t)}{b}$$

Substituting the optimal control $u^*$ into the state equations produces a coupled system of four differential equations: $\dot{x}_1 = x_2$, $\dot{x}_2 = -p_2 / b$, $\dot{p}_1 = 0$, and $\dot{p}_2 = -p_1$. To adapt this system for numerical solution when $t_f$ is free, the time rescaling $\tau = t / t_f$ is applied, using primes ($'$) to denote derivatives with respect to $\tau$, and introducing the fifth state variable $r(\tau) \equiv t_f$: $$x_1' = r \cdot x_2$$ $$x_2' = r \cdot \left( -\frac{p_2}{b} \right)$$ $$p_1' = 0$$ $$p_2' = -r \cdot p_1$$ $$r' = 0$$

Solving these five first-order differential equations over $\tau \in [0, 1]$ requires five boundary conditions. Four boundary conditions come directly from the physical initial and terminal states: - At $\tau = 0$: $x_1(0) = 10$ and $x_2(0) = 0$ (2 conditions on the left). - At $\tau = 1$: $x_1(1) = 0$ and $x_2(1) = 0$ (2 conditions on the right).

The fifth boundary condition determines $r$ (the final time $t_f$) and is obtained from the master transversality equation. Because final time $t_f$ is free and there is no terminal state cost function $\phi$, the Hamiltonian evaluated at the terminal time must vanish identically: $H(t_f) = 0$.

[01:09:29](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4169s) Free Final Time Boundary Conditions and Time Scaling Formulation

The discussion continues with finalizing the necessary conditions for the optimal control problem when the final time $t_f$ is free. To determine the terminal condition associated with $t_f$, the transversality equation involving the Hamiltonian is applied: the Hamiltonian evaluated at the terminal time plus the partial derivative of the terminal cost $h$ with respect to $t_f$ must equal zero:

$$H(t_f) + \frac{\partial h}{\partial t_f} = 0$$

Considering the terminal cost term involving time weighting, the derivative evaluates directly to $\alpha t_f$, yielding the boundary equation:

$$H(t_f) + \alpha t_f = 0$$

To handle the unknown final time numerically over a fixed solver horizon, the final time is treated as an auxiliary state variable, denoted by $r$, with trivial dynamics $\dot{r} = 0$. In the boundary condition, $t_f$ is replaced directly with $r$.

By applying a time-scaling transformation, the integration domain $[0, t_f]$ is mapped onto the normalized interval $[0, 1]$. Multiplying the system derivatives by the scaling factor $r$ results in a set of five coupled first-order ordinary differential equations governing the state variables $(x_1, x_2)$, the costate variables $(p_1, p_2)$, and the parameter state $r$. These five differential equations are paired with five boundary conditions (split between $t = 0$ and $t = 1$), forming a two-point boundary value problem (BVP) ready for numerical integration.

[01:10:44](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4244s) Code Implementation and Numerical vs. Analytical Results

Moving from the mathematical derivation to concrete code, Professor Pavone demonstrates how the boundary value problem is implemented programmatically. In the solver setup, the extended state vector is stacked as $y = [x_1, x_2, p_1, p_2, r]^T$. The optimal control input is determined algebraically via the optimality condition:

$$u = -\frac{1}{b} p_2$$

When coding the system dynamics, the standard right-hand side of the differential equations for both the states $\dot{x}$ and costates $\dot{p}$ is multiplied by the scaling factor $r$ (the fifth component of $y$), while the derivative for the dummy state is set to zero ($\dot{r} = 0$). The boundary conditions are assembled by enforcing the initial state conditions on the left endpoint ($t = 0$), the terminal state targets on the right endpoint ($t = 1$), and the transversality condition involving the Hamiltonian $H(1) + \alpha r = 0$.

```mermaid flowchart LR accTitle: Two-Point Boundary Value Problem Structure accDescr: Mapping of the five-dimensional state vector through scaled dynamics onto split boundary conditions. subgraph StateVector["Augmented State Vector y"] X["States: x1, x2"] P["Costates: p1, p2"] R["Time Parameter: r = tf"] end

subgraph Dynamics["Scaled ODEs on [0, 1]"] DX["dx/dtau = r * f(x, u)"] DP["dp/dtau = r * (-dH/dx)"] DR["dr/dtau = 0"] end

subgraph Boundary["5 Boundary Conditions"] BC0["Left (tau = 0): Initial States x(0)"] BC1["Right (tau = 1): Target States x(1) & Transversality H + alpha*r = 0"] end

StateVector --> Dynamics Dynamics --> Boundary ```

Professor Pavone explains that he ran the solver across various parametric combinations of the control penalty weight $b$ and the time weight $\alpha$. By fixing $b$ and increasing $\alpha$, the optimization places greater penalty on total duration relative to control energy expenditure ("alpha larger means that you care more about time than control effort"). As expected, increasing $\alpha$ causes the optimal arrival time $t_f$ to decrease monotonically.

- The specific benchmark problem has a closed-form analytical solution, allowing direct verification of numerical accuracy. - Plotted on screen, the analytical solution forms a continuous blue curve, while the numerical solutions obtained from the BVP solver appear as red dots lying directly on top of the analytical curve, demonstrating high fidelity. - Addressing a student question regarding the nature of constraints, Pavone clarifies that while classic optimization deals with algebraic constraints, dynamic trajectory optimization handles differential equation constraints via costate multipliers. Introducing equality-constrained optimization first helps make the differential equation approach "a little bit less esoteric."

[01:14:31](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4471s) Theoretical Equivalence: Direct Methods vs. Indirect Methods

In the closing minutes of the lecture, Professor Pavone bridges the conceptual divide between direct and indirect optimization methods. As established in the previous lecture, direct methods transcribe an infinite-dimensional continuous optimal control problem into a finite-dimensional nonlinear program (NLP) by discretizing the time horizon into $N$ points, parameterizing states and inputs ($x_i, u_i$), and approximating system differential equations and cost integrals numerically. In contrast, indirect methods rely on variational calculus and Pontryagin's Minimum Principle to construct continuous Euler-Lagrange or Hamiltonian boundary value problems.

```mermaid flowchart TD accTitle: Direct vs Indirect Method Convergence accDescr: Diagram illustrating how direct discretization converges to indirect optimality conditions in the limit. ContinuousProblem["Continuous Optimal Control Problem"]

subgraph DirectPath["Direct Route"] NLP["Transcribe to Finite NLP (N points)"] KKT["Derive KKT Necessary Conditions"] end

subgraph IndirectPath["Indirect Route"] PMP["Apply PMP / Calculus of Variations"] BVP["Continuous Boundary Value Problem"] end

ContinuousProblem -->|"Discretize time (step h_i)"| NLP NLP --> KKT ContinuousProblem -->|"Variational analysis"| PMP PMP --> BVP KKT -.->|"Limit as h_i -> 0 (Consistency)"| BVP ```

The fundamental connection between the two paradigms lies in their necessary optimality conditions: - When writing the Karush-Kuhn-Tucker (KKT) necessary conditions for the transcribed finite-dimensional NLP, the Lagrange multipliers associated with the discretized dynamics constraints correspond directly to the costate variables $p(t)$. - In the limit as the discretization step size $h_i$ approaches zero ($h_i \to 0$, $N \to \infty$), the discrete KKT necessary conditions converge exactly to the continuous necessary conditions of the indirect formulation. - This convergence serves as a formal consistency check. Far from being fundamentally disconnected philosophies, direct and indirect methods are "tightly coupled, as it should be because we're solving the same problem."

Professor Pavone clarifies the exam scope: while indirect methods and two-point boundary value formulations are testable material on Midterm 1, the formal mathematical proof of the convergence between direct KKT conditions and indirect Euler-Lagrange equations is included solely for intellectual completeness and "bonus information" in the lecture notes, and will not appear on problem sets or midterms.

Wrapping up the session, he previews the upcoming week, where trajectory generation transitions to constrained environments requiring obstacle avoidance, setting the stage for algorithmic motion planning.

## Notable quotes > [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) which might indeed result in disaster — Prof. Marco Pavone

> [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) first optimize, then discretize — Prof. Marco Pavone

> [04:04](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=244s) first discretize, then optimize — Prof. Marco Pavone

> [08:47](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=527s) hallmark of any optimal control problem — Prof. Marco Pavone

> [18:21](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1101s) You didn't know that one, eh? You thought he was French — Prof. Marco Pavone

> [18:21](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1101s) legacy of Italy, courtesy of Italy — Prof. Marco Pavone

> [18:21](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=1101s) hiding under the rug — Prof. Marco Pavone

> [49:08](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=2948s) difficulty on steroids — Prof. Marco Pavone

> [01:10:44](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4244s) alpha larger means that you care more about time than control effort — Prof. Marco Pavone

> [01:10:44](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4244s) a little bit less esoteric — Prof. Marco Pavone

> [01:14:31](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4471s) tightly coupled, as it should be because we're solving the same problem — Prof. Marco Pavone

> [01:14:31](https://www.youtube.com/watch?v=Izu4_AUiHyk&t=4471s) bonus information — Prof. Marco Pavone

## Mentioned Marco Pavone, Donald E. Kirk, Joseph-Louis Lagrange (Giuseppe Lodovico Lagrangia), Optimal Control Theory: An Introduction, Principles of Robot Autonomy, Stanford University, NVIDIA, NASA Jet Propulsion Laboratory, Institute for Computational and Mathematical Engineering (ICME), Python, SciPy, MATLAB, AA274A

## Video description For more information about Stanford’s graduate programs, visit: https://online.stanford.edu/graduate-education

Lecture 5 — Advanced Methods for Trajectory Optimization

Note: This lecture was recorded as part of the 2019 edition of the course. Course content, structure, assignments, and other materials may have changed since this recording. The course links below point to the most recent edition and may therefore differ from what is presented in the video.

Read the companion textbook, Principles of Robot Autonomy, free online: https://porabook.com

Follow along with the latest course schedule and syllabus: https://stanfordasl.github.io/PoRA-I

Speaker: Prof. Marco Pavone

Dr. Marco Pavone is an Associate Professor of Aeronautics and Astronautics at Stanford University and a Senior Director of Autonomous Vehicle Research at NVIDIA. Prior to joining Stanford, he was a Research Technologist in the Robotics Section at NASA’s Jet Propulsion Laboratory. Dr. Pavone received his Ph.D. in Aeronautics and Astronautics from the Massachusetts Institute of Technology in 2010. His research focuses on the analysis, design, and control of autonomous systems, with particular emphasis on self-driving cars, autonomous aerospace vehicles, and future mobility systems. He has been recognized with numerous awards, including the Presidential Early Career Award for Scientists and Engineers from President Barack Obama, the Office of Naval Research Young Investigator Award, the NSF CAREER Award, the NASA Early Career Faculty Award, the IEEE Kiyo Tomiyasu Award, the CSS Award for Technical Excellence in Aerospace Control, and the Early-Career Spotlight Award from the Robotics Science and Systems Foundation.

Instructor: Prof. Marco Pavone, Associate Professor of Aeronautics and Astronautics, Director of Autonomous Vehicle Research at NVIDIA

Full playlist: https://www.youtube.com/playlist?list=PLR9iNyexfIkk

#StanfordOnline #RobotAutonomy #Robotics #PhysicalAI

[Source](https://www.youtube.com/watch?v=Izu4_AUiHyk)

中文简报

斯坦福大学 AA274A 机器人自主原理 | 2019秋季 | 高级轨迹优化 在本讲座中,Marco Pavone 教授探讨了连续时间轨迹优化的间接方法,将“先优化后离散”的理念与直接非线性规划方法进行了对比。他从有限维拉格朗日乘子和一个几何圆示例切入,引入了连续时间哈密顿量、协态以及由此形成的两点边值问题(TPBVP)。随后,Pavone 在双积分器基准问题上演示了如何利用归一化时间重标度技巧来求解边界条件并处理自由终端时间问题。最后,他证明了直接法与间接法在结构上的一致性,指出在极限情况下 Karush-Kuhn-Tucker(KKT)条件会精确收敛至连续必要条件。 由于决策变量是不可数的控制轨迹 u(t),连续时间最优控制作用于无限维函数空间,这使得计算求解方案分化为间接法(“先优化后离散”)和直接法(“先离散后优化”)两种框架。 (4:04) 间接最优控制通过构建哈密顿量并引入连续协态函数 p(t),将动态优化问题转化为两点边值问题,把静态拉格朗日乘子理论直接推广至微分等式约束。 (32:46) 自由终端时间最优控制问题可通过在 [0, 1] 区间上进行无量纲时间归一化 tau = t / tf,并引入导数为零的常数参数状态 r = tf 扩充状态空间,从而适配现成的数值边值问题(BVP)求解器。 (57:13) 直接与间接轨迹优化在理论上具有一致性:随着时间离散步长趋近于零,离散 Karush-Kuhn-Tucker(KKT)条件精确收敛至连续的欧拉-拉格朗日方程和哈密顿边值条件。 (74:31)

Reading summary

Professor Marco Pavone frames robotic trajectory optimization within continuous-time optimal control theory, contrasting indirect variational derivations with direct numerical discretization. He demonstrates that indirect methods yield coupled two-point boundary value problems governed by the Hamiltonian and costates, which formally converge to direct nonlinear programming formulations in the continuous limit. Optimal Control and Function-Space Optimization (0:05) Trajectory optimization requires actively minimizing performance costs—such as elapsed transit time, actuator effort, or a weighted combination of both—subject to differential equations of motion and actuation limits. Pavone contrasts this with differential flatness, which he categorizes as a dynamic feasibility mapping technique rather than an arbitrary cost functional optimizer. Because optimal control optimizes a continuous control input history over time, it operates over an infinite-dimensional function space with uncountably many decision variables. Computationally, practitioners approach this infinite-dimensional problem through two distinct philosophies: indirect methods ('first optimize, then discretize') and direct methods ('first discretize, then optimize'). Pavone notes that while optimal control computes open-loop trajectories parameterized by time and initial state, physical robotics applications must ultimately combine these open-loop trajectories with feedback tracking controllers to prevent unmodeled disturbances and state drift from causing system failures. Extending Static Multipliers to the Dynamic Hamiltonian (32:46) To build mathematical intuition for indirect methods, Pavone draws a structural parallel to equality-constrained finite-dimensional optimization. In static nonlinear programming, constraints are absorbed into an augmented Lagrangian via Lagrange multipliers, where stationary points of the gradient identify candidate local extrema. In optimal control, the governing differential equations of motion act as continuous equality constraints. Consequently, the problem is augmented using the Hamiltonian, combining running stage costs with system dynamics scaled by time-varying costate variables. These costates serve as the dynamic counterpart to static Lagrange multipliers. Dimension counting confirms the mathematical consistency of the continuous necessary optimality conditions: for an n-dimensional state vector and m control inputs, Pontryagin's Minimum Principle yields n state differential equations, n costate differential equations, and m control stationarity conditions. This creates a system of 2n coupled first-order ordinary differential equations alongside m algebraic equations to solve for exactly 2n + m unknown functions of time. Transversality Conditions and Boundary Value Problems (42:09) Integrating the system's 2n first-order differential equations introduces 2n constants of integration that require boundary conditions. While initial system states supply n known conditions at the start time, the remaining conditions are derived from terminal variations via the master transversality boundary equation. This equation relates terminal cost derivatives, costates, the Hamiltonian, and admissible variations in final state and final time. Because the required constraints are split between the initial and terminal times, the necessary conditions form a Two-Point Boundary Value Problem (TPBVP) rather than a standard initial value problem. In numerical implementations—such as SciPy's boundary value problem solvers—practitioners specify the differential dynamics and a boundary residual function that measures candidate deviations from endpoint constraints, which the solver iteratively drives to zero. Time Rescaling for Free Final Time Optimization (57:13) Off-the-shelf boundary value problem solvers operate over a fixed, predetermined integration domain. However, minimum-time optimal control problems treat the final arrival time as an unconstrained decision variable. To resolve this mismatch, Pavone introduces a dimensionless time variable defined by physical time divided by the final time, mapping the integration domain onto a normalized interval from zero to one. Under this transformation, time derivatives are scaled by the unknown final horizon, which is incorporated as an auxiliary state variable with zero dynamic derivative. Pavone demonstrates this workflow on a displaced double integrator balancing duration against control effort. Evaluating the Hamiltonian transversality condition at the terminal boundary provides the necessary fifth constraint to solve for the four dynamic state-costate variables and the optimal transit duration simultaneously. Limiting Equivalence of Direct and Indirect Approaches (74:31) Pavone concludes by establishing the formal theoretical link between direct and indirect optimization. Direct methods convert infinite-dimensional continuous control problems into finite-dimensional nonlinear programs by discretizing time into finite intervals and enforcing dynamics at discrete collocation points, producing necessary optimality conditions governed by Karush-Kuhn-Tucker (KKT) multipliers. In the continuous limit as the discretization step size approaches zero and the number of nodes approaches infinity, the discrete KKT necessary conditions converge exactly to the continuous Euler-Lagrange equations and Hamiltonian boundary value conditions. The discrete Lagrange multipliers associated with system dynamics directly mirror the continuous costate functions, demonstrating that direct and indirect methods are mathematically consistent representations of the same underlying optimization problem. While indirect optimal control establishes exact analytical boundary value formulations for unconstrained state spaces, practical robotics requires extending these formulations to handle state inequalities, such as obstacle avoidance, which transitions trajectory optimization into algorithmic motion planning.

Source quotes

[4:04] Prof. Marco Pavone: which might indeed result in disaster [4:04] Prof. Marco Pavone: first optimize, then discretize [4:04] Prof. Marco Pavone: first discretize, then optimize [8:47] Prof. Marco Pavone: hallmark of any optimal control problem [18:21] Prof. Marco Pavone: You didn't know that one, eh? You thought he was French [18:21] Prof. Marco Pavone: legacy of Italy, courtesy of Italy [18:21] Prof. Marco Pavone: hiding under the rug [49:08] Prof. Marco Pavone: difficulty on steroids [70:44] Prof. Marco Pavone: alpha larger means that you care more about time than control effort [70:44] Prof. Marco Pavone: a little bit less esoteric [74:31] Prof. Marco Pavone: tightly coupled, as it should be because we're solving the same problem [74:31] Prof. Marco Pavone: bonus information

Original transcript

[0:05] All right, so I think we can get started. [0:07] Today, we are going to talk about advanced methods [0:10] for trajectory optimization. [0:13] So this is the last lecture on the topic [0:15] of trajectory optimization. [0:16] Next week, we'll look at a problem of motion planning [0:19] that in the language of this class [0:22] should be understood as a trajectory optimization [0:24] with a set of constraints that is with obstacles. [0:28] So today, what we're going to do is [0:30] to revisit the problem of trajectory planning [0:33] by casting it as an optimal control problem. [0:38] When we talked about differential flatness methods [0:41] for trajectory planning, what we said is that in that context, [0:45] we are interested in a feasible solution [0:49] with some nice properties. [0:51] But we're not trying to, in general, [0:54] optimize a cost function. [0:56] But in some cases, it's very important [0:58] to have a trajectory that not only is a good trajectory [1:01] but actually optimizes some cost function, [1:04] like, for example, minimizes time, minimizes control effort, [1:07] minimizes the combination of time and control effort, [1:10] and so on. [1:11] So this is really the topic of optimal control. [1:13] I teach a class on this topic in the spring. [1:16] Today, I'm going to share with you some key ideas. [1:20] And as in the style of this class, [1:22] I'm going to do a deep dive in a specific technique [1:26] by giving you some actionable insights [1:29] to solve some problems with the specific techniques. [1:34] In particular, we're going to focus the discussion [1:37] on indirect methods. [1:39] And toward the end of the class, I'm [1:41] going to shed some light on the connection [1:42] between indirect methods and direct methods [1:45] for optimal control. [1:48] The suggested reading for this lecture is this book by Kirk. [1:53] It's a little bit dated, but it's [1:54] a very accessible introduction to the field of optimal control. [1:58] The field of optimal control tends [1:59] to be extremely mathematically heavy. [2:02] It's a fairly sophisticated topic. [2:04] This book does a good job in being rigorous, [2:07] while at the same time being quite accessible. [2:11] All right, so as we said earlier this week, [2:15] the trajectory planning problem can [2:18] be cast as-- actually should be cast as an optimal control [2:21] problem, whereby the idea is that we are given [2:23] the equations of motion of our robotic system [2:27] in the form of a differential equation. [2:29] As usual, x is the state, u is the input. [2:32] The differential equation, for example, [2:34] could represent the equation of motion [2:37] of a unicycle, differential drive, simple car, and so on. [2:41] We are given input constraints in the form, for example, [2:45] of upper bounds on control authority, maximum speed, [2:49] maximum acceleration, maximum angular velocity, and so on. [2:55] We're possibly given the state constraints [2:57] in the form of obstacle avoidance. [2:59] But as we said, we're going to deal with obstacle avoidance [3:02] next week. [3:03] For this week, we consider the case [3:05] where we have no constraints. [3:06] Which means that the feasible set of states [3:08] is the entire Euclidean space. [3:11] And finally, we're given a cost function [3:13] that has an additive structure, terminal term plus a running [3:17] cost. [3:19] The integral term captures the notion of cost [3:22] that is accrued throughout the execution of the trajectory. [3:26] The terminal term captures the notion [3:29] of penalizing whether at the final time, tf, [3:34] we are in a position that is not exactly the position where [3:37] we want to be, for example. [3:42] So it's a very general formulation. [3:43] Usually, you're given the initial condition. [3:46] The task is to find a control history u that [3:53] with an associated state trajectory [3:55] x, obtained through the solution of differential [3:59] equation of motion, such that the cost is minimized. [4:04] Now, this is a complicated optimization problem [4:07] because we are optimizing over the space of functions. [4:11] Control input u is a function of time. [4:19] So you may be more familiar with optimization problem, where [4:21] you optimize over a set of finite decision variables, [4:25] say, x1, x2, x3. [4:26] Here, you are optimizing over a function, which, [4:29] if you think about, entails optimizing over [4:32] an uncountable set of decision variables, [4:36] each one for all possible value of t. t is a continuous number. [4:40] So you have one decision variable per t. [4:42] So you have uncountable number of decision variable. [4:45] So people refer to this class of problem [4:47] as infinite dimensional problems because the space of function [4:50] is infinite dimensional. [4:52] So that doesn't mean that this problem is infinitely more [4:55] complicated than standard optimization [4:57] but certainly is more complicated [4:59] than standard optimization. [5:01] So, as we discussed last time, the standard setup [5:06] for optimal control entails computing open loop control [5:13] trajectories, u, that only depend on time and possibly [5:17] the initial condition. [5:18] Remember that open loop means that your control action at time [5:24] t is the same, regardless of where [5:28] you are in the state space. [5:30] Which basically implies that if performance is nominal, [5:34] then you're doing the right thing. [5:36] But if, for whatever reason, due to model mismatches, [5:38] disturbances, and so on, you start drifting away, [5:41] you keep applying the same action [5:43] that you should have applied if performance [5:45] was nominal, which might indeed result in disaster. [5:49] And then we discuss why you want to augment open loop [5:52] control with the trajectory tracking and so on. [5:55] But for the sake of optimal control, [5:57] the standard setup is to optimize over [5:59] open loop function, meaning function of the time t. [6:02] Now, there are two main methods to do so. [6:04] First class of methods referred to as indirect methods, whereby [6:07] the idea is to first write down the necessary optimality [6:11] condition for the infinite dimensional optimization problem [6:16] that I presented in the previous slide [6:18] and then solve for those necessary optimality conditions. [6:24] The other approach, referred to as direct methods, [6:26] entails transcribing the problem into a finite dimensional [6:31] nonlinear optimization problem through discretization [6:34] and then solve for that optimization problem. [6:39] So to remember, indirect methods can be summarized [6:43] as we first optimize in terms of writing down [6:45] the necessary optimality conditions and then discretize. [6:48] Well, direct methods is the opposite. [6:50] We first discretize to do the transcription into a nonlinear [6:54] optimization problem and then we optimize. [6:57] So today we're going to discuss mostly indirect methods. [7:00] And then we are going to discuss direct methods, something [7:03] that actually we already discussed on Tuesday, [7:06] but in particular, in terms of what are the commonalities [7:10] and what is the link between these two classes of methods. [7:14] Any questions so far? [7:18] Yeah. [7:20] What is the methodology that you gave us [7:23] for differentially flat systems? [7:25] Which class will that fall under? [7:27] It doesn't fall under neither of those classes. [7:32] Philosophically, it's more related to direct methods [7:34] because we don't write down necessary optimality conditions. [7:38] But in reality, the point is that with differential flatness, [7:42] you are not trying to solve an optimal control problem. [7:45] You are trying to solve a feasibility problem, [7:47] whereby you just need to find a contraction control action, u, [7:52] that satisfy the differential equation [7:55] and additional constraints, like a initial condition and terminal [7:59] conditions and so on. [8:00] In reality, we said that we can also [8:02] include some notion of optimality [8:05] by setting up a cost function in the flat output [8:08] space, for example, in terms of Jack and snap. [8:10] But strictly speaking, differential flatness [8:12] looks more at the feasibility problem [8:14] rather than an optimality problem. [8:15] And we said that is more conceptually related [8:17] to direct methods because there is [8:19] no notion of writing down the necessary optimality condition. [8:23] The idea is to transform the problem [8:26] into a finite dimensional problem [8:28] or even into a linear system of equations, as we saw last time. [8:32] Does that make sense? [8:33] Yeah. [8:34] So if you will, it could be categorized [8:36] as a specific application of direct methods, where we are not [8:41] too strict in enforcing costing or optimizing [8:44] with respect to cost function. [8:47] So let's start with indirect methods. [8:51] So as I said, conceptually, indirect methods, [8:55] the procedures behind indirect methods [8:58] is fairly straightforward. [9:00] You should be familiar with the standard unconstrained [9:06] optimization setup, whereby you are given a nonlinear function f [9:10] of x. [9:12] x Is a vector that belongs to Rn. [9:16] So it's a vector with n components. [9:19] Each component is a real number. [9:22] You have a question? [9:24] All good? [9:26] All right. [9:27] And then you want to find the global minimum [9:30] or the global minima of this function, f. [9:34] So one way that you can solve this problem [9:36] is assuming that the function is differentiable. [9:41] You set the gradient of this function equal to 0. [9:45] That's a necessary condition of optimality [9:47] for unconstrained optimization. [9:49] You solve for those values of x, such that the gradient of f [9:53] is equal to 0. [9:54] And those are all candidates for optimality. [9:57] And then you pick the one that gives you the lowest cost. [10:01] So that's an example of approach whereby you first write down [10:06] the necessary optimality condition [10:08] and then you solve for them. [10:09] In the case of unconstrained nonlinear optimization, [10:12] the necessary optimality condition [10:13] is gradient of f equal to 0. [10:16] Solving for them entails solving a nonlinear algebraic equation. [10:22] With an indirect method for optimal [10:24] control is instantiated this framework. [10:27] In the same way, the only difference [10:30] is that of writing down the necessary optimality condition [10:34] for an optimal control problem is much more complicated. [10:39] I'm going to show you those necessary optimality conditions. [10:41] I'm not going to prove them. [10:43] But to justify them and to give you a little bit more [10:46] insight into what they mean, I want [10:48] to draw a parallel with the topic [10:52] of finite dimensional optimization. [10:54] But we want twist. [10:55] And the twist is that now we are also considering constraints. [11:01] So on top of minimizing a cost function f, [11:07] we have to consider a set of unconstrained m constraints [11:12] of the type h of x equals 0. [11:17] So what do you think this setup, finite dimensional optimization [11:22] means? [11:22] This x is a vector with n components. [11:25] So why do you think this setup is, in a way, [11:29] the finite dimensional equivalent and optimal control [11:31] problem? [11:32] What do we have to include constraints [11:34] to have something that is closer to an optimal control problem? [11:38] In other words, which is the equality constraint [11:42] that is the hallmark of any optimal control problem? [11:47] Every optimal control problem has an equality constraint. [11:49] Which constraint is that one? [11:54] Let's go back. [11:55] Well, here, there is only one equality constraint. [11:58] Any optimal control problem entails [12:02] finding controls and corresponding state that [12:05] satisfy the differential equation of motion. [12:08] Differential equation of motion is indeed an equality [12:10] constraint, is a differential equation, as opposed [12:13] to an algebraic equation, but still is an equality constraint. [12:18] So in terms of finite dimensional optimization, [12:20] equality constraint in the nonlinear optimization problem [12:24] are the class of problems that comes [12:25] closest to optimal control. [12:29] So in order to shed some light on how [12:32] indirect methods for optimal control problem work, [12:35] I'm going to tell you a little bit more [12:36] about the equality constraint nonlinear optimization problems. [12:40] And then I'm going to draw a parallelism [12:42] between the necessary optimality condition for equality [12:45] constrained optimization problem and necessary optimality [12:49] conditions for optimal control problems. [12:51] Yeah. [12:53] With constraints as a function equal to 0, [12:56] do you have some kind of constraint [12:57] that's not inequality? [12:59] For example, your velocity needs to be less than something, [13:02] how would you [INAUDIBLE]? [13:03] Yes, so of course, the marginal version [13:07] is a nonlinear optimization problem with equality [13:09] and inequality constraints. [13:11] It is possible to derive necessary optimality conditions [13:13] for those problems as well. [13:16] But that is beyond the scope of this class. [13:18] Here, for me, the reason why I'm going through the equality [13:21] constraint nonlinear optimization topic [13:26] is more from a pedagogical standpoint, [13:28] as opposed to teaching you how to solve general nonlinear [13:31] optimization problems. [13:32] But you are absolutely right. [13:33] In general, even for optimal control problems, [13:38] you might have a more sophisticated class [13:41] of constraints. [13:43] And that leads to more sophisticated, necessary [13:46] optimality conditions. [13:47] That, again, is beyond the scope of this class. [13:49] But if you're interested, I'll be happy to, of course, [13:51] talk to you about this, give you some readings. [13:53] And obviously, you can also take my class in the spring. [13:58] All right, so you may or you may not [14:00] familiar with how to write down the necessary optimality [14:03] conditions for equality constraint, [14:08] nonlinear optimization problems. [14:09] Just to make sure that everyone is on the same page, [14:12] I'm going to summarize to you what [14:15] the necessary conditions are. [14:17] So you know that if there are no constraints, [14:21] then the necessary optimality condition [14:23] is gradient of f is equal to 0. [14:26] The question is, what is now the necessary optimality conditions [14:30] for the case where we have equality constraints? [14:35] This boils down to a very elegant theory [14:38] in mathematics and optimization, referred to as Lagrange theory. [14:43] The bottom line is, define an augmented cost function. [14:49] So the idea is reasoning directly [14:52] in terms of these constraints, it is tricky. [14:55] So the idea is actually to move these constraints into the cost [15:02] function f and then optimize a function without constraints, [15:10] whereby we penalize the violation [15:13] of those constraints as well. [15:15] That's the idea. [15:16] So basically, including in the cost [15:18] function a penalty term that penalizes the violation [15:22] of equality constraints. [15:23] In doing so, we remove the constraints. [15:26] And we can then solve the problem [15:27] as an unconstrained optimization problem, for which we already [15:30] know how to write down a necessary optimality condition [15:33] in terms of gradient equal to 0. [15:37] Of course, there is a lot behind it. [15:40] But as I said, the idea is to set up an augmented cost [15:45] function x and lambda. [15:47] X are the previous decision variables. [15:50] L lambda are additional decision variables [15:54] defined as f of x plus the sum from i equal 1 to m of lambda i, [16:03] hix. [16:08] So this augmented cost function that now accounts [16:12] as an independent variables, both the decision variable x [16:16] and an additional vector of variables lambda-- [16:19] lambda 1, lambda 2, lambda m. [16:24] So this function is referred to as the Lagrangian. [16:37] So the main results in equality constraint non-linear [16:41] optimization problem is that a Necessary Optimality Condition, [16:46] NOC, for a point x star to be a minimum for the equality [17:00] constraint nonlinear optimization problem [17:02] is that the augmented cost function is a stationary point. [17:08] Which means that the gradient of the Lagrangian is equal to 0. [17:13] Which means that the partial derivative [17:14] of the Lagrangian with respect to x is equal to 0. [17:17] And the partial derivative of the Lagrangian with respect [17:19] to the lambdas is equal to 0. [17:25] Sorry, what is NOC [INAUDIBLE]? [17:27] Necessary Optimality Condition. [17:32] So here, of course, there is quite a bit of technical results [17:40] that I'm hiding under the rug. [17:42] But the main intuition, as I said, is as follows. [17:46] Solving directly a problem with the reasoning directly [17:49] in terms of the constraints is hard. [17:51] So the idea is to turn the problem [17:53] into an unconstrained optimization [17:55] problem, for which we already know [17:57] how to write down the necessary optimality condition. [17:59] And the way to do so is to move the constraints to the cost [18:03] function and penalize them through penalty [18:07] variables lambda i's referred to as Lagrange multipliers-- [18:15] Lagrange. [18:21] so something that is very important to note [18:24] is some of you may think that Lagrange is French. [18:27] It is he's not. [18:29] He was Italian. [18:33] So don't get mistaken by the fact [18:35] that-- so Lagrange was born Lagrangie. [18:43] He was born in the same city I'm from, Torino. [18:46] And at the age of about 40 moved to French. [18:50] And well, French people started calling him Lagrange. [18:53] That looks more French. [18:54] And now, he's known as Lagrange. [18:57] Everyone thinks he's French. [18:58] But he's Italian. [18:59] [LAUGHTER] So having said that-- you didn't know that one, eh? [19:02] You thought he was French. [19:04] Oh, I told you. [19:05] Yes, you see. [19:06] I told this to everyone. [19:08] [LAUGHTER] [19:08] OK, so having said that, so these legacy of Italy, [19:14] courtesy of Italy, all right. [19:15] So the idea is that now, we have this augmented cost function, [19:19] where we have a penalized constraint. [19:21] The magic of Lagrange optimality theory is that a point x star. [19:29] And this is the condition for a point [19:31] x star to be a minimum of the original equality constraint [19:35] and optimization problem. [19:36] A necessary condition is that it has [19:38] to be a minimum for the unconstrained optimization [19:41] problem with respect to an augmented cost function. [19:44] What is a little bit weird here is [19:47] that in order to compute the minimum for the augmented cost [19:51] function, we are solving, not only for x, but also [19:54] for the lambdas. [19:58] Another thing to consider here is [20:01] that when you take the derivative of the Lagrangian [20:05] with respect to the lambdas-- like, for example, [20:08] if you take the derivative of the Lagrangian with respect [20:10] to the i-th Lagrange multiplier, what do you get? [20:17] What's that? [20:20] Not 0. [20:21] If you take the derivative with respect [20:22] to the lambda i's, you get hi. [20:27] So this set of conditions basically [20:29] enforce that the point is feasible for the original [20:33] optimization problem. [20:34] Feasible means that it satisfies the equality constraints. [20:37] And the other part ensures that there is indeed a local minimum. [20:41] Practically, what do you do? [20:42] You set up the Lagrangian, you find a point of stationarity. [20:45] You solve for both the x's and the lambdas. [20:48] You then discard the lambdas. [20:50] Those were just auxiliary variables [20:51] that allowed you to crack the problem. [20:53] And then the x becomes a candidate [20:57] for optimality for the equality constraint problem. [21:01] Let's do an example just to clarify things. [21:05] By the way, here, I say a point x star is a local minimum. [21:13] On top of satisfying this equation, it has to be regular. [21:16] Regular means that, basically, the constraint gradients, [21:21] the gradients of the constraints have to be linearly independent. [21:23] So there is a mild technical condition for this to be true. [21:27] This is a little bit beyond the scope of this class [21:29] but just to give you a heads up that there is just [21:31] a mild condition for this to be true. [21:33] But again, here, my point is to give you [21:34] an intuition for the necessary optimality conditions [21:36] for optimal control problem, as opposed [21:39] to give a lecture on nonlinear optimization. [21:43] Let me give you an example. [21:45] So assume that you want to solve the problem, minimum [21:49] of x1 plus x2 subject to x1 squared plus x2 squared [21:56] equal to. [22:01] So geometrically, what does this mean? [22:06] So this y-axis-- oh no, this is the x2 axis. [22:13] This is the x1 axis. [22:18] This is the circle such that the x1 squared plus x2 [22:24] squared is equal to 2. [22:27] So this is the set of feasible points for the optimization [22:31] problem, meaning that the only points that can be considered [22:36] as a solution to that optimization problem [22:38] are those that lie on this circle. [22:41] That's what this equality constraint here means. [22:45] Now, among all those points, x1, x2, [22:47] we want to find those that minimize the linear combination. [22:55] In particular, from a geometric standpoint, [22:57] you can visualize the sum x1 plus x2 [23:02] as lines, which actually form a 45 degree angle. [23:09] So this will be x1 plus x2 equals c. [23:15] This will be x1 plus x2 equal. [23:19] What? [23:20] 0. [23:21] 0. [23:22] This would be x1 plus x2 equal minus c, [23:30] well, If it's more or less symmetric and so on. [23:33] So this basically are the level curves of the cost function [23:36] x1 plus x2. [23:37] So geometrically, if you were to find the minimum, [23:39] what would you do? [23:43] So C is greater than 0, which is greater than minus C. [23:47] So ideally, geometrically, what you want to do [23:50] is to move this line as much to the left as possible [23:54] but with the constraint that there [23:55] should be an intersection with the circle. [23:57] Because otherwise, you don't have [23:59] any feasible x1 plus x1, x2 for your optimization problem. [24:06] So you start from here, maybe here. [24:09] X1 plus x2 is equal to 10. [24:11] You start moving to the left. [24:13] Here, x1 plus x2 is equal to 0. [24:15] You keep moving to the left. [24:17] The more you move to the left, the more x1 plus x2 [24:19] is small until you reach a point of tangency here. [24:24] So by the symmetry of the problem, [24:27] it's not too hard to convince yourself that this is a minus 1, [24:31] minus 1. [24:33] So minus 1, minus 1 is indeed the solution to this nonlinear [24:37] optimization problem. [24:38] Well, let's see if we can find the solution [24:40] by applying the Lagrange multiplier theorem. [24:53] So what do we do to solve this problem? [24:55] Step number one, we form the Lagrangian. [24:59] The cost function is x1 plus x2. [25:07] This is f. [25:11] And to be consistent with our notation, [25:14] x1 squared plus x2 squared minus 2 equals 0. [25:19] So this is the function h. [25:22] The constraint function here, we only [25:24] have one equality constraint. [25:25] So i is equal to 1. [25:32] We have a number of Lagrange multipliers [25:34] that is equal to the number of equality constraints, one [25:37] Lagrange multiplier per constraint. [25:39] Each lambda I is a real. [25:45] So in this case, we have only one Lagrange multiplier [25:49] times the constraint, x1 squared plus x2 squared minus 2. [25:55] Two, we write down the necessary optimality condition [25:59] that, as I said, amount to setting [26:02] the gradient of the augmented function, [26:04] the Lagrangian function equal to 0. [26:06] Remember that now, the Lagrangian function [26:08] depends on both x and lambda? [26:09] So I have to take partial derivative with respect [26:11] to both x and lambda. [26:14] So partial derivative of lambda with respect to x1 [26:19] is equal to 1 plus 2 lambda x1. [26:25] And this has to be equal to 0 as a necessary optimality [26:28] condition. [26:29] Partial derivative of the Lagrangian with respect to x2 [26:33] is 1 plus 2 lambda x2 equal to 0. [26:37] Partial derivative of the Lagrangian [26:40] with respect to lambda 1, well, with respect to lambda [26:44] because we have only one constraint, [26:47] is equal to x1 squared plus x2 squared minus 2 equal to 0. [26:52] See how the partial derivative with respect to the Lagrange [26:55] multiplier gives you the equality constraint that [26:58] basically enforces that what you're solving for [27:01] is indeed a feasible point for the original equality constraint [27:04] optimization problem. [27:05] This is the link between the augmented problem [27:09] and the original problem. [27:11] OK, now, we have three unknowns-- x1, x2, [27:15] and lambda, three equations. [27:18] We solve for them. [27:19] The solution will be a candidate for optimality. [27:21] In particular, from this one, we see [27:25] that the x1 is equal to minus 1 over 2 lambda. [27:29] X2 is equal to 1 minus 1 over 2 lambda. [27:35] I plug this into here. [27:36] So 1 over 4 lambda squared plus 1 over 4 lambda [27:41] squared equal to? [27:44] So this implies that 1 over 2 lambda square is equal to 2. [27:58] So this means that lambda squared is equal to 1/4, [28:04] which means that lambda is equal to plus, minus 1/2. [28:15] So then I go back into these two equations, [28:19] I substitute for lambda. [28:21] So if lambda is equal to 1/2, then x1 is equal to minus 1, [28:31] and x2 is equal to minus 1. [28:34] If lambda is equal to minus 1/2, then x1 is equal to 1 and x2 is [28:42] equal to 1. [28:45] So these are the stationary points for the Lagrangians. [28:51] You throw away the Lagrange multiplier was just [28:54] a tool to get to the solution, but it's not [28:56] what you care about. [28:57] And the Lagrange multiplier actually [29:00] incorporates some interesting information [29:03] about the optimization problem, for example, [29:05] in terms of sensitivity. [29:07] But for the sake of this class, this [29:08] was just a tool to solve the problem. [29:10] We don't really care about it. [29:11] Throw it away-- so a candidate for optimality is minus 1, [29:14] minus 1. [29:15] And a candidate for optimality is 1, 1. [29:18] So then what you do, you go back to your cost function. [29:22] You plug in the first point, minus 1, minus 1. [29:25] We get the cost, minus 2. [29:28] You plug in the second candidate for optimality, 1, 1. [29:32] And you get the cost 2. [29:33] Since 2 is greater than minus 2, actually, [29:35] the minimum, in this case, the global minimum [29:38] is represented by this point. [29:45] Yeah. [29:46] What would happen if you plug-in those numbers [29:48] into Lagrange equation number [INAUDIBLE], the Lagrange? [29:54] Not the lambda, but the big L. If were to plug-in those-- [30:00] Yeah, so if you plug in, they're-- so the intuition [30:04] of the Lagrangian is that-- [30:06] I mean there are some more sophisticated interpretations, [30:09] but intuition, the Lagrangian is an augmented cost function, [30:11] whereby you penalize this constraint. [30:13] At the minimum, since you have a feasible point, this goes to 0. [30:16] And then you have the minimum for your function. [30:22] But for the sake of this class, just [30:23] think of it as basically an unconstrained penalized version [30:27] of your original equality constraint problem-- [30:31] so very important to understand that the procedure. [30:34] You first set up this generalized cost function [30:38] that makes the problem easier. [30:39] And that tells you what the solution of the original problem [30:45] is. [30:45] You write down the necessary optimality condition [30:48] for this augmented cost function and then you solve for them. [30:52] You throw away the Lagrange multipliers [30:54] that you use to augment the problem. [30:56] And you retain the information about the original decision [31:00] variables as your candidates for optimality. [31:04] is that enough to prove that this is the global minima. [31:07] In this case, yes. [31:10] Because since this is a continuous function [31:13] on a compact domain, so there is a minimum and maximum. [31:16] And we find that those are completely [31:20] characterized by the Lagrange stationary point. [31:25] Why wouldn't you try all the points on [INAUDIBLE]? [31:27] Yeah. [31:28] In this case, yeah. [31:29] But it's not always so. [31:30] For example, even in unconstrained optimization-- [31:41] so for example, think about this case. [31:50] So the points that-- [31:51] actually, to make it even simpler. [31:56] So the points that satisfy the gradient equal to 0 condition [32:00] are this point and this point here. [32:04] But indeed, that the global minimum is here. [32:09] So in order for the gradient equal to 0 condition [32:16] to be conditioned for necessary condition for local optimality, [32:19] it is important that the point is [32:22] in the interior of the domain. [32:24] Here, what went wrong is that it was at the boundary. [32:28] So there are some of those nuances. [32:29] But again, I don't want to give a lecture about nonlinear [32:32] optimization. [32:33] I just want to give you an example of some intuition [32:36] on nonlinear optimization to see how [32:39] to interpret the necessary optimality condition [32:42] for optimal control problems. [32:46] Cool! [32:47] Having said that, and here, of course, [32:50] I won't do the demonstration, but the idea of indirect methods [32:55] to solve optimal control problem is [32:58] very similar to the idea of solving [33:00] equality constraint linear optimization [33:03] problem on a finite dimensional set of variables. [33:07] In particular, the idea is to first construct [33:11] an augmented cost function, in the case of optimal control, [33:15] referred to as the Hamiltonian. [33:27] So the Hamiltonian has a similar meaning [33:30] of the Lagrangian function. [33:32] So it's given by your running cost [33:37] g, similar to what you had here with the f for equality [33:40] constraint optimization problem, plus p transpose of t [33:49] times a of x, u, t. [33:57] The function a is the one that was giving you the equality [33:59] constraint point p are additional variables [34:04] that play the same role of Lagrange multipliers. [34:12] But in optimal control, they are referred [34:17] to as the costate variables. [34:21] That is just terminology. [34:23] So in optimal control, typically, people [34:25] don't refer to these variables as Lagrange multipliers but as [34:28] costates. [34:30] Two, they are a function of time. [34:34] So the Lagrange multipliers or costate [34:38] itself are functions, Which makes sense. [34:43] Since the original set of variables or decision variables [34:50] were functions x and u, makes sense that Lagrange multipliers [34:53] also are functions. [34:58] So that would be step number one. [34:59] Step number two entails writing down the necessary optimality [35:03] condition. [35:04] In the case of optimal control problem, [35:06] the set of necessary optimality condition [35:09] is represented by a set of coupled differential [35:11] equations, ordinary differential equations. [35:16] So this is a little bit annoying because now, we [35:19] don't have a simple gradient of f equal to 0 conditions, [35:23] meaning that we have to solve nonlinear algebraic equations. [35:26] We actually have to solve differential equations. [35:30] But the idea will be the same. [35:32] We're given these equations. [35:34] I'm going to give an example. [35:36] We're given these equations. [35:37] We solve for the x, u, and p-- so for the original decision [35:41] variables, x and u and the Lagrange multipliers. [35:45] Then we throw away the Lagrange multipliers. [35:47] And what we get is a candidate for local optimality, [35:50] the same way that in Lagrange theorems, [35:52] we get candidates for local optimality. [35:59] Note the similarities. [36:03] First, the Hamiltonian looks extremely [36:06] similar to the Lagrangian. [36:07] One term is represented by the cost function. [36:14] And the other term is a penalization term [36:17] for the deviation from the equality constraint-- [36:21] in this case, the differential equations of motion, similarity [36:24] number one. [36:25] Similarity number two, one of the conditions, [36:32] in particular the x dot equal partial derivative h [36:37] with respect to p, gives rise to what? [36:41] If you take the partial derivative of the Hamiltonian [36:44] with respect to p, what do I get? [36:47] Partial constraint. [36:49] Yeah, So I get the a, x, u, t, which is really the differential [36:55] equation. [36:56] Same way that when I was taking the partial derivative [37:00] with respect to the Lagrange multiplier, [37:02] I was getting equality constraints. [37:05] That's neat. [37:08] Finally, in this setting, so a finite dimensional setting, [37:13] x is a vector with n components. [37:16] We are assuming that the decision [37:17] vector has n components. [37:20] Lambda is a vector with m components [37:22] because we're assuming we have m equality constraints. [37:25] So I have n plus m, decision variables. [37:32] And how many equations do I have? [37:35] So this equation gives me how many constraints? [37:42] 1.5 dimension. [37:43] Well, so n, this is what I have, n components. [37:47] And here, m. [37:49] So I have n plus m variables and n plus m equations. [37:54] Which means that I have a number of equations that is sufficient [37:56] in order to solve for all the variables, which is great. [38:00] What about here? [38:01] So here, I have here, n differential equation, [38:07] 1 per component of the state vector. [38:12] Here, I have how many equations? [38:18] How many costate function or Lagrange multiplier function [38:24] do I have? [38:28] Remember, one equality constraint, [38:31] since I have one equality constraint, [38:32] a per state variable, and I have n state variables? [38:41] Well, another way to see this is that I'm [38:44] taking the partial derivative with respect to x. [38:47] And x is n variables. [38:48] So what do I get as an output? [38:50] This is a vector, a scalar and matrix. [38:52] It's n. [38:54] n, so n differential equation from here [38:57] and differential equation from here, [38:59] I'm assuming that I have m inputs as usual. [39:02] So I have m algebraic equations from the last one. [39:06] Which means that I have 2n plus m equations 2n of them are ODEs, [39:15] ordinary Differential Equations, and the other M are algebraic. [39:23] How many decision variables do I have? [39:26] I have n variables because we're saying that the state vector is [39:34] n dimensional. [39:35] We have one Lagrange multiplier per constraint. [39:38] And I have n constraint because the differential equation [39:41] is n dimensional. [39:42] So P is n dimensional as well. [39:45] And I have n control. [39:46] So I have 2n plus m functions to solve for. [39:54] 2n plus m functions to solve for and 2n plus m equations, good. [39:58] Actually, I have a number of equations that is sufficient [40:01] in order to pinpoint all the decision variables [40:06] and Lagrange multipliers. [40:10] The known thing about this necessary optimality condition [40:14] is that these are differential equations. [40:17] So to solve for them, I need also boundary conditions. [40:22] We know that to solve differential equations, [40:27] I mean you need to have initial condition that the pinpoint, [40:30] the constants of integration. [40:32] In particular, how many constants of integration [40:35] do I have here? [40:40] I'm going to call out every ICME student here to answer. [40:43] I know there is one ICME student. [40:46] So roughly speaking, for each first order differential [40:50] equation, you have one constant of integration. [40:55] So for example, if I have the equation x dot equal x, [41:05] what's the solution? [41:09] [INAUDIBLE] x. [41:12] Well, yes, but then, of course, I also [41:14] have a constant in front, which depends [41:16] on the initial condition. [41:18] Let's say that the initial condition is x0 is equal to 0. [41:21] In that case, what is a equal to? [41:25] [INAUDIBLE] [41:26] 1. [41:27] Not 1. [41:28] Because if you set a is 0, this is equal a. [41:32] And this is equal to x of 0 that we're saying is equal to 0. [41:36] So now, what am I saying? [41:39] Yeah, OK, yeah. [41:42] All right, so here, you have n constants of integration. [41:51] So n of them come from the initial boundary condition, x, [41:54] t0 equal to t0. [42:09] The second one comes from this more convoluted expression-- [42:15] don't read too much into it, use it more as [42:18] a tool to solve these problems. [42:20] Here, delta xf captures the notion of admissible variation [42:27] at the final endpoint. [42:28] Whether we want to reach a specific point at time tf [42:32] or actually that is free, part of the optimization. [42:35] Delta tf captures the admissible variation [42:39] with respect to the final time whether the final time is fixed, [42:42] meaning I want to get somewhere in 10 seconds [42:44] or the final time is free, meaning [42:46] that I want to get somewhere, as soon as possible, [42:49] but I'm not specifying the final time. [42:55] Which basically means that assuming [42:57] that we can solve the set of differential equation, [43:00] in order to do so, we have a differential equation, [43:02] but we had to set up the boundary conditions correctly. [43:06] Actually, I'll skip this one. [43:07] This is the last annoying slide before doing the fun stuff. [43:11] So in particular, the way to interpret this slide [43:16] is as follows. [43:23] So small h here is the terminal cost. [43:31] So this reads as delta h partial derivative with respect [43:38] to x minus p delta xf plus-- of course, [43:46] I'm omitting the other independent variables [43:50] for brevity-- plus the Hamiltonian [43:56] plus the partial derivative of the final cost with respect [44:01] to time. [44:05] Delta tf has to be equal to 0. [44:08] So that's the master equation. [44:11] So now, the task is, how do I find the boundary conditions [44:16] in such a way that I can pinpoint [44:17] all constants of integration? [44:19] In all cases, we are assuming that we are [44:23] fixing the initial conditions. [44:24] So we already have n conditions. [44:26] But we need n more because we have a 2n differential [44:31] equations, which means that 2n constant of integration. [44:35] So let's see how to use that master equation in order [44:39] to pinpoint those boundary conditions. [44:42] In particular, assume as-- [44:52] yeah, let's consider this case first. [44:54] I think it's the easiest. [44:55] So assume that tf-- [44:58] OK, so the easiest is assume that the tf is fixed [45:04] and xf is fixed. [45:07] So the final time and final conditions are specified. [45:10] I want to get somewhere specifically at final time tf [45:14] equals 10, say. [45:16] In that case, how do I make substitutions for the delta xf [45:20] and delta tf? [45:21] This, again, has the interpretation [45:24] of admissible variation. [45:30] So if both the final time and the final state are fixed, [45:34] is there any admissible variation [45:37] for the final time and final state? [45:40] No, since they're fixed, every variation is inadmissible. [45:44] Which means that delta xf has to be considered equal to 0. [45:47] Delta tf has to be equal to 0. [45:48] This is more a mnemonic thing rather than a conceptual thing. [45:51] It means that this equation tells you nothing. [45:53] But on the other hand, if xf is fixed on top of the condition x, [45:58] t0 is equal to 0, we also have the condition x, tf equal xf. [46:02] And this is another set of n equations. [46:05] So in total, I have 2n equations that allow me to pinpoint the 2n [46:10] constant of integration. [46:14] So that was an easy case. [46:16] A slightly more complicated case is the case when tf is free-- [46:22] this is a very typical case by the way-- [46:25] and xf is fixed. [46:28] So you want to get to a final condition as soon as possible. [46:34] But a specific final time is not specified. [46:37] You want to minimize that time. [46:39] So actually, time becomes part of the optimization. [46:42] So in this case, we still have x, t0 equal to x0. [47:00] So tf is now free. [47:02] So since xf is fixed, what is the admissible variation for xf? [47:07] So this has to be 0. [47:09] So this term goes away. [47:11] But delta tf is free, which means that it [47:14] can take any possible value. [47:16] So for this equation to be satisfied [47:19] for every possible value of delta tf, [47:21] what should this term be equal to? [47:23] [INAUDIBLE] to 0. [47:25] This has to be equal to 0. [47:26] So this master equation is implying [47:29] that h plus partial derivative of a small h with respect [47:35] to time has is to be equal to 0. [47:40] And then we have the condition x, tf is equal to xf. [47:45] So I have n equations from here, n equations from here, [47:49] and one equation from here. [47:51] So in this case, we have a 2n plus 1 equations for 2n constant [47:57] of integration. [47:59] So why do we think we need an additional equation [48:02] to solve for everything? [48:04] So what do we have here that we have additionally [48:07] to determine that we did not have here? [48:14] We have the time is free. [48:16] So on top of the 2n constant of integration, [48:20] I need an additional equation that allows me to pinpoint [48:23] the optimal final time, which is given by this one. [48:28] So everything come together. [48:30] So at this point, you should understand the trick. [48:33] So when you want to break down the boundary conditions, [48:37] usually, you have an initial condition. [48:41] And then you go into the master equations [48:44] and you make the appropriate substitutions for delta xf [48:46] and delta tf, depending on whether the final condition [48:50] for the state and/or the final condition for the time [48:52] are free or not. [48:55] Can you remind me what little h is again? [48:57] Little h is the terminal cost for your optimal control [49:01] problem. [49:04] OK, so I'm going to do an example that [49:06] should clarify all of this. [49:08] But before we do that, something to consider [49:12] is that in your experience, you might [49:15] have been exposed primarily to initial value problems, [49:19] whereby you have differential equations, whereby [49:22] all the boundary conditions are enforced at the initial time, [49:29] so on one side-- [49:31] for example, initial position and initial velocity. [49:35] In the solution to the necessary optimality condition [49:40] for optimal control problem, you have some boundary conditions [49:44] that are specified at initial time [49:47] and some boundary conditions that are [49:48] satisfied at the final time. [49:52] So this class of problems are referred to [49:55] as the notorious two-point boundary value problems, [50:01] much more complicated than initial value problem. [50:03] So if you thought that the differential equations were [50:06] difficult, this is difficulty on steroids. [50:10] The difficulties, now, you have to find a function that [50:15] not only satisfy the differential equation, [50:17] but satisfies constraints both at initial time and final time. [50:21] So if you think a little bit, that is much trickier to do. [50:25] It is tricky. [50:27] Fortunately, there are some solvers out there to do so. [50:31] I'm going to give you an example in Python [50:34] that you're going to use for your last problem in the P set. [50:39] And the solver is referred to as CVP solver. [50:42] The CVP solver is a handy function in Matlab [50:45] that allows you to solve two-point boundary value [50:48] problem of this form, whereby you have a boundary [50:50] condition and initial time and the final time, exactly what we [50:53] have for optimal control problems. [50:55] If you're a matter of user, which you should not, [50:57] because you're taking this class, [50:59] there is also a boundary value problem solver [51:01] model called BVP4C. [51:04] There are a lot of interesting aspects [51:06] of numerical optimization behind them. [51:09] But again, in the sake of this class, [51:10] I want to give you the operational insights [51:13] about these tools. [51:14] So let's consider a simple example. [51:16] So this example has nothing to do with optimal control. [51:18] It's just an example to show you how [51:20] to solve a two-point boundary value problem. [51:23] I'll discuss it later, how to apply a two-point boundary value [51:26] problem solver to optimal control problem. [51:30] So here, I have a differential equation [51:31] whereby z dot 1 is equal to z2. [51:34] Z dot 2 is equal to minus absolute value of z1. [51:37] z1 is equal to 0. [51:38] And z1, 4 is equal to minus 2. [51:41] So the initial time is 0. [51:42] The final time is 4. [51:44] I have a boundary condition at initial time [51:48] and a boundary condition at the final time. [51:55] All right, so that's how the solver works. [52:02] And it is indeed pretty easy. [52:06] So first of all, you install it. [52:09] And we provide a link in the PC to do so. [52:12] So the way it works is that you have [52:14] to specify a couple of things and then it [52:16] works, in general, pretty well. [52:17] So the first thing that you specify [52:20] is the callback function that describes your-- [52:25] actually, let me write here. [52:36] So we have z dot 1 is equal to z2. [52:45] Z dot 2 is equal to minus z1. [52:53] Z1, 0, 0-- z1, 0 is equal to 0. [52:57] Z1 at 4 is equal to minus 2. [53:05] So the first thing that you do is [53:10] to set up the callback function that [53:13] specify the right-hand side of the differential equation. [53:16] So basically, you tell the solver what [53:18] the differential equation is. [53:19] In particular, the first element of the differential equation [53:26] is z2. [53:26] Remember that in Python the counter starts from 0? [53:29] So z2 here is z1. [53:32] And the second component of the differential equation [53:36] is minus absolute value of z1, which is 0. [53:41] Because again, in Python the counter starts from 0. [53:44] So that's a differential equation. [53:46] The second thing to set up is the function [53:51] that specifies the residuals of the boundary conditions. [53:55] The residuals means by how much your current boundary [54:00] values are not fulfilling the boundary conditions. [54:03] So basically, this is an error term [54:05] that the solver uses in order to eventually find [54:08] the solution to the two-point variable problem. [54:11] So in particular, the residual for the first boundary condition [54:16] is simply the value of z1 evaluated at 0 here. [54:27] And M second residual is z1, 4 plus 2. [54:34] So basically, you take everything [54:36] to the left-hand side. [54:38] What you get should be equal to 0. [54:40] What the solver wants to know is if that is actually [54:43] 0 or something else. [54:44] If it is something else, then it's [54:45] going to penalize it so that you get to a solution that satisfies [54:48] the boundary conditions. [54:50] Does this make sense? [54:52] This is for this solver, just the thing that [54:54] is a little bit less intuitive. [54:56] But if you think about, it's just [54:57] a way to specify by how much you're violating the boundary [55:00] conditions. [55:01] Once you do that, then setting up the problem is easy. [55:04] You define the problem by telling the solver [55:06] how many differential equations we have. [55:08] I'm going to pause this code so that you [55:10] have a template that you can use for your P set. [55:14] You specify the number of left boundary conditions, [55:17] so the number of conditions at initial time. [55:26] You thought it was over, but it was not. [55:28] So left boundary conditions, of course, [55:30] since you know that we have two differential equations, if you [55:33] have a one on the left, it means that you have one on the right. [55:36] The domain of integration between time [55:38] equals 0 at time equal 4. [55:40] What is the OD function, the callback function [55:42] for the differential equation? [55:44] And finally, what are the boundary conditions [55:46] in terms of the residuals? [55:48] The last step is to specify the initial guess, typically [55:53] a constant function-- so something [55:55] that you give the solver to help the solver. [55:58] In this case, we're saying that the guess [56:00] is that the first variable is equal to 1 throughout. [56:04] And the second variable z2 is equal to 0 throughout-- [56:09] just give something sensible, 0, 0, if you have no idea. [56:13] And then you call the solver. [56:17] And that's it. [56:19] So you do that over here. [56:26] So here, we have z1 and z2. [56:29] We see that, for example, yeah, z2, [56:37] its derivative is always negative [56:40] because it's minus absolute value. [56:42] So as you can see, it goes down. [56:44] For z1, the derivative has the same sign of z2. [56:48] So as long as z2 is positive, z1 is going up. [56:51] And when z2 is negative, then z1 goes down. [56:55] So the differential equation, at least from intuition standpoint, [56:58] is satisfied. [56:59] Boundary condition, z1, 0 is equal to 0. [57:03] And z1, 4 is equal to minus 2. [57:06] So this is to show that, actually, we [57:08] have solved the two-point variable problem-- [57:10] so pretty handy tool. [57:13] As it is, that's useful for optimal control becomes useful [57:19] once we apply it to the nested optimality condition. [57:21] Before doing so-- I already got to the key point [57:24] of this lecture-- we need to do one more thing. [57:28] The thing is that if you sit here, [57:34] this solver and pretty much any solver [57:36] out there assume that you know the final time. [57:41] By an optimal control, oftentimes, the final time [57:44] is up to optimization. [57:48] So what do you do? [57:51] What you do is that you look back to your slides and lectures [57:55] or video recordings and then you remember that there [57:58] is a very nice article that is basically a bunch of tricks [58:03] to take your two-point multivariable [58:05] problem with weird features, like, for example, [58:08] the final time is not specified, and so on. [58:11] And there are a bunch of tricks to recast the two-point boundary [58:15] value problem in standard form, meaning in this form, [58:19] meaning in a form where now you can call a solver. [58:23] So there are a number of tricks. [58:25] The most important one is the one [58:27] that allows you to handle a final time that is not specified [58:32] and up to optimization. [58:35] And that trick is, again, related to the notion [58:39] of time rescaling. [58:42] And the idea is fairly simple. [58:46] The problem is that assume that you have an optimal control [58:51] problem whereby tf is free. [58:53] So you write down a necessary optimality condition. [58:56] But once you try to call the solver, tf is free. [58:58] So you cannot really apply it directly. [59:03] So then you do time rescaling. [59:04] Then you introduce a new time, tau, is [59:07] equal to the true time over tf. [59:13] You do know tf. [59:14] But you know that at some point, you're [59:16] going to get at the end of the operation. [59:18] So when t is equal to tf, t-tau is equal to 1. [59:21] So with respect to tau, now, the domain of integration [59:26] is fully specified is between 0 and 1. [59:29] So now, what you do is to view x of t, as x of tau [59:38] t similar to what we did on Wednesday. [59:41] And then the derivative of x of t with respect to t [59:46] is equal to the derivative of x with respect to tau times [59:50] the derivative of tau with respect to t, [59:56] which is equal to 1 over tf. [59:59] Which means that the derivative with respect to tau [60:03] is equal to tf derivative with respect to t. [60:09] So then the idea is to introduce a time scaling. [60:15] tf now just becomes-- so we express [60:18] all the derivatives with respect to the new time, tau. [60:22] Tau is, regardless of the specific value of tf, [60:26] is always between 0 and 1. [60:29] You replace all the derivatives with respect [60:30] to t as derivative with respect to tau [60:34] by simply multiplying by tf. [60:36] Now, tf is a constant. [60:37] You don't know it. [60:38] It's something that you're solving for. [60:41] So you add a state variable with trivial dynamics, [60:45] r dot equal to 0, where r is tf basically. [60:52] And then the solver will pick up the right value of tf [60:55] in order to satisfy the boundary condition that [60:58] come from the master equation that I did. [61:02] Here, by solving the master equation, [61:04] I'll give you an example that makes this very clear. [61:07] And that's something that you have to do for your PC. [61:10] So let's assume we want to solve this optimal control problem. [61:13] It is an important one. [61:14] It's simple. [61:15] But it's actually an insightful one. [61:31] So x double dot is equal to u. [61:35] So I have a double integrator-- [61:38] so think of a particle on a line that has momentum. [61:46] So x double dot is equal to u. x0 is equal to 10. [61:52] x dot 0 is equal to 0. [61:58] X, tf is equal to 0. [62:02] And x dot tf is equal to 0. [62:08] So the idea is that you start-- so let's say that this [62:11] is the origin of your line. [62:13] So you start here, a coordinate at 10 with velocity 0. [62:19] And you want to get here. [62:23] But you want to do so in a way whereby you minimize [62:29] the time plus control effort. [62:44] So the reason why this is an interesting problem [62:47] is that you have dynamics. [62:49] It's a double integrator. [62:50] And you are considering a cost function [62:52] that is very common one-- a combination of time [62:57] and controlled effort, here represented as a U square. [63:00] tf is free. [63:03] And you are trying to optimize it. [63:05] So now, the task is we have this problem, [63:08] how do we use everything we learned so far to write down [63:10] the necessary optimality conditions to set up a two-point [63:13] [INAUDIBLE] problem that I can solve? [63:15] And then I just call the solver, and I'm [63:18] done, which is, by the way, what you [63:19] have to do for the problem in your last problem in the P set. [63:25] So let's do that. [63:27] So you had to be methodical. [63:29] So the first step is you write down the Hamiltonian. [63:33] So the Hamiltonian here is equal to what? [63:38] G. [63:39] G. So g is 1/2 bu squared plus the term penalizing [63:47] the dynamics. [63:49] Let me be very specific. [63:51] So x double dot is equal to u. [63:53] First of all, we had to put the problem in the same notation [63:57] of the optimal control problem. [63:58] Remember, in the optimal control problem, [64:00] we have a first order differential equation. [64:03] So what we do is a standard trick. [64:05] We denote by x1 is equal to x. [64:12] Then x2 is equal to x dot. [64:16] So I can express, equivalently, the dynamics as x1 equal to x2 [64:21] and x dot 2 is equal to u. [64:24] Do you know this trick to go from first order differential [64:26] equation-- sorry, second order differential equation [64:29] to a system of two first order differential equation. [64:33] So yeah, this is just an equivalence. [64:36] So how many differential equations do I have? [64:38] 2. [64:39] How many Lagrange multipliers I'm going to have? [64:42] 2, 1 per differential equation. [64:45] So this is a plus p1 times x2 plus p2 times u. [64:53] That's the first component of differential equation. [64:55] That's the second component of differential equation. [64:59] Good. [65:03] So we have set up the Lagrangian-- oh, sorry, [65:05] Hamiltonian. [65:07] We have to take the partial derivative. [65:10] So partial derivative of h with respect-- [65:14] so what are the necessary optimality conditions? [65:20] Here. [65:22] We have partial derivative h with respect to p. [65:24] We will get exact differential equation. [65:26] So I'm not going to write them again. [65:29] The interesting one is partial. [65:33] pidot. [65:37] One is equal to minus partial derivative of h [65:40] with respect to x1. [65:43] So-- go here, derivative with respect to x1 is 0. [65:47] Here, I don't have any dependency with respect to x1. [65:50] So this is 0 is equal to minus partial derivative h [65:55] relative to x2-- [65:58] 0, 0, 1. [66:02] So this is equal to minus p1. [66:07] And then you have the third one, the partial derivative [66:10] of h with respect to u, which is equal to bu plus p2. [66:24] This has to be equal to 0. [66:26] Which means that u is equal to minus p2 over b. [66:36] And now, you're pretty much done. [66:38] So you have the set of differential equation [66:42] x dot 1 equal to x2, x dot 2 equal to u. [66:49] But u is equal to minus p2 over b. [66:54] p dot 1 is equal to 0. [66:58] p dot 2 is equal to minus p1. [67:04] So that's your set of differential equation [67:05] you want to solve for. [67:07] But the final time is free. [67:10] So now, you cannot directly call the two-point boundary value [67:13] solver. [67:14] So you have to do the time rescaling trick. [67:17] Which means that now, you rescale with respect [67:19] to time tau. [67:21] Which means let's denote the derivative with respect [67:29] to tau as prime instead of dot. [67:34] All of this becomes derivative with respect to tau. [67:39] But when I take the derivative with respect to tau, [67:42] I have to multiply by tf, as we showed before, for consistency. [67:52] In order to represent everything in the same formalism [67:56] that the BVP solver wants, we add an additional variable r [68:00] that represents tf. [68:01] We give it the trivial dynamic 0. [68:05] So that will be r. [68:06] That will be r. [68:08] That will be r. [68:12] And now, I have to write down the boundary conditions. [68:16] So I have four boundary conditions already. [68:27] Yeah, I'll leave this one. [68:33] So what are the boundary conditions? [68:37] So I have x0 equal 10, x dot 0, so x2, equals 0. [68:53] And x, tf is equal to 0. [68:58] So x of tf is equal to 0. [69:07] And then x dot tf is equal to 0. [69:13] I still needed to write down the additional boundary [69:21] condition with respect to time. [69:22] I go into the master equation. [69:26] I don't have a dimensional variation with respect to x. [69:29] So the only equation that we have to satisfy is h. [69:36] So the Lagrangian plus the partial derivative of a small h [69:39] with respect to t, the t. [69:41] So what's the derivative of this with respect to if you view h [69:45] as a function of t. [69:46] What's the derivative of t? [69:48] [INAUDIBLE] [69:50] It's alpha tf, yeah. [69:53] Plus alpha tf equals 0. [70:10] Yeah, so now, r is a variable with the trivial dynamics. [70:19] What you do is in this equation, I just substitute the tf with r. [70:25] And now, I have a set of five differential equations with five [70:30] boundary conditions and the domain [70:32] integration between 0 and 1 because I've [70:34] done the time scaling. [70:37] Here, I've written the differential equation [70:39] in a more compact form-- [70:40] just to verify what if I find, this is straightforward. [70:44] But let me show you the code. [70:52] Yeah, so I went ahead and I implemented this stuff. [70:54] So u is equal to minus 1/b here times a p2. [71:04] I called the vector z is x1, x2, p1, p2, r. [71:13] So this is saying that the u-- oh, no, is y, sorry. [71:27] Y. So u is equal to minus 1 over y3. [71:32] This is y3, probably. [71:36] Consider. [71:38] This is Y3. [71:40] y3 And so we are encoding that one. [71:46] And so x dot, which will be x1, x [71:49] dot x2 dot is equal to y4, which is this one, times the dynamics. [71:57] You can easily verify the dynamics, [71:58] what I want to show that indeed I'm multiplying by the time [72:02] scaling factor. [72:04] And then, basically, encoding the dynamics [72:08] also for p and then the dynamics for the dummy state that [72:12] represents the final time tf. [72:16] So just take your time to go through this code, [72:19] but that's pretty much it. [72:20] So once you understand this, you can fairly easily [72:23] solve a large variety of optimal control problems [72:26] by applying these techniques. [72:32] The other thing would be just to set up the boundary conditions. [72:35] This is exactly what we have there. [72:37] We have the one on the left, the ones on the right. [72:40] And the weird one will be this one here, the one [72:45] that entails the Hamiltonian. [72:48] So what it did was to-- [72:57] what they did was to solve this problem for different values [73:01] of B and alpha. [73:07] So I keep B fixed and I change alpha. [73:11] Changing alpha means that, basically, you [73:12] give more weight on the fact that they are taking longer. [73:17] So alpha larger means that you care more about time [73:20] than control effort. [73:22] So I solve the problem for several combinations [73:24] of parameters. [73:26] And you can see that, as alpha is increased, [73:32] the final time, so the time I get to the destination, [73:35] decreases. [73:36] Blue is the analytical curve in terms of this problem [73:41] can be solved analytically. [73:42] You don't need numerical methods. [73:44] The red dots are the result of the numerical integration-- [73:48] so to show you that actually this method is quite accurate. [73:52] I'm going to pause this code as well. [73:57] So any questions here? [73:59] Yeah, so the [INAUDIBLE] method and the Hamilton method [74:04] is a type of constraint? [74:06] That's right. [74:07] And yeah, which in particular entails [74:08] solving differential equations, as opposed [74:10] to algebraic equations. [74:11] Yeah, because the constraints are variables. [74:13] Yeah, that's the third point. [74:15] That if I show you right away, the differential equation, [74:18] you will say, well, what is this stuff? [74:21] Instead, by showing first, equality constraint [74:25] and optimization, I hope that this seems a little bit [74:27] less esoteric. [74:31] All right, the last couple of minutes, the other very small [74:35] thing that I wanted to chat about [74:36] is the relation between direct methods and indirect methods. [74:40] So I won't go through the entire discussion [74:42] because it would take another 10 minutes. [74:45] But the idea here is, as I said, indirect methods-- [74:48] we already discussed this on Tuesday-- [74:51] the idea is to transcribe the problem into a nonlinear [74:53] optimization problem by doing a discretization of differential [74:57] equation and of the integral for the cost. [75:00] We already discussed this. [75:01] So we already know this. [75:03] Now, the question is, what is the relation [75:05] between the solution that a direct optimization [75:11] solver provides and the solution that you will [75:14] get with an indirect method. [75:16] So what's the connection between the two approaches? [75:18] Well, the connection is as follows. [75:21] So this is now a finite dimensional nonlinear [75:24] optimization problem-- finite dimensional [75:26] because you have a finite number of decision variable x of i, [75:29] where i is equal to 1 but dot capital N. Capital [75:33] n is the number of discretization points. [75:36] So the connection is that if you write down [75:39] the necessary optimality condition for this nonlinear [75:42] optimization problem-- [75:49] I'm not going through all the details. [75:50] This is more for your own-- it's bonus information. [75:53] I'm not going to ask you about this, neither in the pre-set [75:57] nor in the midterm 1 nor in the midterm 2. [76:00] It's just for your own edification. [76:02] If you write down the necessary optimality condition [76:04] for the nonlinear optimization problem for the transcribed one, [76:09] and if you take the time step, hi, [76:12] so basically the discretization in time, [76:14] if you make it smaller and smaller, [76:17] the necessary optimality condition [76:19] that arise from discretization and nonlinear optimization [76:21] problem converge to the necessary optimality condition [76:26] for the optimal control problem. [76:28] So that's the consistency check. [76:30] So if you go the direct route, so you first transcribe [76:33] and you write down the necessary condition, [76:35] but if the time step is very small, [76:38] those necessary conditions look like almost identical, [76:41] indeed are the discretized version [76:43] of the necessary optimality condition [76:45] for the optimal control problem that are [76:46] leveraged by indirect methods. [76:50] So oftentimes, this is not said. [76:52] So it is not mentioned. [76:53] So it seems that these two approaches [76:54] are completely different. [76:56] Indeed, they are tightly coupled, [76:59] as it should be because we're solving the same problem. [77:03] As I said, this is more information for you. [77:05] It's going to be presented in the lecture notes. [77:07] You have the slides here. [77:08] But the topic of indirect methods [77:11] is going to be covered in the midterm. [77:13] This relation between direct and indirect [77:15] is not going to be covered in the midterm. [77:18] All right, tools out there, I already [77:20] mentioned those on Tuesdays. [77:22] Next time, so next week, we'll focus now [77:24] on the problem of trajectory planning and optimization [77:26] with constraint, so where we have [77:28] to account for obstacle avoidance, [77:30] which will lead us to talk about motion planning algorithms. [77:34] OK, so have a nice weekend, and I'll see you on Tuesday.