-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayback.py
96 lines (84 loc) · 1.89 KB
/
playback.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
import numpy as np
## This file takes a logged episode and plays it back
WORLD = {
'EMPTY': -3,
'OBSTACLE': -2,
'ROBOT': -1,
'GOAL': 0
}
EPISODE_NUM = 30
with open("logging/episode"+str(EPISODE_NUM)+".txt", 'r') as f:
s = f.readline()
actions = np.loadtxt("logging/episode"+str(EPISODE_NUM)+".txt", skiprows =1)
s = (s.replace("+", "").replace("e", "").replace("\n", "").\
replace(".", "").replace("0", "").split(" "))
clean_state = []
for i, st in enumerate(s):
if st == "-1":
player_loc = i / 10, i % 10
if len(st) > 0:
clean_state.append(st)
else:
clean_state.append(0)
world = np.array(map(int, clean_state)).reshape((10,10))
# s = np.fromstring(s)
print clean_state
print actions
if len(actions.shape) == 1:
actions = np.array([actions])
def display_world(world):
lineStr = ""
for j in range(len(world[0]) + 2):
lineStr += "X "
print lineStr
for i in range(len(world)):
# Initialize string for that line
lineStr = "X "
for j in range(len(world[i])):
# Print O for obstacle, G for goal,
# R for robot, and E for empty
if world[i][j] == WORLD['OBSTACLE']:
lineStr += "O "
elif world[i][j] == WORLD['GOAL']:
lineStr += "G "
elif world[i][j] == WORLD['ROBOT']:
lineStr += "R "
elif world[i][j] == WORLD['EMPTY']:
lineStr += " "
else:
lineStr += (str(world[i][j]) + " ")
print lineStr + "X "
lineStr = ""
for j in range(len(world[0]) + 2):
lineStr += "X "
print lineStr
return
# ballloc
# for action
display_world(world)
x, y = player_loc
k = 1
for a in actions:
player_loc = x, y
print x, y
index = np.argmax(a)
if index == 0:
x, y = x , y-1
elif index == 1:
x, y = x , y+1
elif index == 2:
x, y = x-1 , y
else:
x, y = x+1 , y
x = max(0, x)
y = max(0, y)
try:
if world[x, y] != WORLD['OBSTACLE']:
world[x, y] = k
else:
x, y = player_loc
except:
x, y = player_loc
k += 1
display_world(world)
print player_loc