-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jason Frame
committed
Mar 20, 2012
1 parent
501da83
commit e7443b7
Showing
7 changed files
with
628 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# ----------- | ||
# 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 | ||
f = g + heuristic[x][y] | ||
|
||
open = [[f, 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[2] | ||
y = next[3] | ||
g = next[1] | ||
f = 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 | ||
h2 = heuristic[x2][y2] | ||
f2 = g2 + h2 | ||
open.append([f2, 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# ---------- | ||
# 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, 1, 0, 0, 0], | ||
[0, 1, 0, 1, 0, 0], | ||
[0, 1, 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_r(out, x, y): | ||
explore = [] | ||
current_value = out[x][y] | ||
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 grid[x2][y2] == 0 and current_value < out[x2][y2]: | ||
explore.append([x2, y2]) | ||
out[x2][y2] = out[x][y] + cost_step | ||
for x in explore: | ||
compute_value_r(out, x[0], x[1]) | ||
|
||
def compute_value(): | ||
value = [[99 for row in range(len(grid[0]))] for col in range(len(grid))] | ||
value[goal[0]][goal[1]] = 0 | ||
compute_value_r(value, goal[0], goal[1]) | ||
return value #make sure your function returns a grid of values as demonstrated in the previous video. | ||
|
||
out = compute_value() | ||
for i in out: | ||
print i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# ----------- | ||
# User Instructions: | ||
# | ||
# Modify the function search() so that it returns | ||
# a table of values called expand. This table | ||
# will keep track of which step each node was | ||
# expanded. | ||
# | ||
# For grading purposes, please leave the return | ||
# statement at the bottom. | ||
# ---------- | ||
|
||
|
||
grid = [[0, 0, 1, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0], | ||
[0, 0, 1, 0, 1, 0], | ||
[0, 0, 1, 0, 1, 0], | ||
[0, 0, 1, 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 = 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))] | ||
iteration = 0 | ||
|
||
x = init[0] | ||
y = init[1] | ||
g = 0 | ||
|
||
open = [[g, x, y]] | ||
|
||
found = False # flag that is set when search is complete | ||
resign = False # flag set if we can't find expand | ||
|
||
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] = iteration | ||
iteration += 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 | ||
open.append([g2, x2, y2]) | ||
closed[x2][y2] = 1 | ||
return expand #Leave this line for grading purposes! | ||
|
||
|
||
|
||
|
||
print search() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# ----------- | ||
# User Instructions: | ||
# | ||
# Modify the function search() so that it returns | ||
# a table of values called expand. This table | ||
# will keep track of which step each node was | ||
# expanded. | ||
# | ||
# For grading purposes, please leave the return | ||
# statement at the bottom. | ||
# ---------- | ||
|
||
|
||
grid = [[0, 0, 1, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0], | ||
[0, 0, 1, 0, 1, 0], | ||
[0, 0, 1, 0, 1, 0], | ||
[0, 0, 1, 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 = 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))] | ||
iteration = 0 | ||
|
||
x = init[0] | ||
y = init[1] | ||
g = 0 | ||
|
||
open = [[g, x, y]] | ||
|
||
found = False # flag that is set when search is complete | ||
resign = False # flag set if we can't find expand | ||
|
||
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] = iteration | ||
iteration += 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 | ||
open.append([g2, x2, y2]) | ||
closed[x2][y2] = 1 | ||
return expand #Leave this line for grading purposes! | ||
|
||
|
||
|
||
|
||
print search() |
Oops, something went wrong.