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
# [Optimal control problem with free initial time](@id tutorial-free-times-initial)
6
6
7
-
In this tutorial, we explore an optimal control problem with free initial time `t0`.
7
+
In this tutorial, we explore a minimum-time optimal control problem where the **initial time $t_0$ is free** (but negative). We aim to determine the latest possible starting time for the system to still reach the goal at a fixed final time $t_f = 0$.
8
8
9
-
## Definition of the problem
9
+
## Problem definition
10
10
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.
11
+
We consider the classic **double integrator** system. The goal is to move from rest at position $0$ to position $1$ (also at rest), using bounded control:
12
+
13
+
- Initial condition: $x(t_0) = [0, 0]$,
14
+
- Final condition: $x(t_f) = [1, 0]$ with $t_f = 0$ fixed,
15
+
- Control bounds: $u(t) \in [-1, 1]$,
16
+
- The objective is to **maximize $t_0$**, i.e., minimize $-t_0$, which corresponds to a **minimum-time formulation** with fixed arrival time and free departure time.
12
17
13
18
```@example initial_time
14
19
using OptimalControl
15
20
16
-
tf = 0 # final time
21
+
tf = 0 # Final time
22
+
x0 = [0, 0] # Initial state
23
+
xf = [1, 0] # Final state
17
24
18
25
@def ocp begin
19
26
20
-
t0 ∈ R, variable # the initial time is free
27
+
t0 ∈ R, variable # t₀ is free and will be optimized
21
28
t ∈ [t0, tf], time
22
29
x ∈ R², state
23
30
u ∈ R, control
24
31
25
-
-1 ≤ u(t) ≤ 1
32
+
-1 ≤ u(t) ≤ 1 # Control bounds
26
33
27
-
x(t0) == [0, 0]
28
-
x(tf) == [1, 0]
34
+
x(t0) == x0 # Initial constraint
35
+
x(tf) == xf # Final constraint
29
36
30
-
0.05 ≤ -t0 ≤ Inf # t0 is negative
37
+
0.05 ≤ -t0 ≤ Inf # Ensure t₀ is negative and bounded
31
38
32
-
ẋ(t) == [x₂(t), u(t)]
39
+
ẋ(t) == [x₂(t), u(t)] # Dynamics
33
40
34
-
-t0 → min # or t0 → max
41
+
-t0 → min # Equivalent to maximizing t₀
35
42
36
43
end
37
44
nothing # hide
38
45
```
39
46
40
-
#Direct resolution
47
+
## Direct solution
41
48
42
-
Let us solve the problem with a direct method.
49
+
We solve the problem using a **direct transcription method**.
43
50
44
51
```@example initial_time
45
52
using NLPModelsIpopt
46
-
sol = solve(ocp)
53
+
direct_sol = solve(ocp)
47
54
nothing # hide
48
55
```
49
56
50
-
The initial time obtained with the direct method is:
57
+
The optimal initial time obtained is:
51
58
52
59
```@example initial_time
53
-
t0 = variable(sol)
60
+
t0 = variable(direct_sol)
54
61
```
55
62
56
-
We can plot the solution.
63
+
We can now plot the solution:
57
64
58
65
```@example initial_time
59
66
using Plots
60
-
plot(sol; label="direct", size=(700, 700))
61
-
```
62
-
63
-
## Mathematical verification
64
-
65
-
Let us compute the solution analytically, thanks to the Pontryagin's Maximum Principle (PMP). First, we define the pseudo-Hamiltonian
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
-
73
-
```math
74
-
\begin{aligned}
75
-
\dot{p}_1(t) &= 0, \\
76
-
\dot{p}_2(t) &= -p_1(t),
77
-
\end{aligned}
78
-
```
70
+
## Pontryagin's Maximum Principle
79
71
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]$.
72
+
The optimal control appears to be **bang-bang**, switching once from $u = 1$ to $u = -1$. This motivates us to apply the Pontryagin's Maximum Principle (PMP) analytically to verify and interpret the solution.
81
73
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
Let $(x(\cdot), u(\cdot), t_0)$ be the solution. By PMP, there exists $(p(\cdot), p^0) \neq 0$ with $p^0 \leq 0$ and $p(\cdot)$ absolutely continuous, such that $H(x(t_0), p(t_0), u(t_0)) = -p^0$. The adjoint equations are:
108
81
109
82
```math
110
-
x_2(0) = 0 \quad \Rightarrow \quad t_0 = 2\, t_s.
83
+
\dot{p}_1(t) = 0, \quad \dot{p}_2(t) = -p_1(t),
111
84
```
112
85
113
-
The final condition of $x_1$ gives:
86
+
so $p_1(t) = \alpha$, $p_2(t) = -\alpha (t-t_0) + \beta$. The maximization condition gives:
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.
135
+
We now solve the PMP system **numerically**using the direct solution as an initial guess.
140
136
141
137
```@example initial_time
142
-
# pseudo-Hamiltonian
143
138
H(x, p, u) = p[1]*x[2] + p[2]*u
144
139
145
-
# Define flows for u = +1 and u = -1
146
-
const u_pos = 1
147
-
const u_neg = -1
140
+
# Define flows for constant controls
141
+
u_pos = 1
142
+
u_neg = -1
148
143
149
144
using OrdinaryDiffEq
150
145
f_pos = Flow(ocp, (x, p, t0) -> u_pos)
151
146
f_neg = Flow(ocp, (x, p, t0) -> u_neg)
147
+
nothing # hide
152
148
```
153
149
154
-
Shooting function adapted for free initial time
155
-
```@example initial_time
150
+
The **shooting function** encodes:
151
+
- continuity of state and costate,
152
+
- satisfaction of boundary conditions,
153
+
- switching condition ($p_2(t_1) = 0$),
154
+
- pseudo-Hamiltonian condition at $t_0$.
156
155
156
+
```@example initial_time
157
157
function shoot!(s, p0, t1, t0)
158
-
# Initial conditions
159
-
x0 = [0, 0] # fixed initial state
160
-
tf = 0 # fixed final time
161
-
162
-
# The flows (time runs from t0 to tf = 0)
163
-
x1, p1 = f_pos(t0, x0, p0, t1) # from t0 to t1
164
-
xf, pf = f_neg(t1, x1, p1, tf) # from t1 to tf = 0
165
-
166
-
# Final control
167
-
uf = -1
168
-
169
-
# Target final state
170
-
xf_target = [1, 0]
171
-
172
-
# Shooting conditions
173
-
s[1:2] = xf .- xf_target # reach the target at tf = 0
174
-
s[3] = H(xf, pf, uf) - 1 # final condition on pseudo-Hamiltonian
0 commit comments