forked from microsoft/oac-explore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_collector.py
244 lines (208 loc) · 7.26 KB
/
path_collector.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
from collections import deque, OrderedDict
import torch
from utils.env_utils import env_producer
from utils.eval_util import create_stats_ordered_dict
from utils.rng import get_global_pkg_rng_state, set_global_pkg_rng_state
import numpy as np
import ray
from optimistic_exploration import get_optimistic_exploration_action
class MdpPathCollector(object):
def __init__(
self,
env,
max_num_epoch_paths_saved=None,
render=False,
render_kwargs=None,
):
# The class state which we do not expect to mutate
if render_kwargs is None:
render_kwargs = {}
self._render = render
self._render_kwargs = render_kwargs
self._max_num_epoch_paths_saved = max_num_epoch_paths_saved
# The class mutable internal state
self._env = env
self._epoch_paths = deque(maxlen=self._max_num_epoch_paths_saved)
self._num_steps_total = 0
self._num_paths_total = 0
def collect_new_paths(
self,
policy,
max_path_length,
num_steps,
discard_incomplete_paths,
optimistic_exploration=False,
optimistic_exploration_kwargs={}
):
paths = []
num_steps_collected = 0
while num_steps_collected < num_steps:
max_path_length_this_loop = min( # Do not go over num_steps
max_path_length,
num_steps - num_steps_collected,
)
path = rollout(
self._env,
policy,
max_path_length=max_path_length_this_loop,
optimistic_exploration=optimistic_exploration,
optimistic_exploration_kwargs=optimistic_exploration_kwargs
)
path_len = len(path['actions'])
if (
# incomplete path
path_len != max_path_length and
# that did not end in a terminal state
not path['terminals'][-1] and
# and we should discard such path
discard_incomplete_paths
):
break
num_steps_collected += path_len
paths.append(path)
self._num_paths_total += len(paths)
self._num_steps_total += num_steps_collected
self._epoch_paths.extend(paths)
return paths
def get_epoch_paths(self):
return self._epoch_paths
def end_epoch(self, epoch):
self._epoch_paths = deque(maxlen=self._max_num_epoch_paths_saved)
def get_diagnostics(self):
path_lens = [len(path['actions']) for path in self._epoch_paths]
stats = OrderedDict([
('num steps total', self._num_steps_total),
('num paths total', self._num_paths_total),
])
stats.update(create_stats_ordered_dict(
"path length",
path_lens,
always_show_all_stats=True,
))
return stats
def get_snapshot(self):
return dict(
env_mj_state=self._env.sim.get_state(),
env_rng=self._env.np_random.get_state(),
_epoch_paths=self._epoch_paths,
_num_steps_total=self._num_steps_total,
_num_paths_total=self._num_paths_total
)
def restore_from_snapshot(self, ss):
self._env.sim.set_state(ss['env_mj_state'])
self._env.np_random.set_state(ss['env_rng'])
self._epoch_paths = ss['_epoch_paths']
self._num_steps_total = ss['_num_steps_total']
self._num_paths_total = ss['_num_paths_total']
@ray.remote(num_cpus=1)
class RemoteMdpPathCollector(MdpPathCollector):
def __init__(self,
domain_name, env_seed, policy_producer,
max_num_epoch_paths_saved=None,
render=False,
render_kwargs=None,
):
torch.set_num_threads(1)
env = env_producer(domain_name, env_seed)
self._policy_producer = policy_producer
super().__init__(env,
max_num_epoch_paths_saved=max_num_epoch_paths_saved,
render=render,
render_kwargs=render_kwargs,
)
def async_collect_new_paths(self,
max_path_length,
num_steps,
discard_incomplete_paths,
deterministic_pol,
pol_state_dict):
if deterministic_pol:
policy = self._policy_producer(deterministic=True)
policy.stochastic_policy.load_state_dict(pol_state_dict)
else:
policy = self._policy_producer()
policy.load_state_dict(pol_state_dict)
self.collect_new_paths(policy,
max_path_length, num_steps,
discard_incomplete_paths)
def get_global_pkg_rng_state(self):
return get_global_pkg_rng_state()
def set_global_pkg_rng_state(self, state):
set_global_pkg_rng_state(state)
def rollout(
env,
agent,
max_path_length=np.inf,
render=False,
render_kwargs=None,
optimistic_exploration=False,
optimistic_exploration_kwargs={},
):
"""
The following value for the following keys will be a 2D array, with the
first dimension corresponding to the time dimension.
- observations
- actions
- rewards
- next_observations
- terminals
The next two elements will be lists of dictionaries, with the index into
the list being the index into the time
- agent_infos
- env_infos
"""
if render_kwargs is None:
render_kwargs = {}
observations = []
actions = []
rewards = []
terminals = []
agent_infos = []
env_infos = []
o = env.reset()
agent.reset()
next_o = None
path_length = 0
if render:
env.render(**render_kwargs)
while path_length < max_path_length:
if not optimistic_exploration:
a, agent_info = agent.get_action(o)
else:
a, agent_info = get_optimistic_exploration_action(
o, **optimistic_exploration_kwargs)
next_o, r, d, env_info = env.step(a)
observations.append(o)
rewards.append(r)
terminals.append(d)
actions.append(a)
agent_infos.append(agent_info)
env_infos.append(env_info)
path_length += 1
if d:
break
o = next_o
if render:
env.render(**render_kwargs)
actions = np.array(actions)
if len(actions.shape) == 1:
actions = np.expand_dims(actions, 1)
observations = np.array(observations)
if len(observations.shape) == 1:
observations = np.expand_dims(observations, 1)
next_o = np.array([next_o])
next_observations = np.vstack(
(
observations[1:, :],
np.expand_dims(next_o, 0)
)
)
return dict(
observations=observations,
actions=actions,
rewards=np.array(rewards).reshape(-1, 1),
next_observations=next_observations,
terminals=np.array(terminals).reshape(-1, 1),
agent_infos=agent_infos,
env_infos=env_infos,
)