Skip to content

Commit

Permalink
polish(pu): refine comments and render_eval configs for various commo…
Browse files Browse the repository at this point in the history
…n environments (#154)

* polish(pu): polish comments and render in cartpole, pendulum, atari, lunarlander

* polish(pu): polish comments and render in tictactoe, gomoku, connect4, 2048
  • Loading branch information
puyuan1996 authored Nov 29, 2023
1 parent e2531ce commit 545fb10
Show file tree
Hide file tree
Showing 23 changed files with 963 additions and 400 deletions.
14 changes: 14 additions & 0 deletions lzero/policy/alphazero.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class AlphaZeroPolicy(Policy):
# (int) The number of channels of hidden states in AlphaZero model.
num_channels=32,
),
# (bool) Whether to enable the sampled-based algorithm (e.g. Sampled EfficientZero)
# this variable is used in ``collector``.
sampled_algo=False,
# (bool) Whether to enable the gumbel-based algorithm (e.g. Gumbel Muzero)
gumbel_algo=False,
# (bool) Whether to use multi-gpu training.
multi_gpu=False,
# (bool) Whether to use cuda for network.
Expand Down Expand Up @@ -350,6 +355,15 @@ def _get_simulation_env(self):
else:
raise NotImplementedError
self.simulate_env = GomokuEnv(gomoku_alphazero_config.env)
elif self._cfg.simulation_env_name == 'connect4':
from zoo.board_games.connect4.envs.connect4_env import Connect4Env
if self._cfg.simulation_env_config_type == 'play_with_bot':
from zoo.board_games.connect4.config.connect4_alphazero_bot_mode_config import connect4_alphazero_config
elif self._cfg.simulation_env_config_type == 'self_play':
from zoo.board_games.connect4.config.connect4_alphazero_sp_mode_config import connect4_alphazero_config
else:
raise NotImplementedError
self.simulate_env = Connect4Env(connect4_alphazero_config.env)
else:
raise NotImplementedError

Expand Down
67 changes: 47 additions & 20 deletions zoo/atari/entry/atari_eval.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,56 @@
# According to the model you want to evaluate, import the corresponding config.
from lzero.entry import eval_muzero
import numpy as np

