Skip to content

Commit 3ad1ee0

Browse files
committed
add columns math vs code
1 parent bd950e4 commit 3ad1ee0

5 files changed

Lines changed: 97 additions & 14 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*.jld2
1212
*.json
1313
!.zenodo.json
14+
!.markdownlint.json
1415

1516
# System-specific files and directories generated by the BinaryProvider and BinDeps packages
1617
# They contain absolute paths specific to the host computer, and so should not be committed

.markdownlint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"MD046": false,
3+
"MD013": false
4+
}

docs/src/assets/Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ version = "0.5.6+0"
16771677
deps = ["ADNLPModels", "CTBase", "CTDirect", "CTFlows", "CTModels", "CTParser", "CommonSolve", "DocStringExtensions", "ExaModels"]
16781678
path = "/Users/ocots/Research/logiciels/dev/control-toolbox/OptimalControl"
16791679
uuid = "5f98b655-cc9a-415a-b60e-744165666948"
1680-
version = "1.1.2"
1680+
version = "1.1.3"
16811681

16821682
[[deps.Opus_jll]]
16831683
deps = ["Artifacts", "JLLWrappers", "Libdl"]

docs/src/example-double-integrator-energy.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,15 @@ We assume that the mass is constant and equal to one, and that there is no frict
1313
\dot x_1(t) = x_2(t), \quad \dot x_2(t) = u(t),\quad u(t) \in \R,
1414
```
1515

16-
which is simply the [double integrator](https://en.wikipedia.org/w/index.php?title=Double_integrator&oldid=1071399674) system.
17-
Let us consider a transfer starting at time $t_0 = 0$ and ending at time $t_f = 1$, for which we want to minimise the transfer energy
16+
which is simply the [double integrator](https://en.wikipedia.org/w/index.php?title=Double_integrator&oldid=1071399674) system. Let us consider a transfer starting at time $t_0 = 0$ and ending at time $t_f = 1$, for which we want to minimise the transfer energy
1817

1918
```math
2019
\frac{1}{2}\int_{0}^{1} u^2(t) \, \mathrm{d}t
2120
```
2221

2322
starting from $x(0) = (-1, 0)$ and aiming to reach the target $x(1) = (0, 0)$.
2423

25-
First, we need to import the [OptimalControl.jl](https://control-toolbox.org/OptimalControl.jl) package to define the
26-
optimal control problem, [NLPModelsIpopt.jl](https://jso.dev/NLPModelsIpopt.jl) to solve it,
27-
and [Plots.jl](https://docs.juliaplots.org) to visualise the solution.
24+
First, we need to import the [OptimalControl.jl](https://control-toolbox.org/OptimalControl.jl) package to define the optimal control problem, [NLPModelsIpopt.jl](https://jso.dev/NLPModelsIpopt.jl) to solve it, and [Plots.jl](https://docs.juliaplots.org) to visualise the solution.
2825

2926
```@example main
3027
using OptimalControl
@@ -36,6 +33,11 @@ using Plots
3633

3734
Let us define the problem with the [`@def`](@ref) macro:
3835

36+
```@raw html
37+
<div style="display: flex; gap: 1rem; align-items: flex-start; margin-bottom: 1em;">
38+
<div style="flex: 1;">
39+
```
40+
3941
```@example main
4042
t0 = 0
4143
tf = 1
@@ -47,12 +49,34 @@ ocp = @def begin
4749
u ∈ R, control
4850
x(t0) == x0
4951
x(tf) == xf
50-
(t) == [x₂(t), u(t)]
52+
(t) == [x₂(t), u(t)]
5153
0.5∫( u(t)^2 ) → min
5254
end
5355
nothing # hide
5456
```
5557

58+
```@raw html
59+
</div>
60+
<div style="flex: 1;">
61+
```
62+
63+
### Mathematical formulation
64+
65+
```math
66+
\begin{aligned}
67+
& \text{Minimise} && \frac{1}{2}\int_0^1 u^2(t) \,\mathrm{d}t \\
68+
& \text{subject to} \\
69+
& && \dot{x}_1(t) = x_2(t), \\[0.5em]
70+
& && \dot{x}_2(t) = u(t), \\[1.0em]
71+
& && x(0) = (-1,0), \quad x(1) = (0,0).
72+
\end{aligned}
73+
```
74+
75+
```@raw html
76+
</div>
77+
</div>
78+
```
79+
5680
!!! note "Nota bene"
5781

