Skip to content

Commit f7abf6c

Browse files
Merge pull request #255 from control-toolbox/222-manipulation-of-the-nlp
Add Percival
2 parents aa7f075 + aa83cda commit f7abf6c

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ MINPACK = "4854310b-de5a-5eb6-a2a5-c1dee2bd17f9"
1414
NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71"
1515
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
1616
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
17+
Percival = "01435c0c-c90d-11e9-3788-63660f8fbccc"
1718
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1819
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
1920
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"

docs/src/tutorial-nlp.md

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,41 @@ When calling `solve(ocp)` three steps are performed internally:
1111
- then, this DOCP is solved (with the internal function `solve_docp`),
1212
- finally, a functional solution of the OCP is rebuilt from the solution of the discretized problem, with [`build_solution`](@ref).
1313

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.
1517

1618
```@example main
1719
using OptimalControl
1820
using NLPModelsIpopt
1921
using Plots
2022
```
2123

22-
and define a test problem
24+
## Definition of the optimal control problem
25+
26+
We define a test problem
2327

2428
```@example main
2529
@def ocp begin
30+
2631
t ∈ [ 0, 1 ], time
2732
x ∈ R², state
2833
u ∈ R, control
34+
2935
x(0) == [ -1, 0 ]
3036
x(1) == [ 0, 0 ]
37+
3138
ẋ(t) == [ x₂(t), u(t) ]
39+
3240
∫( 0.5u(t)^2 ) → min
41+
3342
end
3443
nothing # hide
3544
```
3645

37-
First let us discretize the problem.
46+
## Discretization and NLP problem
47+
48+
We discretize the problem.
3849

3950
```@example main
4051
docp, nlp = direct_transcription(ocp)
@@ -43,22 +54,27 @@ nothing # hide
4354

4455
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`.
4556

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.
4862

4963
```@example main
5064
using NLPModelsIpopt
5165
nlp_sol = ipopt(nlp; print_level=4, mu_strategy="adaptive", tol=1e-8, sb="yes")
5266
nothing # hide
5367
```
5468

55-
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).
5670

5771
```@example main
5872
sol = build_solution(docp, primal=nlp_sol.solution, dual=nlp_sol.multipliers)
5973
plot(sol)
6074
```
6175

76+
## Initial guess
77+
6278
An initial guess, including warm start, can be passed to [`direct_transcription`](@ref) the same way as for `solve`.
6379

6480
```@example main
@@ -71,4 +87,14 @@ It can also be changed after the transcription is done, with [`set_initial_gues
7187
```@example main
7288
set_initial_guess(docp, nlp, sol)
7389
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.
94+
95+
```@example main
96+
using Percival
97+
98+
output = percival(nlp)
99+
print(output)
74100
```

0 commit comments

Comments
 (0)