Reinforcement Learning Agents
The PyCRM framework provides agent implementations specifically designed to learn from counterfactual experiences generated by Reward Machines and Counting Reward Machines.
Introduction
Thepycrm.agents module provides reinforcement learning algorithms that integrate with Reward Machines and Counting Reward Machines to efficiently learn task policies. These agents are designed to take advantage of the counterfactual experience generation capabilities provided by the pycrm framework.
The framework includes two main types of agent implementations:
- Tabular agents for discrete state and action spaces
- Deep RL agents based on Stable Baselines 3 for continuous domains
Tabular Agents
Tabular agents are suitable for environments with discrete state and action spaces. The framework provides:Q-Learning (QL)
The standard Q-Learning algorithm is implemented inpycrm.agents.tabular.ql. This provides a baseline implementation that uses the standard Q-learning update rule:
Counterfactual Q-Learning (CQL)
Thepycrm.agents.tabular.cql module implements Counterfactual Q-Learning, which extends standard Q-Learning to take advantage of the counterfactual experience generation capabilities of Reward Machines and Counting Reward Machines.
- Takes a real step in the environment
- Generates counterfactual experiences using the CrossProduct environment
- Updates Q-values for all valid counterfactual experiences
- Significantly accelerates learning compared to standard Q-Learning
Deep RL Agents
For environments with continuous state or action spaces, the framework provides integrations with Stable Baselines 3. The framework currently supports Counterfactual versions of DQN, SAC, TD3, and DDPG algorithms.Counterfactual DQN (C-DQN)
Thepycrm.agents.sb3.dqn.cdqn module implements Counterfactual Deep Q-Network, extending the DQN algorithm from Stable Baselines 3 to learn from counterfactual experiences.
Counterfactual SAC (C-SAC)
Thepycrm.agents.sb3.sac.csac module implements Counterfactual Soft Actor-Critic (C-SAC), extending the SAC algorithm from Stable Baselines 3 to learn from counterfactual experiences.
Counterfactual TD3 (C-TD3)
Thepycrm.agents.sb3.td3.ctd3 module implements Counterfactual Twin Delayed Deep Deterministic Policy Gradient (C-TD3), extending the TD3 algorithm from Stable Baselines 3.
Counterfactual DDPG (C-DDPG)
Thepycrm.agents.sb3.ddpg.cddpg module implements Counterfactual Deep Deterministic Policy Gradient (C-DDPG), extending the DDPG algorithm from Stable Baselines 3.
- Collecting transitions from the environment
- Generating counterfactual experiences for each transition
- Adding these experiences to the replay buffer
- Training the policy network using both real and counterfactual experiences
Vectorised Environment Support
All counterfactual deep RL agents provide specialised support for vectorised environments through thepycrm.agents.sb3.wrapper module, which includes:
DispatchSubprocVecEnv: An extension of Stable Baselines 3’sSubprocVecEnvthat enables efficient parallel generation of counterfactual experiences
Performance Benefits
Agents that leverage counterfactual experiences show several advantages:- Faster Convergence: Learning from counterfactual experiences often reduces the number of episodes needed to learn optimal policies by orders of magnitude.
- Better Sample Efficiency: By extracting more information from each environment interaction, these agents make better use of collected experiences.
- More Robust Policies: Since the agent explores the reward machine state space more completely, the resulting policies tend to be more robust.
Requesting Custom Agent Implementations
Need support for a different RL algorithm? We’re happy to add it! Open an issue on our GitHub repository and we’ll prioritize implementing it.
- Integration with additional Stable Baselines 3 algorithms (PPO, A2C, etc.)
- Support for other deep RL frameworks (RLlib, Pytorch, etc.)
- Custom agent architectures or learning algorithms
- Specialized handling for your environment type
- The algorithm or implementation you need
- Your use case or environment
- Any specific requirements or constraints