def test_identical(): def env_fn(): return knights_archers_zombies_v10.env() # ,20) n_envs = 2 # single threaded env1 = vectorize_aec_env_v0(knights_archers_zombies_v10.env(), n_envs) env2 = vectorize_aec_env_v0(knights_archers_zombies_v10.env(), n_envs, num_cpus=1) env1.reset(seed=42) env2.reset(seed=42) def policy(obs, agent): return [env1.action_space(agent).sample() for i in range(env1.num_envs)] envs_done = 0 for agent in env1.agent_iter(200000): assert env1.agent_selection == env2.agent_selection agent = env1.agent_selection obs1, rew1, agent_done1, env_done1, agent_passes1, infos1 = env1.last() obs2, rew2, agent_done2, env_done2, agent_passes2, infos2 = env2.last() assert np.all(np.equal(obs1, obs2)) assert np.all(np.equal(agent_done1, agent_done2)) assert np.all(np.equal(agent_passes1, agent_passes2)) assert np.all(np.equal(env_done1, env_done2)) assert np.all(np.equal(obs1, obs2)) assert all( np.all(np.equal(r1, r2)) for r1, r2 in zip(env1.rewards.values(), env2.rewards.values()) ) assert recursive_equal(infos1, infos2) actions = policy(obs1, agent) env1.step(actions) env2.step(actions) # env.envs[0].render() for j in range(2): # if agent_passes[j]: # print("pass") if rew1[j] != 0: print(j, agent, rew1, agent_done1[j]) if env_done1[j]: print(j, "done") envs_done += 1 if envs_done == n_envs + 1: print("test passed") return
def test_all(): NUM_ENVS = 5 def test_vec_env(vec_env): vec_env.reset() obs, rew, agent_done, env_done, agent_passes, infos = vec_env.last() print(np.asarray(obs).shape) assert len(obs) == NUM_ENVS act_space = vec_env.action_space(vec_env.agent_selection) assert np.all(np.equal(obs, vec_env.observe(vec_env.agent_selection))) assert len(vec_env.observe(vec_env.agent_selection)) == NUM_ENVS vec_env.step([act_space.sample() for _ in range(NUM_ENVS)]) obs, rew, agent_done, env_done, agent_passes, infos = vec_env.last( observe=False) assert obs is None def test_infos(vec_env): vec_env.reset() infos = vec_env.infos[vec_env.agent_selection] assert infos[1]["legal_moves"] def test_seed(vec_env): vec_env.reset(seed=4) def test_some_done(vec_env): vec_env.reset() act_space = vec_env.action_space(vec_env.agent_selection) assert not any(done for dones in vec_env.dones.values() for done in dones) vec_env.step([act_space.sample() for _ in range(NUM_ENVS)]) assert any(rew != 0 for rews in vec_env.rewards.values() for rew in rews) any_done_first = any(done for dones in vec_env.dones.values() for done in dones) vec_env.step([act_space.sample() for _ in range(NUM_ENVS)]) any_done_second = any(done for dones in vec_env.dones.values() for done in dones) assert any_done_first and any_done_second def select_action(vec_env, passes, i): my_info = vec_env.infos[vec_env.agent_selection][i] if False and not passes[i] and "legal_moves" in my_info: return random.choice(my_info["legal_moves"]) else: act_space = vec_env.action_space(vec_env.agent_selection) return act_space.sample() for num_cpus in [0, 1]: test_vec_env( vectorize_aec_env_v0(rps_v2.env(), NUM_ENVS, num_cpus=num_cpus)) test_vec_env( vectorize_aec_env_v0(mahjong_maker(), NUM_ENVS, num_cpus=num_cpus)) test_infos( vectorize_aec_env_v0(hanabi_maker(), NUM_ENVS, num_cpus=num_cpus)) test_some_done( vectorize_aec_env_v0(mahjong_maker(), NUM_ENVS, num_cpus=num_cpus)) test_vec_env( vectorize_aec_env_v0(knights_archers_zombies_v10.env(), NUM_ENVS, num_cpus=num_cpus)) test_vec_env( vectorize_aec_env_v0(simple_world_comm_v2.env(), NUM_ENVS, num_cpus=num_cpus))
elif event.key in self.action_mapping: action = self.action_mapping[event.key] return action @property def available_agents(self): return self.env.agent_name_mapping if __name__ == "__main__": from pettingzoo.butterfly import knights_archers_zombies_v10 clock = pygame.time.Clock() env = knights_archers_zombies_v10.env() env.reset() manual_policy = knights_archers_zombies_v10.ManualPolicy(env) for agent in env.agent_iter(): clock.tick(env.metadata["render_fps"]) observation, reward, done, info = env.last() if agent == manual_policy.agent: action = manual_policy(observation, agent) else: action = env.action_space(agent).sample() env.step(action)
def env_fn(): return knights_archers_zombies_v10.env() # ,20)
def test_pettingzoo_pad_action_space(): _env = simple_world_comm_v2.env() wrapped_env = pad_action_space_v0(_env) api_test(wrapped_env) seed_test(lambda: sticky_actions_v0(simple_world_comm_v2.env(), 0.5), 100) def test_pettingzoo_parallel_env(): _env = simple_world_comm_v2.parallel_env() wrapped_env = pad_action_space_v0(_env) parallel_test.parallel_api_test(wrapped_env) wrappers = [ supersuit.color_reduction_v0( knights_archers_zombies_v10.env(vector_state=False), "R"), supersuit.resize_v1( dtype_v0(knights_archers_zombies_v10.env(vector_state=False), np.uint8), x_size=5, y_size=10, ), supersuit.resize_v1( dtype_v0(knights_archers_zombies_v10.env(vector_state=False), np.uint8), x_size=5, y_size=10, linear_interp=True, ), supersuit.dtype_v0(knights_archers_zombies_v10.env(), np.int32), supersuit.flatten_v0(knights_archers_zombies_v10.env()),