Skip to content
Browse files

4-17

  • Loading branch information...
1 parent ac1de0d commit ce09566445f6cc333cfbb6056bf2aab421f6db70 huanghuang committed Mar 18, 2012
Showing with 164 additions and 0 deletions.
  1. +92 −0 4-12.py
  2. +72 −0 4-17.py
View
92 4-12.py
@@ -0,0 +1,92 @@
+# -----------
+# User Instructions:
+#
+# Modify the the search function so that it becomes
+# an A* search algorithm as defined in the previous
+# lectures.
+#
+# Your function should return the expanded grid
+# which shows, for each element, the count when
+# it was expanded or -1 if the element was never expanded.
+#
+# Your function only needs to work for a 5x6 grid.
+# You do not need to modify the heuristic.
+# ----------
+
+grid = [[0, 1, 0, 0, 0, 0],
+ [0, 1, 0, 0, 0, 0],
+ [0, 1, 0, 0, 0, 0],
+ [0, 1, 0, 0, 0, 0],
+ [0, 0, 0, 0, 1, 0]]
+
+heuristic = [[9, 8, 7, 6, 5, 4],
+ [8, 7, 6, 5, 4, 3],
+ [7, 6, 5, 4, 3, 2],
+ [6, 5, 4, 3, 2, 1],
+ [5, 4, 3, 2, 1, 0]]
+
+init = [0, 0]
+goal = [len(grid)-1, len(grid[0])-1]
+
+delta = [[-1, 0 ], # go up
+ [ 0, -1], # go left
+ [ 1, 0 ], # go down
+ [ 0, 1 ]] # go right
+
+delta_name = ['^', '<', 'v', '>']
+
+cost = 1
+
+# ----------------------------------------
+# modify code below
+# ----------------------------------------
+
+def search():
+ closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
+ closed[init[0]][init[1]] = 1
+
+ expand = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
+ action = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
+
+
+ x = init[0]
+ y = init[1]
+ g = 0
+
+ open = [[g, x, y]]
+
+ found = False # flag that is set when search is complet
+ resign = False # flag set if we can't find expand
+ count = 0
+
+ while not found and not resign:
+ if len(open) == 0:
+ resign = True
+ else:
+ open.sort()
+ open.reverse()
+ next = open.pop()
+ x = next[1]
+ y = next[2]
+ g = next[0]
+ expand[x][y] = count
+ count += 1
+
+ if x == goal[0] and y == goal[1]:
+ found = True
+ else:
+ for i in range(len(delta)):
+ x2 = x + delta[i][0]
+ y2 = y + delta[i][1]
+ if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
+ if closed[x2][y2] == 0 and grid[x2][y2] == 0:
+ g2 = g + cost + heuristic[x2][y2]
+ open.append([g2, x2, y2])
+ closed[x2][y2] = 1
+ for i in range(len(expand)):
+ print expand[i]
+ return expand #Leave this line for grading purposes!
+
+
+search()
+
View
72 4-17.py
@@ -0,0 +1,72 @@
+# ----------
+# User Instructions:
+#
+# Create a function compute_value() which returns
+# a grid of values. Value is defined as the minimum
+# number of moves required to get from a cell to the
+# goal.
+#
+# If it is impossible to reach the goal from a cell
+# you should assign that cell a value of 99.
+
+# ----------
+
+grid = [[0, 1, 0, 0, 0, 0],
+ [0, 1, 0, 0, 0, 0],
+ [0, 1, 0, 0, 0, 0],
+ [0, 1, 0, 0, 0, 0],
+ [0, 0, 0, 0, 1, 0]]
+
+init = [0, 0]
+goal = [len(grid)-1, len(grid[0])-1]
+
+delta = [[-1, 0 ], # go up
+ [ 0, -1], # go left
+ [ 1, 0 ], # go down
+ [ 0, 1 ]] # go right
+
+delta_name = ['^', '<', 'v', '>']
+
+cost_step = 1 # the cost associated with moving from a cell to an adjacent one.
+
+# ----------------------------------------
+# insert code below
+# ----------------------------------------
+
+def compute_value():
+ def motion(state, move, grid):
+ row_index = state[0] + move[0]
+ column_index = state[1] + move[1]
+ if not 0<=row_index<len(grid) or not 0<=column_index<len(grid[0]):
+ return []
+ return [row_index, column_index]
+
+ value = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
+ open_list = []
+ used_list = []
+ open_list.append([0,goal[0],goal[1]])
+ used_list.append(goal)
+ while open_list:
+ open_list.sort()
+ open_list.reverse()
+ next = open_list.pop()[1:]
+ movements = []
+ for d in delta:
+ movements.append(motion(next, d, grid))
+ movements = filter(None, movements)
+ for m in movements:
+ if m not in used_list:
+ used_list.append([m[0],m[1]])
+ if grid[m[0]][m[1]] == 1:
+ value[m[0]][m[1]] = 99
+ else:
+ v = value[next[0]][next[1]]+1
+ value[m[0]][m[1]] = v
+ open_list.append([v, m[0],m[1]])
+ return value #make sure your function returns a grid of values as demonstrated in the previous video.
+
+
+
+value = compute_value()
+for v in value:
+ print v

0 comments on commit ce09566

Please sign in to comment.
Something went wrong with that request. Please try again.