-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathenvs.py
182 lines (147 loc) · 5.79 KB
/
envs.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
import gym
import numpy as np
import torch
from gym.spaces.box import Box
from wrapper.benchmarks import *
from wrapper.monitor import *
from wrapper.vec_env import VecEnvWrapper
from wrapper.shmem_vec_env import ShmemVecEnv
from wrapper.dummy_vec_env import DummyVecEnv
try:
import dm_control2gym
except ImportError:
pass
try:
import roboschool
except ImportError:
pass
try:
import pybullet_envs
except ImportError:
pass
def make_env(env_id, seed, rank, log_dir, allow_early_resets, args):
def _thunk():
if env_id.startswith("dm"):
_, domain, task = env_id.split('.')
env = dm_control2gym.make(domain_name=domain, task_name=task)
else:
env = gym.make(
env_id,
setting = args.setting,
container_size = args.container_size,
item_set = args.item_size_set,
data_name = args.dataset_path,
load_test_data = args.load_dataset,
internal_node_holder = args.internal_node_holder,
leaf_node_holder = args.leaf_node_holder,
LNES = args.lnes,
shuffle = args.shuffle,
sample_from_distribution = args.sample_from_distribution,
sample_left_bound = args.sample_left_bound,
sample_right_bound = args.sample_right_bound
)
env.seed(seed + rank)
obs_shape = env.observation_space.shape
if str(env.__class__.__name__).find('TimeLimit') >= 0:
env = TimeLimitMask(env)
if log_dir is not None:
env = Monitor(
env,
os.path.join(log_dir, str(rank)),
allow_early_resets=allow_early_resets)
if len(env.observation_space.shape) == 3:
raise NotImplementedError(
"CNN models work only for atari,\n"
"please use a custom wrapper for a custom pixel input env.\n")
# If the input has shape (W,H,3), wrap for PyTorch convolutions
obs_shape = env.observation_space.shape
if len(obs_shape) == 3 and obs_shape[2] in [1, 3]:
env = TransposeImage(env, op=[2, 0, 1])
return env
return _thunk
def make_vec_envs(args,
log_dir,
allow_early_resets):
env_name = args.id
seed = args.seed
num_processes = args.num_processes
device = args.device
envs = [
make_env(env_name, seed, i, log_dir, allow_early_resets, args)
for i in range(num_processes)
]
if len(envs) >= 1:
"""
If you don't specify observation_space, we'll have to create a dummy
environment to get it.
"""
env = gym.make(env_name,
setting = args.setting,
item_set = args.item_size_set,
container_size=args.container_size,
internal_node_holder = args.internal_node_holder,
leaf_node_holder = args.leaf_node_holder,
LNES = args.lnes,
shuffle=args.shuffle,
sample_from_distribution=args.sample_from_distribution,
sample_left_bound=args.sample_left_bound,
sample_right_bound=args.sample_right_bound
)
spaces = [env.observation_space, env.action_space]
envs = ShmemVecEnv(envs, spaces, context='fork')
else:
envs = DummyVecEnv(envs)
envs = VecPyTorch(envs, device)
return envs
class TimeLimitMask(gym.Wrapper):
def step(self, action):
obs, rew, done, info = self.env.step(action)
if done and self.env._max_episode_steps == self.env._elapsed_steps:
info['bad_transition'] = True
return obs, rew, done, info
def reset(self, **kwargs):
return self.env.reset(**kwargs)
class TransposeObs(gym.ObservationWrapper):
def __init__(self, env=None):
"""
Transpose observation space (base class)
"""
super(TransposeObs, self).__init__(env)
class TransposeImage(TransposeObs):
def __init__(self, env=None, op=[2, 0, 1]):
"""
Transpose observation space for images
"""
super(TransposeImage, self).__init__(env)
assert len(op) == 3, "Error: Operation, {str(op)}, must be dim3"
self.op = op
obs_shape = self.observation_space.shape
self.observation_space = Box(
self.observation_space.low[0, 0, 0],
self.observation_space.high[0, 0, 0], [
obs_shape[self.op[0]], obs_shape[self.op[1]],
obs_shape[self.op[2]]
],
dtype=self.observation_space.dtype)
def observation(self, ob):
return ob.transpose(self.op[0], self.op[1], self.op[2])
class VecPyTorch(VecEnvWrapper):
def __init__(self, venv, device):
"""Return only every `skip`-th frame"""
super(VecPyTorch, self).__init__(venv)
self.device = device
# TODO: Fix data types
def reset(self):
obs = self.venv.reset()
obs = torch.from_numpy(np.array(obs)).float().to(self.device)
return obs
def step_async(self, actions):
if isinstance(actions, torch.LongTensor):
# Squeeze the dimension for discrete actions
actions = actions.squeeze(1)
self.venv.step_async(actions)
def step_wait(self):
obs, reward, done, info = self.venv.step_wait()
obs = torch.from_numpy(np.array(obs)).float().to(self.device)
reward = torch.from_numpy(reward).unsqueeze(dim=1).float()
return obs, reward, done, info