Skip to content

Commit b686a1f

Browse files
committed
review math
1 parent 4b457b3 commit b686a1f

File tree

1 file changed

+53
-66
lines changed

1 file changed

+53
-66
lines changed

docs/src/tutorial-free-times-initial.md

Lines changed: 53 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,18 @@ Draft = false
66

77
In this tutorial, we explore an optimal control problem with free initial time `t0`.
88

9-
Here are the required packages for the tutorial:
10-
11-
```@example initial_time
12-
using LinearAlgebra: norm
13-
using OptimalControl
14-
using NLPModelsIpopt
15-
using BenchmarkTools
16-
using DataFrames
17-
using Plots
18-
using Printf
19-
```
20-
219
## Definition of the problem
2210

23-
We consider the double integrator in minimum time but we fix the final time and let the initial time free. The objective is thus to maximise `t0`.
11+
We consider the double integrator in minimum time. We start from the initial condition $x(t_0) = [0, 0]$ and we aim to reach at the final time $t_f = 0$ the target $x(t_f) = [1, 0]$. The final time is fixed but the initial time is free. The objective is thus to maximise `t0` which is negative.
2412

2513
```@example initial_time
14+
using OptimalControl
15+
2616
tf = 0 # final time
17+
2718
@def ocp begin
28-
t0 ∈ R, variable # the initial time is free
19+
20+
t0 ∈ R, variable # the initial time is free
2921
t ∈ [t0, tf], time
3022
x ∈ R², state
3123
u ∈ R, control
@@ -35,11 +27,12 @@ tf = 0 # final time
3527
x(t0) == [0, 0]
3628
x(tf) == [1, 0]
3729
38-
0.05 ≤ -t0 ≤ Inf
30+
0.05 ≤ -t0 ≤ Inf # t0 is negative
3931
4032
ẋ(t) == [x₂(t), u(t)]
4133
42-
t0 → max
34+
-t0 → min # or t0 → max
35+
4336
end
4437
nothing # hide
4538
```
@@ -49,10 +42,12 @@ nothing # hide
4942
Let us solve the problem with a direct method.
5043

5144
```@example initial_time
45+
using NLPModelsIpopt
5246
sol = solve(ocp)
47+
nothing # hide
5348
```
5449

55-
The initial time solution is:
50+
The initial time obtained with the direct method is:
5651

