Proximal Policy Optimization (PPO)

PPO is an on-policy policy gradient algorithm that uses a clipped objective to constrain policy updates. It aims to combine the stability of Trust Region Policy Optimization (TRPO) with the simplicity and scalability of vanilla policy gradients, effectively maintaining a balance between exploration and exploitation.

AgileRL offers support for recurrent policies in PPO to solve Partially Observable Markov Decision Processes (POMDPs). For more information, please refer to the Partially Observable Markov Decision Processes (POMDPs) documentation, or our tutorial on solving Pendulum-v1 with masked angular velocity observations here.

Compatible Action Spaces

Discrete

Box

MultiDiscrete

MultiBinary

✔️

✔️

✔️

✔️

LunarLanderContinuous-v3 Example

import numpy as np
from tqdm import tqdm

from agilerl.algorithms.ppo import PPO
from agilerl.rollouts.on_policy import collect_rollouts
from agilerl.utils.utils import make_vect_envs

# Create environment
num_envs = 16
max_steps = 100000
env = make_vect_envs('LunarLanderContinuous-v3', num_envs=num_envs)
observation_space = env.single_observation_space
action_space = env.single_action_space

# Create PPO agent
agent = PPO(
    observation_space,
    action_space,
    lr=1e-3,
    batch_size=128,
    learn_step=2048
)

pbar = tqdm(total=max_steps)
total_steps = 0
while total_steps < max_steps:
    agent.set_training_mode(True)

    # Collect rollouts and save in the agent's rollout buffer
    episode_scores = collect_rollouts(agent, env)

    agent.learn()    # Learn from rollout buffer

    total_steps += agent.learn_step
    agent.steps += agent.learn_step
    pbar.update(agent.learn_step)
    if episode_scores:
        pbar.set_description(f"Score: {np.mean(episode_scores)}")

Neural Network Configuration

To configure the architecture of the network’s encoder / head, pass a kwargs dict to the PPO net_config field. Full arguments can be found in the documentation of EvolvableMLP, EvolvableCNN, EvolvableMultiInput, and EvolvableLSTM.

For discrete / vector observations:

NET_CONFIG = {
      "encoder_config": {'hidden_size': [32, 32]},  # Network head hidden size
      "head_config": {'hidden_size': [32]}      # Network head hidden size
  }

For image observations:

NET_CONFIG = {
    "encoder_config": {
      'channel_size': [32, 32], # CNN channel size
      'kernel_size': [8, 4],   # CNN kernel size
      'stride_size': [4, 2],   # CNN stride size
    },
    "head_config": {'hidden_size': [32]}  # Network head hidden size
  }

For dictionary / tuple observations containing any combination of image, discrete, and vector observations:

CNN_CONFIG = {
    "channel_size": [32, 32], # CNN channel size
    "kernel_size": [8, 4],   # CNN kernel size
    "stride_size": [4, 2],   # CNN stride size
}

NET_CONFIG = {
    "encoder_config": {
      "latent_dim": 32,
      # Config for nested EvolvableCNN objects
      "cnn_config": CNN_CONFIG,
      # Config for nested EvolvableMLP objects
      "mlp_config": {
          "hidden_size": [32, 32]
      },
      "vector_space_mlp": True # Process vector observations with an MLP
    },
    "head_config": {'hidden_size': [32]}  # Network head hidden size
  }

For recurrent observations:

NET_CONFIG = {
  "encoder_config": {
    "hidden_state_size": 64,
    "num_layers": 1,
    "max_seq_len": 512,
  },
  "head_config": {
    "hidden_size": [64],
  }
}
# Create PPO agent
agent = PPO(
  observation_space=observation_space,
  action_space=action_space,
  net_config=NET_CONFIG
  )

Evolutionary Hyperparameter Optimization

AgileRL allows for efficient hyperparameter optimization during training to provide state-of-the-art results in a fraction of the time. For more information on how this is done, please refer to the Evolutionary Hyperparameter Optimization documentation.

Saving and Loading Agents

To save an agent, use the save_checkpoint method:

from agilerl.algorithms.ppo import PPO

agent = PPO(observation_space, action_space)   # Create PPO agent

checkpoint_path = "path/to/checkpoint"
agent.save_checkpoint(checkpoint_path)

To load a saved agent, use the load method:

from agilerl.algorithms.ppo import PPO

checkpoint_path = "path/to/checkpoint"
agent = PPO.load(checkpoint_path)

Parameters

class agilerl.algorithms.ppo.PPO(*args: Any, **kwargs: Any)

Proximal Policy Optimization (PPO).

Paper: https://arxiv.org/abs/1707.06347v2

