Skip to content

Commit a35da8f

Browse files
authored
Merge pull request #21 from control-toolbox/auto-juliaformatter-pr
[AUTO] JuliaFormatter.jl run
2 parents 254eb3d + 70ad916 commit a35da8f

File tree

4 files changed

+235
-188
lines changed

4 files changed

+235
-188
lines changed

docs/extras/disc.jl

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ vmax = 0.1 # maximal authorized speed
1010
mf = 0.6 # final mass to target
1111

1212
ocp = @def begin # definition of the optimal control problem
13-
1413
tf R, variable
1514
t [t0, tf], time
1615
x = (r, v, m) R³, state
@@ -25,7 +24,6 @@ ocp = @def begin # definition of the optimal control problem
2524
(t) == F0(x(t)) + u(t) * F1(x(t))
2625

2726
r(tf) max
28-
2927
end;
3028

3129
# Dynamics
@@ -37,17 +35,19 @@ b = 2
3735
F0(x) = begin
3836
r, v, m = x
3937
D = Cd * v^2 * exp(-β*(r - 1)) # Drag force
40-
return [ v, -D/m - 1/r^2, 0 ]
38+
return [v, -D/m - 1/r^2, 0]
4139
end
4240

4341
F1(x) = begin
4442
r, v, m = x
45-
return [ 0, Tmax/m, -b*Tmax ]
43+
return [0, Tmax/m, -b*Tmax]
4644
end
4745

4846
# Solve the problem with different discretization methods
4947
solutions = []
50-
schemes = [:trapeze, :midpoint, :euler, :euler_implicit, :gauss_legendre_2, :gauss_legendre_3]
48+
schemes = [
49+
:trapeze, :midpoint, :euler, :euler_implicit, :gauss_legendre_2, :gauss_legendre_3
50+
]
5151
for scheme in schemes
5252
sol = solve(ocp; disc_method=scheme, tol=1e-8, display=false)
5353
push!(solutions, (scheme, sol))
@@ -71,30 +71,27 @@ using DataFrames
7171
using BenchmarkTools
7272

