You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/tutorial-free-times-initial.md
+53-66Lines changed: 53 additions & 66 deletions
Original file line number
Diff line number
Diff line change
@@ -6,26 +6,18 @@ Draft = false
6
6
7
7
In this tutorial, we explore an optimal control problem with free initial time `t0`.
8
8
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
-
21
9
## Definition of the problem
22
10
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.
24
12
25
13
```@example initial_time
14
+
using OptimalControl
15
+
26
16
tf = 0 # final time
17
+
27
18
@def ocp begin
28
-
t0 ∈ R, variable # the initial time is free
19
+
20
+
t0 ∈ R, variable # the initial time is free
29
21
t ∈ [t0, tf], time
30
22
x ∈ R², state
31
23
u ∈ R, control
@@ -35,11 +27,12 @@ tf = 0 # final time
35
27
x(t0) == [0, 0]
36
28
x(tf) == [1, 0]
37
29
38
-
0.05 ≤ -t0 ≤ Inf
30
+
0.05 ≤ -t0 ≤ Inf # t0 is negative
39
31
40
32
ẋ(t) == [x₂(t), u(t)]
41
33
42
-
t0 → max
34
+
-t0 → min # or t0 → max
35
+
43
36
end
44
37
nothing # hide
45
38
```
@@ -49,10 +42,12 @@ nothing # hide
49
42
Let us solve the problem with a direct method.
50
43
51
44
```@example initial_time
45
+
using NLPModelsIpopt
52
46
sol = solve(ocp)
47
+
nothing # hide
53
48
```
54
49
55
-
The initial time solution is:
50
+
The initial time obtained with the direct method is:
56
51
57
52
```@example initial_time
58
53
t0 = variable(sol)
@@ -61,105 +56,97 @@ t0 = variable(sol)
61
56
We can plot the solution.
62
57
63
58
```@example initial_time
64
-
plot(sol; label="direct", size=(800, 800))
59
+
using Plots
60
+
plot(sol; label="direct", size=(700, 700))
65
61
```
66
62
67
63
## Mathematical verification
68
64
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
+
70
67
```math
71
-
H= p_1 x_2 + p_2 u + 1
68
+
H(x, p, u) = p_1 x_2 + p_2 u.
72
69
```
73
70
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
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
88
83
89
-
Optimal control:
90
84
```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}.
93
86
```
94
87
95
-
Now we integrate the system:
88
+
At switching time $t_s$ whe have:
96
89
97
-
On ( t in [t_0, t_s] ) :
98
90
```math
99
-
x_2' = u = 1 \quad \Rightarrow \quad x_2(t) = t - t_0 \\
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:
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:
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.
148
140
149
-
Define the pseudo-Hamiltonian and switching function
150
141
```@example initial_time
151
-
142
+
# pseudo-Hamiltonian
152
143
H(x, p, u) = p[1]*x[2] + p[2]*u
153
144
154
-
# Hamiltonian lift for switching condition
155
-
F1(x) = [0, 1]
156
-
H1 = Lift(F1)
157
-
158
145
# Define flows for u = +1 and u = -1
159
146
const u_pos = 1
160
147
const u_neg = -1
161
148
162
-
149
+
using OrdinaryDiffEq
163
150
f_pos = Flow(ocp, (x, p, t0) -> u_pos)
164
151
f_neg = Flow(ocp, (x, p, t0) -> u_neg)
165
152
```
@@ -191,7 +178,6 @@ end
191
178
192
179
Get initial guess from direct solution
193
180
```@example initial_time
194
-
195
181
t = time_grid(sol)
196
182
x = state(sol)
197
183
p = costate(sol)
@@ -208,6 +194,7 @@ println("t0 = ", t0)
208
194
#Test shooting function at initial guess
209
195
s = zeros(4)
210
196
shoot!(s, p0, t1, t0)
197
+
using LinearAlgebra: norm
211
198
println("\nNorm of the shooting function: ‖s‖ = ", norm(s), "\n")
0 commit comments