Skip to content

Commit 55cf043

Browse files
authored
Merge pull request #545 from control-toolbox/544-general-update-compat
Up compat
2 parents ea1e64b + 72a3d64 commit 55cf043

11 files changed

Lines changed: 198 additions & 191 deletions

File tree

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1616
CTBase = "0.16"
1717
CTDirect = "0.14"
1818
CTFlows = "0.8"
19-
CTModels = "0.3"
20-
CTParser = "0.2"
19+
CTModels = "0.5"
20+
CTParser = "0.4"
2121
CommonSolve = "0.2"
2222
DocStringExtensions = "0.9"
2323
julia = "1.10"

docs/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1919
CTBase = "0.16"
2020
CTDirect = "0.14"
2121
CTFlows = "0.8"
22-
CTModels = "0.3"
23-
CTParser = "0.2"
22+
CTModels = "0.5"
23+
CTParser = "0.4"
2424
Documenter = "1.8"
2525
DocumenterMermaid = "0.2"
2626
JLD2 = "0.5"

docs/src/assets/Manifest.toml

Lines changed: 118 additions & 112 deletions
Large diffs are not rendered by default.

docs/src/assets/Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
2020
CTBase = "0.16"
2121
CTDirect = "0.14"
2222
CTFlows = "0.8"
23-
CTModels = "0.3"
24-
CTParser = "0.2"
23+
CTModels = "0.5"
24+
CTParser = "0.4"
2525
Documenter = "1.8"
2626
DocumenterMermaid = "0.2"
2727
JLD2 = "0.5"

docs/src/tutorial-double-integrator-energy.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,17 @@ export_ocp_solution(sol; filename="my_solution")
119119
end # hide
120120
sol_jld = import_ocp_solution(ocp; filename="my_solution")
121121
println("Objective from computed solution: ", objective(sol))
122-
println("Objective from imported solution: ", objective(sol_jld))
122+
# println("Objective from imported solution: ", objective(sol_jld))
123+
# the type of the imported solution is not the same as the original one
124+
println("Type of the imported solution: ", typeof(sol_jld))
125+
# the type of the original solution
126+
println("Type of the original solution: ", typeof(sol))
123127
```
124128

129+
!!! danger "Bug"
130+
131+
The `import_ocp_solution` function does not return a solution of the same type as the original one. This is a bug that will be fixed in a future release.
132+
125133
### JSON
126134

127135
```@example main

docs/src/tutorial-flow.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,11 @@ u(t, x, p) = p * (1 + tan(t))
233233
nothing # hide
234234
```
235235

236-
As before, the `Flow` function aims to compute $(x, p)$ from the optimal control problem `ocp` and the control in feedback form `u(t, x, p)`. However, we must indicate that the control depends on $t$, that is it is non-autonomous.
236+
As before, the `Flow` function aims to compute $(x, p)$ from the optimal control problem `ocp` and the control in feedback form `u(t, x, p)`.
237+
Since the problem is non-autonomous, we must provide a control law that depends on time.
237238

238239
```@example main
239-
f = Flow(ocp, u; autonomous=false)
240+
f = Flow(ocp, u)
240241
nothing # hide
241242
```
242243

docs/src/tutorial-plot.md

Lines changed: 28 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,8 @@ plot!(plt, args...; kw...) # modifies Plot `plt`
1010

1111
More precisely, the signature of `plot`, to plot a solution, is as follows.
1212

13-
```julia
14-
# only sol is required
15-
function plot(
16-
sol::CTModels.Solution, # optimal control solution
17-
description::Symbol...; # description to choose what to plot
18-
layout::Symbol, # layout of the subplots
19-
control::Symbol, # plot the norm or components of the control
20-
time::Symbol, # normalise the time or not
21-
size::Tuple, # size of the figure
22-
state_style::Union{NamedTuple,Symbol}, # style: state trajectory
23-
costate_style::Union{NamedTuple,Symbol}, # style: costate trajectory
24-
control_style::Union{NamedTuple,Symbol}, # style: control trajectory
25-
kwargs..., # other attributes from Plots
26-
)
27-
```
28-
29-
You can provide additional information to the `plot` function by supplying the optimal control problem: `plot(sol, ocp)`. The function signature when including the optimal control problem is as follows:
30-
31-
```julia
32-
# only sol and model are required
33-
function Plots.plot(
34-
sol::CTModels.Solution,
35-
ocp::CTModels.Model, # optimal control problem
36-
description::Symbol...;
37-
layout::Symbol,
38-
control::Symbol,
39-
time::Symbol,
40-
size::Tuple,
41-
time_style::Union{NamedTuple,Symbol}, # style: vertical time axes
42-
state_style::Union{NamedTuple,Symbol},
43-
state_bounds_style::Union{NamedTuple,Symbol}, # style: state bounds
44-
control_style::Union{NamedTuple,Symbol},
45-
control_bounds_style::Union{NamedTuple,Symbol}, # style: control bounds
46-
costate_style::Union{NamedTuple,Symbol},
47-
path_style::Union{NamedTuple,Symbol}, # style: path constraints
48-
path_bounds_style::Union{NamedTuple,Symbol}, # style: path constraints bounds
49-
dual_style::Union{NamedTuple,Symbol}, # style: path constraints dual variable
50-
kwargs...,
51-
)
13+
```@docs
14+
plot(::CTModels.Solution, ::Symbol...)
5215
```
5316

