Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add shuffle traj to offline env #187

Merged
merged 1 commit into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions examples/behavior_cloning/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@ def test_env():
cfg = cfg_parser.parse_args()

# create environment, set environment parallelism to 9
env = make("OfflineEnv", env_num=1, cfg=cfg, asynchronous=True)
# env = make("OfflineEnv", env_num=1, cfg=cfg, asynchronous=True)
env = make("OfflineEnv", env_num=1, cfg=cfg, asynchronous=False)

for ep_index in range(10):
done = False
step = 0
env.reset()

while not np.all(done):
obs, reward, done, info = env.step(env.random_action())

step += 1
print(ep_index, step)
print(ep_index, step)


if __name__ == "__main__":
Expand Down
1 change: 0 additions & 1 deletion openrl/envs/offline/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def offline_make(dataset, render_mode, disable_env_checker, **kwargs):
env_num = kwargs["env_num"]
seed = kwargs.pop("seed", None)
assert seed is not None, "seed must be set"

env = OfflineEnv(dataset, env_id, env_num, seed)
return env

Expand Down
19 changes: 15 additions & 4 deletions openrl/envs/offline/offline_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ def __init__(self, dataset_path, env_id: int, env_num: int, seed: int):
self.agent_num = self.dataset.agent_num
self.traj_num = len(self.dataset.trajectories["episode_lengths"])
self.traj_index = None
self.epoch_index = None
self.traj_length = None
self.step_index = None
self.sample_indexes = None
self.seed(seed)

def seed(self, seed=None):
if seed is not None:
Expand All @@ -48,11 +51,19 @@ def reset(self, *, seed=None, options=None):
if seed is not None:
self.seed(seed)

if self.traj_index is None:
self.traj_index = 0
if self.epoch_index is None:
self.epoch_index = 0
else:
self.traj_index += 1
self.traj_index %= self.traj_num
self.epoch_index += 1
self.epoch_index %= self.traj_num
if self.epoch_index == 0:
if self._np_random is None:
self.seed(0)
self.sample_indexes = self._np_random.permutation(self.traj_num)

assert self.sample_indexes is not None
self.traj_index = self.sample_indexes[self.epoch_index]

self.traj_length = self.dataset.trajectories["episode_lengths"][self.traj_index]
assert (
self.traj_length
Expand Down
3 changes: 2 additions & 1 deletion openrl/envs/vec_env/base_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def step(self, actions):
:param actions: the action
:return: observation, reward, done, information
"""

results = self._step(actions)
self.vector_render()
return results
Expand Down Expand Up @@ -315,7 +316,7 @@ def random_action(self, infos: Optional[List[Dict[str, Any]]] = None):
action_masks = prepare_action_masks(
infos, agent_num=self.agent_num, as_batch=False
)
print(action_masks)

return np.array(
[
[
Expand Down
2 changes: 2 additions & 0 deletions openrl/envs/vec_env/sync_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,14 @@ def _step(self, actions: ActType):
Returns:
The batched environment step results
"""

_actions = iterate_action(self.action_space, actions)

observations, infos = [], []

for i, (env, action) in enumerate(zip(self.envs, _actions)):
returns = env.step(action)

assert isinstance(
returns, tuple
), "step return must be tuple, but got: {}".format(type(returns))
Expand Down
1 change: 1 addition & 0 deletions openrl/envs/vec_env/wrappers/vec_monitor_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def use_monitor(self):

def step(self, action: ActType, extra_data: Optional[Dict[str, Any]] = None):
returns = self.env.step(action, extra_data)

self.vec_info.append(info=returns[-1])

return returns
Expand Down