def create_envs(self, n_envs: int, eval_env: bool = False, no_log: bool = False) -> VecEnv: env = pistonball_v5.parallel_env() env = ss.color_reduction_v0(env, mode="B") env = ss.resize_v0(env, x_size=84, y_size=84, linear_interp=True) env = ss.frame_stack_v1(env, 3) env = ss.pettingzoo_env_to_vec_env_v1(env) print(n_envs) env = ss.concat_vec_envs_v1(env, n_envs, num_cpus=4, base_class="stable_baselines3") env = VecMonitor(env) env = self._maybe_normalize(env, eval_env) if is_image_space( env.observation_space) and not is_image_space_channels_first( env.observation_space): if self.verbose > 0: print("Wrapping into a VecTransposeImage") env = VecTransposeImage(env) return env
def test_env_black_death_wrapper(): env = knights_archers_zombies_v10.parallel_env(spawn_rate=50, max_cycles=300) env = black_death_v3(env) env = pettingzoo_env_to_vec_env_v1(env) env.reset() for i in range(300): actions = [env.action_space.sample() for i in range(env.num_envs)] obss, rews, dones, infos = env.step(actions)
def test_multiagent_mutliproc_single_proc_equivalency(): env = simple_spread_v2.parallel_env(max_cycles=10) env = pettingzoo_env_to_vec_env_v1(env) num_envs = 3 # uses single threaded vector environment venv1 = concat_vec_envs_v1(env, num_envs, num_cpus=0) # uses multiprocessing vector environment venv2 = concat_vec_envs_v1(env, num_envs, num_cpus=4) check_vec_env_equivalency(venv1, venv2)
def test_env_black_death_assertion(): env = knights_archers_zombies_v10.parallel_env(spawn_rate=50, max_cycles=2000) env = pettingzoo_env_to_vec_env_v1(env) with pytest.raises(AssertionError): for i in range(100): env.reset() for i in range(2000): actions = [ env.action_space.sample() for i in range(env.num_envs) ] obss, rews, dones, infos = env.step(actions)
def test_good_env(): env = simple_spread_v2.parallel_env() max_num_agents = len(env.possible_agents) env = pettingzoo_env_to_vec_env_v1(env) assert env.num_envs == max_num_agents obss = env.reset() for i in range(55): actions = [env.action_space.sample() for i in range(env.num_envs)] # Check we're not passing a thing that gets mutated keep_obs = copy.deepcopy(obss) new_obss, rews, dones, infos = env.step(actions) assert hash(str(keep_obs)) == hash(str(obss)) assert len(new_obss) == max_num_agents assert len(rews) == max_num_agents assert len(dones) == max_num_agents assert len(infos) == max_num_agents # no agent death, only env death if any(dones): assert all(dones) obss = new_obss
def make_env(): env = pistonball_v6.parallel_env() env = pettingzoo_env_to_vec_env_v1(env) return env
print(params) def image_transpose(env): if is_image_space(env.observation_space) and not is_image_space_channels_first( env.observation_space ): env = VecTransposeImage(env) return env env = pistonball_v5.parallel_env() env = ss.color_reduction_v0(env, mode="B") env = ss.resize_v0(env, x_size=84, y_size=84) env = ss.frame_stack_v1(env, 3) env = ss.pettingzoo_env_to_vec_env_v1(env) env = ss.concat_vec_envs_v1(env, n_envs, num_cpus=1, base_class="stable_baselines3") env = VecMonitor(env) env = image_transpose(env) eval_env = pistonball_v5.parallel_env() eval_env = ss.color_reduction_v0(eval_env, mode="B") eval_env = ss.resize_v0(eval_env, x_size=84, y_size=84) eval_env = ss.frame_stack_v1(eval_env, 3) eval_env = ss.pettingzoo_env_to_vec_env_v1(eval_env) eval_env = ss.concat_vec_envs_v1( eval_env, 1, num_cpus=1, base_class="stable_baselines3" ) eval_env = VecMonitor(eval_env) eval_env = image_transpose(eval_env)
3), ] @pytest.mark.parametrize("env", parallel_wrappers) def test_pettingzoo_parallel_api_gen(env): parallel_test.parallel_api_test(env, num_cycles=50) wrapper_fns = [ lambda: supersuit.pad_action_space_v0(generated_agents_parallel_v0.env()), lambda: supersuit.pad_observations_v0(generated_agents_parallel_v0.env()), lambda: supersuit.agent_indicator_v0(generated_agents_parallel_v0.env()), lambda: supersuit.vectorize_aec_env_v0(generated_agents_parallel_v0.env(), 2), lambda: supersuit.pad_action_space_v0(generated_agents_parallel_v0. parallel_env()), lambda: supersuit.pad_observations_v0(generated_agents_parallel_v0. parallel_env()), lambda: supersuit.agent_indicator_v0(generated_agents_parallel_v0. parallel_env()), lambda: supersuit.pettingzoo_env_to_vec_env_v1(generated_agents_parallel_v0 .parallel_env()), ] @pytest.mark.parametrize("wrapper_fn", wrapper_fns) def test_pettingzoo_missing_optional_error_message(wrapper_fn): with pytest.raises(AssertionError, match=" must have "): wrapper_fn()
def test_bad_action_spaces_env(): env = simple_world_comm_v2.parallel_env() with pytest.raises(AssertionError): env = pettingzoo_env_to_vec_env_v1(env)
def test_multi_threaded_concatenate(): env = make_env() env = pettingzoo_env_to_vec_env_v1(env) env = concat_vec_envs_v1(env, 2, num_cpus=2) dict_vec_env_test(env)
def test_pettingzoo_vec_env(): env = make_env() env = pettingzoo_env_to_vec_env_v1(env) dict_vec_env_test(env)
def test_env_is_wrapped_pettingzoo(): env = simple_spread_v2.parallel_env() venv1 = pettingzoo_env_to_vec_env_v1(env) num_envs = 3 venv1 = concat_vec_envs_v1(venv1, num_envs) assert venv1.env_is_wrapped(frame_skip_gym) == [False] * 9