-
Notifications
You must be signed in to change notification settings - Fork 5
/
manual_control.py
executable file
·266 lines (224 loc) · 5.7 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/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
from mini_behavior.grid import GridDimension
import numpy as np
# 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 render_furniture():
global show_furniture
show_furniture = not show_furniture
if show_furniture:
img = np.copy(env.furniture_view)
# i, j = env.agent.cur_pos
i, j = env.agent_pos
ymin = j * TILE_PIXELS
ymax = (j + 1) * TILE_PIXELS
xmin = i * TILE_PIXELS
xmax = (i + 1) * TILE_PIXELS
img[ymin:ymax, xmin:xmax, :] = GridDimension.render_agent(
img[ymin:ymax, xmin:xmax, :], env.agent_dir)
img = env.render_furniture_states(img)
window.show_img(img)
else:
obs = env.gen_obs()
redraw(obs)
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_cartesian(event):
print('pressed', event.key)
if event.key == 'escape':
window.close()
return
if event.key == 'backspace':
reset()
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
# Spacebar
if event.key == ' ':
render_furniture()
return
if event.key == 'pageup':
step('choose')
return
if event.key == 'enter':
env.save_state()
return
if event.key == 'pagedown':
show_states()
return
if event.key == '0':
switch_dim(None)
return
if event.key == '1':
switch_dim(0)
return
if event.key == '2':
switch_dim(1)
return
if event.key == '3':
switch_dim(2)
return
def key_handler_primitive(event):
print('pressed', event.key)
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-InstallingAPrinter-8x8-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)
env.teleop_mode()
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)
if env.mode == "cartesian":
window.reg_key_handler(key_handler_cartesian)
elif env.mode == "primitive":
window.reg_key_handler(key_handler_primitive)
if args.load is None:
reset()
else:
load()
# Blocking event loop
window.show(block=True)