-
Notifications
You must be signed in to change notification settings - Fork 56
/
demo_gym.py
81 lines (61 loc) · 2.22 KB
/
demo_gym.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
"""
How to use the IKEA furniture assembly environment with gym interface.
For parallel execution, set `--num_envs` flag to larger than 1.
"""
import time
import gym
import imageio
from env import make_vec_env
def main(config):
"""
Makes a gym environment with name @env_id and runs one episode.
If @config.num_env is larger than 1, it will use SubprocVecEnv to run
multiple environments in parallel.
"""
num_env = config.num_env
env_id = config.env_id
if num_env == 1:
# create a gym environment instance
env = gym.make(env_id, **config.__dict__)
# reset the environment
observation = env.reset()
done = False
st = time.time()
for i in range(500):
# take a step with a randomly sampled action
observation, reward, done, info = env.step(env.action_space.sample())
print('FPS = {}'.format(500 / (time.time() - st)))
# close the environment instance
env.close()
else:
envs = make_vec_env(env_id, num_env, config=config)
envs.reset()
img = envs.render('rgb_array')
imageio.imwrite('vec_env_image.png', img)
st = time.time()
for i in range(500):
# take a step with a randomly sampled action
observation, reward, done, info = envs.step(
[envs.action_space.sample() for _ in range(num_env)]
)
print('FPS = {}'.format((500 * num_env) / (time.time() - st)))
envs.close()
def argsparser():
"""
Returns argument parser for furniture assembly environment.
"""
import argparse
from util import str2bool
parser = argparse.ArgumentParser("Demo for IKEA Furniture Assembly Environment")
parser.add_argument('--env_id', type=str, default='furniture-baxter-v0')
parser.add_argument('--num_env', type=int, default=1)
parser.add_argument('--seed', type=int, default=123)
parser.add_argument('--debug', type=str2bool, default=False)
import config.furniture as furniture_config
furniture_config.add_argument(parser)
parser.set_defaults(visual_ob=True)
args = parser.parse_args()
return args
if __name__ == '__main__':
args = argsparser()
main(args)