From a039a6f2005ce4880e69eee61ce3c35c3cac6072 Mon Sep 17 00:00:00 2001 From: Chipe1 Date: Thu, 3 Aug 2017 13:27:48 +0530 Subject: [PATCH] Changed state from list to tuple --- probability.py | 8 ++++---- tests/test_probability.py | 14 +++++++------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/probability.py b/probability.py index 5a5870f64..5c9e28245 100644 --- a/probability.py +++ b/probability.py @@ -661,14 +661,14 @@ def __init__(self, m): self.nrows = len(m) self.ncols = len(m[0]) # list of empty spaces in the map - self.empty = [[i, j] for i in range(self.nrows) for j in range(self.ncols) if not m[i][j]] + self.empty = [(i, j) for i in range(self.nrows) for j in range(self.ncols) if not m[i][j]] def sample(self): """Returns a random kinematic state possible in the map""" pos = random.choice(self.empty) # 0N 1E 2S 3W orient = random.choice(range(4)) - kin_state = pos + [orient] + kin_state = pos + (orient,) return kin_state def ray_cast(self, sensor_num, kin_state): @@ -679,10 +679,10 @@ def ray_cast(self, sensor_num, kin_state): # 0 # 3R1 # 2 - delta = [(sensor_num%2 == 0)*(sensor_num - 1), (sensor_num%2 == 1)*(2 - sensor_num)] + delta = ((sensor_num%2 == 0)*(sensor_num - 1), (sensor_num%2 == 1)*(2 - sensor_num)) # sensor direction changes based on orientation for _ in range(orient): - delta = [delta[1], -delta[0]] + delta = (delta[1], -delta[0]) range_count = 0 while (0 <= pos[0] < self.nrows) and (0 <= pos[1] < self.nrows) and (not self.m[pos[0]][pos[1]]): pos = vector_add(pos, delta) diff --git a/tests/test_probability.py b/tests/test_probability.py index 2ec860876..e974a7c89 100644 --- a/tests/test_probability.py +++ b/tests/test_probability.py @@ -192,9 +192,9 @@ def P_motion_sample(kin_state, v, w): # for simplicity the robot first rotates and then moves orient = (orient + w)%4 for _ in range(orient): - v = [v[1], -v[0]] - pos = list(vector_add(pos, v)) - return pos + [orient] + v = (v[1], -v[0]) + pos = vector_add(pos, v) + return pos + (orient,) def P_sensor(x, y): """Conditional probability for sensor reading""" @@ -207,8 +207,8 @@ def P_sensor(x, y): return 0 from utils import print_table - a = {'v': [0, 0], 'w': 0} - z = [2, 4, 1, 6] + a = {'v': (0, 0), 'w': 0} + z = (2, 4, 1, 6) S = monte_carlo_localization(a, z, 1000, P_motion_sample, P_sensor, m) grid = [[0]*17 for _ in range(11)] for x, y, _ in S: @@ -217,8 +217,8 @@ def P_sensor(x, y): print("GRID:") print_table(grid) - a = {'v': [0, 1], 'w': 0} - z = [2, 3, 5, 7] + a = {'v': (0, 1), 'w': 0} + z = (2, 3, 5, 7) S = monte_carlo_localization(a, z, 1000, P_motion_sample, P_sensor, m, S) grid = [[0]*17 for _ in range(11)] for x, y, _ in S: