def atari_preprocessing( env: Union[ParallelEnvWrapper, SequentialEnvWrapper] ) -> Union[ParallelEnvWrapper, SequentialEnvWrapper]: # Preprocessing env = supersuit.max_observation_v0(env, 2) # repeat_action_probability is set to 0.25 # to introduce non-determinism to the system env = supersuit.sticky_actions_v0(env, repeat_action_probability=0.25) # skip frames for faster processing and less control # to be compatable with gym, use frame_skip(env, (2,5)) env = supersuit.frame_skip_v0(env, 4) # downscale observation for faster processing env = supersuit.resize_v0(env, 84, 84) # allow agent to see everything on the screen # despite Atari's flickering screen problem env = supersuit.frame_stack_v1(env, 4) # set dtype to float32 env = supersuit.dtype_v0(env, np.float32) return env
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 run_parallel2(args): """ Test parallel mode with supersuit env wrappers. """ parallel_env = eval(args.env).parallel_env() # as per openai baseline's MaxAndSKip wrapper, maxes over the last 2 frames # to deal with frame flickering env = supersuit.max_observation_v0(parallel_env, 2) # repeat_action_probability is set to 0.25 to introduce non-determinism to the system env = supersuit.sticky_actions_v0(env, repeat_action_probability=0.25) # skip frames for faster processing and less control # to be compatable with gym, use frame_skip(env, (2,5)) env = supersuit.frame_skip_v0(env, 4) # downscale observation for faster processing env = supersuit.resize_v0(env, 84, 84) # allow agent to see everything on the screen despite Atari's flickering screen problem parallel_env = supersuit.frame_stack_v1(env, 4) parallel_env.seed(1) observations = parallel_env.reset() print(parallel_env.agents) max_cycles = 500 for step in range(max_cycles): actions = {agent: 1 for agent in parallel_env.agents} observations, rewards, dones, infos = parallel_env.step(actions) parallel_env.render()
def env_creator(): env = pistonball_v4.env(n_pistons=20, local_ratio=0, time_penalty=-0.1, continuous=True, random_drop=True, random_rotate=True, ball_mass=0.75, ball_friction=0.3, ball_elasticity=1.5, max_cycles=125) env = ss.color_reduction_v0(env, mode='B') env = ss.dtype_v0(env, 'float32') env = ss.resize_v0(env, x_size=84, y_size=84) env = ss.normalize_obs_v0(env, env_min=0, env_max=1) env = ss.frame_stack_v1(env, 3) return env
def env_creator(args): env = game_env.env(obs_type='grayscale_image') #env = clip_reward_v0(env, lower_bound=-1, upper_bound=1) env = sticky_actions_v0(env, repeat_action_probability=0.25) env = resize_v0(env, 84, 84) #env = color_reduction_v0(env, mode='full') #env = frame_skip_v0(env, 4) env = frame_stack_v0(env, 4) env = agent_indicator_v0(env, type_only=False) return env
def _load_env(self, env_name, pettingzoo_params): from pettingzoo import atari from supersuit import resize_v0, frame_skip_v0, reshape_v0, max_observation_v0 env = importlib.import_module( 'pettingzoo.atari.{}'.format(env_name)).env( obs_type='grayscale_image', **pettingzoo_params) env = max_observation_v0(env, 2) env = frame_skip_v0(env, 4) env = resize_v0(env, 84, 84) env = reshape_v0(env, (1, 84, 84)) return env
def get_env(config): name = env_name.replace('-', '_') env = __import__(f'pettingzoo.atari.{name}', fromlist=[None]) env = env.parallel_env(obs_type='grayscale_image') env = frame_skip_v0(env, 4) env = resize_v0(env, 84, 84) env = frame_stack_v1(env, 4) env = agent_indicator_v0(env) return ParallelPettingZooEnv( env, random_action=config['random_action'], random_proba=config['random_action_probability'])
def make_env(env_name): if env_name == "pistonball": env = pistonball_v0.parallel_env(max_frames=100) env = supersuit.resize_v0(env, 16, 16) env = supersuit.dtype_v0(env, np.float32) env = supersuit.normalize_obs_v0(env) return supersuit.flatten_v0(env) if env_name == "KAZ": env = knights_archers_zombies_v2.parallel_env(max_frames=100) env = supersuit.resize_v0(env, 32, 32) env = supersuit.dtype_v0(env, np.float32) env = supersuit.normalize_obs_v0(env) return supersuit.flatten_v0(env) if env_name == "pursuit": env = pursuit_v1.parallel_env(max_frames=100) env = supersuit.resize_v0(env, 32, 32) return supersuit.flatten_v0(env) elif env_name == "waterworld": return waterworld_v1.parallel_env(max_frames=100) elif env_name == "multiwalker": return multiwalker_v3.parallel_env(max_frames=100) else: raise RuntimeError("bad environment name")
def __init__(self, env_name, device='cpu', frame_skip=4): env = importlib.import_module( 'pettingzoo.atari.{}'.format(env_name)).parallel_env( obs_type='grayscale_image') env = MaxAndSkipMAALE(env, skip=frame_skip) env = from_parallel(env) env = resize_v0(env, 84, 84) self._env = env self.name = env_name self.device = torch.device(device) self.agents = self._env.possible_agents self.subenvs = { agent: SubEnv(agent, device, self.state_spaces[agent], self.action_spaces[agent]) for agent in self.agents }
def make_env(env_name='boxing_v1', seed=1, obs_type='rgb_image'): '''https://www.pettingzoo.ml/atari''' if env_name == 'slimevolley_v0': env = SlimeVolleyWrapper(gym.make("SlimeVolley-v0")) else: # PettingZoo envs env = eval(env_name).parallel_env(obs_type=obs_type) if obs_type == 'rgb_image': # as per openai baseline's MaxAndSKip wrapper, maxes over the last 2 frames # to deal with frame flickering env = supersuit.max_observation_v0(env, 2) # repeat_action_probability is set to 0.25 to introduce non-determinism to the system env = supersuit.sticky_actions_v0(env, repeat_action_probability=0.25) # skip frames for faster processing and less control # to be compatable with gym, use frame_skip(env, (2,5)) env = supersuit.frame_skip_v0(env, 4) # downscale observation for faster processing env = supersuit.resize_v0(env, 84, 84) # allow agent to see everything on the screen despite Atari's flickering screen problem env = supersuit.frame_stack_v1(env, 4) else: env = supersuit.frame_skip_v0(env, 4) # env = PettingZooWrapper(env) # need to be put at the end if env_name in AtariEnvs: # normalize the observation of Atari for both image or RAM env = supersuit.dtype_v0( env, 'float32' ) # need to transform uint8 to float first for normalizing observation: https://github.com/PettingZoo-Team/SuperSuit env = supersuit.normalize_obs_v0( env, env_min=0, env_max=1) # normalize the observation to (0,1) # assign observation and action spaces env.observation_space = list(env.observation_spaces.values())[0] env.action_space = list(env.action_spaces.values())[0] env.seed(seed) return env
def env_creator(args): env = env_constr.env( ) #killable_knights=False, killable_archers=False) resize_size = 84 if model == None else 32 env = supersuit.resize_v0(env, resize_size, resize_size, linear_interp=True) env = supersuit.color_reduction_v0(env) env = supersuit.pad_action_space_v0(env) env = supersuit.pad_observations_v0(env) # env = supersuit.frame_stack_v0(env,2) env = supersuit.dtype_v0(env, np.float32) env = supersuit.normalize_obs_v0(env) if model == "MLPModelV2": env = supersuit.flatten_v0(env) env = PettingZooEnv(env) return env
def __init__(self, env_name, device='cpu', frame_skip=4): def raw_env(**kwargs): return BaseAtariEnv(game="surround", num_players=1, mode_num=2, **kwargs) env = parallel_wrapper_fn( base_env_wrapper_fn(raw_env))(obs_type="grayscale_image") env = MaxAndSkipMAALE(env, skip=frame_skip) env = from_parallel(env) env = recolor_observations(env) env = resize_v0(env, 84, 84) self._env = env self.name = env_name self.device = torch.device(device) self.agents = self._env.possible_agents self.subenvs = { agent: SubEnv(agent, device, self.state_spaces[agent], self.action_spaces[agent]) for agent in self.agents }
def wrap_env(env, obs_type='ram'): env = env.parallel_env(obs_type=obs_type) env_agents = env.unwrapped.agents if obs_type == 'rgb_image': env = supersuit.max_observation_v0( env, 2 ) # as per openai baseline's MaxAndSKip wrapper, maxes over the last 2 frames to deal with frame flickering env = supersuit.sticky_actions_v0( env, repeat_action_probability=0.25 ) # repeat_action_probability is set to 0.25 to introduce non-determinism to the system env = supersuit.frame_skip_v0( env, 4 ) # skip frames for faster processing and less control to be compatable with gym, use frame_skip(env, (2,5)) env = supersuit.resize_v0( env, 84, 84) # downscale observation for faster processing env = supersuit.frame_stack_v1( env, 4 ) # allow agent to see everything on the screen despite Atari's flickering screen problem else: env = supersuit.frame_skip_v0( env, 4 ) # RAM version also need frame skip, essential for boxing-v1, etc # normalize the observation of Atari for both image or RAM env = supersuit.dtype_v0( env, 'float32' ) # need to transform uint8 to float first for normalizing observation: https://github.com/PettingZoo-Team/SuperSuit env = supersuit.normalize_obs_v0( env, env_min=0, env_max=1) # normalize the observation to (0,1) env.observation_space = list(env.observation_spaces.values())[0] env.action_space = list(env.action_spaces.values())[0] env.agents = env_agents env = Dict2TupleWrapper(env) return env
wrapped_env = pad_action_space_v0(_env) api_test.api_test(wrapped_env) seed_test.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_play_test(wrapped_env) wrappers = [ supersuit.color_reduction_v0(knights_archers_zombies_v4.env(), "R"), supersuit.resize_v0(dtype_v0(knights_archers_zombies_v4.env(), np.uint8), x_size=5, y_size=10), supersuit.resize_v0(dtype_v0(knights_archers_zombies_v4.env(), np.uint8), x_size=5, y_size=10, linear_interp=True), supersuit.dtype_v0(knights_archers_zombies_v4.env(), np.int32), supersuit.flatten_v0(knights_archers_zombies_v4.env()), supersuit.reshape_v0(knights_archers_zombies_v4.env(), (512 * 512, 3)), supersuit.normalize_obs_v0(dtype_v0(knights_archers_zombies_v4.env(), np.float32), env_min=-1, env_max=5.0), supersuit.frame_stack_v1(knights_archers_zombies_v4.env(), 8), supersuit.pad_observations_v0(knights_archers_zombies_v4.env()), supersuit.pad_action_space_v0(knights_archers_zombies_v4.env()),
def create_single_env(args): env_name = args.env if args.num_envs > 1: keep_info = True # keep_info True to maintain dict type for parallel envs (otherwise cannot pass VectorEnv wrapper) else: keep_info = False '''https://www.pettingzoo.ml/atari''' if "slimevolley" in env_name or "SlimeVolley" in env_name: print(f'Load SlimeVolley env: {env_name}') env = gym.make(env_name) if env_name in [ 'SlimeVolleySurvivalNoFrameskip-v0', 'SlimeVolleyNoFrameskip-v0', 'SlimeVolleyPixel-v0' ]: # For image-based envs, apply following wrappers (from gym atari) to achieve pettingzoo style env, # or use supersuit (requires input env to be either pettingzoo or gym env). # same as: https://github.com/hardmaru/slimevolleygym/blob/master/training_scripts/train_ppo_pixel.py # TODO Note: this cannot handle the two obervations in above SlimeVolley envs, # since the wrappers are for single agent. if env_name != 'SlimeVolleyPixel-v0': env = NoopResetEnv(env, noop_max=30) env = MaxAndSkipEnv(env, skip=4) env = WarpFrame(env) # #env = ClipRewardEnv(env) env = FrameStack(env, 4) env = SlimeVolleyWrapper( env, args.against_baseline) # slimevolley to pettingzoo style env = NFSPPettingZooWrapper( env, keep_info=keep_info ) # pettingzoo to nfsp style, keep_info True to maintain dict type for parallel envs elif env_name in AtariEnvs: # PettingZoo Atari envs print(f'Load PettingZoo Atari env: {env_name}') if args.ram: obs_type = 'ram' else: obs_type = 'rgb_image' env = eval(env_name).parallel_env(obs_type=obs_type) env_agents = env.unwrapped.agents # this cannot go through supersuit wrapper, so get it first and reassign it if obs_type == 'rgb_image': # as per openai baseline's MaxAndSKip wrapper, maxes over the last 2 frames # to deal with frame flickering env = supersuit.max_observation_v0(env, 2) # repeat_action_probability is set to 0.25 to introduce non-determinism to the system env = supersuit.sticky_actions_v0(env, repeat_action_probability=0.25) # skip frames for faster processing and less control # to be compatable with gym, use frame_skip(env, (2,5)) env = supersuit.frame_skip_v0(env, 4) # downscale observation for faster processing env = supersuit.resize_v0(env, 84, 84) # allow agent to see everything on the screen despite Atari's flickering screen problem env = supersuit.frame_stack_v1(env, 4) else: env = supersuit.frame_skip_v0( env, 4 ) # RAM version also need frame skip, essential for boxing-v1, etc # env = PettingZooWrapper(env) # need to be put at the end # normalize the observation of Atari for both image or RAM env = supersuit.dtype_v0( env, 'float32' ) # need to transform uint8 to float first for normalizing observation: https://github.com/PettingZoo-Team/SuperSuit env = supersuit.normalize_obs_v0( env, env_min=0, env_max=1) # normalize the observation to (0,1) # assign observation and action spaces env.observation_space = list(env.observation_spaces.values())[0] env.action_space = list(env.action_spaces.values())[0] env.agents = env_agents env = NFSPPettingZooWrapper( env, keep_info=keep_info ) # pettingzoo to nfsp style, keep_info True to maintain dict type for parallel envs) elif env_name in ClassicEnvs: # PettingZoo Classic envs print(f'Load PettingZoo Classic env: {env_name}') if env_name in ['rps_v1', 'rpsls_v1']: env = eval(env_name).parallel_env() env = PettingzooClassicWrapper(env, observation_mask=1.) else: # only rps_v1 can use parallel_env at present env = eval(env_name).env() env = PettingzooClassic_Iterate2Parallel( env, observation_mask=None ) # since Classic games do not support Parallel API yet env = NFSPPettingZooWrapper(env, keep_info=keep_info) elif "LaserTag" in env_name: # LaserTag: https://github.com/younggyoseo/pytorch-nfsp print(f'Load LaserTag env: {env_name}') env = gym.make(env_name) env = wrap_pytorch(env) else: # gym env print(f'Load Gym env: {env_name}') try: env = gym.make(env_name) except: print(f"Error: No such env: {env_name}!") # may need more wrappers here, e.g. Pong-ram-v0 need scaled observation! # Ref: https://towardsdatascience.com/deep-q-network-dqn-i-bce08bdf2af env = NFSPAtariWrapper(env, keep_info=keep_info) env.seed(args.seed) return env
first_obs, _, _, _ = env.step(5) assert np.all(np.equal(first_obs, base_obs.reshape([64, 3]))) def new_continuous_dummy(): base_act_spaces = Box(low=np.float32(0.0), high=np.float32(10.0), shape=[3]) return DummyEnv(base_obs, base_obs_space, base_act_spaces) def new_dummy(): return DummyEnv(base_obs, base_obs_space, base_act_spaces) wrappers = [ supersuit.color_reduction_v0(new_dummy(), "R"), supersuit.resize_v0(dtype_v0(new_dummy(), np.uint8), x_size=5, y_size=10), supersuit.resize_v0(dtype_v0(new_dummy(), np.uint8), x_size=5, y_size=10, linear_interp=True), supersuit.dtype_v0(new_dummy(), np.int32), supersuit.flatten_v0(new_dummy()), supersuit.reshape_v0(new_dummy(), (64, 3)), supersuit.normalize_obs_v0(new_dummy(), env_min=-1, env_max=5.0), supersuit.frame_stack_v1(new_dummy(), 8), supersuit.reward_lambda_v0(new_dummy(), lambda x: x / 10), supersuit.clip_reward_v0(new_dummy()), supersuit.clip_actions_v0(new_continuous_dummy()), supersuit.frame_skip_v0(new_dummy(), 4), supersuit.frame_skip_v0(new_dummy(), (4, 6)), supersuit.sticky_actions_v0(new_dummy(), 0.75), supersuit.delay_observations_v0(new_dummy(), 1), ]
params = json.load(f) 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_freq = max(eval_freq // (n_envs*n_agents), 1) model = PPO("MlpPolicy", env, verbose=3, gamma=0.95, n_steps=256, ent_coef=0.0905168, learning_rate=0.00062211, vf_coef=0.042202, max_grad_norm=0.9, gae_lambda=0.99, n_epochs=5, clip_range=0.3, batch_size=256) eval_callback = EvalCallback(eval_env, best_model_save_path='./logs/', log_path='./logs/', eval_freq=eval_freq, deterministic=True, render=False) model.learn(total_timesteps=n_timesteps, callback=eval_callback) model = PPO.load("./logs/best_model") mean_reward, std_reward = evaluate_policy(model, eval_env, n_eval_episodes=10) print(mean_reward) print(std_reward) render_env = base_env.copy().parallel_env() render_env = ss.color_reduction_v0(render_env, mode='B') render_env = ss.resize_v0(render_env, x_size=84, y_size=84) render_env = ss.frame_stack_v1(render_env, 3) obs_list = [] i = 0 render_env.reset() while True: for agent in render_env.agent_iter(): observation, _, done, _ = render_env.last() action = model.predict(observation, deterministic=True)[0] if not done else None render_env.step(action) i += 1 if i % (len(render_env.possible_agents)) == 0:
monitor_gym=True, save_code=True) writer = SummaryWriter(f"/tmp/{experiment_name}") # TRY NOT TO MODIFY: seeding device = torch.device( 'cuda' if torch.cuda.is_available() and args.cuda else 'cpu') random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.backends.cudnn.deterministic = args.torch_deterministic # petting zoo env = pistonball_v4.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_v0(env) envs = ss.concat_vec_envs_v0(env, args.num_envs, num_cpus=0, base_class='stable_baselines3') envs = VecMonitor(envs) if args.capture_video: envs = VecVideoRecorder(envs, f'videos/{experiment_name}', record_video_trigger=lambda x: x % 150000 == 0, video_length=400) envs = VecPyTorch(envs, device) args.num_envs = envs.num_envs args.batch_size = int(args.num_envs * args.num_steps)
'cuda' if torch.cuda.is_available() and args.cuda else 'cpu') random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.backends.cudnn.deterministic = args.torch_deterministic # https://github.com/cpnota/autonomous-learning-library/blob/5ee29eac4ad22d6de00f89345dce6f9c55569149/all/environments/multiagent_atari.py#L26 # def make_atari(env_name, pettingzoo_params): env = importlib.import_module( args.gym_id).parallel_env(obs_type='grayscale_image') env = ss.agent_indicator_v0(env) env = ss.clip_reward_v0(env) env = max_observation_v0(env, 2) env = frame_skip_v0(env, 4) env = resize_v0(env, 84, 84) env = ss.pettingzoo_env_to_vec_env_v0(env) envs = ss.concat_vec_envs_v0(env, args.num_envs, num_cpus=args.num_envs, base_class='stable_baselines3') envs = VecMonitor(envs) if args.capture_video: envs = VecVideoRecorder(envs, f'videos/{experiment_name}', record_video_trigger=lambda x: x % 150000 == 0, video_length=400) envs = VecPyTorch(envs, device) args.num_envs = envs.num_envs args.batch_size = int(args.num_envs * args.num_steps) args.minibatch_size = int(args.batch_size // args.n_minibatch)