-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSLAM1D.py
More file actions
308 lines (248 loc) · 10.7 KB
/
SLAM1D.py
File metadata and controls
308 lines (248 loc) · 10.7 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import numpy as np
from numpy.linalg import inv
from numpy import dot
from matplotlib import pyplot as plt
import seaborn
class BasicMovement:
def __init__(self, maxSpeed, covariance, robotFeaturesDim):
self.maxSpeed = maxSpeed
self.covariance = np.atleast_2d(covariance)
self.robotFeaturesDim = robotFeaturesDim
# Input the real state
def move(self, state, covariance=None, command=None):
move = self.__choose_command(state) if command is None else command
noise = self.__get_noise(state, covariance)
newState = state + move + noise
return newState, move
def __choose_command(self, state): # TO CHANGE FOR 2D
dim = self.robotFeaturesDim
way = 1 - 2 * np.random.randint(2)
speed = self.maxSpeed * np.random.rand()
newCommand = np.zeros_like(state)
newCommand[:dim] = way * speed # TO CHANGE FOR 2D
return newCommand
def __get_noise(self, state, covariance):
dim = self.robotFeaturesDim
noise = np.zeros_like(state)
covariance = self.covariance if covariance is None else covariance
noise[:dim] = np.random.multivariate_normal(np.zeros(dim), covariance, 1).T
return noise
class BasicMeasurement:
def __init__(self, covariance, robotFeaturesDim, envFeaturesDim, measureFunction, gradMeasureFunction, detectionSize=0):
self.covariance = np.atleast_2d(covariance)
self.robotFeaturesDim = robotFeaturesDim
self.envFeaturesDim = envFeaturesDim
self.measureFunction = measureFunction
self.gradMeasureFunction = gradMeasureFunction
self.detectionSize = detectionSize
# Input the real state
def measure(self, state):
dim = state.shape[0]
dimR = self.robotFeaturesDim
dimE = self.envFeaturesDim
rState = state[:dimR]
envState = state[dimR:]
nbLandmark = (dim - dimR) / dimE
mes = np.zeros(nbLandmark)
landmarkIds = np.zeros(nbLandmark).astype(int)
j = 0
for i, landmark in enumerate(envState.reshape((nbLandmark, dimE, 1))):
if (np.linalg.norm(rState - landmark) < self.detectionSize) or (self.detectionSize is 0):
mes[j] = self.measureFunction(rState, landmark)
landmarkIds[j] = int(i)
j += 1
mes = mes[:j]
landmarkIds = landmarkIds[:j]
mes = np.array(mes)
noise = self.__get_noise(mes)
mes += noise # TO CHANGE for 2D ---------------------------------------------
return mes.reshape((len(mes), 1)), landmarkIds
def __get_noise(self, mes):
noise = np.squeeze(np.random.multivariate_normal(np.zeros(self.envFeaturesDim), self.covariance, len(mes)))
return noise
class EKFModel:
def __init__(self, dimension, robotFeaturesDim, envFeaturesDim, motionModel, mesModel, covMes, muInitial):
self.robotFeaturesDim = robotFeaturesDim
self.envFeaturesDim = envFeaturesDim
self.dimension = dimension
self.Sigma = np.eye(dimension)
self.mu = muInitial.copy() # np.zeros((1, dimension))
self.S = np.zeros(dimension * robotFeaturesDim).reshape((dimension, robotFeaturesDim))
self.S[:robotFeaturesDim] = np.eye(robotFeaturesDim)
self.Z = covMes
self.motionModel = motionModel
self.mesModel = mesModel
def update(self, measures, landmarkIds, command, U):
self.__motion_update(command, U)
for ldmIndex, ldmMes in zip(landmarkIds, measures):
self.__measurement_update(ldmMes, ldmIndex)
return self.Sigma, self.mu
def __motion_update(self, command, U):
previousMeanState = self.mu
_, meanStateChange = self.motionModel.move(previousMeanState, command=command)
self.mu += meanStateChange
self.Sigma += dot(dot(self.S, U), self.S.T)
def __measurement_update(self, ldmMes, ldmIndex):
mu = self.mu
Sigma = self.Sigma
meanMes, gradMeanMes = self.__get_mean_measurement_params(mu, ldmIndex)
z = np.atleast_2d(ldmMes)
zM = np.atleast_2d(meanMes)
C = gradMeanMes
toInvert = inv(dot(dot(C.T, Sigma), C) + self.Z)
K = dot(dot(Sigma, C), toInvert)
self.mu += dot(K, z - zM)
self.Sigma = dot(np.eye(self.dimension) - dot(K, C.T), Sigma)
def __get_mean_measurement_params(self, mu, ldmIndex):
realIndex = self.robotFeaturesDim + ldmIndex * self.envFeaturesDim
ldmMeanState = mu[realIndex: realIndex + self.envFeaturesDim]
rMeanState = mu[:self.robotFeaturesDim]
meanMes = self.mesModel.measureFunction(rMeanState, ldmMeanState)
gradMeanMes = self.mesModel.gradMeasureFunction(mu, ldmIndex)
return meanMes, gradMeanMes
class EIFModel:
def __init__(self, dimension, robotFeaturesDim, envFeaturesDim, motionModel, mesModel, covMes, muInitial):
self.robotFeaturesDim = robotFeaturesDim
self.envFeaturesDim = envFeaturesDim
self.dimension = dimension
self.H = np.eye(dimension)
self.b = dot(muInitial.T, self.H)
self.S = np.zeros(dimension * robotFeaturesDim).reshape((dimension, robotFeaturesDim))
self.S[:robotFeaturesDim] = np.eye(robotFeaturesDim)
self.invZ = inv(covMes)
self.motionModel = motionModel
self.mesModel = mesModel
def update(self, measures, landmarkIds, command, U):
self.__motion_update(command, U)
for ldmIndex, ldmMes in zip(landmarkIds, measures):
self.__measurement_update(ldmMes, ldmIndex)
return self.H, self.b
def __motion_update(self, command, U):
previousMeanState = self.estimate()
_, meanStateChange = self.motionModel.move(previousMeanState, command=command)
self.H = inv(inv(self.H) + dot(dot(self.S, U), self.S.T))
self.b = dot((previousMeanState + meanStateChange).T, self.H)
def __measurement_update(self, ldmMes, ldmIndex):
mu = self.estimate()
meanMes, gradMeanMes = self.__get_mean_measurement_params(mu, ldmIndex)
z = np.atleast_2d(ldmMes)
zM = np.atleast_2d(meanMes)
C = gradMeanMes
self.H += dot(dot(C, self.invZ), C.T)
self.b += dot(dot((z - zM + dot(C.T, mu)).T, self.invZ), C.T)
def __get_mean_measurement_params(self, mu, ldmIndex):
realIndex = self.robotFeaturesDim + ldmIndex * self.envFeaturesDim
# import ipdb; ipdb.set_trace()
ldmMeanState = mu[realIndex: realIndex + self.envFeaturesDim]
rMeanState = mu[:self.robotFeaturesDim]
meanMes = self.mesModel.measureFunction(rMeanState, ldmMeanState)
gradMeanMes = self.mesModel.gradMeasureFunction(mu, ldmIndex)
return meanMes, gradMeanMes
def estimate(self, H=None, b=None):
H = self.H if H is None else H
b = self.b if b is None else b
return dot(b, inv(H)).T
measureFunction = lambda rState, landmark: np.sign(landmark[0, 0] - rState[0, 0]) * np.linalg.norm(rState - landmark)
def gradMeasureFunction(state, ldmIndex):
grad = np.zeros_like(state)
grad[0] = -1
grad[ldmIndex+1] = 1
return grad
T = 10000 # Number of timesteps
nbLandmark = 10
maxSpeed = 3
robotFeaturesDim = 1
envFeaturesDim = 1
dimension = robotFeaturesDim + nbLandmark * envFeaturesDim
# Detection parameters
spaceBetween = 100
detectionSize = 15
covarianceMotion = np.eye(robotFeaturesDim) * 10 # motion noise variance
covarianceMeasurements = np.eye(envFeaturesDim) * 10 # measurement noise variance
motionModel = BasicMovement(maxSpeed, covarianceMotion, robotFeaturesDim)
measurementModel = BasicMeasurement(covarianceMeasurements, robotFeaturesDim, envFeaturesDim, measureFunction, gradMeasureFunction, detectionSize)
state = np.zeros((dimension, 1)) # Real robot state
state[1:] = np.arange(0, nbLandmark * spaceBetween, spaceBetween).reshape(nbLandmark, 1)
# state[1] = -100
muSimple = np.zeros_like(state) # Estimated robot state basic
# mu[1] = 50
# mu[1:nbLandmark+1] = np.arange(0, nbLandmark * spaceBetween, spaceBetween).reshape(nbLandmark, 1)
muSimple = state.copy()
muSimple[1:] += np.random.multivariate_normal([0], covarianceMeasurements, nbLandmark).reshape(nbLandmark, 1)
muEIF = np.zeros_like(state) # Estimated robot state using EIF Algorithm
# muEIF[1] = 50
# muEIF[1:nbLandmark+1] = np.arange(0, nbLandmark * spaceBetween, spaceBetween).reshape(nbLandmark, 1)
muEIF = muSimple.copy()
muEKF = np.zeros_like(state) # Estimated robot state using EIF Algorithm
# muEIF[1] = 50
# muEIF[1:nbLandmark+1] = np.arange(0, nbLandmark * spaceBetween, spaceBetween).reshape(nbLandmark, 1)
muEKF = muSimple.copy()
eif = EIFModel(dimension, robotFeaturesDim, envFeaturesDim, motionModel, measurementModel, covarianceMeasurements, muSimple)
ekf = EKFModel(dimension, robotFeaturesDim, envFeaturesDim, motionModel, measurementModel, covarianceMeasurements, muSimple)
mus_simple = np.zeros((T, dimension))
mus_eif = np.zeros((T, dimension))
mus_ekf = np.zeros((T, dimension))
states = np.zeros((T, dimension))
mus_simple[0] = np.squeeze(muSimple)
mus_eif[0] = np.squeeze(muEIF)
mus_ekf[0] = np.squeeze(muEKF)
states[0] = np.squeeze(state)
print("BEFORE")
print("EIF estimate :")
print(muEIF)
print("EKF estimate :")
print(muEKF)
print("Real state :")
print(state)
print('\n')
listStates = []
listCommands = []
listMeasures = []
listLandmarkIds = []
for t in range(1, T):
state, motionCommand = motionModel.move(state)
measures, landmarkIds = measurementModel.measure(state)
listStates.append(state)
listCommands.append(motionCommand)
listMeasures.append(measures)
listLandmarkIds.append(landmarkIds)
for t in range(1, T):
state = listStates[t-1]
motionCommand = listCommands[t-1]
measures = listMeasures[t-1]
landmarkIds = listLandmarkIds[t-1]
H, b = eif.update(measures, landmarkIds, motionCommand, covarianceMotion)
# + measurement !
muEIF = eif.estimate()
mus_eif[t] = np.squeeze(muEIF)
for t in range(1, T):
muSimple += listCommands[t-1]
mus_simple[t] = np.squeeze(muSimple)
states[t] = np.squeeze(listStates[t-1])
for t in range(1, T):
state = listStates[t-1]
motionCommand = listCommands[t-1]
measures = listMeasures[t-1]
landmarkIds = listLandmarkIds[t-1]
Sigma, muEKF = ekf.update(measures, landmarkIds, motionCommand, covarianceMotion)
mus_ekf[t] = np.squeeze(muEKF)
print('\n')
print('AFTER')
print("EIF estimate :")
print(muEIF)
print("EKF estimate :")
print(muEKF)
print("Real state :")
print(state)
print(states[5, 0])
print(mus_simple[5, 0])
print(mus_eif[5, 0])
print(mus_ekf[5, 0])
plt.figure()
plt.plot(states[:, 0])
plt.plot(mus_simple[:, 0])
plt.plot(mus_eif[:, 0])
plt.plot(mus_ekf[:, 0])
plt.legend(['Real position', 'Simple estimate', 'EIF estimate', 'EKF estimate'])
plt.title("{0} landmarks".format(nbLandmark))
plt.show()