-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_utils.py
227 lines (179 loc) · 7.91 KB
/
plot_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import matplotlib.pyplot as plt
from casadi import cos, sin, pi, sqrt, linspace
import matplotlib
from pandas import read_csv
import numpy as np
def latexify(fig_width=None, fig_height=None):
"""Set up matplotlib's RC params for LaTeX plotting.
Call this before plotting a figure.
Parameters
----------
fig_width : float, optional, inches
fig_height : float, optional, inches
columns : {1, 2}
"""
# code adapted from http://www.scipy.org/Cookbook/Matplotlib/LaTeX_Examples
# Width and max height in inches for IEEE journals taken from
# computer.org/cms/Computer.org/Journal%20templates/transactions_art_guide.pdf
if fig_width is None:
fig_width = 5 # width in inches
if fig_height is None:
golden_mean = (sqrt(5)-1.0)/2.0 # Aesthetic ratio
fig_height = fig_width*golden_mean # height in inches
params = {'backend': 'ps',
'text.latex.preamble': r"\usepackage{gensymb} \usepackage{amsmath}",
'axes.labelsize': 10, # fontsize for x and y labels (was 10)
'axes.titlesize': 10,
'legend.fontsize': 10, # was 10
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'text.usetex': True,
'figure.figsize': [fig_width, fig_height],
'font.family': 'serif'
}
matplotlib.rcParams.update(params)
def visualize_trajectory(x, y, phi, L, obstacles=None, save_fig=False, fig_name=None):
latexify(fig_width=3, fig_height=3)
marker_size = 5
fig, ax = plt.subplots()
ax.scatter(9, 9, marker='x')
for (xk, yk, phik) in zip(x, y, phi):
r_propeller_x = xk + L*cos(phik)
r_propeller_y = yk + L*sin(phik)
l_propeller_x = xk - L*cos(phik)
l_propeller_y = yk - L*sin(phik)
ax.scatter(xk, yk, marker_size, marker='s', color = 'black')
ax.plot([xk, r_propeller_x], [yk, r_propeller_y], 'b')
ax.plot([xk, l_propeller_x], [yk, l_propeller_y], 'b')
if obstacles != None:
for o in obstacles:
px_obst_k = o[0][0]
py_obst_k = o[0][1]
r_obst_k = o[1]
theta = linspace(0, 2*pi, 100)
xobst1 = px_obst_k + r_obst_k*cos(theta)
xobst2 = py_obst_k + r_obst_k*sin(theta)
plt.plot(xobst1, xobst2, 'r')
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
ax.set_xlabel('X (m)')
ax.set_ylabel('Y (m)')
ax.set_aspect(1)
plt.show()
if save_fig:
fig.savefig(fig_name + '.svg', format='svg', dpi=1200)
def plot_controls(tgrid, u, save_fig=False, fig_name=None):
latexify(fig_width=2, fig_height=1.5)
fig, ax = plt.subplots()
ax.step(tgrid[0:-1], u[0,:], where='post', label=r'$F_1$')
ax.step(tgrid[0:-1], u[1,:], where='post', label=r'$F_2$')
ax.set_xlabel(r'$k$ (iteration)')
ax.set_ylabel(r'$F$ (N)')
ax.legend()
ax.grid()
plt.show()
if save_fig:
fig.savefig(fig_name + '.svg', format='svg', dpi=1200)
def plot_pose(tgrid, pose, save_fig=False):
latexify()
fig, ax = plt.subplots()
ax.plot(tgrid, pose[0,:], label=r'$x$')
ax.plot(tgrid, pose[1,:], label=r'$y$')
ax.plot(tgrid, pose[2,:], label=r'$\phi$')
ax.set_xlabel(r'$t$ (sec)')
ax.set_ylabel(r'$u$ (N)')
ax.legend()
ax.grid()
plt.show()
if save_fig:
fig.savefig('drone_pose.svg', format='svg', dpi=1200)
def plot_RL_statistics(save_fig=False):
latexify(fig_width=11, fig_height=3)
# load trainig statistics
sac_reward = read_csv('report/SAC7_reward.csv')
td3_reward = read_csv('report/TD37_reward.csv')
sac_actor_loss = read_csv('report/SAC7_actor_loss.csv')
td3_actor_loss = read_csv('report/TD37_actor_loss.csv')
sac_critic_loss = read_csv('report/SAC7_critic_loss.csv')
td3_critic_loss = read_csv('report/TD37_critic_loss.csv')
fig, ax = plt.subplots(1,3)
ax[0].plot(1e-5*sac_reward.values[:,1], 1e-3*sac_reward.values[:,2], label='SAC')
ax[0].plot(1e-5*td3_reward.values[:,1], 1e-3*td3_reward.values[:,2], label='TD3')
ax[0].set_xlabel(r'time steps $\times\ 10^5$')
ax[0].set_ylabel(r'episode return $\times\ 10^3$')
ax[0].set_ylim(-14, -4)
ax[0].legend()
ax[1].plot(1e-5*sac_actor_loss.values[:,1], 1e-3*sac_actor_loss.values[:,2], label='SAC')
ax[1].plot(1e-5*td3_actor_loss.values[:,1], 1e-3*td3_actor_loss.values[:,2], label='TD3')
ax[1].set_xlabel(r'time steps $\times\ 10^5$')
ax[1].set_ylabel(r'actor loss $\times\ 10^3$')
ax[1].set_ylim(0, 8)
ax[1].legend()
ax[2].plot(1e-5*sac_critic_loss.values[:,1], 1e-4*sac_critic_loss.values[:,2], label='SAC')
ax[2].plot(1e-5*td3_critic_loss.values[:,1], 1e-4*td3_critic_loss.values[:,2], label='TD3')
ax[2].set_xlabel(r'time steps $\times\ 10^5$')
ax[2].set_ylabel(r'critic loss $\times\ 10^4$')
ax[2].set_ylim(-1, 25)
ax[2].legend()
plt.show()
if save_fig:
fig.savefig('report/RL_statistics.svg', format='svg', dpi=1200)
def plot_controls_RL(t_grid_td3, u_traj_td31, t_grid_sac, u_traj_sac1, save_fig=False):
latexify(fig_width=2, fig_height=1.5)
fig, ax = plt.subplots()
ax.step(t_grid_td3[0:-1], u_traj_td31[:,0], where='post', label=r'$F_1$ TD3')
ax.step(t_grid_td3[0:-1], u_traj_td31[:,1], where='post', label=r'$F_2$ TD3')
ax.step(t_grid_sac[0:-1], u_traj_sac1[:,0], '--', where='post', label=r'$F_1$ SAC')
ax.step(t_grid_sac[0:-1], u_traj_sac1[:,1], '--', where='post', label=r'$F_2$ SAC')
ax.set_xlabel(r'$k$ (iteration)')
ax.set_ylabel(r'$F$ (N)')
ax.legend(loc='upper right')
ax.grid()
plt.show()
if save_fig:
fig.savefig('report/controls_RL.svg', format='svg', dpi=1200)
def visualize_trajectory_RL(x_traj_td31, x_traj_td32, x_traj_sac1, x_traj_sac2, L, save_fig=False):
latexify(fig_width=3, fig_height=3)
marker_size = 5
fig, ax = plt.subplots()
ax.scatter(9, 9, marker='x')
for (xk, yk, phik) in zip(x_traj_td31[::15,0], x_traj_td31[::15,1], x_traj_td31[::15,2]):
r_propeller_x = xk + L*np.cos(phik)
r_propeller_y = yk + L*np.sin(phik)
l_propeller_x = xk - L*np.cos(phik)
l_propeller_y = yk - L*np.sin(phik)
ax.scatter(xk, yk, marker_size, marker='s', color = 'black')
ax.plot([xk, r_propeller_x], [yk, r_propeller_y], 'b')
ax.plot([xk, l_propeller_x], [yk, l_propeller_y], 'b')
for (xk, yk, phik) in zip(x_traj_td32[::15,0], x_traj_td32[::15,1], x_traj_td32[::15,2]):
r_propeller_x = xk + L*np.cos(phik)
r_propeller_y = yk + L*np.sin(phik)
l_propeller_x = xk - L*np.cos(phik)
l_propeller_y = yk - L*np.sin(phik)
ax.scatter(xk, yk, marker_size, marker='s', color = 'black')
ax.plot([xk, r_propeller_x], [yk, r_propeller_y], 'b')
ax.plot([xk, l_propeller_x], [yk, l_propeller_y], 'b')
for (xk, yk, phik) in zip(x_traj_sac1[::15,0], x_traj_sac1[::15,1], x_traj_sac1[::15,2]):
r_propeller_x = xk + L*np.cos(phik)
r_propeller_y = yk + L*np.sin(phik)
l_propeller_x = xk - L*np.cos(phik)
l_propeller_y = yk - L*np.sin(phik)
ax.scatter(xk, yk, marker_size, marker='s', color = 'black')
ax.plot([xk, r_propeller_x], [yk, r_propeller_y], 'r')
ax.plot([xk, l_propeller_x], [yk, l_propeller_y], 'r')
for (xk, yk, phik) in zip(x_traj_sac2[::15,0], x_traj_sac2[::15,1], x_traj_sac2[::15,2]):
r_propeller_x = xk + L*np.cos(phik)
r_propeller_y = yk + L*np.sin(phik)
l_propeller_x = xk - L*np.cos(phik)
l_propeller_y = yk - L*np.sin(phik)
ax.scatter(xk, yk, marker_size, marker='s', color = 'black')
ax.plot([xk, r_propeller_x], [yk, r_propeller_y], 'r')
ax.plot([xk, l_propeller_x], [yk, l_propeller_y], 'r')
ax.set_xlim([0, 10])
ax.set_ylim([0, 10])
ax.set_xlabel('X (m)')
ax.set_ylabel('Y (m)')
ax.set_aspect(1)
plt.show()
if save_fig:
fig.savefig('report/trajectory_RL.svg', format='svg', dpi=1200)