Skip to content

Commit 8c603c8

Browse files
committed
up tuto disc
1 parent dab6071 commit 8c603c8

File tree

7 files changed

+271
-47
lines changed

7 files changed

+271
-47
lines changed

docs/Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
77
MINPACK = "4854310b-de5a-5eb6-a2a5-c1dee2bd17f9"
88
MadNLP = "2621e9c9-9eb4-46b1-8089-e8c72242dfb6"
9+
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
910
NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71"
1011
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
1112
OptimalControl = "5f98b655-cc9a-415a-b60e-744165666948"
1213
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
1314
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
1415
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
15-
Tutorials = "cb10daa6-a5e5-4c25-a171-ae181b8ea3c9"
1616

1717
[compat]
18+
DataFrames = "1.7"
1819
DifferentiationInterface = "0.7"
1920
Documenter = "1.8"
2021
ForwardDiff = "0.10, 1"
2122
LinearAlgebra = "1"
2223
MINPACK = "1.3"
2324
MadNLP = "0.8"
25+
NLPModels = "0.21"
2426
NLPModelsIpopt = "0.10"
2527
NonlinearSolve = "4.6"
2628
OptimalControl = "1.0"

docs/extras/disc.jl

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
using OptimalControl # to define the optimal control problem and more
2+
using NLPModelsIpopt # to solve the problem via a direct method
3+
using Plots # to plot the solution
4+
5+
t0 = 0 # initial time
6+
r0 = 1 # initial altitude
7+
v0 = 0 # initial speed
8+
m0 = 1 # initial mass
9+
vmax = 0.1 # maximal authorized speed
10+
mf = 0.6 # final mass to target
11+
12+
ocp = @def begin # definition of the optimal control problem
13+
14+
tf R, variable
15+
t [t0, tf], time
16+
x = (r, v, m) R³, state
17+
u R, control
18+
19+
x(t0) == [r0, v0, m0]
20+
m(tf) == mf
21+
0 u(t) 1
22+
r(t) r0
23+
0 v(t) vmax
24+
25+
(t) == F0(x(t)) + u(t) * F1(x(t))
26+
27+
r(tf) max
28+
29+
end;
30+
31+
# Dynamics
32+
Cd = 310
33+
Tmax = 3.5
34+
β = 500
35+
b = 2
36+
37+
F0(x) = begin
38+
r, v, m = x
39+
D = Cd * v^2 * exp(-β*(r - 1)) # Drag force
40+
return [ v, -D/m - 1/r^2, 0 ]
41+
end
42+
43+
F1(x) = begin
44+
r, v, m = x
45+
return [ 0, Tmax/m, -b*Tmax ]
46+
end
47+
48+
# Solve the problem with different discretization methods
49+
solutions = []
50+
schemes = [:trapeze, :midpoint, :euler, :euler_implicit, :gauss_legendre_2, :gauss_legendre_3]
51+
for scheme in schemes
52+
sol = solve(ocp; disc_method=scheme, tol=1e-8, display=false)
53+
push!(solutions, (scheme, sol))
54+
end
55+
56+
# Plot the results
57+
x_style = (legend=:none,)
58+
p_style = (legend=:none,)
59+
styles = (state_style=x_style, costate_style=p_style)
60+
61+
scheme, sol = solutions[1]
62+
plt = plot(sol; label=string(scheme), styles...)
63+
for (scheme, sol) in solutions[2:end]
64+
plt = plot!(sol; label=string(scheme), styles...)
65+
end
66+
plot(plt; size=(800, 800))
67+
68+
# Benchmark the problem with different AD backends
69+
using NLPModels
70+
using DataFrames
71+
using BenchmarkTools
72+
73+
# DataFrame to store the results
74+
data = DataFrame(
75+
Backend=Symbol[],
76+
NNZO=Int[],
77+
NNZJ=Int[],
78+
NNZH=Int[],
79+
PrepTime=Float64[],
80+
ExecTime=Float64[],
81+
Objective=Float64[],
82+
Iterations=Int[]
83+
)
84+
85+
# The different AD backends to test
86+
backends = [:optimized, :manual]
87+
88+
# Loop over the backends
89+
for adnlp_backend in backends
90+
91+
println("Testing backend: ", adnlp_backend)
92+
93+
# Discretize the problem with a large grid size and Gauss-Legendre method
94+
bt = @btimed direct_transcription($ocp;
95+
disc_method=:euler,
96+
grid_size=1000,
97+
adnlp_backend=$adnlp_backend,
98+
)
99+
docp, nlp = bt.value
100+
prepa_time = bt.time
101+
102+
# Get the number of non-zero elements
103+
nnzo = get_nnzo(nlp) # Gradient of the Objective
104+
nnzj = get_nnzj(nlp) # Jacobian of the constraints
105+
nnzh = get_nnzh(nlp) # Hessian of the Lagrangian
106+
107+
# Solve the problem
108+
bt = @btimed ipopt($nlp;
109+
print_level=0,
110+
mu_strategy="adaptive",
111+
tol=1e-8,
112+
sb="yes",
113+
)
114+
nlp_sol = bt.value
115+
exec_time = bt.time
116+
117+
# Store the results in the DataFrame
118+
push!(data,
119+
(
120+
Backend=adnlp_backend,
121+
NNZO=nnzo,
122+
NNZJ=nnzj,
123+
NNZH=nnzh,
124+
PrepTime=prepa_time,
125+
ExecTime=exec_time,
126+
Objective=-nlp_sol.objective,
127+
Iterations=nlp_sol.iter
128+
)
129+
)
130+
end
131+
println(data)

