Skip to content

Commit dc1feb1

Browse files
committed
adding plotting recipes
1 parent 3512be9 commit dc1feb1

File tree

6 files changed

+103
-44
lines changed

6 files changed

+103
-44
lines changed

Project.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,11 @@ version = "0.1.5"
77
Altro = "5dcf52e5-e2fb-48e0-b826-96f46d2e3e73"
88
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
99
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
10-
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
1110
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
12-
MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11"
1311
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
1412
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1513
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
14+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1615
RobotDynamics = "38ceca67-d8d3-44e8-9852-78a5596522e1"
1716
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
1817
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

examples/intro_example.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,6 @@ prob = GameProblem(N,dt,x0,model,opts,game_obj,game_con)
7575
@time newton_solve!(prob)
7676

7777
# Visualize the Results
78-
Algames.plot_traj!(prob.model, prob.pdtraj.pr)
79-
Algames.plot_violation!(prob.stats)
78+
using Plots
79+
plot(prob.model, prob.pdtraj.pr)
80+
plot(prob.stats)

src/Algames.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ using Altro
66
using BenchmarkTools
77
using ForwardDiff
88
using LinearAlgebra
9-
using MeshCat
10-
using GeometryBasics
119
using Parameters
1210
using Printf
1311
using Random
12+
using RecipesBase
1413
using RobotDynamics
1514
using Rotations
1615
using SparseArrays
@@ -162,8 +161,9 @@ export
162161

163162
# Plots
164163
export
165-
plot_traj!,
166-
plot_violation!
164+
recipe_traj,
165+
recipe_violation
166+
167167

168168
# Utils
169169
include("utils.jl")

src/plots/solver_plots.jl

+93-34
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,119 @@
22
# Trajectory Plot
33
################################################################################
44

5-
function plot_traj!(model::AbstractGameModel, traj::Traj; plt=plot())
6-
plot!(plt, legend=false, aspect_ratio=:equal)
5+
# function plot_traj!(model::AbstractGameModel, traj::Traj; plt=plot())
6+
# plot!(plt, legend=false, aspect_ratio=:equal)
7+
# N = length(traj)
8+
# for i = 1:model.p
9+
# xi = [Algames.state(traj[k])[model.pz[i][1]] for k=1:N]
10+
# yi = [Algames.state(traj[k])[model.pz[i][2]] for k=1:N]
11+
# plot!(xi, yi, label=false)
12+
# scatter!(xi, yi)
13+
# end
14+
# display(plt)
15+
# return nothing
16+
# end
17+
18+
@recipe function recipe_traj(model::AbstractGameModel, traj::Traj)
719
N = length(traj)
20+
x = []
21+
y = []
822
for i = 1:model.p
923
xi = [Algames.state(traj[k])[model.pz[i][1]] for k=1:N]
1024
yi = [Algames.state(traj[k])[model.pz[i][2]] for k=1:N]
11-
plot!(xi, yi, label=false)
12-
scatter!(xi, yi)
25+
push!(x, xi)
26+
push!(y, yi)
1327
end
14-
display(plt)
15-
return nothing
28+
aspect_ratio --> :equal
29+
xguide --> "X"
30+
yguide --> "Y"
31+
title --> "Trajectories"
32+
legend --> :false
33+
seriestype --> [fill(:scatter, model.p)... fill(:path, model.p)...]
34+
return [x, x], [y, y]
1635
end
1736

37+
1838
################################################################################
1939
# Constraint Violation Plot
2040
################################################################################
2141

