-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
204 lines (172 loc) · 6.72 KB
/
util.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
'''taken and modified from https://github.com/racersmith/RL_Quadcopter_2'''
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import csv
def log_run(agent, file_name):
labels = ['time',
'x', 'y', 'z',
'phi', 'theta', 'psi',
'x_velocity', 'y_velocity', 'z_velocity',
'x_accel', 'y_accel', 'z_accel',
'phi_velocity', 'theta_velocity', 'psi_velocity', 'rotor_speed1',
'rotor_speed2', 'rotor_speed3', 'rotor_speed4',
'reward']
results = {x: [] for x in labels}
# Run the simulation, and save the results.
with open(file_name, 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(labels)
state = agent.reset_episode()
while True:
rotor_speeds = agent.act(state)
state, reward, done = agent.task.step(rotor_speeds)
to_write = [agent.task.sim.time]
to_write += list(agent.task.sim.pose)
to_write += list(agent.task.sim.v)
to_write += list(agent.task.sim.linear_accel)
to_write += list(agent.task.sim.angular_v)
to_write += list(agent.task.sim.rotor_speeds)
to_write += [reward]
for ii in range(len(labels)):
results[labels[ii]].append(to_write[ii])
writer.writerow(to_write)
if done:
break
return results
def load_log(file_path):
return pd.read_csv(file_path)
def plot_log(file_path):
results = load_log(file_path)
plot_run(results)
def normalize_angle(angles):
# Adjust angles to range -pi to pi
norm_angles = np.copy(angles)
for i in range(len(norm_angles)):
while norm_angles[i] > np.pi:
norm_angles[i] -= 2 * np.pi
return norm_angles
def plot_run(results, standalone=True):
if standalone:
plt.subplots(figsize=(13, 7))
plt.subplot(3, 3, 1)
plt.title('Position')
plt.plot(results['time'], results['x'], label='x')
plt.plot(results['time'], results['y'], label='y')
plt.plot(results['time'], results['z'], label='z')
plt.xlabel('time, seconds')
plt.ylabel('Position')
plt.grid(True)
if standalone:
plt.legend()
plt.subplot(3, 3, 2)
plt.title('Velocity')
plt.plot(results['time'], results['x_velocity'], label='x')
plt.plot(results['time'], results['y_velocity'], label='y')
plt.plot(results['time'], results['z_velocity'], label='z')
plt.xlabel('time, seconds')
plt.ylabel('Velocity')
plt.grid(True)
if standalone:
plt.legend()
plt.subplot(3, 3, 3)
plt.title('Orientation')
plt.plot(results['time'], normalize_angle(results['phi']), label='phi')
plt.plot(results['time'], normalize_angle(results['theta']), label='theta')
plt.plot(results['time'], normalize_angle(results['psi']), label='psi')
plt.xlabel('time, seconds')
plt.grid(True)
if standalone:
plt.legend()
plt.subplot(3, 3, 4)
plt.title('Angular Velocity')
plt.plot(results['time'], results['phi_velocity'], label='phi')
plt.plot(results['time'], results['theta_velocity'], label='theta')
plt.plot(results['time'], results['psi_velocity'], label='psi')
plt.xlabel('time, seconds')
plt.grid(True)
if standalone:
plt.legend()
plt.subplot(3, 3, 5)
plt.title('Rotor Speed')
plt.plot(results['time'], results['rotor_speed1'], label='Rotor 1')
plt.plot(results['time'], results['rotor_speed2'], label='Rotor 2')
plt.plot(results['time'], results['rotor_speed3'], label='Rotor 3')
plt.plot(results['time'], results['rotor_speed4'], label='Rotor 4')
plt.xlabel('time, seconds')
plt.ylabel('Rotor Speed, revolutions / second')
plt.ylim([0, 900])
plt.grid(True)
if standalone:
plt.legend()
plt.subplot(3, 3, 6)
plt.title('Reward')
plt.plot(results['time'], results['reward'], label='Reward')
plt.xlabel('time, seconds')
plt.ylabel('Reward')
plt.ylim([-1, 1])
if standalone:
plt.legend(loc=3)
ax2 = plt.twinx()
ax2.plot(results['time'], np.cumsum(results['reward']), color='xkcd:red', label='Accum. Reward')
ax2.set_ylabel('Accumulated Reward')
if standalone:
ax2.legend(loc=4)
plt.grid(True)
if standalone:
plt.tight_layout()
plt.show()
def plot_data(ax, time, y_data, data_labels):
for data, label in zip(y_data, data_labels):
ax.plot(time, data, label=label)
if label is not None:
ax.legend(loc=0)
def subplot_constructor(rows, cols, titles, x_label, y_labels, subplot_size=(5,3)):
fig, axes = plt.subplots(rows, cols, figsize=(cols*subplot_size[0], rows*subplot_size[1]))
for ax, title, y_label in zip(axes.ravel(), titles, y_labels):
ax.set_xlabel(x_label)
ax.set_ylabel(y_label)
ax.set_title(title)
ax.grid(True)
plt.tight_layout()
return fig, axes
def grade(agent, file_name, trials=10):
titles = ['Position', 'Velocity', 'Acceleration', 'Rotor Speed', 'Reward', 'Accumulated Reward']
data_labels = [[None],
[None],
[None],
[None],
[None],
[None]]
axis_labels = ['Distance, m',
'Velocity, m/s',
'Acceleration, m/s^2',
'Rotor Speed, RPM',
'Reward/Step',
'Accumulated Reward']
fig, axes = subplot_constructor(2, 3, titles, 'Time, seconds', axis_labels, (5, 4))
rewards = []
for i in range(trials):
if trials > 10:
label = [None]
else:
label = ["Run {}".format(i)]
results = log_run(agent, file_name)
rewards.append(np.sum(results['reward']))
x_data = results['time']
y_data = [[results['z']],
[results['z_velocity']],
[results['z_accel']],
[results['rotor_speed1']],
[results['reward']],
[np.cumsum(results['reward'])]]
for ax, y in zip(axes.ravel(), y_data):
plot_data(ax, x_data, y, label)
plt.show()
avg_reward = np.mean(rewards)
max_reward = len(results['time'])
grade = 100*avg_reward/max_reward
print("Average accumulated reward over the last {} runs is {:.3f} out of {:.0f} possible or {:.3f}%".format(trials,
avg_reward,
max_reward,
grade))