Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a20e04

Browse files
Chipe1norvig
authored andcommitted
Changed state from list to tuple (aimacode#603)
1 parent 3a0de56 commit 2a20e04

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

probability.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -661,14 +661,14 @@ def __init__(self, m):
661661
self.nrows = len(m)
662662
self.ncols = len(m[0])
663663
# list of empty spaces in the map
664-
self.empty = [[i, j] for i in range(self.nrows) for j in range(self.ncols) if not m[i][j]]
664+
self.empty = [(i, j) for i in range(self.nrows) for j in range(self.ncols) if not m[i][j]]
665665

666666
def sample(self):
667667
"""Returns a random kinematic state possible in the map"""
668668
pos = random.choice(self.empty)
669669
# 0N 1E 2S 3W
670670
orient = random.choice(range(4))
671-
kin_state = pos + [orient]
671+
kin_state = pos + (orient,)
672672
return kin_state
673673

674674
def ray_cast(self, sensor_num, kin_state):
@@ -679,10 +679,10 @@ def ray_cast(self, sensor_num, kin_state):
679679
# 0
680680
# 3R1
681681
# 2
682-
delta = [(sensor_num%2 == 0)*(sensor_num - 1), (sensor_num%2 == 1)*(2 - sensor_num)]
682+
delta = ((sensor_num%2 == 0)*(sensor_num - 1), (sensor_num%2 == 1)*(2 - sensor_num))
683683
# sensor direction changes based on orientation
684684
for _ in range(orient):
685-
delta = [delta[1], -delta[0]]
685+
delta = (delta[1], -delta[0])
686686
range_count = 0
687687
while (0 <= pos[0] < self.nrows) and (0 <= pos[1] < self.nrows) and (not self.m[pos[0]][pos[1]]):
688688
pos = vector_add(pos, delta)

tests/test_probability.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ def P_motion_sample(kin_state, v, w):
192192
# for simplicity the robot first rotates and then moves
193193
orient = (orient + w)%4
194194
for _ in range(orient):
195-
v = [v[1], -v[0]]
196-
pos = list(vector_add(pos, v))
197-
return pos + [orient]
195+
v = (v[1], -v[0])
196+
pos = vector_add(pos, v)
197+
return pos + (orient,)
198198

199199
def P_sensor(x, y):
200200
"""Conditional probability for sensor reading"""
@@ -207,8 +207,8 @@ def P_sensor(x, y):
207207
return 0
208208

209209
from utils import print_table
210-
a = {'v': [0, 0], 'w': 0}
211-
z = [2, 4, 1, 6]
210+
a = {'v': (0, 0), 'w': 0}
211+
z = (2, 4, 1, 6)
212212
S = monte_carlo_localization(a, z, 1000, P_motion_sample, P_sensor, m)
213213
grid = [[0]*17 for _ in range(11)]
214214
for x, y, _ in S:
@@ -217,8 +217,8 @@ def P_sensor(x, y):
217217
print("GRID:")
218218
print_table(grid)
219219

220-
a = {'v': [0, 1], 'w': 0}
221-
z = [2, 3, 5, 7]
220+
a = {'v': (0, 1), 'w': 0}
221+
z = (2, 3, 5, 7)
222222
S = monte_carlo_localization(a, z, 1000, P_motion_sample, P_sensor, m, S)
223223
grid = [[0]*17 for _ in range(11)]
224224
for x, y, _ in S:

0 commit comments

Comments
 (0)