Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

unit 4

  • Loading branch information...
commit e7443b76f5ba407c8bbde5bfe856d4f91215317b 1 parent 501da83
Jason Frame authored
95 udacity/cs373/unit-4/a-star.py
... ... @@ -0,0 +1,95 @@
  1 +# -----------
  2 +# User Instructions:
  3 +#
  4 +# Modify the the search function so that it becomes
  5 +# an A* search algorithm as defined in the previous
  6 +# lectures.
  7 +#
  8 +# Your function should return the expanded grid
  9 +# which shows, for each element, the count when
  10 +# it was expanded or -1 if the element was never expanded.
  11 +#
  12 +# Your function only needs to work for a 5x6 grid.
  13 +# You do not need to modify the heuristic.
  14 +# ----------
  15 +
  16 +grid = [[0, 1, 0, 0, 0, 0],
  17 + [0, 1, 0, 0, 0, 0],
  18 + [0, 1, 0, 0, 0, 0],
  19 + [0, 1, 0, 0, 0, 0],
  20 + [0, 0, 0, 0, 1, 0]]
  21 +
  22 +heuristic = [[9, 8, 7, 6, 5, 4],
  23 + [8, 7, 6, 5, 4, 3],
  24 + [7, 6, 5, 4, 3, 2],
  25 + [6, 5, 4, 3, 2, 1],
  26 + [5, 4, 3, 2, 1, 0]]
  27 +
  28 +init = [0, 0]
  29 +goal = [len(grid)-1, len(grid[0])-1]
  30 +
  31 +delta = [[-1, 0 ], # go up
  32 + [ 0, -1], # go left
  33 + [ 1, 0 ], # go down
  34 + [ 0, 1 ]] # go right
  35 +
  36 +delta_name = ['^', '<', 'v', '>']
  37 +
  38 +cost = 1
  39 +
  40 +# ----------------------------------------
  41 +# modify code below
  42 +# ----------------------------------------
  43 +
  44 +def search():
  45 + closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
  46 + closed[init[0]][init[1]] = 1
  47 +
  48 + expand = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
  49 + action = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
  50 +
  51 +
  52 + x = init[0]
  53 + y = init[1]
  54 + g = 0
  55 + f = g + heuristic[x][y]
  56 +
  57 + open = [[f, g, x, y]]
  58 +
  59 + found = False # flag that is set when search is complet
  60 + resign = False # flag set if we can't find expand
  61 + count = 0
  62 +
  63 + while not found and not resign:
  64 + if len(open) == 0:
  65 + resign = True
  66 + else:
  67 + open.sort()
  68 + open.reverse()
  69 + next = open.pop()
  70 + x = next[2]
  71 + y = next[3]
  72 + g = next[1]
  73 + f = next[0]
  74 + expand[x][y] = count
  75 + count += 1
  76 +
  77 + if x == goal[0] and y == goal[1]:
  78 + found = True
  79 + else:
  80 + for i in range(len(delta)):
  81 + x2 = x + delta[i][0]
  82 + y2 = y + delta[i][1]
  83 + if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
  84 + if closed[x2][y2] == 0 and grid[x2][y2] == 0:
  85 + g2 = g + cost
  86 + h2 = heuristic[x2][y2]
  87 + f2 = g2 + h2
  88 + open.append([f2, g2, x2, y2])
  89 + closed[x2][y2] = 1
  90 + for i in range(len(expand)):
  91 + print expand[i]
  92 + return expand #Leave this line for grading purposes!
  93 +
  94 +
  95 +search()
