-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
87 lines (73 loc) · 2.41 KB
/
run.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
"""
Test simulated envs or Collect human demonstration data
"""
import argparse
import collections
import pickle
import numpy as np
import envs_launcher
import devices
import utilities
def getargs():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
'--input-type',
type=str,
default='pbg',
help='Input device type; must be equal to any of: ' +
', '.join(devices.REGISTRY.keys()))
parser.add_argument(
'--input-scaling',
type=tuple,
default=(10, 25),
help='Scaling applied to device inputs; (pos, orn).')
parser.add_argument(
'--action-space',
type=int,
default=6,
help='Degrees of freedom; must be equal to either 3 or 6.')
parser.add_argument(
'--save-demo-data',
type=bool,
default=False,
help='Save demonstration data.')
parser.add_argument(
'--demo-data-path',
type=str,
default='human_demo_data/default',
help='Location to save demonstration data')
args = parser.parse_args()
assert args.input_type in devices.REGISTRY, \
'arg `input-type` must be equal to any of: ' + ', '.join(devices.REGISTRY.keys())
assert args.action_space == 3 or args.action_space == 6, \
'arg `action-space` must be equal to either 3 or 6.'
return args
def main():
args = getargs()
environment = envs_launcher.env_creator(args)
obs = environment.reset()
memory = collections.deque()
device_cls = devices.REGISTRY[args.input_type]
device = device_cls(*args.input_scaling)
device.start()
done = False
while not done:
device.update()
action = device.pose[:args.action_space]
new_obs, reward, done, info = environment.step(action)
if args.save_demo_data and np.count_nonzero(action) > 0: # we don't want to save all-zero actions
memory.append((obs, action, reward, new_obs, done))
obs = new_obs
else:
device.disconnect()
if args.save_demo_data:
# save all the transitions
file_name = args.demo_data_path
out_file = open(file_name, 'wb')
pickle.dump(memory, out_file)
out_file.close()
utilities.prGreen('Transition saved')
utilities.prGreen('Steps: {}'.format(len(memory)))
if __name__ == '__main__':
main()