From 5d8abefdc2dcf6487643ece3a87cc5d48b14cafd Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Mon, 10 Jun 2024 19:24:22 +0200 Subject: [PATCH 1/2] fix plot constant negative value --- src/plot.jl | 2 +- test/test_plot_joseph.jl | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/test_plot_joseph.jl diff --git a/src/plot.jl b/src/plot.jl index 5f6295a1..7261faae 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -63,7 +63,7 @@ function __plot_time!(p::Union{Plots.Plot, Plots.Subplot}, sol::OptimalControlSo if (ymin != Inf) && (ymax != -Inf) && (abs(ymax-ymin) ≤ abs(ymin)*tol) ymiddle = (ymin+ymax)/2.0 - ylims!(p, (0.9*ymiddle, 1.1*ymiddle)) + ymiddle > 0 ? ylims!(p, (0.9*ymiddle, 1.1*ymiddle)) : ylims!(p, (1.1*ymiddle, 0.9*ymiddle)) end return p diff --git a/test/test_plot_joseph.jl b/test/test_plot_joseph.jl new file mode 100644 index 00000000..29540eaa --- /dev/null +++ b/test/test_plot_joseph.jl @@ -0,0 +1,53 @@ +# using OptimalControl + +# @def ocp begin + +# t ∈ [ 0, 1 ], time +# x ∈ R, state +# u ∈ R, control +# x(0) == 0 +# -1 ≤ u(t) ≤ 1 +# ẋ(t) == -x(t) + u(t) +# x(1) → min + +# end + +# sol = solve(ocp, grid_size=20, print_level=5) +# plt = plot(sol) + +# a la main +using CTBase + +n=1 +m=1 +t0=0 +tf=1 +x0=0 +x = t -> -1+1e-8*rand() +p = t -> +1+1e-8*rand() +u = t -> -1 +objective = 1 +# +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_name = "x" +sol.state_components_names = [ "x" ] +sol.costate = p +sol.control = u +sol.control_name = "u" +sol.control_components_names = [ "u" ] +sol.objective = objective +sol.iterations = 0 +sol.stopping = :dummy +sol.message = "ceci est un test" +sol.success = true + +plt = plot(sol) \ No newline at end of file From f8773468cfb6f0a7018f39417495ce9295fe244b Mon Sep 17 00:00:00 2001 From: Olivier Cots Date: Mon, 10 Jun 2024 19:40:22 +0200 Subject: [PATCH 2/2] fix plot constant negative value --- src/plot.jl | 6 +++++- test/test_plot_joseph.jl | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/plot.jl b/src/plot.jl index 7261faae..8dc17dbe 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -63,7 +63,11 @@ function __plot_time!(p::Union{Plots.Plot, Plots.Subplot}, sol::OptimalControlSo if (ymin != Inf) && (ymax != -Inf) && (abs(ymax-ymin) ≤ abs(ymin)*tol) ymiddle = (ymin+ymax)/2.0 - ymiddle > 0 ? ylims!(p, (0.9*ymiddle, 1.1*ymiddle)) : ylims!(p, (1.1*ymiddle, 0.9*ymiddle)) + if (abs(ymiddle) < 1e-12) + ylims!(p, (-0.1, 0.1)) + else + ymiddle > 0 ? ylims!(p, (0.9*ymiddle, 1.1*ymiddle)) : ylims!(p, (1.1*ymiddle, 0.9*ymiddle)) + end end return p diff --git a/test/test_plot_joseph.jl b/test/test_plot_joseph.jl index 29540eaa..fcb5c708 100644 --- a/test/test_plot_joseph.jl +++ b/test/test_plot_joseph.jl @@ -24,8 +24,8 @@ t0=0 tf=1 x0=0 x = t -> -1+1e-8*rand() -p = t -> +1+1e-8*rand() -u = t -> -1 +p = t -> 0+1e-8*rand() +u = t -> 0 objective = 1 # N=201