Parameters:
  • observation_space (gym.spaces.Space) – Observation space of the environment

  • action_space (gym.spaces.Space) – Action space of the environment

  • index (int, optional) – Index to keep track of object instance during tournament selection and mutation, defaults to 0

  • hp_config (HyperparameterConfig, optional) – RL hyperparameter mutation configuration, defaults to None, whereby algorithm mutations are disabled.

  • net_config (dict, optional) – Network configuration, defaults to None

  • batch_size (int, optional) – Size of batched sample from replay buffer for learning, defaults to 64

  • lr (float, optional) – Learning rate for optimizer, defaults to 1e-4

  • learn_step (int, optional) – Learning frequency, defaults to 2048

  • gamma (float, optional) – Discount factor, defaults to 0.99

  • gae_lambda (float, optional) – Lambda for general advantage estimation, defaults to 0.95

  • mut (str, optional) – Most recent mutation to agent, defaults to None

  • action_std_init (float, optional) – Initial action standard deviation, defaults to 0.0

  • clip_coef (float, optional) – Surrogate clipping coefficient, defaults to 0.2

  • ent_coef (float, optional) – Entropy coefficient, defaults to 0.01

  • vf_coef (float, optional) – Value function coefficient, defaults to 0.5

  • max_grad_norm (float, optional) – Maximum norm for gradient clipping, defaults to 0.5

  • target_kl (float, optional) – Target KL divergence threshold, defaults to None

  • normalize_images (bool, optional) – Flag to normalize images, defaults to True

  • update_epochs (int, optional) – Number of policy update epochs, defaults to 4

  • actor_network (nn.Module, optional) – Custom actor network, defaults to None

  • critic_network (nn.Module, optional) – Custom critic network, defaults to None

  • share_encoders (bool, optional) – Flag to share encoder parameters between actor and critic, defaults to False

  • num_envs (int, optional) – Number of parallel environments, defaults to 1

  • rollout_buffer_config (dict[str, Any] | None, optional) – Extra keyword arguments forwarded to the rollout buffer constructor, defaults to None (treated as an empty dict).

  • recurrent (bool, optional) – Flag to use hidden states for recurrent policies, defaults to False

  • device (str, optional) – Device for accelerated computing, ‘cpu’ or ‘cuda’, defaults to ‘cpu’

  • accelerator (accelerate.Accelerator(), optional) – Accelerator for distributed computing, defaults to None

  • wrap (bool, optional) – Wrap models for distributed training upon creation, defaults to True

  • bptt_sequence_type (BPTTSequenceType, optional) – Type of sequence for BPTT learning, defaults to BPTTSequenceType.CHUNKED

  • max_seq_len (int, optional) – Maximum sequence length for truncated BPTT, defaults to None, where complete episodes are used as sequences.

add_scores(scores: list[float]) None

Add scores to the metrics.

Parameters:

scores (list[float]) – List of scores to add.

clean_up() None

Clean up the algorithm by deleting the networks and optimizers.

Returns:

None

Return type:

None

clone(index: int | None = None, wrap: bool = True) Self

Create a clone of the algorithm.

Parameters:
  • index (int | None, optional) – The index of the clone, defaults to None

  • wrap (bool, optional) – If True, wrap the models in the clone with the accelerator, defaults to False

Returns:

A clone of the algorithm

Return type:

EvolvableAlgorithm

static copy_attributes(agent: EvolvableAlgorithm, clone: EvolvableAlgorithm) EvolvableAlgorithm

Copy the non-evolvable attributes of the algorithm to a clone.

Parameters:

clone (EvolvableAlgorithm) – The clone of the algorithm.

Returns:

The clone of the algorithm.

Return type:

EvolvableAlgorithm

create_rollout_buffer() None

Create a rollout buffer with the current configuration.

evaluate_actions(obs: ndarray | Tensor, actions: ndarray | Tensor, hidden_state: dict[str, ndarray | Tensor] | None = None, action_mask: ndarray | Tensor | None = None) tuple[Tensor, Tensor, Tensor]

Evaluate the actions.

Parameters:
  • obs (ArrayOrTensor) – Environment observation, or multiple observations in a batch

  • actions (ArrayOrTensor) – Actions to evaluate

  • hidden_state (dict[str, ArrayOrTensor] | None) – Hidden state for recurrent policies, defaults to None. Expected shape: dict with tensors of shape (batch_size, 1, hidden_size).

  • action_mask (ArrayOrTensor | None) – Mask of legal actions 1=legal 0=illegal, defaults to None

Returns:

Log probability, entropy, state values

Return type:

tuple[torch.Tensor, torch.Tensor, torch.Tensor]

evolvable_attributes(networks_only: bool = False) dict[str, EvolvableModuleProtocol | ModuleDictProtocol | Optimizer | dict[str, Optimizer] | OptimizerWrapperProtocol]

