Skip to content

Commit

Permalink
Merge pull request #120 from control-toolbox/plot
Browse files Browse the repository at this point in the history
Plot limits
  • Loading branch information
ocots authored Oct 24, 2023
2 parents ab06379 + a7f4d7c commit 7c9b382
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 240 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/Documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ jobs:
registry: control-toolbox/ct-registry
- uses: julia-actions/julia-buildpkg@v1
with:
version: '1.8'
version: '1.9'
- name: Install dependencies
run: julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- name: Build and deploy
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # If authenticating with GitHub Actions token
DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} # If authenticating with SSH deploy key
GKSwstype: 100 # To make GitHub Action work, disable showing a plot window with the GR backend of the Plots package
run: julia --project=docs/ -e 'ENV["GKSwstype"]="nul" ; include("docs/make.jl")'
8 changes: 3 additions & 5 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@ using Documenter
using CTBase

makedocs(
warnonly = [:cross_references, :autodocs_block],
sitename = "CTBase.jl",
format = Documenter.HTML(prettyurls = false),
pages = [
"Introduction" => "index.md",
"API" => ["api-ctbase.md",
#"api-callbacks.md",
#"api-checking.md",
"api-default.md",
"api-callbacks.md",
"api-description.md",
"api-diffgeom.md",
"api-exceptions.md",
#"api-functions.md",
"api-model.md",
"api-parser.md",
"api-plot.md",
"api-print.md",
"api-ctrepl.md",
"api-repl.md",
"api-types.md",
"api-utils.md"],
"Developers" => "api-developers.md"
Expand Down
19 changes: 0 additions & 19 deletions docs/src/api-checking.md

This file was deleted.

17 changes: 0 additions & 17 deletions docs/src/api-default.md

This file was deleted.

4 changes: 2 additions & 2 deletions docs/src/api-developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
## Index

```@index
Pages = ["api-callbacks.md"]
Pages = ["api-developers.md"]
Modules = [CTBase]
Order = [:module, :constant, :type, :function, :macro]
Private = false
Public = false
```

## Documentation
Expand Down
19 changes: 0 additions & 19 deletions docs/src/api-functions.md

This file was deleted.

135 changes: 0 additions & 135 deletions docs/src/api-plot.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,138 +6,3 @@ Order = [:module, :constant, :type, :function, :macro]
Pages = ["plot.jl"]
Private = false
```

## Examples

```@setup main
using CTBase
# create a solution
n=2
m=1
t0=0.0
tf=1.0
x0=[-1.0, 0.0]
xf=[0.0, 0.0]
a = x0[1]
b = x0[2]
C = [-(tf-t0)^3/6.0 (tf-t0)^2/2.0
-(tf-t0)^2/2.0 (tf-t0)]
D = [-a-b*(tf-t0), -b]+xf
p0 = C\D
α = p0[1]
β = p0[2]
x(t) = [a+b*(t-t0)+β*(t-t0)^2/2.0-α*(t-t0)^3/6.0, b+β*(t-t0)-α*(t-t0)^2/2.0]
p(t) = [α, -α*(t-t0)+β]
u(t) = [p(t)[2]]
objective = 0.5*(α^2*(tf-t0)^3/3+β^2*(tf-t0)-α*β*(tf-t0)^2)
#
N=201
times = range(t0, tf, N)
#
sol = OptimalControlSolution()
sol.state_dimension = n
sol.control_dimension = m
sol.times = times
sol.time_name="t"
sol.state = x
sol.state_components_names = [ "x" * ctindices(i) for i ∈ range(1, n)]
sol.costate = p
sol.control = u
sol.control_components_names = [ "u" ]
sol.objective = objective
sol.iterations = 0
sol.stopping = :dummy
sol.message = "ceci est un test"
sol.success = true
```

Let us consider we have defined an optimal control problem

```julia
ocp = Model()
...
```

and solve it

```julia
sol = solve(ocp)
```

We can plot the solution with the default layout `:split`.

```@example main
plot(sol, layout=:split, size=(800, 600))
```

Or with the layout `:group`.

```@example main
plot(sol, layout=:group, size=(800, 300))
```

You can specify some styles:

```@example main
plot_sol = plot(sol,
state_style=(color=:blue,),
costate_style=(color=:black, linestyle=:dash),
control_style=(color=:red, linewidth=2),
size=(800, 600))
```

You can also add additional plots with the command `plot!`.

```@setup main
using CTBase
# create a other solution
n=2
m=1
t0=0.0
tf=1.0
x0=[-0.5, 0.0]
xf=[0.0, 0.0]
a = x0[1]
b = x0[2]
C = [-(tf-t0)^3/6.0 (tf-t0)^2/2.0
-(tf-t0)^2/2.0 (tf-t0)]
D = [-a-b*(tf-t0), -b]+xf
p0 = C\D
α = p0[1]
β = p0[2]
x(t) = [a+b*(t-t0)+β*(t-t0)^2/2.0-α*(t-t0)^3/6.0, b+β*(t-t0)-α*(t-t0)^2/2.0]
p(t) = [α, -α*(t-t0)+β]
u(t) = [p(t)[2]]
objective = 0.5*(α^2*(tf-t0)^3/3+β^2*(tf-t0)-α*β*(tf-t0)^2)
#
N=201
times = range(t0, tf, N)
#
sol2 = OptimalControlSolution()
sol2.state_dimension = n
sol2.control_dimension = m
sol2.times = times
sol2.time_name="t"
sol2.state = x
sol2.state_components_names = [ "x" * ctindices(i) for i ∈ range(1, n)]
sol2.costate = p
sol2.control = u
sol2.control_components_names = [ "u" ]
sol2.objective = objective
sol2.iterations = 0
sol2.stopping = :dummy
sol2.message = "ceci est un test"
sol2.success = true
```

```@example main
plot!(plot_sol, sol2,
state_style=(color=:purple,),
costate_style=(color=:grey, linestyle=:dash),
control_style=(color=:pink, linewidth=2),
size=(800, 600))
```
2 changes: 1 addition & 1 deletion docs/src/api-ctrepl.md → docs/src/api-repl.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Index

```@index
Pages = ["api-ctrepl.md"]
Pages = ["api-repl.md"]
Modules = [CTBase]
Order = [:module, :constant, :type, :function, :macro]
Private = false
Expand Down
4 changes: 2 additions & 2 deletions src/ctparser_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ has(e, x, t) = begin
if :yes args
:yes
else @match ee begin
  :( $eee($tt) ) => (tt == t && has(eee, x)) ? :yes : ee
  _ => ee end
:( $eee($tt) ) => (tt == t && has(eee, x)) ? :yes : ee
_ => ee end
end
end
expr_it(e, foo(x, t), x -> x) == :yes
Expand Down
Loading

0 comments on commit 7c9b382

Please sign in to comment.