Skip to content

Commit

Permalink
Merge pull request #145 from control-toolbox/fixplot
Browse files Browse the repository at this point in the history
default plot size
  • Loading branch information
ocots authored Jun 8, 2024
2 parents 406d822 + 7260d71 commit 1dddea4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/CTBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ using Interpolations: linear_interpolation, Line, Interpolations # for default i
using MLStyle # pattern matching
using Parameters # @with_kw: to have default values in struct
using Plots
import Plots: plot, plot! # import instead of using to overload the plot and plot! functions
using Printf # to print an OptimalControlModel
using Printf # to print an Opt imalControlModel
using DataStructures # OrderedDict for aliases
using Unicode # unicode primitives
using PrettyTables # to print a table
Expand Down
28 changes: 23 additions & 5 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function __plot_time!(p::Union{Plots.Plot, Plots.Subplot}, sol::OptimalControlSo
end

# reset ylims: ylims=:auto
CTBase.plot!(p, sol, :time, (s, i), time; ylims=:auto, xlabel=t_label, label=label, kwargs...) # use simple plot
Plots.plot!(p, sol, :time, (s, i), time; ylims=:auto, xlabel=t_label, label=label, kwargs...) # use simple plot

# change ylims if the gap between min and max is less than a tol
tol = 1e-3
Expand Down Expand Up @@ -262,7 +262,7 @@ Plot the optimal control solution `sol` using the layout `layout`.
- `time` can be `:default` or `:normalized`.
- The keyword arguments `state_style`, `control_style` and `costate_style` are passed to the `plot` function of the `Plots` package. The `state_style` is passed to the plot of the state, the `control_style` is passed to the plot of the control and the `costate_style` is passed to the plot of the costate.
"""
function CTBase.plot!(p::Plots.Plot, sol::OptimalControlSolution; layout::Symbol=:split,
function Plots.plot!(p::Plots.Plot, sol::OptimalControlSolution; layout::Symbol=:split,
control::Symbol=:components, time::Symbol=:default,
state_style=(), control_style=(), costate_style=(), kwargs...)

Expand Down Expand Up @@ -326,6 +326,17 @@ function CTBase.plot!(p::Plots.Plot, sol::OptimalControlSolution; layout::Symbol

end

function __size_plot(sol::OptimalControlSolution, control::Symbol)
n = sol.state_dimension
#m = sol.control_dimension
m = @match control begin
:components => sol.control_dimension
:norm => 1
:all => sol.control_dimension + 1
_ => throw(IncorrectArgument("No such choice for control. Use :components, :norm or :all"))
end
return (600, 140*(n+m))
end

"""
$(TYPEDSIGNATURES)
Expand All @@ -337,10 +348,17 @@ Plot the optimal control solution `sol` using the layout `layout`.
- The argument `layout` can be `:group` or `:split` (default).
- The keyword arguments `state_style`, `control_style` and `costate_style` are passed to the `plot` function of the `Plots` package. The `state_style` is passed to the plot of the state, the `control_style` is passed to the plot of the control and the `costate_style` is passed to the plot of the costate.
"""
function CTBase.plot(sol::OptimalControlSolution; layout::Symbol=:split,
control::Symbol=:components, time::Symbol=:default, state_style=(), control_style=(), costate_style=(), kwargs...)
function Plots.plot(sol::OptimalControlSolution;
layout::Symbol=:split,
control::Symbol=:components,
time::Symbol=:default,
size=__size_plot(sol, control),
state_style=(),
control_style=(),
costate_style=(),
kwargs...)
#
p = __initial_plot(sol; layout=layout, kwargs...)
p = __initial_plot(sol; layout=layout, control=control, size=size, kwargs...)
#
return plot!(p, sol; layout=layout, control=control, time=time,
state_style=state_style, control_style=control_style, costate_style=costate_style, kwargs...)
Expand Down
14 changes: 7 additions & 7 deletions src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,18 @@ function Base.show(io::IO, ::MIME"text/plain", ocp::OptimalControlModel{<: TimeD
#is_variable_dependent(ocp) && push!(header, "variable")
push!(header, "variable")
push!(header, "dynamics*", "objective*", "constraints")
data = hcat(__is_time_not_set(ocp) ? "" : "",
__is_state_not_set(ocp) ? "" : "",
__is_control_not_set(ocp) ? "" : "")
data = hcat(__is_time_not_set(ocp) ? "" : "",
__is_state_not_set(ocp) ? "" : "",
__is_control_not_set(ocp) ? "" : "")
#is_variable_dependent(ocp) &&
begin
(data = hcat(data,
__is_variable_not_set(ocp) ? "" : ""))
__is_variable_not_set(ocp) ? "" : ""))
end
data = hcat(data,
__is_dynamics_not_set(ocp) ? "" : "",
__is_objective_not_set(ocp) ? "" : "",
isempty(ocp.constraints) ? "" : "")
__is_dynamics_not_set(ocp) ? "" : "",
__is_objective_not_set(ocp) ? "" : "",
isempty(ocp.constraints) ? "" : "")
println("")
pretty_table(data, header=header, header_crayon=crayon"yellow")
nothing
Expand Down
4 changes: 3 additions & 1 deletion test/test_plot_manual.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CTBase
#using Plots

layout = :split
size = (900, 600)
Expand Down Expand Up @@ -51,11 +52,12 @@ sol.message = "ceci est un test"
sol.success = true

#
plt = plot(sol, layout=layout, size=size, control=control_plt)
plt = plot(sol, layout=layout, control=control_plt) #, size=size)
#plot(sol, layout=:group)
#ps=plot(sol, :time, (:state, 1))
#plot!(ps, sol, :time, (:control, 1))

return
# ----------------------------------------
# SOL 2
n=2
Expand Down

0 comments on commit 1dddea4

Please sign in to comment.