57 udacity/cs373/unit-4/dynamic-value.py
... ... @@ -0,0 +1,57 @@
  1 +# ----------
  2 +# User Instructions:
  3 +#
  4 +# Create a function compute_value() which returns
  5 +# a grid of values. Value is defined as the minimum
  6 +# number of moves required to get from a cell to the
  7 +# goal.
  8 +#
  9 +# If it is impossible to reach the goal from a cell
  10 +# you should assign that cell a value of 99.
  11 +
  12 +# ----------
  13 +
  14 +grid = [[0, 1, 0, 0, 0, 0],
  15 + [0, 1, 0, 0, 0, 0],
  16 + [0, 1, 1, 0, 0, 0],
  17 + [0, 1, 0, 1, 0, 0],
  18 + [0, 1, 0, 0, 1, 0]]
  19 +
  20 +init = [0, 0]
  21 +goal = [len(grid)-1, len(grid[0])-1]
  22 +
  23 +delta = [[-1, 0 ], # go up
  24 + [ 0, -1], # go left
  25 + [ 1, 0 ], # go down
  26 + [ 0, 1 ]] # go right
  27 +
  28 +delta_name = ['^', '<', 'v', '>']
  29 +
  30 +cost_step = 1 # the cost associated with moving from a cell to an adjacent one.
  31 +
  32 +# ----------------------------------------
  33 +# insert code below
  34 +# ----------------------------------------
  35 +
  36 +def compute_value_r(out, x, y):
  37 + explore = []
  38 + current_value = out[x][y]
  39 + for i in range(len(delta)):
  40 + x2 = x + delta[i][0]
  41 + y2 = y + delta[i][1]
  42 + if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
  43 + if grid[x2][y2] == 0 and current_value < out[x2][y2]:
  44 + explore.append([x2, y2])
  45 + out[x2][y2] = out[x][y] + cost_step
  46 + for x in explore:
  47 + compute_value_r(out, x[0], x[1])
  48 +
  49 +def compute_value():
  50 + value = [[99 for row in range(len(grid[0]))] for col in range(len(grid))]
  51 + value[goal[0]][goal[1]] = 0
  52 + compute_value_r(value, goal[0], goal[1])
  53 + return value #make sure your function returns a grid of values as demonstrated in the previous video.
  54 +
  55 +out = compute_value()
  56 +for i in out:
  57 + print i
83 udacity/cs373/unit-4/expansion.py
... ... @@ -0,0 +1,83 @@
  1 +# -----------
  2 +# User Instructions:
  3 +#
  4 +# Modify the function search() so that it returns
  5 +# a table of values called expand. This table
  6 +# will keep track of which step each node was
  7 +# expanded.
  8 +#
  9 +# For grading purposes, please leave the return
  10 +# statement at the bottom.
  11 +# ----------
  12 +
  13 +
  14 +grid = [[0, 0, 1, 0, 0, 0],
  15 + [0, 0, 0, 0, 0, 0],
  16 + [0, 0, 1, 0, 1, 0],
  17 + [0, 0, 1, 0, 1, 0],
  18 + [0, 0, 1, 0, 1, 0]]
  19 +
  20 +init = [0, 0]
  21 +goal = [len(grid)-1, len(grid[0])-1]
  22 +
  23 +delta = [[-1, 0 ], # go up
  24 + [ 0, -1], # go left
  25 + [ 1, 0 ], # go down
  26 + [ 0, 1 ]] # go right
  27 +
  28 +delta_name = ['^', '<', 'v', '>']
  29 +
  30 +cost = 1
  31 +
  32 +
  33 +# ----------------------------------------
  34 +# modify code below
  35 +# ----------------------------------------
  36 +
  37 +def search():
  38 + closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
  39 + closed[init[0]][init[1]] = 1
  40 +
  41 + expand = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
  42 + iteration = 0
  43 +
  44 + x = init[0]
  45 + y = init[1]
  46 + g = 0
  47 +
  48 + open = [[g, x, y]]
  49 +
  50 + found = False # flag that is set when search is complete
  51 + resign = False # flag set if we can't find expand
  52 +
  53 + while not found and not resign:
  54 + if len(open) == 0:
  55 + resign = True
  56 + else:
  57 + open.sort()
  58 + open.reverse()
  59 + next = open.pop()
  60 + x = next[1]
  61 + y = next[2]
  62 + g = next[0]
  63 +
  64 + expand[x][y] = iteration
  65 + iteration += 1
  66 +
  67 + if x == goal[0] and y == goal[1]:
  68 + found = True
  69 + else:
  70 + for i in range(len(delta)):
  71 + x2 = x + delta[i][0]
  72 + y2 = y + delta[i][1]
  73 + if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
  74 + if closed[x2][y2] == 0 and grid[x2][y2] == 0:
  75 + g2 = g + cost
  76 + open.append([g2, x2, y2])
  77 + closed[x2][y2] = 1
  78 + return expand #Leave this line for grading purposes!
  79 +
  80 +
  81 +
  82 +
  83 +print search()
