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-nlp.md
+32-6Lines changed: 32 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,30 +11,41 @@ When calling `solve(ocp)` three steps are performed internally:
11
11
- then, this DOCP is solved (with the internal function `solve_docp`),
12
12
- finally, a functional solution of the OCP is rebuilt from the solution of the discretized problem, with [`build_solution`](@ref).
13
13
14
-
These steps can also be done separately, for instance if you want to use your own NLP solver. Let us load the modules
14
+
These steps can also be done separately, for instance if you want to use your own NLP solver.
15
+
16
+
Let us load the packages.
15
17
16
18
```@example main
17
19
using OptimalControl
18
20
using NLPModelsIpopt
19
21
using Plots
20
22
```
21
23
22
-
and define a test problem
24
+
## Definition of the optimal control problem
25
+
26
+
We define a test problem
23
27
24
28
```@example main
25
29
@def ocp begin
30
+
26
31
t ∈ [ 0, 1 ], time
27
32
x ∈ R², state
28
33
u ∈ R, control
34
+
29
35
x(0) == [ -1, 0 ]
30
36
x(1) == [ 0, 0 ]
37
+
31
38
ẋ(t) == [ x₂(t), u(t) ]
39
+
32
40
∫( 0.5u(t)^2 ) → min
41
+
33
42
end
34
43
nothing # hide
35
44
```
36
45
37
-
First let us discretize the problem.
46
+
## Discretization and NLP problem
47
+
48
+
We discretize the problem.
38
49
39
50
```@example main
40
51
docp, nlp = direct_transcription(ocp)
@@ -43,22 +54,27 @@ nothing # hide
43
54
44
55
The DOCP contains information related to the transcription, including a copy of the original OCP, and the NLP is the resulting discretized problem, in our case an `ADNLPModel`.
45
56
46
-
You could then use the solver of your choice to solve it.
47
-
For an example we use the `ipopt` solver from [`NLPModelsIpopt.jl`](https://github.com/JuliaSmoothOptimizers/NLPModelsIpopt.jl) package to solve the NLP problem.
57
+
We can now use the solver of our choice to solve it.
58
+
59
+
## Resolution of the NLP problem
60
+
61
+
For a first example we use the `ipopt` solver from [`NLPModelsIpopt.jl`](https://github.com/JuliaSmoothOptimizers/NLPModelsIpopt.jl) package to solve the NLP problem.
Then we can rebuild and plot an OCP solution (note that the multipliers are optional, but the OCP costate will not be retrieved if the multipliers are not provided).
69
+
Then we can rebuild and plot an optimal control problem solution (note that the multipliers are optional, but the OCP costate will not be retrieved if the multipliers are not provided).
56
70
57
71
```@example main
58
72
sol = build_solution(docp, primal=nlp_sol.solution, dual=nlp_sol.multipliers)
59
73
plot(sol)
60
74
```
61
75
76
+
## Initial guess
77
+
62
78
An initial guess, including warm start, can be passed to [`direct_transcription`](@ref) the same way as for `solve`.
63
79
64
80
```@example main
@@ -71,4 +87,14 @@ It can also be changed after the transcription is done, with [`set_initial_gues
71
87
```@example main
72
88
set_initial_guess(docp, nlp, sol)
73
89
nothing # hide
90
+
```
91
+
92
+
For a second example, we use the [`Percival.jl`](https://jso.dev/Percival.jl) to solve the NLP problem
93
+
with as initial guess the solution from the first resolution.
0 commit comments