@@ -168,6 +168,68 @@ def test_particle_filtering():
168
168
# XXX 'A' and 'B' are really arbitrary names, but I'm letting it stand for now
169
169
170
170
171
+ def test_monte_carlo_localization ():
172
+ ## TODO: Add tests for random motion/inaccurate sensors
173
+ random .seed ('aima-python' )
174
+ m = MCLmap ([[0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 ],
175
+ [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 ],
176
+ [1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 ],
177
+ [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 ],
178
+ [0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 0 ],
179
+ [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 ],
180
+ [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 ],
181
+ [0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 ],
182
+ [0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 ],
183
+ [0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 ],
184
+ [0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 0 ]])
185
+
186
+ def P_motion_sample (kin_state , v , w ):
187
+ """Sample from possible kinematic states.
188
+ Returns from a single element distribution (no uncertainity in motion)"""
189
+ pos = kin_state [:2 ]
190
+ orient = kin_state [2 ]
191
+
192
+ # for simplicity the robot first rotates and then moves
193
+ orient = (orient + w )% 4
194
+ for _ in range (orient ):
195
+ v = [v [1 ], - v [0 ]]
196
+ pos = list (vector_add (pos , v ))
197
+ return pos + [orient ]
198
+
199
+ def P_sensor (x , y ):
200
+ """Conditional probability for sensor reading"""
201
+ # Need not be exact probability. Can use a scaled value.
202
+ if x == y :
203
+ return 0.8
204
+ elif abs (x - y ) <= 2 :
205
+ return 0.05
206
+ else :
207
+ return 0
208
+
209
+ from utils import print_table
210
+ a = {'v' : [0 , 0 ], 'w' : 0 }
211
+ z = [2 , 4 , 1 , 6 ]
212
+ S = monte_carlo_localization (a , z , 1000 , P_motion_sample , P_sensor , m )
213
+ grid = [[0 ]* 17 for _ in range (11 )]
214
+ for x , y , _ in S :
215
+ if 0 <= x < 11 and 0 <= y < 17 :
216
+ grid [x ][y ] += 1
217
+ print ("GRID:" )
218
+ print_table (grid )
219
+
220
+ a = {'v' : [0 , 1 ], 'w' : 0 }
221
+ z = [2 , 3 , 5 , 7 ]
222
+ S = monte_carlo_localization (a , z , 1000 , P_motion_sample , P_sensor , m , S )
223
+ grid = [[0 ]* 17 for _ in range (11 )]
224
+ for x , y , _ in S :
225
+ if 0 <= x < 11 and 0 <= y < 17 :
226
+ grid [x ][y ] += 1
227
+ print ("GRID:" )
228
+ print_table (grid )
229
+
230
+ assert grid [6 ][7 ] > 700
231
+
232
+
171
233
# The following should probably go in .ipynb:
172
234
173
235
"""
0 commit comments