22-
function plot_violation!(stats::Statistics; plt=plot(), lw::T=5.0) where {T}
23-
plot!(plt,
24-
size=(500,500),
25-
layout=(1,1,))
26-
iter = stats.iter
27-
dyn = log.(10, max.(1e-10, [stats.dyn_vio[i].max for i=1:iter]))
42+
# function plot_violation!(stats::Statistics; plt=plot(), lw::T=5.0) where {T}
43+
# plot!(plt,
44+
# size=(500,500),
45+
# layout=(1,1,))
46+
# iter = stats.iter
47+
# dyn = log.(10, max.(1e-10, [stats.dyn_vio[i].max for i=1:iter]))
48+
# con = log.(10, max.(1e-10, [stats.con_vio[i].max for i=1:iter]))
49+
# sta = log.(10, max.(1e-10, [stats.sta_vio[i].max for i=1:iter]))
50+
# opt = log.(10, max.(1e-10, [stats.opt_vio[i].max for i=1:iter]))
51+
# y_min = minimum([dyn; con; sta; opt])
52+
# y_max = maximum([dyn; con; sta; opt])
53+
# # Set up plot
54+
# plot!(plt[1,1],
55+
# legend=:bottomleft,
56+
# xlabel="Outer Loop Iterations",
57+
# ylabel="log(cons. vio.)",
58+
# title="Constraint Violation")
59+
# # Add curves
60+
# plot!(plt[1,1], dyn, linewidth=lw, label="dyn", legend=:bottomleft)
61+
# plot!(plt[1,1], con, linewidth=lw, label="con", legend=:bottomleft)
62+
# plot!(plt[1,1], sta, linewidth=lw, label="sta", legend=:bottomleft)
63+
# plot!(plt[1,1], opt, linewidth=lw, label="opt", legend=:bottomleft)
64+
# # Add rectangles
65+
# plot_epochs!(plt, y_min, y_max, stats.outer_iter)
66+
#
67+
# display(plt)
68+
# return nothing
69+
# end
70+
#
71+
# function plot_epochs!(plt, y_min::T, y_max::T, epochs::Vector{Int}) where {T}
72+
# rectangle(w, h, x, y) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])
73+
# i_start = 1
74+
# i_end = -1
75+
# for k = 1:epochs[end]
76+
# i_end = findlast(x -> x==k, epochs )
77+
# plot!(rectangle(i_end-i_start,y_max-y_min,i_start,y_min), opacity=.1, label=false)
78+
# i_start = i_end + 1
79+
# end
80+
# return nothing
81+
# end
82+
83+
@recipe function recipe_violation(stats::Statistics)
84+
rectangle(w, h, x, y) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])
85+
86+
iter = stats.iter
87+
dyn = log.(10, max.(1e-10, [stats.dyn_vio[i].max for i=1:iter]))
2888
con = log.(10, max.(1e-10, [stats.con_vio[i].max for i=1:iter]))
2989
sta = log.(10, max.(1e-10, [stats.sta_vio[i].max for i=1:iter]))
3090
opt = log.(10, max.(1e-10, [stats.opt_vio[i].max for i=1:iter]))
3191
y_min = minimum([dyn; con; sta; opt])
3292
y_max = maximum([dyn; con; sta; opt])
33-
# Set up plot
34-
plot!(plt[1,1],
35-
legend=:bottomleft,
36-
xlabel="Outer Loop Iterations",
37-
ylabel="log(cons. vio.)",
38-
title="Constraint Violation")
39-
# Add curves
40-
plot!(plt[1,1], dyn, linewidth=lw, label="dyn", legend=:bottomleft)
41-
plot!(plt[1,1], con, linewidth=lw, label="con", legend=:bottomleft)
42-
plot!(plt[1,1], sta, linewidth=lw, label="sta", legend=:bottomleft)
43-
plot!(plt[1,1], opt, linewidth=lw, label="opt", legend=:bottomleft)
44-
# Add rectangles
45-
plot_epochs!(plt, y_min, y_max, stats.outer_iter)
46-
47-
display(plt)
48-
return nothing
49-
end
5093

51-
function plot_epochs!(plt, y_min::T, y_max::T, epochs::Vector{Int}) where {T}
52-
rectangle(w, h, x, y) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])
94+
epochs = stats.outer_iter
5395
i_start = 1
5496
i_end = -1
55-
for k = 1:epochs[end]
97+
epo = []
98+
N_epochs = epochs[end]
99+
for k = 1:N_epochs
56100
i_end = findlast(x -> x==k, epochs )
57-
plot!(rectangle(i_end-i_start,y_max-y_min,i_start,y_min), opacity=.1, label=false)
101+
push!(epo, Vector(range(i_start, stop = i_end, length = iter)))
58102
i_start = i_end + 1
59103
end
60-
return nothing
104+
105+
size --> (500,500)
106+
legend --> :bottomleft
107+
ylims --> [y_min, y_max]
108+
xguide --> "Outer Loop Iterations"
109+
yguide --> "log(cons. vio.)"
110+
title --> "Constraint Violation"
111+
linewidth --> [zeros(N_epochs)... 5.0 5.0 5.0 5.0]
112+
label --> [fill("", N_epochs)... "dyn" "con" "sta" "opt" ]
113+
fillrange --> [[fill(y_min*ones(90), N_epochs)... dyn con sta opt ]
114+
[fill(zeros(iter), N_epochs)... zeros(iter) zeros(iter) zeros(iter) zeros(iter) ]]
115+
fillalpha --> [fill(0.1, N_epochs)... 0.0 0.0 0.0 0.0]
116+
117+
y = fill(y_max*ones(iter), N_epochs)
118+
x = Vector(1:iter)
119+
return [epo..., x, x, x, x], [y..., dyn, con, sta, opt]
61120
end

test/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ Altro = "5dcf52e5-e2fb-48e0-b826-96f46d2e3e73"
33
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
44
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
55
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
6-
MeshCat = "283c5d60-a78f-5afe-a0af-af636b173e11"
76
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
87
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
98
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
9+
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1010
RobotDynamics = "38ceca67-d8d3-44e8-9852-78a5596522e1"
1111
Rotations = "6038ab10-8711-5258-84ad-4b1120ba62dc"
1212
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"

test/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ using Altro
44
using BenchmarkTools
55
using ForwardDiff
66
using LinearAlgebra
7-
using MeshCat
87
using Parameters
98
using Printf
109
using Random
10+
using RecipesBase
1111
using RobotDynamics
1212
using Rotations
1313
using SparseArrays

0 commit comments

Comments
 (0)