docs/src/assets/Manifest.toml

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

33
julia_version = "1.11.5"
44
manifest_format = "2.0"
5-
project_hash = "32b2794783d26475326d2bd2122ce29a5a993624"
5+
project_hash = "a0f7a38986e41d62db81da09b6b12da3c238b679"
66

77
[[deps.ADNLPModels]]
88
deps = ["ADTypes", "ForwardDiff", "LinearAlgebra", "NLPModels", "Requires", "ReverseDiff", "SparseArrays", "SparseConnectivityTracer", "SparseMatrixColorings"]

docs/src/assets/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
77
MINPACK = "4854310b-de5a-5eb6-a2a5-c1dee2bd17f9"
88
MadNLP = "2621e9c9-9eb4-46b1-8089-e8c72242dfb6"
9+
NLPModels = "a4795742-8479-5a88-8948-cc11e1c8c1a6"
910
NLPModelsIpopt = "f4238b75-b362-5c4c-b852-0801c9a21d71"
1011
NonlinearSolve = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
1112
OptimalControl = "5f98b655-cc9a-415a-b60e-744165666948"
@@ -15,12 +16,14 @@ Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1516
Tutorials = "cb10daa6-a5e5-4c25-a171-ae181b8ea3c9"
1617

1718
[compat]
19+
DataFrames = "1.7"
1820
DifferentiationInterface = "0.7"
1921
Documenter = "1.8"
2022
ForwardDiff = "0.10, 1"
2123
LinearAlgebra = "1"
2224
MINPACK = "1.3"
2325
MadNLP = "0.8"
26+
NLPModels = "0.21"
2427
NLPModelsIpopt = "0.10"
2528
NonlinearSolve = "4.6"
2629
OptimalControl = "1.0"

docs/src/tutorial-continuation.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
# Discrete continuation
22

3-
```@meta
4-
CurrentModule = OptimalControl
5-
Draft = false
6-
```
7-
83
By using the warm start option, it is easy to implement a basic discrete continuation method, in which a sequence of problems is solved by using each solution as the initial guess for the next problem.
94
This approach typically leads to faster and more reliable convergence than solving each problem with the same initial guess and is particularly useful for problems that require a good initial guess to converge.
105

0 commit comments

Comments
 (0)