Skip to content

Commit 3d10ce8

Browse files
authored
Merge pull request #215 from control-toolbox/214-bug-plot-issue
CTModelsPlots: replace @eval/@layout with local Matrix-based grid to …
2 parents 3b7e027 + 7b749a5 commit 3d10ce8

File tree

12 files changed

+78
-31
lines changed

12 files changed

+78
-31
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
version:
1414
- '1.10'
1515
- '1.11'
16+
- '1.12'
1617
os:
1718
- ubuntu-latest
1819
arch:

.markdownlint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"MD046": false,
3+
"MD013": false
4+
}

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "CTModels"
22
uuid = "34c4fa32-2049-4079-8329-de33c2a22e2d"
33
authors = ["Olivier Cots <olivier.cots@toulouse-inp.fr>"]
4-
version = "0.6.8"
4+
version = "0.6.9"
55

66
[deps]
77
CTBase = "54762871-cc72-4466-b8e8-f6c8b58076cd"

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[deps]
2+
CTParser = "32681960-a1b1-40db-9bff-a1ca817385d1"
23
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
34
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
45
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"

docs/src/plot.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,46 @@ Order = [:module, :constant, :type, :function, :macro]
3838
julia> private_fun(x)
3939
```
4040

41+
## Simple example
42+
43+
```@example
44+
using CTModels, Plots
45+
import CTParser: @def
46+
47+
t0, tf, x0 = 0.0, 1.0, -1.0
48+
49+
ocp = @def begin
50+
t ∈ [t0, tf], time
51+
x ∈ R, state
52+
u ∈ R, control
53+
x(t0) == x0
54+
-Inf ≤ x(t) + u(t) ≤ 0, (mixed_con)
55+
ẋ(t) == u(t)
56+
∫(0.5u(t)^2) → min
57+
end
58+
59+
sol = CTModels.build_solution(
60+
ocp,
61+
collect(range(t0, tf; length=201)),
62+
t -> x0 * exp(-t),
63+
t -> -x0 * exp(-t),
64+
Float64[],
65+
t -> exp(t - tf) - 1;
66+
objective = exp(-1) - 1,
67+
iterations = 0,
68+
constraints_violation = 0.0,
69+
message = "",
70+
status = :optimal,
71+
successful = true,
72+
)
73+
74+
plot(sol)
75+
```
76+
4177
## Documentation
4278

4379
```@autodocs
4480
Modules = [CTModelsPlots]
4581
Order = [:module, :constant, :type, :function, :macro]
4682
Pages = ["plot.jl", "plot_utils.jl", "plot_default.jl", "CTModelsPlots.jl"]
47-
```
83+
```

docs/src/print.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,29 @@ Modules = [CTModels, Base]
88
Order = [:module, :constant, :type, :function, :macro]
99
```
1010

11+
## Simple example
12+
13+
```@example
14+
using CTModels, Plots
15+
import CTParser: @def
16+
17+
t0, tf, x0 = 0.0, 1.0, -1.0
18+
19+
ocp = @def begin
20+
t ∈ [t0, tf], time
21+
x ∈ R, state
22+
u ∈ R, control
23+
x(t0) == x0
24+
-Inf ≤ x(t) + u(t) ≤ 0, (mixed_con)
25+
ẋ(t) == u(t)
26+
∫(0.5u(t)^2) → min
27+
end
28+
```
29+
1130
## Documentation
1231

1332
```@autodocs
1433
Modules = [CTModels]
1534
Order = [:module, :constant, :type, :function, :macro]
1635
Pages = ["print.jl"]
17-
```
36+
```

ext/plot.jl

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,19 +186,6 @@ end
186186
"""
187187
$(TYPEDSIGNATURES)
188188
189-
Return an expression `a{r*h}` to control relative plot height when using custom layouts.
190-
191-
Used for vertical space control in plot trees.
192-
"""
193-
function __height(r::Real)::Expr
194-
i = Expr(:call, :*, r, :h)
195-
a = Expr(:curly, :a, i)
196-
return a
197-
end
198-
199-
"""
200-
$(TYPEDSIGNATURES)
201-
202189
Return an empty plot for a `PlotLeaf`.
203190
204191
Used as a placeholder in layout trees.
@@ -382,11 +369,10 @@ function __initial_plot(
382369
nblines = 0
383370
if (!(node_xp isa EmptyPlot) && !(node_u isa EmptyPlot))
384371
nblines = n + l
385-
a = __height(round(n / nblines; digits=2))
386-
@eval lay = @layout [
387-
$a
388-
b
389-
]
372+
h = round(n / nblines; digits=2)
373+
lay = Matrix{Any}(undef, 2, 1)
374+
lay[1, 1] = (label = :a, width = :auto, height = h)
375+
lay[2, 1] = (label = :b, blank = false)
390376
root = PlotNode(lay, [node_xp, node_u])
391377
elseif !(node_xp isa EmptyPlot)
392378
root = node_xp
@@ -428,11 +414,10 @@ function __initial_plot(
428414
# update the root node
429415
if !(node_cocp isa EmptyPlot)
430416
nblines += nc
431-
c = __height(round(nc / nblines; digits=2))
432-
@eval lay = @layout [
433-
a
434-
$c
435-
]
417+
h = round(nc / nblines; digits=2)
418+
lay = Matrix{Any}(undef, 2, 1)
419+
lay[1, 1] = (label = :a, blank = false)
420+
lay[2, 1] = (label = :b, width = :auto, height = h)
436421
root = PlotNode(lay, [root, node_cocp])
437422
end
438423
end

src/CTModels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ See also: [`Time`](@ref), [`Times`](@ref).
8888
const TimesDisc = Union{Times,StepRangeLen}
8989

9090
"""
91-
Type alias for a dictionnary of constraints. This is used to store constraints before building the model.
91+
Type alias for a dictionary of constraints. This is used to store constraints before building the model.
9292
9393
```@example
9494
julia> const TimesDisc = Union{Times, StepRangeLen}

src/default.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ __format() = :JLD
1616
$(TYPEDSIGNATURES)
1717
1818
Used to set the default value of the label of a constraint.
19-
A unique value is given to each constraint using the `gensym` function and prefixing by `:unamed`.
19+
A unique value is given to each constraint using the `gensym` function and prefixing by `:unnamed`.
2020
"""
21-
__constraint_label() = gensym(:unamed)
21+
__constraint_label() = gensym(:unnamed)
2222

2323
"""
2424
$(TYPEDSIGNATURES)

src/types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ struct SolverInfos{TI<:Dict{Symbol,Any}} <: AbstractSolverInfos
558558
message::String # the message corresponding to the status criterion
559559
successful::Bool # whether or not the method has finished successfully: CN1, stagnation vs iterations max
560560
constraints_violation::Float64 # the constraints violation
561-
infos::TI # additional informations
561+
infos::TI # additional information
562562
end
563563

564564
# ------------------------------------------------------------------------------ #

0 commit comments

Comments
 (0)