-
Notifications
You must be signed in to change notification settings - Fork 2
/
Learning_module_2d.py
290 lines (198 loc) · 8.8 KB
/
Learning_module_2d.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import *
from scipy.ndimage import uniform_filter1d
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import minimize, minimize_scalar
def objective(X, a0, v_d, GPx, GPy):
alpha = X[0]
freq = X[1]
X = np.array([alpha, freq]).transpose()
mux = GPx.predict(X.reshape(1, -1))
muy = GPy.predict(X.reshape(1, -1))
#return (a0*freq*np.cos(alpha) + mux - v_d[0])**2 + (a0*freq*np.sin(alpha) + mux - v_d[1])**2
return (a0*freq)**2 + (mux - v_d[0])**2 + 2*a0*freq*np.cos(alpha)*(mux - v_d[0]) + (muy - v_d[1])**2 + 2*a0*freq*np.sin(alpha)*(muy - v_d[1])
class LearningModule:
def __init__(self):
# kernel list is the kernel cookbook from scikit-learn
kernel = RBF(length_scale=1.0, length_scale_bounds=(1e-2, 10.0)) + WhiteKernel()
#create the X and Y GP regression objects
self.gprX = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=5)
self.gprY = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=5)
self.X = []
self.Yx = []
self.Yy = []
self.a0 = 0
self.f = 0
self.Dx = 0
self.Dy = 0
def estimateDisturbance(self, px, py, time):
N = (int)(1 / 0.035 / 2) #filter position data due to noisy sensing
px = uniform_filter1d(px, N, mode="nearest")
py = uniform_filter1d(py, N, mode="nearest")
#calculate velocity via position derivative
vx = np.gradient(px, time)
vy = np.gradient(py, time)
# apply smoothing to the velocity signal
vx = uniform_filter1d(vx, (int)(N/2), mode="nearest")
vy = uniform_filter1d(vy, (int)(N/2), mode="nearest")
self.Dx = np.mean(vx)
self.Dy = np.mean(vy)
print("Estimated a D value of [" + str(self.Dx) + ", " + str(self.Dy) + "].")
# px, py, alpha, time are numpy arrays, freq is constant
# returns an estimate of a_0
def learn(self, px, py, alpha, freq, time):
#set time to start at 0
time -= time[0]
# apply smoothing to the position signals before calculating velocity
# dt is ~ 35 ms, so filter time ~= 0.035*N (this gives N = 38)
N = (int)(1 / 0.035 / 2) #filter position data due to noisy sensing
px = uniform_filter1d(px, N, mode="nearest")
py = uniform_filter1d(py, N, mode="nearest")
#calculate velocity via position derivative
vx = np.gradient(px, time)
vy = np.gradient(py, time)
# apply smoothing to the velocity signal
vx = uniform_filter1d(vx, (int)(N/2), mode="nearest")
vy = uniform_filter1d(vy, (int)(N/2), mode="nearest")
#calculate speed to fit a_0
speed = np.sqrt( (vx - self.Dx )**2 + (vy - self.Dy)**2 )
#alpha = 1k means the controller is off, delete those frames
todel = np.argwhere(alpha >= 500)
if len(todel) > 0:
todel = int(todel[0])
alpha = alpha[0:todel-1]
freq = freq[0:todel-1]
px = px[0:todel-1]
py = py[0:todel-1]
vx = vx[0:todel-1]
vy = vy[0:todel-1]
time = time[0:todel-1]
speed = speed[0:todel-1]
#smoothing creates a boundary effect -- let's remove it
alpha = alpha[N:-N]
freq = freq[N:-N]
px = px[N:-N]
py = py[N:-N]
vx = vx[N:-N]
vy = vy[N:-N]
time = time[N:-N]
speed = speed[N:-N]
a0 = np.median(speed / freq)
#generate empty NP arrays for X (data) and Y (outputs)
#X = alpha.reshape(-1,1)
X = np.vstack( [alpha, freq] ).transpose()
#v_e = v_actual - v_desired = v - a0*f*[ cos alpha; sin alpha]
Yx = vx - a0 * freq * np.cos(alpha)
Yy = vy - a0 * freq * np.sin(alpha)
self.gprX.fit(X, Yx)
self.gprY.fit(X, Yy)
print("GP Learning Complete!")
print("r^2 are " + str(self.gprX.score(X, Yx)) + " and " + str(self.gprY.score(X, Yy)) )
a = np.linspace( np.min(X), np.max(X))
f = np.zeros(a.shape) + freq[0]
Xe = np.vstack( [a, f] ).transpose()
e = self.gprX.predict(Xe)
#plt.figure()
#plt.plot(X, Yx, 'kx')
#plt.plot(a, e, '-r')
#plt.show()
#plot the velocity error versus time
#plt.figure()
#plt.plot(time, vx, time, a0*freq*np.cos(alpha))
#plt.show()
self.X = X; self.Yx = Yx; self.Yy = Yy
self.a0 = a0
self.freq = freq
return a0
def visualize(self):
alpha_range = np.linspace( np.min(self.X[:,0]), np.max(self.X[:,0]), 200 )
freq_range = np.linspace( np.min(self.X[:,1]), np.max(self.X[:,1]), 200 )
alpha,freq = np.meshgrid(alpha_range, freq_range)
print(alpha.shape)
print(freq.shape)
alpha_flat = np.ndarray.flatten(alpha)
freq_flat = np.ndarray.flatten(freq)
print(alpha_flat.shape)
print(freq_flat.shape)
X = np.vstack( [alpha_flat, freq_flat] ).transpose()
#evaluate the GPs
muX,sigX = self.gprX.predict(X, return_std=True)
muY,sigY = self.gprY.predict(X, return_std=True)
#plot what the GP looks like for x velocity
plt.figure()
plt.contourf(alpha, freq, np.reshape(sigX, alpha.shape ))
plt.xlabel('alpha')
plt.ylabel('f')
plt.title('X Velocity Uncertainty')
plt.colorbar()
plt.plot(self.X[:,0], self.X[:,1], 'kx')
plt.show()
#plot what the GP looks like for y velocity
plt.figure()
plt.contourf(alpha, freq, np.reshape(sigY, alpha.shape ))
plt.xlabel('alpha')
plt.ylabel('f')
plt.title('Y Velocity Uncertainty')
plt.colorbar()
plt.plot(self.X[:,0], self.X[:,1], 'kx')
plt.show()
#plot pm 2 stdev
#plt.fill_between(alpha_range, muX - 2*sigX, muX + 2*sigX)
#plt.fill_between(alpha_range, muX - sigX, muX + sigX)
#plot the data
#plt.plot(self.X[:,0], self.Yx, 'xk')
#plot the approximate function
#plt.plot(alpha_range, muX, 'g')
#plt.title('X Axis Learning')
#plt.xlabel("alpha")
#plt.ylabel("V_e^x")
#plot what the GP looks like for y velocity
#plt.figure()
#plot pm 2 stdev
#plt.fill_between(alpha_range, muY - 2*sigY, muY + 2*sigY)
#plt.fill_between(alpha_range, muY - sigY, muY + sigY)
#plot the data
#plt.plot(self.X[:,0], self.Yy, 'xk')
#plot the approximate function
#plt.plot(alpha_range, muY, 'g')
#plt.title('Y Axis Learning')
#plt.xlabel("alpha")
#plt.ylabel("V_e^x")
def error(self, vd):
#alpha desired comes from arctan of desired velocity
alpha_d = np.array(math.atan2(vd[1], vd[0]))
f_d = np.linalg.norm(vd) / self.a0
X = np.array([alpha_d, f_d])
#estimate the uncertainty for the desired alpha
muX,sigX = self.gprX.predict(X.reshape(1,-1), return_std=True)
muY,sigY = self.gprY.predict(X.reshape(1,-1), return_std=True)
return muX, muY, sigX, sigY
def predict(self, vd):
#alpha desired comes from arctan of desired velocity
alpha_d = np.array(math.atan2(vd[1], vd[0]))
f_d = np.linalg.norm(vd) / self.a0
X = np.array([alpha_d, f_d])
#estimate the uncertainty for the desired alpha
muX = self.gprX.predict(X.reshape(1,-1))
muY = self.gprY.predict(X.reshape(1,-1))
#select the initial alpha guess as atan2 of v_d - v_error
x0 = np.hstack( [alpha_d, f_d] )
result = minimize(objective, x0, args=(self.a0, vd, self.gprX, self.gprY), bounds=[(-np.pi, np.pi), (0, 5)])
#result = minimize_scalar(objective, method='Bounded', args=(self.a0, self.freq, vd, self.gprX, self.gprY), bounds=[-np.pi, np.pi] )
X = np.array(result.x)
#generate the uncertainty for the new alpha we're sending
muX,sigX = self.gprX.predict(X.reshape(1,-1), return_std=True)
muY,sigY = self.gprY.predict(X.reshape(1,-1), return_std=True)
return X, muX, muY, sigX, sigY
'''
#get bounds on learning - 2stdv ~= 95% of data
plt.figure()
plt.fill_between(time, v_learned[:,0] - 2*gpX[:,1], v_learned[:,0] + 2*gpX[:,1])
plt.fill_between(time, v_learned[:,0] - gpX[:,1], v_learned[:,0] + gpX[:,1])
plt.plot(time, v_learned[:,0], '-r', label="learned")
plt.plot(time, v_desired[:,0], '-b', label="desired")
plt.plot(time, vx, '-k', label="data")
plt.legend()
plt.show()
'''