Skip to content
ajuanijustusPublic

About

Single-file RL implementations in PyTorch and JAX that follow the paper equations, written for studying.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Equation-2-RL (eq2rl)

Clean RL implementations in PyTorch & JAX for Study/Revision #come-study-with-me #basics #rl #marl #harl

Hi! When I first started studying Reinforcement Learning, I desperately wanted a single, go-to repository with clean implementations in PyTorch and JAX; something that mapped directly to the math in the actual papers. Instead, I (more often than not) found myself digging through thousands of lines of production framework code.

This repo is strictly a study guide. We replicate Deep RL algorithms by stripping away all the engineering bloat (no heavy parallelization, no complex wrappers). Everything is implemented purely so you can see exactly how paper equations translate into clean, execution-ready code.

Philosophy

  • Zero Bloat: No vectorized environments, no complex multiprocessing frameworks, no hardware-specific optimization hacks. If it’s not in the original paper's pseudo-code, it doesn't go in here.
  • Conceptual Parity, Not Line-by-Line: We don't force JAX to look like PyTorch. PyTorch scripts are naturally stateful, object-oriented, and imperative. JAX scripts are naturally stateless, functional, and transformed (jit, grad). We embrace the unique flavor of both.
  • 100% Self-Contained: No shared utils.py file or custom abstract base classes. If a script needs a replay buffer or a helper function, it's written right there inside the file. You can grab a single file, run it, and walk away.

Flat Directory Structure

To keep things completely scannable, we avoid nested folder rabbit holes. Each algorithm gets exactly one folder with exactly three files:

eq2rl/
├── README.md               # You are here!
│
├── dqn/
│   ├── math_dqn.md         # The paper math, directly referencing the paper and decoded into plain English
│   ├── pytorch_dqn.py      # Standalone PyTorch code
│   └── jax_dqn.py          # Standalone JAX code
│
└── ppo/
    ├── math_ppo.md
    ├── pytorch_ppo.py
    └── jax_ppo.py

Algorithms

This is how I visualise the progression of RL algoritms. Use this roadmap as a diagnostic guide: every single algorithm downstream was invented specifically to fix a catastrophic limitation or breaking point of the algorithm directly upstream from it.

flowchart TD
    %% Base Nodes
    DQN["DQN (2013)\nValue-Based (Discrete)"]:::single
    REINFORCE["REINFORCE (1992)\nPolicy-Based"]:::single
    
    %% The Stochastic Branch (PPO Path)
    A2C["A2C / A3C (2016)\nAdvantage Actor-Critic"]:::single
    TRPO["TRPO (2015)\nTrust Region Optimization"]:::single
    PPO["PPO (2017)\nProximal Policy Optimization"]:::single
    MAPPO["MAPPO (2021)\nMulti-Agent PPO"]:::marl
    HATRPO["HATRPO (2021)\nHeterogeneous TRPO"]:::hetero
    HAPPO["HAPPO (2021)\nHeterogeneous PPO"]:::hetero

    %% The Deterministic / Max Entropy Branch (Continuous Path)
    DDPG["DDPG (2015)\nDeep Deterministic PG"]:::single
    TD3["TD3 (2018)\nTwin Delayed DDPG"]:::single
    SAC["SAC (2018)\nSoft Actor-Critic"]:::single
    MADDPG["MADDPG (2017)\nMulti-Agent DDPG"]:::marl
    MASAC["MASAC (2021)\nMulti-Agent SAC"]:::marl

    %% The Value Factorization Track (Discrete MARL)
    VDN["VDN (2017)\nValue Decomposition"]:::marl
    QMIX["QMIX (2018)\nMonotonic Value Mixing"]:::marl

    %% The Offline RL Track
    CQL["CQL (2020)\nConservative Q-Learning"]:::single

    REINFORCE ---> |"Advantage baseline subtraction to address high policy gradient variance"| A2C
    DQN ---> |"Value-scoring Critic baseline to address continuous action space optimization failure"| A2C
    
    A2C ---> |"KL-constrained Trust Regions to address unstable policy steps"| TRPO
    TRPO ---> |"First-order ratio clipping to address Trust-region compute overhead"| PPO
    A2C ---> |"First-order ratio clipping to address high step-size optimization sensitivity"| PPO
    
    PPO ---> |"CTDE to address non-stationarity"| MAPPO
    MAPPO ---> |"Sequential Advantage Decomposition to address shared-weight constraints in asymmetric roles"| HAPPO
    TRPO ---> |"Sequential factorized policy updates to address joint update trust region violations"| HATRPO
    HATRPO ---> |"First-order advantage clipping to address Trust-region compute overhead"| HAPPO
    
    DQN ---> |"Deterministic Actor networks to address infinite-action max-Q lookup failure"| DDPG
    DDPG ---> |"Twin Critics + Delayed Updates to address severe target overestimation bias"| TD3
    TD3 ---> |"Max-Entropy exploration rewards to address premature deterministic local-minima convergence"| SAC
    
    DDPG ---> |"Multi-agent Centralized Critic to address joint-action environment non-stationarity"| MADDPG
    SAC ---> |"Joint Max-Entropy + CTDE Critic to address uncoordinated continuous multi-agent exploration"| MASAC
    MADDPG ---> |"Stochastic entropy regularization to address continuous multi-agent action instability"| MASAC

    DQN --> |"Additive local Q-values to address independent learner non-stationarity"| VDN
    VDN --> |"Monotonic Neural Mixing Networks to address linear value-decomposition limitations"| QMIX

    DQN --> |"Conservative OOD penalty to address offline action exploitation hallucinations"| CQL

    %% Styling Classes
    classDef single fill:#0284C7,stroke:#0369A1,stroke-width:2px,color:#fff;
    classDef marl fill:#16A34A,stroke:#15803D,stroke-width:2px,color:#fff;
    classDef hetero fill:#7C3AED,stroke:#6D28D9,stroke-width:2px,color:#fff;