83 udacity/cs373/unit-4/grid-search.py
... ... @@ -0,0 +1,83 @@
  1 +# -----------
  2 +# User Instructions:
  3 +#
  4 +# Modify the function search() so that it returns
  5 +# a table of values called expand. This table
  6 +# will keep track of which step each node was
  7 +# expanded.
  8 +#
  9 +# For grading purposes, please leave the return
  10 +# statement at the bottom.
  11 +# ----------
  12 +
  13 +
  14 +grid = [[0, 0, 1, 0, 0, 0],
  15 + [0, 0, 0, 0, 0, 0],
  16 + [0, 0, 1, 0, 1, 0],
  17 + [0, 0, 1, 0, 1, 0],
  18 + [0, 0, 1, 0, 1, 0]]
  19 +
  20 +init = [0, 0]
  21 +goal = [len(grid)-1, len(grid[0])-1]
  22 +
  23 +delta = [[-1, 0 ], # go up
  24 + [ 0, -1], # go left
  25 + [ 1, 0 ], # go down
  26 + [ 0, 1 ]] # go right
  27 +
  28 +delta_name = ['^', '<', 'v', '>']
  29 +
  30 +cost = 1
  31 +
  32 +
  33 +# ----------------------------------------
  34 +# modify code below
  35 +# ----------------------------------------
  36 +
  37 +def search():
  38 + closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
  39 + closed[init[0]][init[1]] = 1
  40 +
  41 + expand = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
  42 + iteration = 0
  43 +
  44 + x = init[0]
  45 + y = init[1]
  46 + g = 0
  47 +
  48 + open = [[g, x, y]]
  49 +
  50 + found = False # flag that is set when search is complete
  51 + resign = False # flag set if we can't find expand
  52 +
  53 + while not found and not resign:
  54 + if len(open) == 0:
  55 + resign = True
  56 + else:
  57 + open.sort()
  58 + open.reverse()
  59 + next = open.pop()
  60 + x = next[1]
  61 + y = next[2]
  62 + g = next[0]
  63 +
  64 + expand[x][y] = iteration
  65 + iteration += 1
  66 +
  67 + if x == goal[0] and y == goal[1]:
  68 + found = True
  69 + else:
  70 + for i in range(len(delta)):
  71 + x2 = x + delta[i][0]
  72 + y2 = y + delta[i][1]
  73 + if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
  74 + if closed[x2][y2] == 0 and grid[x2][y2] == 0:
  75 + g2 = g + cost
  76 + open.append([g2, x2, y2])
  77 + closed[x2][y2] = 1
  78 + return expand #Leave this line for grading purposes!
  79 +
  80 +
  81 +
  82 +
  83 +print search()