5417
## Argument Overview
@@ -60,7 +23,7 @@ The table below summarizes the main plotting arguments and links to the correspo
6023
| [Basic concepts](@ref tutorial-plot-basic) | `size`, `state_style`, `costate_style`, `control_style`, `time_style`, `kwargs...` |
6124
| [Split vs. group layout](@ref tutorial-plot-layout) | `layout` |
6225
| [Plotting control norm](@ref tutorial-plot-control) | `control` |
63-
| [Normalized time](@ref tutorial-plot-time) | `time` |
26+
| [Normalised time](@ref tutorial-plot-time) | `time` |
6427
| [Constraints](@ref tutorial-plot-constraints) | `state_bounds_style`, `control_bounds_style`, `path_style`, `path_bounds_style`, `dual_style` |
6528
| [What to plot](@ref tutorial-plot-select) | `description...` |
6629

@@ -157,11 +120,11 @@ plot(sol;
157120
control_style = (color=:red, linewidth=2)) # style: control trajectory
158121
```
159122

160-
If you provide the optimal control problem, vertical axes at the initial and final times are automatically plotted.
123+
Vertical axes at the initial and final times are automatically plotted.
161124
Additionally, you can choose not to display the state and costate trajectories by setting their styles to `:none`.
162125

163126
```@example main
164-
plot(sol, ocp;
127+
plot(sol;
165128
state_style = :none, # do not plot the state
166129
costate_style = :none, # do not plot the costate
167130
control_style = (color = :red,), # plot the control in red
@@ -171,7 +134,7 @@ plot(sol, ocp;
171134
To select what to display, you can also use the `description` argument by providing a list of symbols such as `:state`, `:costate`, and `:control`.
172135

173136
```@example main
174-
plot(sol, ocp, :state, :control) # plot the state and the control
137+
plot(sol, :state, :control) # plot the state and the control
175138
```
176139

177140
!!! note "Select what to plot"
@@ -283,7 +246,7 @@ plot(t, norm∘u; label="‖u‖", xlabel="t")
283246

284247
## [Normalised time](@id tutorial-plot-time)
285248

286-
We consider a [LQR example](https://control-toolbox.org/Tutorials.jl/stable/tutorial-lqr-basic.html) and solve the problem for different values of the final time `tf`. Then, we plot the solutions on the same figure using a normalized time $s = (t - t_0) / (t_f - t_0)$, enabled by the keyword argument `time = :normalize` (or `:normalise`) in the `plot` function.
249+
We consider a [LQR example](https://control-toolbox.org/Tutorials.jl/stable/tutorial-lqr-basic.html) and solve the problem for different values of the final time `tf`. Then, we plot the solutions on the same figure using a normalised time $s = (t - t_0) / (t_f - t_0)$, enabled by the keyword argument `time = :normalize` (or `:normalise`) in the `plot` function.
287250

288251
```@example main
289252
# definition of the problem, parameterised by the final time
@@ -310,16 +273,15 @@ for tf ∈ tfs
310273
end
311274
312275
# create plots
313-
plt = plot(solutions[1]; time=:normalize)
314-
for sol ∈ solutions[2:end]
315-
plot!(plt, sol; time=:normalize)
276+
plt = plot()
277+
for (tf, sol)zip(tfs, solutions)
278+
plot!(plt, sol; time=:normalize, label="tf = $tf", xlabel="s")
316279
end
317280
318281
# make a custom plot: keep only state and control
319-
N = length(tfs)
320-
px1 = plot(plt[1]; legend=false, xlabel="s", ylabel="x₁")
321-
px2 = plot(plt[2]; label=reshape(["tf = $tf" for tf ∈ tfs], (1, N)), xlabel="s", ylabel="x₂")
322-
pu = plot(plt[5]; legend=false, xlabel="s", ylabel="u")
282+
px1 = plot(plt[1]; legend=false) # x₁
283+
px2 = plot(plt[2]; legend=true) # x₂
284+
pu = plot(plt[5]; legend=false) # u
323285
324286
using Plots.PlotMeasures # for leftmargin, bottommargin
325287
plot(px1, px2, pu; layout=(1, 3), size=(800, 300), leftmargin=5mm, bottommargin=5mm)
@@ -346,15 +308,15 @@ ocp = @def begin
346308
tf → min
347309
end
348310
sol = solve(ocp)
349-
plot(sol, ocp)
311+
plot(sol)
350312
```
351313

352314
On the plot, you can see the lower and upper bounds of the path constraint. Additionally, the dual variable associated with the path constraint is displayed alongside it.
353315

354-
You can customize the plot styles. For style options related to the state, costate, and control, refer to the [Basic Concepts](@ref tutorial-plot-basic) section.
316+
You can customise the plot styles. For style options related to the state, costate, and control, refer to the [Basic Concepts](@ref tutorial-plot-basic) section.
355317

356318
```@example main
357-
plot(sol, ocp;
319+
plot(sol;
358320
state_bounds_style = (linestyle = :dash,),
359321
control_bounds_style = (linestyle = :dash,),
360322
path_style = (color = :green,),
@@ -369,31 +331,31 @@ plot(sol, ocp;
369331
You can choose what to plot using the `description` argument. To plot only one subgroup:
370332

371333
```julia
372-
plot(sol, ocp, :state) # plot only the state
373-
plot(sol, ocp, :costate) # plot only the costate
374-
plot(sol, ocp, :control) # plot only the control
375-
plot(sol, ocp, :path) # plot only the path constraint
376-
plot(sol, ocp, :dual) # plot only the path constraint dual variable
334+
plot(sol, :state) # plot only the state
335+
plot(sol, :costate) # plot only the costate
336+
plot(sol, :control) # plot only the control
337+
plot(sol, :path) # plot only the path constraint
338+
plot(sol, :dual) # plot only the path constraint dual variable
377339
```
378340

379341
You can combine elements to plot exactly what you need:
380342

381343
```@example main
382-
plot(sol, ocp, :state, :control, :path)
344+
plot(sol, :state, :control, :path)
383345
```
384346

385347
Similarly, you can choose what not to plot passing `:none` to the corresponding style.
386348

387349
```julia
388-
plot(sol, ocp; state_style=:none) # do not plot the state
389-
plot(sol, ocp; costate_style=:none) # do not plot the costate
390-
plot(sol, ocp; control_style=:none) # do not plot the control
391-
plot(sol, ocp; path_style=:none) # do not plot the path constraint
392-
plot(sol, ocp; dual_style=:none) # do not plot the path constraint dual variable
350+
plot(sol; state_style=:none) # do not plot the state
351+
plot(sol; costate_style=:none) # do not plot the costate
352+
plot(sol; control_style=:none) # do not plot the control
353+
plot(sol; path_style=:none) # do not plot the path constraint
354+
plot(sol; dual_style=:none) # do not plot the path constraint dual variable
393355
```
394356

395357
For instance, let's plot everything except the dual variable associated with the path constraint.
396358

397359
```@example main
398-
plot(sol, ocp; dual_style=:none)
360+
plot(sol; dual_style=:none)
399361
```

src/OptimalControl.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import CTModels:
2929
constraint!,
3030
objective!,
3131
definition!,
32+
time_dependence!,
3233
# model
33-
build_model,
34+
build,
3435
Model,
3536
PreModel,
3637
# getters

test/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
1010
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1111

1212
[compat]
13-
CTModels = "0.3"
13+
CTModels = "0.5"
1414
DifferentiationInterface = "0.6"
1515
ForwardDiff = "0.10"
1616
LinearAlgebra = "1"

test/ctdirect/problems/goddard.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ function goddard_all()
113113
label=:boundary,
114114
)
115115
CTModels.definition!(pre_ocp, Expr(:goddard_all))
116-
ocp = CTModels.build_model(pre_ocp)
116+
CTModels.time_dependence!(pre_ocp; autonomous=true)
117+
ocp = CTModels.build(pre_ocp)
117118

118119
return ((
119120
ocp=ocp,
@@ -174,7 +175,8 @@ function goddard_all_outplace()
174175
pre_ocp, :boundary; f=bc, lb=[r0, v0, m0, mf], ub=[r0, v0, m0, mf], label=:boundary
175176
)
176177
CTModels.definition!(pre_ocp, Expr(:goddard_all))
177-
ocp = CTModels.build_model(pre_ocp)
178+
CTModels.time_dependence!(pre_ocp; autonomous=true)
179+
ocp = CTModels.build(pre_ocp)
178180

179181
return ((
180182
ocp=ocp,

0 commit comments

Comments
 (0)