Loading

Legend: 🟦 Single-Agent Baseline Algorithms, 🟩 Multi-Agent Cooperative Algorithms, 🟪 Heterogeneous-Agent Algorithms

Algorithm Agent Type Environment math.md PyTorch JAX
DQN 🟦 Value-Based (Discrete) CartPole-v1 🔴 🔴 🔴
REINFORCE 🟦 Policy-Based CartPole-v1 🔴 🔴 🔴
A2C 🟦 Actor-Critic CartPole-v1 / Pendulum-v1 🔴 🔴 🔴
TRPO 🟦 Trust Region Optimization Pendulum-v1 🔴 🔴 🔴
PPO 🟦 Proximal Policy Optimization Pendulum-v1 🔴 🔴 🔴
DDPG 🟦 Deep Deterministic PG Pendulum-v1 🔴 🔴 🔴
TD3 🟦 Twin Delayed DDPG Pendulum-v1 🔴 🔴 🔴
SAC 🟦 Soft Actor-Critic Pendulum-v1 🔴 🔴 🔴
CQL 🟦 Conservative Q-Learning (Offline) Pendulum-v1 (Dataset) 🔴 🔴 🔴
VDN 🟩 Value Decomposition simple_spread 🔴 🔴 🔴
QMIX 🟩 Monotonic Value Mixing simple_spread 🔴 🔴 🔴
MADDPG 🟩 Multi-Agent DDPG simple_spread 🔴 🔴 🔴
MAPPO 🟩 Multi-Agent PPO simple_spread 🔴 🔴 🔴
MASAC 🟩 Multi-Agent SAC simple_spread 🔴 🔴 🔴
HATRPO 🟪 Heterogeneous TRPO simple_spread 🔴 🔴 🔴
HAPPO 🟪 Heterogeneous PPO simple_spread 🔴 🔴 🔴

Legend: 🟢 Complete | 🟡 Work in Progress / Open PR | 🔴 Not Started

Feel free to jump into any of the completed modules or grab a WIP one to study and finish it up!

How to Use This Repo

  1. Pick an algorithm you want to study (e.g., dqn).
  2. Read the math_dqn.md inside that folder first. It starts with the intuition from the original paper and breaks down the math/algorithm into plain language.
  3. Open pytorch_dqn.py and jax_dqn.py side-by-side.
  4. Look at how PyTorch uses objects and backward passes while JAX uses stateless state dictionaries and functional loss math.

Quick Start (Dependencies)

We keep dependencies completely minimal. You just need standard gymnasium (plus PettingZoo for multi-agent settings), torch, jax, and optax.

pip install gymnasium pettingzoo torch jax optax

To run any script, just execute it directly:

python dqn/pytorch_dqn.py

About

Single-file RL implementations in PyTorch and JAX that follow the paper equations, written for studying.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages