-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfollow.py
123 lines (99 loc) · 3.53 KB
/
follow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -----------
# 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():
print 'Grid\n '
for i in range(len(grid)):
print grid[i]
track = 0
closed = [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
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))]
trackPath = [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
gaction = [[-1 for row in range(len(grid[0]))] for col in range(len(grid))]
closed[init[0]][init[1]] = 1
expand[init[0]][init[1]] = track
x = init[0]
y = init[1]
g = 0
gaction[init[0]][init[1]] = g
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]
if x == goal[0] and y == goal[1]:
gaction[x][y]=g
trackPath[x][y] = '*'
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
track +=1
expand[x2][y2] = track
action[x2][y2] = i
trackPath[x][y]=delta_name[i]
gaction[x][y]=g
print 'Gaction \n'
for i in range(len(gaction)):
print gaction[i]
print 'Action \n'
for i in range(len(action)):
print action[i]
print 'Track \n'
for i in range(len(trackPath)):
print trackPath[i]
policy = [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
x = goal[0]
y = goal[1]
policy[x][y] = '*'
while x >= init[0] and y >= init[1]:
x2 = x - delta[action[x][y]][0]
y2 = y - delta[action[x][y]][1]
if gaction[x2][y2] != -1 and gaction[x][y] - gaction[x2][y2] == 1 :
policy[x2][y2] = delta_name[action[x][y]]
x= x2
y=y2
print 'Policy\n '
for i in range(len(policy)):
print policy[i]
return expand #Leave this line for grading purposes!
print search()