Return the attributes related to the evolvable networks in the algorithm. Includes attributes that are either EvolvableModule or ModuleDict objects, as well as the optimizers associated with the networks.

Parameters:

networks_only (bool, optional) – If True, only include evolvable networks, defaults to False

Returns:

A dictionary of network attributes.

Return type:

dict[str, Any]

finalize_training_step(num_steps: int) None

Finalize the training step for metrics tracking.

Parameters:

num_steps (int) – Number of steps taken during the training step.

property fitness: list[float]

Fitness history.

get_action(obs: ndarray | Tensor, action_mask: ndarray | Tensor | None = None, hidden_state: dict[str, ndarray | Tensor] | None = None, *args: Any, **kwargs: Any) tuple[ndarray, ndarray, ndarray, ndarray] | tuple[ndarray, ndarray, ndarray, ndarray, dict[str, ndarray | Tensor] | None]

Return the next action to take in the environment.

Parameters:
  • obs (ArrayOrTensor) – Environment observation, or multiple observations in a batch

  • action_mask (ArrayOrTensor | None) – Mask of legal actions 1=legal 0=illegal, defaults to None

  • hidden_state (dict[str, ArrayOrTensor] | None) – Hidden state for recurrent policies, defaults to None

Returns:

Action, log probability, entropy, state values, and (if recurrent) next hidden state

Return type:

tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray, dict[str, ArrayOrTensor] | None] | tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]

static get_action_dim(action_space: Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary | list[Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary]) tuple[int, ...]

Return the dimension of the action space as it pertains to the underlying networks (i.e. the output size of the networks).

Parameters:

action_space (spaces.Space or list[spaces.Space].) – The action space of the environment.

Returns:

The dimension of the action space.

Return type:

int.

get_hidden_state_architecture() dict[str, tuple[int, ...]]

Get the hidden state architecture for the environment.

Returns:

Dictionary describing the hidden state architecture (name to shape)

Return type:

dict[str, tuple[int, …]]

get_initial_hidden_state(num_envs: int = 1) dict[str, ndarray | Tensor]

Get the initial hidden state for the environment.

The hidden states are generally cached on a per Module basis. The reason the Cache is per Module is because the user might want to have a custom initialization for the hidden states.

Parameters:

num_envs (int, optional) – Number of environments, defaults to 1

Returns:

Initial hidden state dictionary

Return type:

dict[str, ArrayOrTensor]

get_lr_names() list[str]

Return the learning rates of the algorithm.

get_policy() EvolvableModuleProtocol

Return the policy network of the algorithm.

static get_state_dim(observation_space: Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary | list[Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary]) tuple[int, ...]

Return the dimension of the state space as it pertains to the underlying networks (i.e. the input size of the networks).

Parameters:

observation_space (spaces.Space or list[spaces.Space].) – The observation space of the environment.

Returns:

The dimension of the state space.

Return type:

tuple[int, …].

property hp_config: HyperparameterConfig

Return the hyperparameter configuration for Evo-HPO mutations.

property index: int

Return the index of the algorithm.

init_training_step() None

Initialize the training step for metrics tracking.

static inspect_attributes(agent: EvolvableAlgorithm, input_args_only: bool = False) dict[str, Any]

Inspect and retrieve the attributes of the current object, excluding attributes related to the underlying evolvable networks (i.e. EvolvableModule, torch.optim.Optimizer) and with an option to include only the attributes that are input arguments to the constructor.

Parameters:

input_args_only (bool) – If True, only include attributes that are input arguments to the constructor. Defaults to False.

Returns:

A dictionary of attribute names and their values.

Return type:

dict[str, Any]

learn(experiences: dict[str, ndarray | dict[str, ndarray] | tuple[ndarray, ...] | Tensor | TensorDict | tuple[Tensor, ...] | dict[str, Tensor] | Number | list[ReasoningPrompts] | ReasoningPrompts] | tuple[ndarray | dict[str, ndarray] | tuple[ndarray, ...] | Tensor | TensorDict | tuple[Tensor, ...] | dict[str, Tensor] | Number | list[ReasoningPrompts] | ReasoningPrompts, ...] | None = None) float

Update agent network parameters to learn from experiences.

Parameters:

experiences (ExperiencesType | None) – Optional pre-collected rollout batch. When None (the default), samples are drawn from the agent’s internal rollout buffer.

Returns:

Mean loss value from training.

Return type:

float

classmethod load(path: str, device: str | device = 'cpu', accelerator: Accelerator | None = None) Self

Load an algorithm from a checkpoint.

Parameters:
  • path (string) – Location to load checkpoint from.

  • device (str, optional) – Device to load the algorithm on, defaults to ‘cpu’

  • accelerator (Accelerator | None, optional) – Accelerator object for distributed computing, defaults to None