7373
# DataFrame to store the results
74-
data = DataFrame(
74+
data = DataFrame(;
7575
Backend=Symbol[],
7676
NNZO=Int[],
7777
NNZJ=Int[],
7878
NNZH=Int[],
7979
PrepTime=Float64[],
8080
ExecTime=Float64[],
81-
Objective=Float64[],
82-
Iterations=Int[]
81+
Objective=Float64[],
82+
Iterations=Int[],
8383
)
8484

8585
# The different AD backends to test
8686
backends = [:optimized, :manual]
8787

8888
# Loop over the backends
8989
for adnlp_backend in backends
90-
9190
println("Testing backend: ", adnlp_backend)
9291

9392
# 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,
93+
bt = @btimed direct_transcription(
94+
$ocp; disc_method=:euler, grid_size=1000, adnlp_backend=($adnlp_backend)
9895
)
9996
docp, nlp = bt.value
10097
prepa_time = bt.time
@@ -105,27 +102,23 @@ for adnlp_backend in backends
105102
nnzh = get_nnzh(nlp) # Hessian of the Lagrangian
106103

107104
# Solve the problem
108-
bt = @btimed ipopt($nlp;
109-
print_level=0,
110-
mu_strategy="adaptive",
111-
tol=1e-8,
112-
sb="yes",
113-
)
105+
bt = @btimed ipopt($nlp; print_level=0, mu_strategy="adaptive", tol=1e-8, sb="yes")
114106
nlp_sol = bt.value
115107
exec_time = bt.time
116108

117109
# Store the results in the DataFrame
118-
push!(data,
110+
push!(
111+
data,
119112
(
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-
)
113+
Backend=adnlp_backend,
114+
NNZO=nnzo,
115+
NNZJ=nnzj,
116+
NNZH=nnzh,
117+
PrepTime=prepa_time,
118+
ExecTime=exec_time,
119+
Objective=(-nlp_sol.objective),
120+
Iterations=nlp_sol.iter,
121+
),
129122
)
130123
end
131-
println(data)
124+
println(data)

docs/extras/iss.jl

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ const t0 = 0
77
const tf = 1
88
const x0 = -1
99
const xf = 0
10-
const α = 1.5
10+
const α = 1.5
1111
ocp = @def begin
12-
1312
t [t0, tf], time
1413
x R, state
1514
u R, control
@@ -19,8 +18,7 @@ ocp = @def begin
1918

2019
(t) == -x(t) + α * x(t)^2 + u(t)
2120

22-
( 0.5u(t)^2 ) min
23-
21+
(0.5u(t)^2) min
2422
end
2523
nothing # hide
2624

@@ -29,7 +27,7 @@ u(x, p) = p
2927

3028
π((x, p)) = x
3129

32-
S(p0) = π( φ(t0, x0, p0, tf) ) - xf # shooting function
30+
S(p0) = π(φ(t0, x0, p0, tf)) - xf # shooting function
3331

3432
ξ = [0.1] # initial guess
3533

@@ -40,19 +38,20 @@ function fsolve(f, j, x; kwargs...)
4038
catch e
4139
println("Erreur using MINPACK")
4240
println(e)
43-
println("hybrj not supported. Replaced by hybrd even if it is not visible on the doc.")
41+
println(
42+
"hybrj not supported. Replaced by hybrd even if it is not visible on the doc."
43+
)
4444
MINPACK.fsolve(f, x; kwargs...)
4545
end
4646
end
4747

4848
using DifferentiationInterface
49-
import ForwardDiff
49+
using ForwardDiff: ForwardDiff
5050
backend = AutoForwardDiff()
5151

52-
nle! = ( s, ξ) -> s[1] = S(ξ[1]) # auxiliary function
52+
nle! = (s, ξ) -> s[1] = S(ξ[1]) # auxiliary function
5353
jnle! = (js, ξ) -> jacobian!(nle!, similar(ξ), js, backend, ξ) #
5454

55-
5655
indirect_sol = fsolve(nle!, jnle!, ξ; show_trace=true) # resolution of S(p0) = 0
5756
p0_sol = indirect_sol.x[1] # costate solution
5857
println("\ncostate: p0 = ", p0_sol)
@@ -61,71 +60,85 @@ println("shoot: |S(p0)| = ", abs(S(p0_sol)), "\n")
6160
sol = φ((t0, tf), x0, p0_sol)
6261
plot(sol)
6362

64-
using Plots.PlotMeasures
65-
function Plots.plot(S::Function, p0::Float64; Np0=20, kwargs...)
66-
63+
using Plots.PlotMeasures
64+
function Plots.plot(S::Function, p0::Float64; Np0=20, kwargs...)
65+
6766
# times for wavefronts
68-
times = range(t0, tf, length=3)
67+
times = range(t0, tf; length=3)
6968

7069
# times for trajectories
71-
tspan = range(t0, tf, length=100)
70+
tspan = range(t0, tf; length=100)
7271

7372
# interval of initial covector
74-
p0_min = -0.5
75-
p0_max = 2
73+
p0_min = -0.5
74+
p0_max = 2
7675

7776
# covector solution
78-
p0_sol = p0
79-
77+
p0_sol = p0
78+
8079
# plot of the flow in phase space
81-
plt_flow = plot()
82-
p0s = range(p0_min, p0_max, length=Np0)
83-
for i eachindex(p0s)
80+
plt_flow = plot()
81+
p0s = range(p0_min, p0_max; length=Np0)
82+
for i in eachindex(p0s)
8483
sol = φ((t0, tf), x0, p0s[i])
8584
x = state(sol).(tspan)
8685
p = costate(sol).(tspan)
87-
label = i==1 ? "extremals" : false
88-
plot!(plt_flow, x, p, color=:blue, label=label)
89-
end
90-
86+
label = i==1 ? "extremals" : false
87+
plot!(plt_flow, x, p; color=:blue, label=label)
88+
end
89+
9190
# plot of wavefronts in phase space
92-
p0s = range(p0_min, p0_max, length=200)
93-
xs = zeros(length(p0s), length(times))
94-
ps = zeros(length(p0s), length(times))
95-
for i eachindex(p0s)
96-
sol = φ((t0, tf), x0, p0s[i], saveat=times)
97-
xs[i, :] .= state(sol).(times)
98-
ps[i, :] .= costate(sol).(times)
99-
end
100-
for j eachindex(times)
101-
label = j==1 ? "flow at times" : false
102-
plot!(plt_flow, xs[:, j], ps[:, j], color=:green, linewidth=2, label=label)
103-
end
104-
91+
p0s = range(p0_min, p0_max; length=200)
92+
xs = zeros(length(p0s), length(times))
93+
ps = zeros(length(p0s), length(times))
94+
for i in eachindex(p0s)
95+
sol = φ((t0, tf), x0, p0s[i]; saveat=times)
96+
xs[i, :] .= state(sol).(times)
97+
ps[i, :] .= costate(sol).(times)
98+
end
99+
for j in eachindex(times)
100+
label = j==1 ? "flow at times" : false
101+
plot!(plt_flow, xs[:, j], ps[:, j]; color=:green, linewidth=2, label=label)
102+
end
103+
105104
#
106-
plot!(plt_flow, xlims=(-1.1, 1), ylims=(p0_min, p0_max))
107-
plot!(plt_flow, [0, 0], [p0_min, p0_max], color=:black, xlabel="x", ylabel="p", label="x=xf")
108-
105+
plot!(plt_flow; xlims=(-1.1, 1), ylims=(p0_min, p0_max))
106+
plot!(
107+
plt_flow,
108+
[0, 0],
109+
[p0_min, p0_max];
110+
color=:black,
111+
xlabel="x",
112+
ylabel="p",
113+
label="x=xf",
114+
)
115+
109116
# solution
110117
sol = φ((t0, tf), x0, p0_sol)
111118
x = state(sol).(tspan)
112119
p = costate(sol).(tspan)
113-
plot!(plt_flow, x, p, color=:red, linewidth=2, label="extremal solution")
114-
plot!(plt_flow, [x[end]], [p[end]], seriestype=:scatter, color=:green, label=false)
115-
120+
plot!(plt_flow, x, p; color=:red, linewidth=2, label="extremal solution")
121+
plot!(plt_flow, [x[end]], [p[end]]; seriestype=:scatter, color=:green, label=false)
122+
116123
# plot of the shooting function
117-
p0s = range(p0_min, p0_max, length=200)
118-
plt_shoot = plot(xlims=(p0_min, p0_max), ylims=(-2, 4), xlabel="p₀", ylabel="y")
119-
plot!(plt_shoot, p0s, S, linewidth=2, label="S(p₀)", color=:green)
120-
plot!(plt_shoot, [p0_min, p0_max], [0, 0], color=:black, label="y=0")
121-
plot!(plt_shoot, [p0_sol, p0_sol], [-2, 0], color=:black, label="p₀ solution", linestyle=:dash)
122-
plot!(plt_shoot, [p0_sol], [0], seriestype=:scatter, color=:green, label=false)
123-
124+
p0s = range(p0_min, p0_max; length=200)
125+
plt_shoot = plot(; xlims=(p0_min, p0_max), ylims=(-2, 4), xlabel="p₀", ylabel="y")
126+
plot!(plt_shoot, p0s, S; linewidth=2, label="S(p₀)", color=:green)
127+
plot!(plt_shoot, [p0_min, p0_max], [0, 0]; color=:black, label="y=0")
128+
plot!(
129+
plt_shoot,
130+
[p0_sol, p0_sol],
131+
[-2, 0];
132+
color=:black,
133+
label="p₀ solution",
134+
linestyle=:dash,
135+
)
136+
plot!(plt_shoot, [p0_sol], [0]; seriestype=:scatter, color=:green, label=false)
137+
124138
# final plot
125-
plot(plt_flow, plt_shoot; layout=(1,2), leftmargin=15px, bottommargin=15px, kwargs...)
126-
139+
plot(plt_flow, plt_shoot; layout=(1, 2), leftmargin=15px, bottommargin=15px, kwargs...)
127140
end
128141

129142
p0_sol
130143

131-
plot(S, p0_sol; size=(800, 450))
144+
plot(S, p0_sol; size=(800, 450))

0 commit comments

Comments
 (0)