141 udacity/cs373/unit-4/left-turn-policy.py
... ... @@ -0,0 +1,141 @@
  1 +# ----------
  2 +# User Instructions:
  3 +#
  4 +# Implement the function optimum_policy2D() below.
  5 +#
  6 +# You are given a car in a grid with initial state
  7 +# init = [x-position, y-position, orientation]
  8 +# where x/y-position is its position in a given
  9 +# grid and orientation is 0-3 corresponding to 'up',
  10 +# 'left', 'down' or 'right'.
  11 +#
  12 +# Your task is to compute and return the car's optimal
  13 +# path to the position specified in `goal'; where
  14 +# the costs for each motion are as defined in `cost'.
  15 +
  16 +# EXAMPLE INPUT:
  17 +
  18 +# grid format:
  19 +# 0 = navigable space
  20 +# 1 = occupied space
  21 +grid = [[1, 1, 1, 0, 0, 0],
  22 + [1, 1, 1, 0, 1, 0],
  23 + [0, 0, 0, 0, 0, 0],
  24 + [1, 1, 1, 0, 1, 1],
  25 + [1, 1, 1, 0, 1, 1]]
  26 +goal = [2, 0] # final position
  27 +init = [4, 3, 0] # first 2 elements are coordinates, third is direction
  28 +cost = [2, 1, 2] # the cost field has 3 values: right turn, no turn, left turn
  29 +
  30 +# EXAMPLE OUTPUT:
  31 +# calling optimum_policy2D() should return the array
  32 +#
  33 +# [[' ', ' ', ' ', 'R', '#', 'R'],
  34 +# [' ', ' ', ' ', '#', ' ', '#'],
  35 +# ['*', '#', '#', '#', '#', 'R'],
  36 +# [' ', ' ', ' ', '#', ' ', ' '],
  37 +# [' ', ' ', ' ', '#', ' ', ' ']]
  38 +#
  39 +# ----------
  40 +
  41 +
  42 +# there are four motion directions: up/left/down/right
  43 +# increasing the index in this array corresponds to
  44 +# a left turn. Decreasing is is a right turn.
  45 +
  46 +forward = [[-1, 0], # go up
  47 + [ 0, -1], # go left
  48 + [ 1, 0], # go down
  49 + [ 0, 1]] # do right
  50 +forward_name = ['up', 'left', 'down', 'right']
  51 +
  52 +# the cost field has 3 values: right turn, no turn, left turn
  53 +action = [-1, 0, 1]
  54 +action_name = ['R', '#', 'L']
  55 +
  56 +
  57 +# ----------------------------------------
  58 +# modify code below
  59 +# ----------------------------------------
  60 +
  61 +def optimum_policy2D():
  62 + value = [[[999 for row in range(len(grid[0]))] for col in range(len(grid))],
  63 + [[999 for row in range(len(grid[0]))] for col in range(len(grid))],
  64 + [[999 for row in range(len(grid[0]))] for col in range(len(grid))],
  65 + [[999 for row in range(len(grid[0]))] for col in range(len(grid))]]
  66 +
  67 + policy = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
  68 + [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
  69 + [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
  70 + [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]]
  71 +
  72 + policy2D = [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
  73 +
  74 + changed = True
  75 + while changed:
  76 + changed = False
  77 +
  78 + for x in range(len(grid)):
  79 + for y in range(len(grid[0])):
  80 + for d in range(len(forward)):
  81 +
  82 + if x == goal[0] and y == goal[1]:
  83 + if value[d][x][y] != 0:
  84 + value[d][x][y] = 0
  85 + policy[d][x][y] = '*'
  86 + changed = True
  87 +
  88 + elif grid[x][y] == 0:
  89 + for a in range(len(action)):
  90 + d2 = (d + action[a]) % 4
  91 + x2 = x + forward[d2][0]
  92 + y2 = y + forward[d2][1]
  93 +
  94 + if x2 >= 0 and x2 < len(grid) and y2 >= 0 and y2 < len(grid[0]) and grid[x2][y2] == 0:
  95 + v2 = value[d2][x2][y2] + cost[a]
  96 +
  97 + if v2 < value[d][x][y]:
  98 + changed = True
  99 + value[d][x][y] = v2
  100 + policy[d][x][y] = action_name[a]
  101 +
  102 + x = init[0]
  103 + y = init[1]
  104 + d = init[2]
  105 +
  106 + policy2D[x][y] = policy[d][x][y]
  107 + while policy[d][x][y] != '*':
  108 + if policy[d][x][y] == '#':
  109 + d2 = d
  110 + elif policy[d][x][y] == 'R':
  111 + d2 = (d - 1) % 4
  112 + elif policy[d][x][y] == 'L':
  113 + d2 = (d + 1) % 4
  114 + x = x + forward[d2][0]
  115 + y = y + forward[d2][1]
  116 + d = d2
  117 + policy2D[x][y] = policy[d][x][y]
  118 +
  119 + # while value[d][x][y] > 0:
  120 + # best_a = -1
  121 + # best_value = 1000
  122 + # for a in range(len(action)):
  123 + # new_d = (d + action[a]) % 4
  124 + # new_x = x + forward[new_d][0]
  125 + # new_y = y + forward[new_d][1]
  126 + # if value[new_d][new_x][new_y] < best_value:
  127 + # best_value = value[new_d][new_x][new_y]
  128 + # best_a = a
  129 + # policy2D[x][y] = action_name[best_a]
  130 + # d = (d + action[best_a]) % 4
  131 + # x = x + forward[d][0]
  132 + # y = y + forward[d][1]
  133 +
  134 + policy2D[x][y] = '*'
  135 +
  136 + return policy2D # Make sure your function returns the expected grid.
  137 +
  138 +p = optimum_policy2D()
  139 +for i in p:
  140 + print i
  141 +
60 udacity/cs373/unit-4/optimal-policy.py
... ... @@ -0,0 +1,60 @@
  1 +# ----------
  2 +# User Instructions:
  3 +#
  4 +# Create a function optimum_path() which returns
  5 +# a grid of values. Value is defined as the minimum
  6 +# number of moves required to get from a cell to the
  7 +# goal.
  8 +#
  9 +# If it is impossible to reach the goal from a cell
  10 +# you should assign that cell a value of 99.
  11 +
  12 +# ----------
  13 +
  14 +grid = [[0, 1, 0, 0, 0, 0],
  15 + [0, 1, 0, 0, 0, 0],
  16 + [0, 1, 0, 0, 0, 0],
  17 + [0, 1, 0, 0, 0, 0],
  18 + [0, 0, 0, 0, 1, 0]]
  19 +
  20 +init = [0, 0]
  21 +goal = [len(grid)-1, len(grid[0])-1]
  22 +
  23 +delta = [[-1, 0 ], # go up
  24 + [ 0, -1], # go left
  25 + [ 1, 0 ], # go down
  26 + [ 0, 1 ]] # go right
  27 +
  28 +delta_name = ['^', '<', 'v', '>']
  29 +
  30 +cost_step = 1 # the cost associated with moving from a cell to an adjacent one.
  31 +
  32 +# ----------------------------------------
  33 +# insert code below
  34 +# ----------------------------------------
  35 +
  36 +def optimum_path_r(policy, out, x, y):
  37 + explore = []
  38 + current_value = out[x][y]
  39 + for i in range(len(delta)):
  40 + x2 = x + delta[i][0]
  41 + y2 = y + delta[i][1]
  42 + if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
  43 + if grid[x2][y2] == 0 and current_value < out[x2][y2]:
  44 + explore.append([x2, y2])
  45 + policy[x2][y2] = delta_name[(i+2)%4]
  46 + out[x2][y2] = out[x][y] + 1
  47 + for x in explore:
  48 + optimum_path_r(policy, out, x[0], x[1])
  49 +
  50 +def optimum_path():
  51 + policy = [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
  52 + policy[goal[0]][goal[1]] = '*'
  53 + value = [[99 for row in range(len(grid[0]))] for col in range(len(grid))]
  54 + value[goal[0]][goal[1]] = 0
  55 + optimum_path_r(policy, value, goal[0], goal[1])
  56 + return policy #make sure your function returns a grid of values as demonstrated in the previous video.
  57 +
  58 +out = optimum_path()
  59 +for i in out:
  60 + print i
109 udacity/cs373/unit-4/path.py
... ... @@ -0,0 +1,109 @@
  1 +# ----------
  2 +# User Instructions:
  3 +#
  4 +# Modify the the search function so that it returns
  5 +# a shortest path as follows:
  6 +#
  7 +# [['>', 'v', ' ', ' ', ' ', ' '],
  8 +# [' ', '>', '>', '>', '>', 'v'],
  9 +# [' ', ' ', ' ', ' ', ' ', 'v'],
  10 +# [' ', ' ', ' ', ' ', ' ', 'v'],
  11 +# [' ', ' ', ' ', ' ', ' ', '*']]
  12 +#
  13 +# Where '>', '<', '^', and 'v' refer to right, left,
  14 +# up, and down motions. NOTE: the 'v' should be
  15 +# lowercase.
  16 +#
  17 +# Your function should be able to do this for any
  18 +# provided grid, not just the sample grid below.
  19 +# ----------
  20 +
  21 +
  22 +# Sample Test case
  23 +grid = [[0, 0, 1, 0, 0, 0],
  24 + [0, 0, 0, 0, 0, 0],
  25 + [0, 0, 1, 0, 1, 0],
  26 + [0, 0, 1, 0, 1, 0],
  27 + [0, 0, 1, 0, 1, 0]]
  28 +
  29 +init = [0, 0]
  30 +goal = [len(grid)-1, len(grid[0])-1]
  31 +
  32 +delta = [[-1, 0 ], # go up
  33 + [ 0, -1], # go left
  34 + [ 1, 0 ], # go down
  35 + [ 0, 1 ]] # go right
  36 +
  37 +delta_name = ['^', '<', 'v', '>']
  38 +
  39 +cost = 1
  40 +
  41 +# ----------------------------------------
  42 +# modify code below
  43 +# ----------------------------------------
  44 +
  45 +def search():
  46 + closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
  47 + closed[init[0]][init[1]] = 1
  48 +
  49 + count = 0
  50 + expand = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
  51 + prevs = [[None for row in range(len(grid[0]))] for col in range(len(grid))]
  52 +
  53 + x = init[0]
  54 + y = init[1]
  55 + g = 0
  56 +
  57 + open = [[g, x, y]]
  58 +
  59 + found = False # flag that is set when search is complet
  60 + resign = False # flag set if we can't find expand
  61 +
  62 + while not found and not resign:
  63 + if len(open) == 0:
  64 + resign = True
  65 + return 'fail'
  66 + else:
  67 + open.sort()
  68 + open.reverse()
  69 + next = open.pop()
  70 + x = next[1]
  71 + y = next[2]
  72 + g = next[0]
  73 +
  74 + expand[x][y] = count
  75 + count += 1
  76 +
  77 + if x == goal[0] and y == goal[1]:
  78 + found = True
  79 + else:
  80 + for i in range(len(delta)):
  81 + x2 = x + delta[i][0]
  82 + y2 = y + delta[i][1]
  83 + if x2 >= 0 and x2 < len(grid) and y2 >=0 and y2 < len(grid[0]):
  84 + if closed[x2][y2] == 0 and grid[x2][y2] == 0:
  85 + prevs[x2][y2] = [x,y,delta_name[i]]
  86 + g2 = g + cost
  87 + open.append([g2, x2, y2])
  88 + closed[x2][y2] = 1
  89 + for i in range(len(expand)):
  90 + print expand[i]
  91 +
  92 + moves = [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
  93 +
  94 + x = goal[0]
  95 + y = goal[1]
  96 + moves[x][y] = '*'
  97 +
  98 + while prevs[x][y]:
  99 + p = prevs[x][y]
  100 + moves[p[0]][p[1]] = p[2]
  101 + x = p[0]
  102 + y = p[1]
  103 +
  104 + return moves # make sure you return the shortest path.
  105 +
  106 +
  107 +res = search()
  108 +for i in range(len(res)):
  109 + print res[i]

0 comments on commit e7443b7

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