Returns:

An instance of the algorithm

Return type:

RLAlgorithm

load_checkpoint(path: str) None

Load saved agent properties and network weights from checkpoint.

Parameters:

path (string) – Location to load checkpoint from

property mut: Any

Return the mutation object of the algorithm.

mutation_hook() None

Execute the hooks registered with the algorithm.

classmethod population(size: int, observation_space: Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary | list[Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary], action_space: Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary | list[Box | Discrete | MultiDiscrete | Dict | Tuple | MultiBinary], device: str = 'cpu', wrapper_cls: type[SelfAgentWrapper] | None = None, wrapper_kwargs: dict[str, Any] | None = None, resume_from_checkpoint: str | None = None, **kwargs) list[Self | SelfAgentWrapper]

Create a population of algorithms.

Parameters:
  • size (int) – The size of the population.

  • observation_space (GymSpaceType) – The observation space.

  • action_space (GymSpaceType) – The action space.

  • device (str) – Torch device string. Defaults to "cpu".

  • wrapper_cls (type | None) – Optional wrapper class to apply to each agent.

  • wrapper_kwargs (dict[str, Any] | None) – Keyword arguments for the wrapper class.

  • resume_from_checkpoint (str | None) – Path to checkpoint to resume from.

  • kwargs (Any) – Additional keyword arguments to pass to the algorithm constructor.

Returns:

A list of algorithms.

Return type:

list[EvolvableAlgorithm]

preprocess_observation(observation: ndarray | dict[str, ndarray] | tuple[ndarray, ...] | Tensor | TensorDict | tuple[Tensor, ...] | dict[str, Tensor] | Number | list[ReasoningPrompts] | ReasoningPrompts) Tensor | TensorDict | tuple[Tensor, ...] | dict[str, Tensor]

Preprocesses observations for forward pass through neural network.

Parameters:

observation (ObservationType) – Observations of environment

Returns:

Preprocessed observations

Return type:

torch.Tensor[float] or dict[str, torch.Tensor[float]] or tuple[torch.Tensor[float], …]

recompile() None

Recompiles the evolvable modules in the algorithm with the specified torch compiler.

register_mutation_hook(hook: Callable) None

Register a hook to be executed after a mutation is performed on the algorithm.

Parameters:

hook (Callable) – The hook to be executed after mutation.

register_network_group(group: NetworkGroup) None

Set the evaluation network for the algorithm.

Parameters:

name (str) – The name of the evaluation network.

reinit_optimizers(optimizer: OptimizerConfig | None = None) None

Reinitialize the optimizers of an algorithm. If no optimizer is passed, all optimizers are reinitialized.

Parameters:

optimizer (OptimizerConfig | None, optional) – The optimizer to reinitialize, defaults to None, in which case all optimizers are reinitialized.

save_checkpoint(path: str) None

Save a checkpoint of agent properties and network weights to path.

Parameters:

path (string) – Location to save checkpoint at

property scores: list[float]

Per-episode scores.

set_training_mode(training: bool) None

Set the training mode of the algorithm.

Parameters:

training (bool) – If True, set the algorithm to training mode.

share_encoder_parameters() None

Shares the encoder parameters between the actor and critic.

property steps: int

Cumulative global step count.

test(env: str | Env | VectorEnv | AsyncVectorEnv, max_steps: int | None = None, loop: int = 3, vectorized: bool = True, callback: Callable[[float, dict[str, float]], None] | None = None) float

Return mean test score of agent in environment with epsilon-greedy policy.

Parameters:
  • env (GymEnvType) – The environment to be tested in

  • max_steps (int, optional) – Maximum number of testing steps, defaults to None

  • loop (int, optional) – Number of testing loops/episodes to complete. The returned score is the mean. Defaults to 3

  • vectorized (bool, optional) – Whether the environment is vectorized, defaults to True

  • callback (Callable[[float, dict[str, float]], None] | None) – Optional callback function that takes the sum of rewards and the last info dictionary as input, defaults to None

Returns:

Mean test score of agent in environment

Return type:

float

to_device(*experiences: Tensor | TensorDict | tuple[Tensor, ...] | dict[str, Tensor]) tuple[Tensor | TensorDict | tuple[Tensor, ...] | dict[str, Tensor], ...]

Move experiences to the device.

Parameters:

experiences (tuple[torch.Tensor[float], ...]) – Experiences to move to device

Returns:

Experiences on the device

Return type:

tuple[torch.Tensor[float], …]

unwrap_models() None

Unwraps the models in the algorithm from the accelerator.

wrap_models() None

Wrap the models in the algorithm with the accelerator.