-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlotFunctions.py
More file actions
246 lines (190 loc) · 7.94 KB
/
PlotFunctions.py
File metadata and controls
246 lines (190 loc) · 7.94 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import matplotlib.pyplot as plt
import numpy as np
def plot_signal(record_signal, record_each, n_iter, t_T, t_R, dt):
"""
show the evolution of the signal during the gradient descent
"""
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
N_show = n_iter//record_each+1
T = int(np.round(t_T/dt))
R = int(np.round(t_R/dt))
time = np.linspace(0, t_T+t_R, R+T+1)
time_ms = time*1000
col = np.zeros((N_show,3))
for i in np.arange(N_show):
col[i] = (N_show-i)/(2*N_show),0,i/(2*N_show)
for i in np.arange(N_show):
if i == 0:
p = len(record_signal[0])
ax.step(np.linspace(0, (t_T+t_R)*1000, p), record_signal[0], '-', where = 'post', color = col[i])
else:
ax.plot(time_ms, record_signal[i*record_each], '-', color = col[i])
ax.ticklabel_format(axis='y', style='scientific', scilimits=(-3, 3))
ax.set_title(r'Control signal $u$', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel(r'$u$', fontsize=12)
def plot_position(record_position, record_each, n_iter, t_T, t_R, dt):
"""
show the evolution of the position during the gradient descent
"""
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
N_show = n_iter//record_each+1
T = int(np.round(t_T/dt))
R = int(np.round(t_R/dt))
time = np.linspace(0, t_T+t_R, R+T+1)
time_ms = time*1000
col = np.zeros((N_show,3))
for i in np.arange(N_show):
col[i] = (N_show-i)/(2*N_show),0,i/(2*N_show)
for i in np.arange(N_show):
if i == 0:
p = len(record_position[0])
ax.plot(np.linspace(0, (t_T+t_R)*1000, p), record_position[0], '-', color = col[i])
else:
ax.plot(time_ms, record_position[i*record_each], '-', color = col[i])
ax.set_title(r'Position $x_t$', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel('Angle (deg)', fontsize=12)
def plot_velocity(record_velocity, record_each, n_iter, t_T, t_R, dt):
"""
show the evolution of the velocity during the gradient descent
"""
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
N_show = n_iter//record_each+1
T = int(np.round(t_T/dt))
R = int(np.round(t_R/dt))
time = np.linspace(0, t_T+t_R, R+T+1)
time_ms = time*1000
col = np.zeros((N_show,3))
for i in np.arange(N_show):
col[i] = (N_show-i)/(2*N_show),0,i/(2*N_show)
for i in np.arange(N_show):
if i == 0:
p = len(record_velocity[0])
ax.plot(np.linspace(0, (t_T+t_R)*1000, p), record_velocity[0], '-', color = col[i])
else:
ax.plot(time_ms, record_velocity[i*record_each], '-', color = col[i])
ax.set_title('Velocity', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel(r'Velocity $(deg.s^{-1})$ ', fontsize=12)
def plot_variance(record_variance, record_each, n_iter, t_T, t_R, dt):
"""
show the evolution of the variance during the gradient descent
"""
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
N_show = n_iter//record_each+1
T = int(np.round(t_T/dt))
R = int(np.round(t_R/dt))
time = np.linspace(0, t_T+t_R, R+T+1)
time_ms = time*1000
col = np.zeros((N_show,3))
for i in np.arange(N_show):
col[i] = (N_show-i)/(2*N_show),0,i/(2*N_show)
for i in np.arange(N_show):
ax.plot(time_ms, record_variance[i*record_each], '-', color = col[i])
ax.set_title('Positional variance', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel('Positional variance', fontsize=12)
def plot_cost(record_cost, record_each, n_iter):
"""
show the evolution of the cost during the gradient descent
"""
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
N_show = n_iter//record_each+1
record_cost_conc = np.zeros(0)
for i in np.arange(N_show)*record_each:
record_cost_conc = np.concatenate((record_cost_conc, record_cost[i]))
ax.plot(record_cost_conc, lw=2)
ax.set_title('Cost', fontsize = 15)
ax.set_xlabel('Number of iterations', fontsize=12)
ax.set_ylabel('Cost', fontsize=12)
def plot_posT(record_posT, record_each, n_iter):
"""
show the evolution of the position at time T during the gradient descent
"""
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
N_show = n_iter//record_each+1
record_posT_conc = np.zeros(0)
for i in np.arange(N_show)*record_each:
record_posT_conc = np.concatenate((record_posT_conc, record_posT[i]))
ax.plot(record_posT_conc, lw=2)
ax.set_title('Position at time T', fontsize = 15)
ax.set_xlabel('Number of iterations', fontsize=12)
ax.set_ylabel('Angle (deg)', fontsize=12)
def all_plots(record, bang_data, record_each, n_iter, t_T, t_R, dt, xT, v):
"""
show all the plots
"""
record_signal = record.signal
record_position = record.position
record_velocity = record.velocity
record_variance = record.variance
record_cost = record.cost
record_posT = record.positionT
plot_signal(record_signal, record_each, n_iter, t_T, t_R, dt)
# Comparison between bangbang and minimum-variance signals
T = int(np.round(t_T/dt))
R = int(np.round(t_R/dt))
time = np.linspace(0, t_T+t_R, R+T+1)
time_ms = time*1000
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
ax.plot(time_ms, record_signal[n_iter], lw=2, label="Minimum-variance")
p = len(bang_data.signal[0])
ax.step(np.linspace(0, (t_T+t_R)*1000, p), bang_data.signal[0], lw=1, where = 'post', label="Sym. bangbang")
p = len(bang_data.signal[1])
ax.step(np.linspace(0, (t_T+t_R)*1000, p), bang_data.signal[1], lw=1, where = 'post', label="Asym. bangbang")
ax.plot([0,1000*(t_T+t_R)], [0,0],'r--')
ax.set_title(r'Control signal $u$', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel(r'$u$', fontsize=12)
ax.ticklabel_format(axis='y', style='scientific', scilimits=(-3, 3))
ax.legend(prop={'size':10})
plot_position(record_position, record_each, n_iter, t_T, t_R, dt)
# Comparison between bangbang and minimum-variance positions
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
ax.plot(time_ms, record_position[n_iter], lw=2, label="Minimum-variance")
p = len(bang_data.position[0])
ax.plot(np.linspace(0, (t_T+t_R)*1000, p), bang_data.position[0], lw=1, label="Sym. bangbang")
p = len(bang_data.position[1])
ax.plot(np.linspace(0, (t_T+t_R)*1000, p), bang_data.position[1], lw=1, label="Asym. bangbang")
ax.plot(time_ms, xT[0]+v*np.linspace(0,t_T+t_R,T+R+1), 'r--')
ax.set_title(r'Position $x_t$', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel('Angle (deg)', fontsize=12)
ax.legend(loc='lower right',prop={'size':10})
plot_velocity(record_velocity, record_each, n_iter, t_T, t_R, dt)
# Comparison between bangbang and minimum-variance velocities
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
ax.plot(time_ms, record_velocity[n_iter], lw=2, label="Minimum-variance")
p = len(bang_data.velocity[0])
ax.plot(np.linspace(0, (t_T+t_R)*1000, p), bang_data.velocity[0], lw=1, label="Sym. bangbang")
p = len(bang_data.velocity[1])
ax.plot(np.linspace(0, (t_T+t_R)*1000, p), bang_data.velocity[1], lw=1, label="Asym. bangbang")
ax.plot(time_ms, v*np.ones(T+R+1),'r--')
ax.set_title('Velocity', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel(r'Velocity $(deg.s^{-1})$ ', fontsize=12)
ax.legend(prop={'size':10})
plot_variance(record_variance, record_each, n_iter, t_T, t_R, dt)
# Comparison between bangbang and minimum-variance variances
fig_width = 5
fig, ax = plt.subplots(1, 1, figsize=(fig_width, fig_width/1.618))
ax.plot(time_ms, record_variance[n_iter], lw=1, label="Minimum-variance")
ax.plot(time_ms, bang_data.variance[0], lw=1, label="Sym. bangbang")
p = len(bang_data.variance[1])
ax.plot(np.linspace(0, (t_T+t_R)*1000, p), bang_data.variance[1], lw=1, label="Asym. bangbang")
ax.set_title('Positional variance', fontsize = 15)
ax.set_xlabel('Time (ms)', fontsize=12)
ax.set_ylabel('Positional variance', fontsize=12)
ax.legend(loc='lower right',prop={'size':10})
plot_cost(record_cost, record_each, n_iter)
plot_posT(record_posT, record_each, n_iter)