if __name__ == "__main__":
"""
model_path (:obj:`Optional[str]`): The pretrained model path, which should
point to the ckpt file of the pretrained model, and an absolute path is recommended.
In LightZero, the path is usually something like ``exp_name/ckpt/ckpt_best.pth.tar``.
"""
# Take the config of sampled efficientzero as an example
from zoo.atari.config.atari_sampled_efficientzero_config import main_config, create_config
Overview:
Main script to evaluate the MuZero model on Atari games. The script will loop over multiple seeds,
evaluating a certain number of episodes per seed. Results are aggregated and printed.
model_path = "/path/ckpt/ckpt_best.pth.tar"
Variables:
- model_path (:obj:`Optional[str]`): The pretrained model path, pointing to the ckpt file of the pretrained model.
The path is usually something like ``exp_name/ckpt/ckpt_best.pth.tar``.
- seeds (:obj:`List[int]`): List of seeds to use for the evaluations.
- num_episodes_each_seed (:obj:`int`): Number of episodes to evaluate for each seed.
- total_test_episodes (:obj:`int`): Total number of test episodes, calculated as num_episodes_each_seed * len(seeds).
- returns_mean_seeds (:obj:`np.array`): Array of mean return values for each seed.
- returns_seeds (:obj:`np.array`): Array of all return values for each seed.
"""
# Importing the necessary configuration files from the atari muzero configuration in the zoo directory.
from zoo.atari.config.atari_muzero_config import main_config, create_config

returns_mean_seeds = []
returns_seeds = []
# model_path is the path to the trained MuZero model checkpoint.
# If no path is provided, the script will use the default model.
model_path = None

# seeds is a list of seed values for the random number generator, used to initialize the environment.
seeds = [0]
# num_episodes_each_seed is the number of episodes to run for each seed.
num_episodes_each_seed = 1
# total_test_episodes is the total number of test episodes, calculated as the product of the number of seeds and the number of episodes per seed
total_test_episodes = num_episodes_each_seed * len(seeds)
create_config.env_manager.type = 'base' # Visualization requires the 'type' to be set as base
main_config.env.evaluator_env_num = 1 # Visualization requires the 'env_num' to be set as 1

# Setting the type of the environment manager to 'base' for the visualization purposes.
create_config.env_manager.type = 'base'
# The number of environments to evaluate concurrently. Set to 1 for visualization purposes.
main_config.env.evaluator_env_num = 1
# The total number of evaluation episodes that should be run.
main_config.env.n_evaluator_episode = total_test_episodes
main_config.env.render_mode_human = True # Whether to enable real-time rendering
main_config.env.save_video = True # Whether to save the video, if save the video render_mode_human must to be True
main_config.env.save_path = '../config/'
main_config.env.eval_max_episode_steps = int(1e3) # Adjust according to different environments
# A boolean flag indicating whether to render the environments in real-time.
main_config.env.render_mode_human = False

# A boolean flag indicating whether to save the video of the environment.
main_config.env.save_replay = True
# The path where the recorded video will be saved.
main_config.env.save_path = './video'
# The maximum number of steps for each episode during evaluation. This may need to be adjusted based on the specific characteristics of the environment.
main_config.env.eval_max_episode_steps = int(20)

# These lists will store the mean and total rewards for each seed.
returns_mean_seeds = []
returns_seeds = []

# The main evaluation loop. For each seed, the MuZero model is evaluated and the mean and total rewards are recorded.
for seed in seeds:
returns_mean, returns = eval_muzero(
[main_config, create_config],
Expand All @@ -38,11 +63,13 @@
returns_mean_seeds.append(returns_mean)
returns_seeds.append(returns)

# Convert the list of mean and total rewards into numpy arrays for easier statistical analysis.
returns_mean_seeds = np.array(returns_mean_seeds)
returns_seeds = np.array(returns_seeds)

# Printing the evaluation results. The average reward and the total reward for each seed are displayed, followed by the mean reward across all seeds.
print("=" * 20)
print(f'We eval total {len(seeds)} seeds. In each seed, we eval {num_episodes_each_seed} episodes.')
print(f'In seeds {seeds}, returns_mean_seeds is {returns_mean_seeds}, returns is {returns_seeds}')
print('In all seeds, reward_mean:', returns_mean_seeds.mean())
print("=" * 20)
print(f"We evaluated a total of {len(seeds)} seeds. For each seed, we evaluated {num_episodes_each_seed} episode(s).")
print(f"For seeds {seeds}, the mean returns are {returns_mean_seeds}, and the returns are {returns_seeds}.")
print("Across all seeds, the mean reward is:", returns_mean_seeds.mean())
print("=" * 20)
152 changes: 100 additions & 52 deletions zoo/atari/envs/atari_lightzero_env.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import copy
import sys
from typing import List
from typing import List, Any

import gym
import numpy as np
Expand All @@ -14,56 +14,108 @@

@ENV_REGISTRY.register('atari_lightzero')
class AtariLightZeroEnv(BaseEnv):
"""
Overview:
AtariLightZeroEnv is a derived class from BaseEnv and represents the environment for the Atari LightZero game.
This class provides the necessary interfaces to interact with the environment, including reset, step, seed,
close, etc. and manages the environment's properties such as observation_space, action_space, and reward_space.
Properties:
cfg, _init_flag, channel_last, clip_rewards, episode_life, _env, _observation_space, _action_space,
_reward_space, obs, _eval_episode_return, has_reset, _seed, _dynamic_seed
"""
config = dict(
# (int) The number of environment instances used for data collection.
collector_env_num=8,
# (int) The number of environment instances used for evaluator.
evaluator_env_num=3,
# (int) The number of episodes to evaluate during each evaluation period.
n_evaluator_episode=3,
# (str) The name of the Atari game environment.
env_name='PongNoFrameskip-v4',
# (str) The type of the environment, here it's Atari.
env_type='Atari',
# (tuple) The shape of the observation space, which is a stacked frame of 4 images each of 96x96 pixels.
obs_shape=(4, 96, 96),
# (int) The maximum number of steps in each episode during data collection.
collect_max_episode_steps=int(1.08e5),
# (int) The maximum number of steps in each episode during evaluation.
eval_max_episode_steps=int(1.08e5),
# (bool) If True, the game is rendered in real-time.
render_mode_human=False,
# (bool) If True, a video of the game play is saved.
save_replay=False,
# (str) The path to save the video.
replay_path='./video',
# (bool) If set to True, the game screen is converted to grayscale, reducing the complexity of the observation space.
gray_scale=True,
# (int) The number of frames to skip between each action. Higher values result in faster simulation.
frame_skip=4,
# (bool) If True, the game ends when the agent loses a life, otherwise, the game only ends when all lives are lost.
episode_life=True,
# (bool) If True, the rewards are clipped to a certain range, usually between -1 and 1, to reduce variance.
clip_rewards=True,
# (bool) If True, the channels of the observation images are placed last (e.g., height, width, channels).
channel_last=True,
render_mode_human=False,
# (bool) If True, the pixel values of the game frames are scaled down to the range [0, 1].
scale=True,
# (bool) If True, the game frames are preprocessed by cropping irrelevant parts and resizing to a smaller resolution.
warp_frame=True,
save_video=False,
# (bool) If True, the game state is transformed into a string before being returned by the environment.
transform2string=False,
# (bool) If True, additional wrappers for the game environment are used.
game_wrapper=True,
# (dict) The configuration for the environment manager. If shared_memory is set to False, each environment instance
# runs in the same process as the trainer, otherwise, they run in separate processes.
manager=dict(shared_memory=False, ),
# (int) The value of the cumulative reward at which the training stops.
stop_value=int(1e6),
)

@classmethod
def default_config(cls: type) -> EasyDict:
"""
Overview:
Return the default configuration for the Atari LightZero environment.
Arguments:
- cls (:obj:`type`): The class AtariLightZeroEnv.
Returns:
- cfg (:obj:`EasyDict`): The default configuration dictionary.
"""
cfg = EasyDict(copy.deepcopy(cls.config))
cfg.cfg_type = cls.__name__ + 'Dict'
return cfg

def __init__(self, cfg=None):
def __init__(self, cfg: EasyDict) -> None:
"""
Overview:
Initialize the Atari LightZero environment with the given configuration.
Arguments:
- cfg (:obj:`EasyDict`): The configuration dictionary.
"""
self.cfg = cfg
self._init_flag = False
self.channel_last = cfg.channel_last
self.clip_rewards = cfg.clip_rewards
self.episode_life = cfg.episode_life

def _make_env(self):
return wrap_lightzero(self.cfg, episode_life=self.cfg.episode_life, clip_rewards=self.cfg.clip_rewards)

def reset(self):
def reset(self) -> dict:
"""
Overview:
Reset the environment and return the initial observation.
Returns:
- obs (:obj:`dict`): The initial observation after reset.
"""
if not self._init_flag:
self._env = self._make_env()
# Create and return the wrapped environment for Atari LightZero.
self._env = wrap_lightzero(self.cfg, episode_life=self.cfg.episode_life, clip_rewards=self.cfg.clip_rewards)
self._observation_space = self._env.env.observation_space
self._action_space = self._env.env.action_space
self._reward_space = gym.spaces.Box(
low=self._env.env.reward_range[0], high=self._env.env.reward_range[1], shape=(1, ), dtype=np.float32
low=self._env.env.reward_range[0], high=self._env.env.reward_range[1], shape=(1,), dtype=np.float32
)

self._init_flag = True

if hasattr(self, '_seed') and hasattr(self, '_dynamic_seed') and self._dynamic_seed:
np_seed = 100 * np.random.randint(1, 1000)
self._env.env.seed(self._seed + np_seed)
Expand All @@ -73,29 +125,19 @@ def reset(self):
obs = self._env.reset()
self.obs = to_ndarray(obs)
self._eval_episode_return = 0.
self.has_reset = True
obs = self.observe()
# obs.shape: 96,96,1
return obs

def observe(self):
def step(self, action: int) -> BaseEnvTimestep:
"""
Overview:
add action_mask to obs to adapt with MCTS alg..
Execute the given action and return the resulting environment timestep.
Arguments:
- action (:obj:`int`): The action to be executed.
Returns:
- timestep (:obj:`BaseEnvTimestep`): The environment timestep after executing the action.
"""
observation = self.obs

if not self.channel_last:
# move the channel dim to the fist axis
# (96, 96, 3) -> (3, 96, 96)
observation = np.transpose(observation, (2, 0, 1))

action_mask = np.ones(self._action_space.n, 'int8')
return {'observation': observation, 'action_mask': action_mask, 'to_play': -1}

def step(self, action):
obs, reward, done, info = self._env.step(action)
# self._env.render()
self.obs = to_ndarray(obs)
self.reward = np.array(reward).astype(np.float32)
self._eval_episode_return += self.reward
Expand All @@ -105,6 +147,23 @@ def step(self, action):

return BaseEnvTimestep(observation, self.reward, done, info)

def observe(self) -> dict:
"""
Overview:
Return the current observation along with the action mask and to_play flag.
Returns:
- observation (:obj:`dict`): The dictionary containing current observation, action mask, and to_play flag.
"""
observation = self.obs

if not self.channel_last:
# move the channel dim to the fist axis
# (96, 96, 3) -> (3, 96, 96)
observation = np.transpose(observation, (2, 0, 1))

action_mask = np.ones(self._action_space.n, 'int8')
return {'observation': observation, 'action_mask': action_mask, 'to_play': -1}

@property
def legal_actions(self):
return np.arange(self._action_space.n)
Expand All @@ -113,52 +172,41 @@ def random_action(self):
action_list = self.legal_actions
return np.random.choice(action_list)

def render(self, mode='human'):
self._env.render()

def human_to_action(self):
"""
Overview:
For multiplayer games, ask the user for a legal action
and return the corresponding action number.
Returns:
An integer from the action space.
"""
while True:
try:
print(f"Current available actions for the player are:{self.legal_actions}")
choice = int(input(f"Enter the index of next action: "))
if choice in self.legal_actions:
break
else:
print("Wrong input, try again")
except KeyboardInterrupt:
print("exit")
sys.exit(0)
except Exception as e:
print("Wrong input, try again")
return choice

def close(self) -> None:
"""
Close the environment, and set the initialization flag to False.
"""
if self._init_flag:
self._env.close()
self._init_flag = False

def seed(self, seed: int, dynamic_seed: bool = True) -> None:
"""
Set the seed for the environment's random number generator. Can handle both static and dynamic seeding.
"""
self._seed = seed
self._dynamic_seed = dynamic_seed
np.random.seed(self._seed)

@property
def observation_space(self) -> gym.spaces.Space:
"""
Property to access the observation space of the environment.
"""
return self._observation_space

@property
def action_space(self) -> gym.spaces.Space:
"""
Property to access the action space of the environment.
"""
return self._action_space

@property
def reward_space(self) -> gym.spaces.Space:
"""
Property to access the reward space of the environment.
"""
return self._reward_space

def __repr__(self) -> str:
Expand Down
Loading

0 comments on commit 545fb10

Please sign in to comment.