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

Update Lambda wrappers and fix a bunch of the tests #178

Merged
merged 3 commits into from
Sep 17, 2022
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
4 changes: 2 additions & 2 deletions supersuit/lambda_wrappers/observation_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ def _modify_observation(self, observation):
return self.change_observation_fn(observation, self.env.observation_space)

def step(self, action):
observation, rew, done, info = self.env.step(action)
observation, rew, termination, truncation, info = self.env.step(action)
observation = self._modify_observation(observation)
return observation, rew, done, info
return observation, rew, termination, truncation, info

def reset(self, seed=None, return_info=False, options=None):
if not return_info:
Expand Down
4 changes: 2 additions & 2 deletions supersuit/lambda_wrappers/reward_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def __init__(self, env, change_reward_fn):
super().__init__(env)

def step(self, action):
obs, rew, done, info = super().step(action)
return obs, self._change_reward_fn(rew), done, info
obs, rew, termination, truncation, info = super().step(action)
return obs, self._change_reward_fn(rew), termination, truncation, info


reward_lambda_v0 = WrapperChooser(
Expand Down
2 changes: 1 addition & 1 deletion supersuit/vector/multiproc_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def step_wait(self):
compressed_infos = self._receive_info()
infos = decompress_info(self.num_envs, self.idx_starts, compressed_infos)
rewards = self.shared_rews.np_arr
dones = self.shared_dones.np_arr
dones = self.shared_dones.np_arr.dtype(bool)
return (
numpy_deepcopy(self.observations_buffers),
rewards.copy(),
Expand Down
14 changes: 9 additions & 5 deletions test/dummy_aec_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,28 @@ def observe(self, agent):
return self._observations[agent]

def step(self, action, observe=True):
if self.dones[self.agent_selection]:
return self._was_done_step(action)
if (
self.terminations[self.agent_selection]
or self.truncations[self.agent_selection]
):
return self._was_dead_step(action)
self._cumulative_rewards[self.agent_selection] = 0
self.agent_selection = self._agent_selector.next()
self.steps += 1
if self.steps >= 5 * len(self.agents):
self.dones = {a: True for a in self.agents}
self.truncations = {a: True for a in self.agents}

self._accumulate_rewards()
self._dones_step_first()
self._deads_step_first()

def reset(self, seed=None, return_info=False, options=None):
self.agents = self.possible_agents[:]
self._agent_selector = agent_selector(self.agents)
self.agent_selection = self._agent_selector.reset()
self.rewards = {a: 1 for a in self.agents}
self._cumulative_rewards = {a: 0 for a in self.agents}
self.dones = {a: False for a in self.agents}
self.terminations = {a: False for a in self.agents}
self.truncations = {a: False for a in self.agents}
self.infos = {a: {} for a in self.agents}
self.steps = 0

Expand Down
2 changes: 1 addition & 1 deletion test/gym_mock_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ def one_hot(x, n):
def test_rew_lambda():
env = supersuit.reward_lambda_v0(new_dummy(), lambda x: x / 10)
env.reset()
obs, rew, done, info = env.step(0)
obs, rew, termination, truncation, info = env.step(0)
assert rew == 1.0 / 10
13 changes: 10 additions & 3 deletions test/parallel_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def __init__(self, observations, observation_spaces, action_spaces):
self._action_spaces = action_spaces

self.rewards = {a: 1 for a in self.agents}
self.dones = {a: False for a in self.agents}
self.terminations = {a: False for a in self.agents}
self.truncations = {a: False for a in self.agents}
self.infos = {a: {} for a in self.agents}

def observation_space(self, agent):
Expand All @@ -30,7 +31,13 @@ def action_space(self, agent):
def step(self, actions):
for agent, action in actions.items():
assert action in self.action_space(agent)
return self._observations, self.rewards, self.dones, self.infos
return (
self._observations,
self.rewards,
self.terminations,
self.truncations,
self.infos,
)

def reset(self, seed=None, return_info=False, options=None):
if not return_info:
Expand Down Expand Up @@ -60,4 +67,4 @@ def test_basic():
orig_obs = env.reset()
for i in range(10):
action = {agent: env.action_space(agent).sample() for agent in env.agents}
obs, rew, done, info = env.step(action)
obs, rew, termination, truncation, info = env.step(action)
4 changes: 3 additions & 1 deletion test/vec_env_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def test_vec_env_args():
num_envs = 8
vec_env = gym_vec_env_v0(env, num_envs)
vec_env.reset()
obs, rew, dones, infos = vec_env.step([0] + [1] * (vec_env.num_envs - 1))
obs, rew, terminations, truncations, infos = vec_env.step(
[0] + [1] * (vec_env.num_envs - 1)
)
assert not np.any(np.equal(obs[0], obs[1]))


Expand Down