5752
```@example initial_time
5853
t0 = variable(sol)
@@ -61,105 +56,97 @@ t0 = variable(sol)
6156
We can plot the solution.
6257

6358
```@example initial_time
64-
plot(sol; label="direct", size=(800, 800))
59+
using Plots
60+
plot(sol; label="direct", size=(700, 700))
6561
```
6662

6763
## Mathematical verification
6864

69-
Here is the theoretical part using Pontryagin's Maximum Principle:
65+
Let us compute the solution analytically, thanks to the Pontryagin's Maximum Principle (PMP). First, we define the pseudo-Hamiltonian
66+
7067
```math
71-
H = p_1 x_2 + p_2 u + 1
68+
H(x, p, u) = p_1 x_2 + p_2 u.
7269
```
7370

74-
Conditions from Pontryagin’s theorem:
71+
If $(x(\cdot), u(\cdot), t_0)$ is a solution, then, there exists $(p(\cdot), p^0) \ne 0$, $p^0 \le 0$, satisfying the conditions given by the (PMP). First, since the initial time is free, we have $H(x(t_0), p(t_0), u(t_0)) = -p^0$. Then, the costate $p(\cdot)$ satisfies the adjoint equations
72+
7573
```math
7674
\begin{aligned}
77-
78-
\dot{p}_1(t) &= 0, \quad \Rightarrow \quad p_1 = c_1 \quad (\text{constant}) \\
79-
80-
\dot{p}_2(t) &= -p_1 \quad \Rightarrow \quad p_2 = -c_1 t + c_2
75+
\dot{p}_1(t) &= 0, \\
76+
\dot{p}_2(t) &= -p_1(t),
8177
\end{aligned}
8278
```
8379

84-
Switching condition:
85-
```math
86-
p_2(t_s) = 0 \quad \Rightarrow \quad c_2 = c_1 t_s
87-
```
80+
which gives $p_1(t) = \alpha$, $p_2(t) = -\alpha t + \beta$. The maximisation condition from the PMP implies that $u(t) = 1$ if $p_2(t)>0$ and $u(t)=-1$, if $p_2(t)<0$. We switch at time $t_s$ from one control to the other if $p_2(t_s) = 0$, that is when $\beta = \alpha\, t_s$. Note that $p_2$ cannot vanish on an interval of non-empty interior since otherwise, we would have $\alpha = \beta = 0$ and so $H(x(t_0), p(t_0), u(t_0)) = -p^0 = 0$ but $(p(\cdot), p^0) \ne 0$. Hence, there is at most one switching time $t_s$. We can demonstrate that in the setting above, there is exactly one switching time and we switch from $u=-1$ to $u=+1$. Thus, the optimal control is of the form $u(t) = 1$ for $t \in [t_0, t_s)$ and $u(t) = -1$ for $t \in [t_s, 0]$.
81+
82+
Let us find now $\alpha$, $\beta$, $t_s$ and $t_0$. We can integrate the system: On the interval $[t_0, t_s]$ we have $\dot{x}_2(t) = u(t) = 1$, hence, $x_2(t) = t - t_0$. Since $\dot{x}_1(t) = x_2(t)$, we get
8883

89-
Optimal control:
9084
```math
91-
u(t) = 1 \quad \text{on} \quad [t_0, t_s] \\
92-
u(t) = -1 \quad \text{on} \quad [t_s, 0]
85+
x_1(t) = \frac{(t - t_0)^2}{2}.
9386
```
9487

95-
Now we integrate the system:
88+
At switching time $t_s$ whe have:
9689

97-
On ( t in [t_0, t_s] ) :
9890
```math
99-
x_2' = u = 1 \quad \Rightarrow \quad x_2(t) = t - t_0 \\
100-
x_1' = x_2 \quad \Rightarrow \quad x_1(t) = \frac{(t - t_0)^2}{2}
91+
x_2(t_s) = t_s - t_0, \quad x_1(t_s) = \frac{(t_s - t_0)^2}{2},
10192
```
10293

103-
At switching time ( t = t_s ) :
94+
that are the initial conditions of the integrations on the interval $[t_s, t_f]$:
95+
10496
```math
105-
\dot{x}_2(t_s) = t_s - t_0 \\
106-
\dot{x}_1(t_s) = \frac{(t_s - t_0)^2}{2}
97+
\dot{x}_2(t) = u(t) = -1 \quad \Rightarrow \quad x_2(t) = x_2(t_s) - (t - t_s) = 2\, t_s - t_0 - t
10798
```
10899

109-
when ( t in [t_s, 0] ) :
100+
and
101+
110102
```math
111-
\dot{x}_2(t) = u(t) = -1 \quad \Rightarrow \quad x_2(t) = x_2(t_s) - (t - t_s) \\
112-
\dot{x}_1(t) = x_2(t) \quad \Rightarrow \quad x_1(t) = x_1(t_s) + \int_{t_s}^t x_2(s) ds
103+
\dot{x}_1(t) = x_2(t) \quad \Rightarrow \quad x_1(t) = x_1(t_s) + \int_{t_s}^t x_2(\sigma)\, \mathrm{d}\sigma =
104+
\frac{(t_s - t_0)^2}{2} + (2\, t_s - t_0) (t - t_s) - \frac{t^2}{2} + \frac{t_s^2}{2}.
113105
```
114106

115-
Final velocity condition:
107+
The final condition on $x_2$ gives ($t_f = 0$):
108+
116109
```math
117-
x_2(0) = 0 \quad \Rightarrow \quad t_s - t_0 + t_s = 0 \quad \Rightarrow \quad t_0 = 2 t_s
110+
x_2(0) = 0 \quad \Rightarrow \quad t_0 = 2\, t_s.
118111
```
119112

120-
Final position:
113+
The final condition of $x_1$ gives:
114+
121115
```math
122-
x_1(0) = x_1(t_s) + \frac{t_s^2}{2} \quad \Rightarrow \quad x_1(0) = t_s^2 = 1 \quad \Rightarrow \quad t_s = -1
116+
x_1(0) = t_s^2 = 1 \quad \Rightarrow \quad t_s = -1.
123117
```
124118

125119
We deduce:
126-
```math
127-
t_0 = 2 * t_s = -2
128-
```
129120

130-
### Final solution:
131-
- Switching time:
132121
```math
133-
t_s = -1
122+
t_0 = 2\, t_s = -2.
134123
```
135-
- Initial time:
124+
125+
To get $\alpha$ and $\beta$ we use the initial condition on the pseudo-Hamiltonian and the swtiching condition $p_2(t_s) = 0$. The switching condition gives $\beta = \alpha\, t_s = -\alpha$. The initial condition on the pseudo-Hamiltonian gives:
126+
136127
```math
137-
t_0 = -2
128+
H(x(t_0), p(t_0), u(t_0)) = p_1(t_0) x_2(t_0) + p_2(t_0) u(t_0) = -\beta = -p^0 = 1.
138129
```
139130

140-
Control:
131+
The dual variable $p^0 \ne 0$ since otherwise $(p(\cdot), p^0) = 0$ which is not. Then, $p^0 < 0$ and we can normalise it to $p^0 = -1$. At the end, we have:
132+
141133
```math
142-
u(t) = 1 \quad \text{on} \quad [-2, -1] \\
143-
u(t) = -1 \quad \text{on} \quad [-1, 0]
134+
\alpha = 1, \quad \beta = -1, \quad t_s = -1, \quad t_0 = -2.
144135
```
145136

146137
## Indirect method
147138

139+
Let us compute numerically the solution of the PMP using the solution from the direct method. We define the pseudo-Hamiltonian and the flows associated to the two controls.
148140

149-
Define the pseudo-Hamiltonian and switching function
150141
```@example initial_time
151-
142+
# pseudo-Hamiltonian
152143
H(x, p, u) = p[1]*x[2] + p[2]*u
153144
154-
# Hamiltonian lift for switching condition
155-
F1(x) = [0, 1]
156-
H1 = Lift(F1)
157-
158145
# Define flows for u = +1 and u = -1
159146
const u_pos = 1
160147
const u_neg = -1
161148
162-
149+
using OrdinaryDiffEq
163150
f_pos = Flow(ocp, (x, p, t0) -> u_pos)
164151
f_neg = Flow(ocp, (x, p, t0) -> u_neg)
165152
```
@@ -191,7 +178,6 @@ end
191178

192179
Get initial guess from direct solution
193180
```@example initial_time
194-
195181
t = time_grid(sol)
196182
x = state(sol)
197183
p = costate(sol)
@@ -208,6 +194,7 @@ println("t0 = ", t0)
208194
#Test shooting function at initial guess
209195
s = zeros(4)
210196
shoot!(s, p0, t1, t0)
197+
using LinearAlgebra: norm
211198
println("\nNorm of the shooting function: ‖s‖ = ", norm(s), "\n")
212199
213200
# Solve the nonlinear system
@@ -227,10 +214,10 @@ p0_opt = indirect_sol.u[1:2]
227214
t1_opt = indirect_sol.u[3]
228215
t0_opt = indirect_sol.u[4]
229216
```
217+
230218
We can now plot and compare with the direct method.# Compute and plot the optimal trajectory
231219

232220
```@example initial_time
233-
234221
x0 = [0, 0]
235222
tf = 0
236223

0 commit comments

Comments
 (0)