5882
For a comprehensive introduction to the syntax used above to define the optimal control problem, see [this abstract syntax tutorial](@ref manual-abstract-syntax). In particular, non-Unicode alternatives are available for derivatives, integrals, *etc.*
@@ -142,12 +166,12 @@ plot(sol)
142166

143167
## State constraint
144168

145-
### Direct method
169+
### Direct method: constrained case
146170

147171
We add the path constraint
148172

149173
```math
150-
x_2(t) \le 1.2.
174+
x_2(t) \le 1.2.
151175
```
152176

153177
Let us model, solve and plot the optimal control problem with this constraint.
@@ -175,7 +199,7 @@ sol = solve(ocp)
175199
plt = plot(sol; label="Direct", size=(800, 600))
176200
```
177201

178-
### Indirect method
202+
### Indirect method: constrained case
179203

180204
The pseudo-Hamiltonian is (considering the normal case):
181205

@@ -186,13 +210,13 @@ H(x, p, u, \mu) = p_1 x_2 + p_2 u - \frac{u^2}{2} + \mu\, c(x),
186210
with $c(x) = x_2 - a$. Along a boundary arc we have $c(x(t)) = 0$. Differentiating, we obtain:
187211

188212
```math
189-
\frac{\mathrm{d}}{\mathrm{d}t}c(x(t)) = \dot{x}_2(t) = u(t) = 0.
213+
\frac{\mathrm{d}}{\mathrm{d}t}c(x(t)) = \dot{x}_2(t) = u(t) = 0.
190214
```
191215

192-
The zero control is maximising; hence, $p_2(t) = 0$ along the boundary arc.
216+
The zero control is maximising; hence, $p_2(t) = 0$ along the boundary arc.
193217

194218
```math
195-
\dot{p}_2(t) = -p_1(t) - \mu(t) \quad \Rightarrow \mu(t) = -p_1(t).
219+
\dot{p}_2(t) = -p_1(t) - \mu(t) \quad \Rightarrow \mu(t) = -p_1(t).
196220
```
197221

198222
Since the adjoint vector is continuous at the entry time $t_1$ and the exit time $t_2$, we have four unknowns: the initial costate $p_0 \in \mathbb{R}^2$ and the times $t_1$ and $t_2$. We need four equations: the target condition provides two, reaching the constraint at time $t_1$ gives $c(x(t_1)) = 0$, and finally $p_2(t_1) = 0$.
@@ -253,4 +277,4 @@ flow_sol = φ((t0, tf), x0, p0; saveat=range(t0, tf, 100))
253277
254278
# plot the solution on the previous plot
255279
plot!(plt, flow_sol; label="Indirect", color=2, linestyle=:dash)
256-
```
280+
```

docs/src/example-double-integrator-time.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,60 @@ using Plots
3232

3333
Let us define the problem:
3434

35+
```@raw html
36+
<div style="display: flex; gap: 1rem; align-items: flex-start; margin-bottom: 1em;">
37+
<div style="flex: 1;">
38+
```
39+
40+
```@example main
41+
ocp = @def begin
42+
43+
tf ∈ R, variable
44+
t ∈ [0, tf], time
45+
x = (q, v) ∈ R², state
46+
u ∈ R, control
47+
48+
-1 ≤ u(t) ≤ 1
49+
50+
q(0) == -1
51+
v(0) == 0
52+
q(tf) == 0
53+
v(tf) == 0
54+
55+
ẋ(t) == [v(t), u(t)]
56+
57+
tf → min
58+
59+
end
60+
nothing # hide
61+
```
62+
63+
```@raw html
64+
</div>
65+
<div style="flex: 1;">
66+
```
67+
68+
### Mathematical formulation
69+
70+
```math
71+
\begin{aligned}
72+
& \text{Minimise} && t_f \\[0.5em]
73+
& \text{subject to} \\[0.5em]
74+
& && \dot q(t) = v(t), \\
75+
& && \dot v(t) = u(t), \\[0.5em]
76+
& && -1 \le u(t) \le 1, \\[0.5em]
77+
& && q(0) = -1, \\[0.5em]
78+
& && v(0) = 0, \\[0.5em]
79+
& && q(t_f) = 0, \\[0.5em]
80+
& && v(t_f) = 0.
81+
\end{aligned}
82+
```
83+
84+
```@raw html
85+
</div>
86+
</div>
87+
```
88+
3589
```@example main
3690
ocp = @def begin
3791

0 commit comments

Comments
 (0)