-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
partially complete proj3: TODO pass more tests for test 2
- Loading branch information
Haoyang Chen
committed
Apr 22, 2019
1 parent
ee877ef
commit 4311a3c
Showing
16 changed files
with
772 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File renamed without changes.
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,4 @@ | ||
>.<o | ||
^^<< | ||
^^^< | ||
^o^o |
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,4 @@ | ||
ovvo | ||
vv<< | ||
>.<o | ||
>^<< |
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,50 @@ | ||
<<<<<o>>>^<>>>>>^<<<<>>^<<<<>>>^<<<<<<o>>^<>>>>>>> | ||
^<^^vvvvv^^^^>>^^^^^^^^^^^^^^^^^<<<<ov>>^^^o>>^^^^ | ||
v<o>vvvv<<^^>>>^^^^^^^^^^<^^^>^^^<<<<vovvov>>^^^^^ | ||
vvvvvvvv<<<o>>>^^^^^^^^^^<<o>>^^^^^<<<<>vvv<ovvo>^ | ||
vvvvv>><<vvvvv>^^^^<o>^^^^vvvv^^^^^^v<o>vv<<>vv<>^ | ||
vvvv<<^^vvvvvv<^^^^vvv^^^>>vvv<<ovo>vv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^<o>^^^>>>>>^^^^^^^^^o | ||
<<<<<<<<<^^^<<<<^^>vv>>>^^^^<<>>^^<o>>^^^^^^^^^^<< | ||
^<<<<<<^^^^^<<<<o>>v>>>>^^^<<o>>^^<<>^^^^^^^^^^^<< | ||
^<<<<<<^^^^^^<<vv>>>>>>^^^<<<vvo>^vvvo^^^^^^^^^^^< | ||
^<<<<<<^^^^^^vvvv>>>>>>^^^<<<<<>>vvvvvvo^o^^^^^^^^ | ||
^<<<<<<<^^^>>>vvv>>>>>>^^^<<<<<>>>vvvvv<<>>>^^^^^^ | ||
^<<<<<<<<o>>>>>>>>>>>>>^^^<<<<o>>>>v<<<<<o>>^^^^^^ |
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,50 @@ | ||
^<<<<<<<^<<>>>>>>^<<<<>>>>>^<<>^<<<<<<>^<>>^<<<<<< | ||
^<^^^^^^^^<o>^^^^^^<<<^>>>^^^^^^^^^^<^^^^^>^^<^^^o | ||
^<^^^^<o>vvvv^^>>^^^<<o>>>^^^^^^^^^^<o^^vo>vv<ovvv | ||
^<<o>vvv>>vvv<o>>^^^<<<>>>^^^^^^^^^^<>>vvvvvvvvv<< | ||
^<<<>vv<<>>vvvvvv^^^^v<>>^^<<o^^^ov^<>>>vvvvvvv<<< | ||
v<<<>>v<<^>>><>vv<<ovvvo>^^<<<>vvvvv<o>>>>>vvv<<<o | ||
v<<o>>v<<o>^^>>>vvvvvvvvv^^^<>>>vvvvvv>>>>>>vv<<<v | ||
v<vvvvv<<<>^^^>>>vvvvvvv<<^^^o>>vvvvvvv^^>>>>v<<<v | ||
vvvvvvv<<<<^^o>>>vvvv<<<<<o^vv>>vvvvv<<<^>>>>^<<<< | ||
v<<vv<<<<<ovv>>>>><<<<^<<<<>>>>>>>vvvv<<o>>>>^<<<^ | ||
<<<<<<<<<<vv<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<o>>>>>>>>>>>>>>>>^^>>>^^^o^^^^^^^o^^o^^^ | ||
^<<<o>>v<<>>>>>>>>>>>>>>>>^^^o>>>^>>>^^^o^^<^<<^^^ |
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,50 @@ | ||
^<<<<<<<^<<>>>>>>^<<<<>>>>>^<<>^<<<<<<>^<>>^<<<<<< | ||
^<^^^^^^^^<o>^^^>^^<<<^>>>^^^^^^^^^^<^^^^^>^^<^^^o | ||
^<^^^^<o>vvvv^^>>^^^<<o>>>^^^^^^^^^^<o^^vo>vv<ovvv | ||
^<<o>vvv>>vvv<o>>^^^<<<>>>^^^^^^^^^^<>>vvvvvvvvv<< | ||
^<<<>vv<<>>vvvvvv^^^^v<>>^^<<o^^^ov^<>>>vvvvvvv<<< | ||
v<<<>>v<<^>>><>vv<<ovvvo>^^<<<>vvvvv<o>>>>>vvv<<<o | ||
v<<o>>v<<o>^^>>>vvvvvvvvv^^^<>>>vvvvvv>>>>>>vv<<<v | ||
v<vvvvv<<<>^^^>>>vvvvvvv<<^^^o>>vvvvvvv^^>>>>v<<<v | ||
vvvvvvv<<<<^^o>>>vvvv<<<<<o^vv>>vvvv<<<<^>>>>^<<<< | ||
v<<vv<<<<<ovv>>>>><<<<^<<<<>>>>>>>vv<<<<o>>>>^<<<^ | ||
<<<<<<<<<<vv<>>>>^^^^^^^<<>^>>>>>>><<<<<v>>>>^<<<^ | ||
^^^^^^^^<vvv<o>^^^^^^^^^<ovvo>>^^^^^vv<<<<>>^^<<<o | ||
^^^^^^^^>vv<<<>^^^^^<o>vvvvvv>>^^^>>>v<<<<^>>^<<<v | ||
^^^^^<o>>vv<<<ovo>^^<>>>vvvvvv^^^>>>>v<<<<o>>^^<<v | ||
^<o>vvvvvv<<<<>vvvvv<o>>vvvv<<<^o>>>>vv<<<v>>>^^<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<<^>>v | ||
v<<<<<<>>>>^^^^^^^^vvvovvvo>^vo>>vvvvvvvvvvv<<o>>v | ||
v<<<<<<>>^^^^^^^^o>vvvvvvv<>vvv>>vvvvvvvvvvv<vvvvv | ||
v<<<<<o>^^^^^^<ovvvvvvvvv<<>>>v>>>>>>>vvvvvv<vvvvv | ||
v<<<<<vvv<o>vvvvvvvvvvvvv<<o>>>>>>>>>>>vvvv<<<<vvv | ||
v<<<<vvv<<v>>vvvvvvvvvvvv<vv>>>>>^^^^>>>vv<<<<<<<< | ||
v<<<<<<<<<>>>>vvvvvvvv<<<<vv>>>>^^^^^^>>vv<<<<<<<v | ||
<<<<<<<<^^>>>>>>>>>>><<<<<<vv^^^^^^^vovvv<<<^^vvvv | ||
^<<<<<<<o^^^>>>>>>>^^^^^^<<<<<^^^^^>vvvvv<<^^>vvvv | ||
^<<<<<<<vvvo>>^^^^^^^^^^^^^<<<^^^<o>vvvvv<<o>vvvvv | ||
^^^^<<<<<<vv>>^^^^^^^^^^^^^<<<o^^<>>>vvvv<<vvvvv<< | ||
^^^^^^<<<>>>>v^^^^>^^<^^^^^<<<vvvo>>>>vv<<<<vv<<<< | ||
^<^^^^^^^^>>>v<<o>>^^<<o>>^v<<vvv>>>>>.<<<<<<<<<<< | ||
^<<^^^^o^o>>>vvvvv>vv<vvvvv^<>>>>>>>>^^^<<<<^^^^^^ | ||
^<<<o^vvv<>>>>vvvvvvvvvvvvv<^^>>>^^^>^^^^^^^^^^^^^ | ||
^<<<<>>vv<o>>>vvvvvvvvvvvv<<<^^^^^^o>^^^^^^o^^o>^^ | ||
^<<<o>>v<<>>>>>>>>>v<<<<<<<<<o>>^<<>>^^<o^v>>v>>>^ |
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,146 @@ | ||
import os | ||
|
||
__location__ = os.path.realpath( | ||
os.path.join(os.getcwd(), os.path.dirname(__file__))) | ||
|
||
test_path = "dev_cases/" | ||
# right, down, left, up | ||
orientation = [(1, 0), (0, 1), (-1, 0), (0, -1)] | ||
|
||
class Solver(object): | ||
def __init__(self, grid_size, obstacles, dest): | ||
self.row, self.col = grid_size, grid_size | ||
self.dest = dest | ||
self.reward = dict() | ||
self.states = set() | ||
self.obstacles = obstacles | ||
for x in range(grid_size): | ||
for y in range(grid_size): | ||
self.states.add((x, y)) | ||
if (x, y) == dest: | ||
self.reward[x, y] = 99 | ||
elif (x, y) in obstacles: | ||
self.reward[x, y] = -101 | ||
else: | ||
self.reward[x, y] = -1 | ||
|
||
def prettyPrint(self, U): | ||
to_print = "" | ||
for i in range(self.row): | ||
for j in range(self.col): | ||
if (i, j) == self.dest: | ||
to_print += "101 " | ||
else: | ||
to_print += str(U[i, j]) + " " | ||
to_print += "\n" | ||
print(to_print) | ||
print('------') | ||
|
||
def solve(self): | ||
U = self.cal_util() | ||
# self.prettyPrint(U) | ||
pi = dict() | ||
for s in self.states: | ||
up_util = sum([p * U[_s] for (p, _s) in self.T(s, orientation[3])]) | ||
down_util = sum([p * U[_s] for (p, _s) in self.T(s, orientation[1])]) | ||
left_util = sum([p * U[_s] for (p, _s) in self.T(s, orientation[2])]) | ||
right_util = sum([p * U[_s] for (p, _s) in self.T(s, orientation[0])]) | ||
curr_max = max(up_util, down_util, left_util, right_util) | ||
if curr_max == up_util: | ||
pi[s] = orientation[3] | ||
elif curr_max == right_util: | ||
pi[s] = orientation[0] | ||
elif curr_max == down_util: | ||
pi[s] = orientation[1] | ||
elif curr_max == left_util: | ||
pi[s] = orientation[2] | ||
return self.to_grid(pi) | ||
|
||
def to_grid(self, pi): | ||
grid = [[None for _ in range(self.row)] for _ in range(self.col)] | ||
for x in (range(self.row)): | ||
for y in (range(self.col)): | ||
if (x, y) in self.obstacles: | ||
grid[y][x] = 'o' | ||
elif (x, y) == self.dest: | ||
grid[y][x] = '.' | ||
else: | ||
mapping = {(1, 0): '>', (0, 1): 'v', (-1, 0): '<', (0, -1): '^'} | ||
grid[y][x] = mapping[pi[(x, y)]] | ||
with open(os.path.join(__location__, 'output.txt'), 'w') as outfile: | ||
for row in grid: | ||
for e in row: | ||
outfile.write(e) | ||
outfile.write('\n') | ||
|
||
def cal_util(self): | ||
_U = dict([(s, 0) for s in self.states]) | ||
T, gamma, epsilon = self.T, 0.9, 0.1 | ||
while True: | ||
U = _U.copy() | ||
delta = 0 | ||
for s in self.states: | ||
if s == self.dest: continue | ||
reward = self.reward[s[0], s[1]] | ||
_U[s] = reward + gamma * max([sum([p * U[s1] for (p, s1) in T(s, a)]) | ||
for a in orientation]) | ||
delta = max(delta, abs(_U[s] - U[s])) | ||
if delta < epsilon * (1 - gamma) / gamma: | ||
return U | ||
|
||
def T(self, state, action): | ||
def go(state, direction): | ||
curr = (state[0] + direction[0], state[1] + direction[1]) | ||
return curr if curr in self.states else state | ||
if action == None: | ||
return [(0, 0, state)] | ||
else: | ||
# right, down, left, up | ||
# left 2 -> back -> right 0 | ||
# left 2 -> right -> up 3 | ||
# left 2 -> left -> down 1 | ||
|
||
# right 0 -> back -> left 2 | ||
# right 0 -> right -> down 1 | ||
# right 0 -> left -> up 3 | ||
|
||
# up 3 -> back -> down 1 | ||
# up 3 -> right -> right 0 | ||
# up 3 -> left -> left 2 | ||
|
||
# down 1 -> back -> up 3 | ||
# down 1 -> right -> left 2 | ||
# down 1 -> left -> right 0 | ||
right = orientation[(orientation.index(action) + 1) % len(orientation)] | ||
left = orientation[orientation.index(action) - 1] # -1 = 3 | ||
back = orientation[(orientation.index(action) + 2) % len(orientation)] | ||
return [(0.7, go(state, action)), | ||
(0.1, go(state, right)), | ||
(0.1, go(state, left)), | ||
(0.1, go(state, back))] | ||
|
||
def main(): | ||
with open(os.path.join(__location__, test_path + 'input-3.txt'), 'r') as infile: | ||
grid_size = int(infile.readline()) | ||
num_obstacles = int(infile.readline()) | ||
obstacles = list() | ||
dest = None | ||
for i, line in enumerate(infile): | ||
coords = line.strip().split(",") | ||
x, y = int(coords[0]), int(coords[1]) | ||
if i == num_obstacles: | ||
dest = (x, y) | ||
else: | ||
obstacles.append((x, y)) | ||
# print('grid size is: {}\n' | ||
# 'num of obstacles is: {}\n' | ||
# 'obstacles are:\n {}\n' | ||
# 'dest is: {}'.format( | ||
# grid_size, | ||
# num_obstacles, | ||
# obstacles, | ||
# dest)) | ||
solver = Solver(grid_size, obstacles, dest) | ||
solver.solve() | ||
main() | ||
|