Skip to content

Commit b817314

Browse files
committed
add doc jump flow
1 parent 699a229 commit b817314

4 files changed

Lines changed: 92 additions & 7 deletions

File tree

docs/make.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,15 @@ for Module in Modules
104104
DocMeta.setdocmeta!(Module, :DocTestSetup, :(using $Module); recursive=true)
105105
end
106106

107-
mkpath("./docs/src/assets")
108-
cp("./docs/Manifest.toml", "./docs/src/assets/Manifest.toml"; force=true)
109-
cp("./docs/Project.toml", "./docs/src/assets/Project.toml"; force=true)
107+
# For reproducibility
108+
mkpath(joinpath(@__DIR__, "src", "assets"))
109+
cp(joinpath(@__DIR__, "Manifest.toml"), joinpath(@__DIR__, "src", "assets", "Manifest.toml"), force = true)
110+
cp(joinpath(@__DIR__, "Project.toml"), joinpath(@__DIR__, "src", "assets", "Project.toml"), force = true)
110111

111112
repo_url = "github.com/control-toolbox/OptimalControl.jl"
112113

113114
makedocs(;
114-
draft=false, # if draft is true, then the julia code from .md is not executed
115+
draft=true, # if draft is true, then the julia code from .md is not executed
115116
# to disable the draft mode in a specific markdown file, use the following:
116117
# ```@meta
117118
# Draft = false

docs/src/assets/Manifest.toml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/src/assets/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ DataFrames = "1"
3434
Documenter = "1.8"
3535
DocumenterInterLinks = "1"
3636
DocumenterMermaid = "0.2"
37+
ExaModels = "0.8"
3738
JLD2 = "0.5"
3839
JSON3 = "1.14"
3940
LinearAlgebra = "1"

docs/src/manual-flow-ocp.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
```@meta
44
CollapsedDocStrings = false
5+
Draft = false
56
```
67

78
In this tutorial, we explain the `Flow` function, in particular to compute flows from an optimal control problem.
@@ -462,7 +463,7 @@ where $ p^0 = -1 $ since we are in the normal case, and where $c(x) = x - l_b$.
462463
u(x) = 0.
463464
```
464465

465-
From the maximizing condition, along a boundary arc, we have $p(t) = 0$. Differentiating, we obtain $\dot{p}(t) = 2 x(t) - \mu(t) = 0$. Hence, along a boundary arc, the dual variable $\mu$ is given in feedback form by:
466+
From the maximisation condition, along a boundary arc, we have $p(t) = 0$. Differentiating, we obtain $\dot{p}(t) = 2 x(t) - \mu(t) = 0$. Hence, along a boundary arc, the dual variable $\mu$ is given in feedback form by:
466467

467468
```math
468469
\mu(x) = 2x.
@@ -503,3 +504,85 @@ p0 = -0.982237546583301
503504
xf, pf = f(t0, x0, p0, tf)
504505
xf
505506
```
507+
508+
## Jump on the costate
509+
510+
Let consider the following problem:
511+
512+
```@example main
513+
t0=0
514+
tf=1
515+
x0=[0, 1]
516+
l = 1/9
517+
@def ocp begin
518+
t ∈ [ t0, tf ], time
519+
x ∈ R², state
520+
u ∈ R, control
521+
x(t0) == x0
522+
x(tf) == [0, -1]
523+
x₁(t) ≤ l, (x_con)
524+
ẋ(t) == [x₂(t), u(t)]
525+
0.5∫(u(t)^2) → min
526+
end
527+
nothing # hide
528+
```
529+
530+
The pseudo-Hamiltonian of this problem is
531+
532+
```math
533+
H(x, p, u, \mu) = p_1\, x_2 + p_2\, u + 0.5\, p^0 u^2 + \mu\, c(x),
534+
```
535+
536+
where $ p^0 = -1 $ since we are in the normal case, and where the constraint is $c(x) = l - x_1 \ge 0$. Along a boundary arc, when $c(x(t)) = 0$, we have $x_1(t) = l$, so $\dot{x}_1(t) = x_2(t) = 0$. Differentiating again, we obtain $\dot{x}_2(t) = u(t) = 0$ (the constraint is of order 2). Hence, along a boundary arc, the control in feedback form is:
537+
538+
539+
```math
540+
u(x, p) = 0.
541+
```
542+
543+
From the maximisation condition, along a boundary arc, we have $p_2(t) = 0$. Differentiating, we obtain $\dot{p}_2(t) = -p_1(t) = 0$. Differentiating again, we obtain $\dot{p}_1(t) = \mu(t) = 0$. Hence, along a boundary arc, the Lagrange multiplier $\mu$ is given in feedback form by:
544+
545+
```math
546+
\mu(x, p) = 0.
547+
```
548+
549+
Outside a boundary arc, the maximisation condition gives $u(x, p) = p_2$. A deeper analysis of the problem shows that the optimal solution has 3 arcs, the first and the third ones are interior to the constraint. The second arc is a boundary arc, that is $x_1(t) = l$ along the second arc. We denote by $t_1$ and $t_2$ the two switching times. We have $t_1 = 3l = 1/3$ and $t_2 = 1 - 3l = 2/3$, since $l=1/9$. The initial costate solution is $p(0) = [-18, -6]$.
550+
551+
!!! danger "Important"
552+
553+
The costate is discontinuous at $t_1$ and $t_2$ with a jump of $18$.
554+
555+
Let us compute the solution concatenating the flows with the jumps.
556+
557+
```@example main
558+
t1 = 3l
559+
t2 = 1 - 3l
560+
p0 = [-18, -6]
561+
562+
fs = Flow(ocp,
563+
(x, p) -> p[2] # control along regular arc
564+
)
565+
fc = Flow(ocp,
566+
(x, p) -> 0, # control along boundary arc
567+
(x, u) -> l-x[1], # state constraint
568+
(x, p) -> 0 # Lagrange multiplier
569+
)
570+
571+
ν = 18 # jump value of p1 at t1 and t2
572+
573+
f = fs * (t1, [ν, 0], fc) * (t2, [ν, 0], fs)
574+
575+
xf, pf = f(t0, x0, p0, tf) # xf should be [0, -1]
576+
```
577+
578+
Let us solve the problem with a direct method to compare with the solution from the flow.
579+
580+
```@example main
581+
using NLPModelsIpopt
582+
583+
direct_sol = solve(ocp)
584+
plot(direct_sol; label="direct")
585+
586+
flow_sol = f((t0, tf), x0, p0)
587+
plot!(flow_sol; label="flow")
588+
```

0 commit comments

Comments
 (0)