aligator  0.9.0
A primal-dual augmented Lagrangian-type solver for nonlinear trajectory optimization.
Loading...
Searching...
No Matches
plotting.py
Go to the documentation of this file.
1import matplotlib.pyplot as plt
2import numpy as np
3
4from aligator import HistoryCallback, Results
5
6
7def plot_convergence(cb: HistoryCallback, ax: plt.Axes, res: Results = None):
8 from proxsuite_nlp.utils import plot_pd_errs
9
10 prim_infeas = cb.prim_infeas.tolist()
11 dual_infeas = cb.dual_infeas.tolist()
12 if res is not None:
13 prim_infeas.append(res.primal_infeas)
14 dual_infeas.append(res.dual_infeas)
15 plot_pd_errs(ax, prim_infeas, dual_infeas)
16 ax.grid(axis="y", which="major")
17 return
18
19
21 q: np.ndarray, ax: plt.Axes, alpha=0.5, fc="tab:blue"
22) -> plt.Rectangle:
23 from matplotlib import transforms
24
25 w = 1.0
26 h = 0.4
27 center = (q[0] - 0.5 * w, q[1] - 0.5 * h)
28 rect = plt.Rectangle(center, w, h, fc=fc, alpha=alpha)
29 theta = np.arctan2(q[3], q[2])
30 transform_ = transforms.Affine2D().rotate_around(*q[:2], -theta) + ax.transData
31 rect.set_transform(transform_)
32 ax.add_patch(rect)
33 return rect
34
35
36def _axes_flatten_if_ndarray(axes) -> list[plt.Axes]:
37 if isinstance(axes, np.ndarray):
38 axes = axes.flatten()
39 elif not isinstance(axes, list):
40 axes = [axes]
41 return axes
42
43
45 times,
46 us,
47 ncols=2,
48 axes=None,
49 effort_limit=None,
50 joint_names=None,
51 rmodel=None,
52 figsize=(6.4, 6.4),
53) -> tuple[plt.Figure, list[plt.Axes]]:
54 t0 = times[0]
55 tf = times[-1]
56 us = np.asarray(us)
57 nu = us.shape[1]
58 nrows, r = divmod(nu, ncols)
59 nrows += int(r > 0)
60 if axes is None:
61 fig, axes = plt.subplots(nrows, ncols, sharex="col", figsize=figsize)
62 else:
63 fig = axes.flat[0].get_figure()
64 axes = _axes_flatten_if_ndarray(axes)
65
66 if rmodel is not None:
67 effort_limit = rmodel.effortLimit
68 joint_names = rmodel.names
69
70 for i in range(nu):
71 ax: plt.Axes = axes[i]
72 ax.step(times[:-1], us[:, i])
73 if effort_limit is not None:
74 ylim = ax.get_ylim()
75 ax.hlines(-effort_limit[i], t0, tf, colors="k", linestyles="--")
76 ax.hlines(+effort_limit[i], t0, tf, colors="r", linestyles="dashdot")
77 ax.set_ylim(*ylim)
78 if joint_names is not None:
79 joint_name = joint_names[i].lower()
80 ax.set_ylabel(joint_name)
81 fig.supxlabel("Time $t$")
82 fig.suptitle("Control trajectories")
83 fig.tight_layout()
84 return fig, axes
85
86
88 times,
89 vs,
90 rmodel,
91 axes=None,
92 ncols=2,
93 vel_limit=None,
94 figsize=(6.4, 6.4),
95) -> tuple[plt.Figure, list[plt.Axes]]:
96 vs = np.asarray(vs)
97 nv = rmodel.nv
98 assert nv == vs.shape[1]
99 if vel_limit is not None:
100 assert nv == vel_limit.shape[0]
101 idx_to_joint_id_map = {}
102 jid = 0
103 for i in range(nv):
104 if i in rmodel.idx_vs.tolist():
105 jid += 1
106 idx_to_joint_id_map[i] = jid
107 nrows, r = divmod(nv, ncols)
108 nrows += int(r > 0)
109
110 t0 = times[0]
111 tf = times[-1]
112
113 if axes is None:
114 fig, axes = plt.subplots(nrows, ncols, figsize=figsize)
115 fig: plt.Figure
116 else:
117 fig = axes.flat[0].get_figure()
118 axes = _axes_flatten_if_ndarray(axes)
119
120 for i in range(nv):
121 ax: plt.Axes = axes[i]
122 ax.plot(times, vs[:, i])
123 jid = idx_to_joint_id_map[i]
124 joint_name = rmodel.names[jid].lower()
125 if vel_limit is not None:
126 ylim = ax.get_ylim()
127 ax.hlines(-vel_limit[i], t0, tf, colors="k", linestyles="--")
128 ax.hlines(+vel_limit[i], t0, tf, colors="r", linestyles="dashdot")
129 ax.set_ylim(*ylim)
130 ax.set_ylabel(joint_name)
131
132 fig.supxlabel("Time $t$")
133 fig.suptitle("Velocity trajectories")
134 fig.tight_layout()
135 return fig, axes
plt.Rectangle plot_se2_pose(np.ndarray q, plt.Axes ax, alpha=0.5, fc="tab:blue")
Definition plotting.py:22
tuple[plt.Figure, list[plt.Axes]] plot_velocity_traj(times, vs, rmodel, axes=None, ncols=2, vel_limit=None, figsize=(6.4, 6.4))
Definition plotting.py:95
plot_convergence(HistoryCallback cb, plt.Axes ax, Results res=None)
Definition plotting.py:7
tuple[plt.Figure, list[plt.Axes]] plot_controls_traj(times, us, ncols=2, axes=None, effort_limit=None, joint_names=None, rmodel=None, figsize=(6.4, 6.4))
Definition plotting.py:53
list[plt.Axes] _axes_flatten_if_ndarray(axes)
Definition plotting.py:36