forked from falcons-cs-assignments/object-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanual_control.py
201 lines (164 loc) · 4.12 KB
/
manual_control.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
import argparse
from gym_minigrid.wrappers import *
from mini_behavior.window import Window
from mini_behavior.utils.save import get_step, save_demo
import mini_behavior.envs
import numpy as np
from object_nav.envs.igridson.igridson_env import *
# Size in pixels of a tile in the full-scale human view
TILE_PIXELS = 32
show_furniture = False
def redraw(img):
if not args.agent_view:
img = env.render('rgb_array', tile_size=args.tile_size)
window.no_closeup()
window.set_inventory(env)
window.show_img(img)
def show_states():
imgs = env.render_states()
window.show_closeup(imgs)
def reset():
if args.seed != -1:
env.seed(args.seed)
obs = env.reset()
if hasattr(env, 'mission'):
print('Mission: %s' % env.mission)
window.set_caption(env.mission)
redraw(obs)
def load():
if args.seed != -1:
env.seed(args.seed)
env.reset()
obs = env.load_state(args.load)
if hasattr(env, 'mission'):
print('Mission: %s' % env.mission)
window.set_caption(env.mission)
redraw(obs)
def step(action):
prev_obs = env.gen_obs()
obs, reward, done, info = env.step(action)
print('step=%s, reward=%.2f' % (env.step_count, reward))
if args.save:
all_steps[env.step_count] = (prev_obs, action)
if done:
print('done!')
if args.save:
save_demo(all_steps, args.env, env.episode)
reset()
else:
redraw(obs)
# def switch_dim(dim):
# env.switch_dim(dim)
# print(f'switching to dim: {env.render_dim}')
# obs = env.gen_obs()
# redraw(obs)
def key_handler_primitive(event):
print('pressed', event.key)
if event.key == 'backspace':
reset()
return
if event.key == 'escape':
window.close()
return
if event.key == 'left':
step(env.actions.left)
return
if event.key == 'right':
step(env.actions.right)
return
if event.key == 'up':
step(env.actions.forward)
return
if event.key == '0':
step(env.actions.pickup_0)
return
if event.key == '1':
step(env.actions.pickup_1)
return
if event.key == '2':
step(env.actions.pickup_2)
return
if event.key == '3':
step(env.actions.drop_0)
return
if event.key == '4':
step(env.actions.drop_1)
return
if event.key == '5':
step(env.actions.drop_2)
return
if event.key == 't':
step(env.actions.toggle)
return
if event.key == 'o':
step(env.actions.open)
return
if event.key == 'c':
step(env.actions.close)
return
if event.key == 'k':
step(env.actions.cook)
return
if event.key == 's':
step(env.actions.slice)
return
if event.key == 'i':
step(env.actions.drop_in)
return
if event.key == 'pagedown':
show_states()
return
parser = argparse.ArgumentParser()
parser.add_argument(
"--env",
help="gym environment to load",
default='MiniGrid-igridson-16x16-N2-v0'
)
parser.add_argument(
"--seed",
type=int,
help="random seed to generate the environment with",
default=-1
)
parser.add_argument(
"--tile_size",
type=int,
help="size at which to render tiles",
default=32
)
parser.add_argument(
'--agent_view',
default=False,
help="draw the agent sees (partially observable view)",
action='store_true'
)
# NEW
parser.add_argument(
"--save",
default=False,
help="whether or not to save the demo_16"
)
# NEW
parser.add_argument(
"--load",
default=None,
help="path to load state from"
)
args = parser.parse_args()
env = gym.make(args.env)
if args.save:
# We do not support save for cartesian action space
assert env.mode == "primitive"
all_steps = {}
if args.agent_view:
env = RGBImgPartialObsWrapper(env)
env = ImgObsWrapper(env)
window = Window('mini_behavior - ' + args.env)
window.reg_key_handler(key_handler_primitive)
if args.load is None:
reset()
else:
load()
# Blocking event loop
window.show(block=True)