This repository contains the final code for comparing several reinforcement learning algorithms on Gymnasium control environments. The implemented algorithms include DQN, PPO, SAC, and TD3. The project can run baseline experiments, perform one-parameter hyperparameter sweeps, generate plots, and save numerical results for later analysis.
RL_Final_Code/
+-- algorithms/
| +-- dqn.py
| +-- ppo_cartpole.py
| +-- ppo_pendulum.py
| +-- sac_2018.py
| +-- td3.py
+-- compare.py
+-- experiment_config.py
+-- main.py
+-- requirements.txt
+-- run.ipynb
+-- utils.py
+-- results/
- DQN: for discrete-action environments.
- PPO: supports both discrete and continuous action spaces through separate implementations.
- SAC: for continuous-action environments.
- TD3: for continuous-action environments.
The main script automatically skips incompatible algorithm-environment pairs. For example, DQN is skipped on continuous-action environments, while SAC and TD3 are skipped on discrete-action environments.
The default configuration uses:
CartPole-v1Acrobot-v1Pendulum-v1
Create and activate a Python environment, then install the required packages:
pip install -r requirements.txtThe project was written for Gymnasium, PyTorch, NumPy, pandas, matplotlib, and tqdm. The exact package versions are listed in requirements.txt.
All main experiment settings are defined in experiment_config.py.
Important options include:
ALGORITHMS_TO_RUN: algorithms selected for execution.ENV_NAMES_TO_RUN: environments selected for execution.RUN_TUNING: whether to run hyperparameter tuning after the baseline.TOTAL_TIMESTEPS: number of environment interaction steps per seed.SEEDS: random seeds used for repeated runs.- Algorithm-specific hyperparameters and tuning value lists.
To run a shorter test, reduce TOTAL_TIMESTEPS, use fewer seeds, or set RUN_TUNING = False.
From the project root directory, run:
python main.pyAlternatively, open run.ipynb and execute the notebook cells. The notebook is mainly a convenient wrapper for running the same experiment pipeline and inspecting results.
Experiment outputs are saved under:
results/<environment>/<algorithm>/
Each algorithm-environment folder may contain:
summary.csv: summary statistics for baseline and tuning runs.episode_returns.csv: per-seed episode return curves.efficiency_metrics.csv: timing and sample-efficiency related metrics.all_returns.npz: saved return arrays.figures/: baseline, tuning, and tuned-vs-baseline plots.
Trained model weights are saved under:
results/models/
The main workflow in main.py is:
- Read the selected environments and algorithms from
experiment_config.py. - Detect whether each environment has a discrete or continuous action space.
- Skip incompatible algorithm-environment combinations.
- Run baseline training across all selected seeds.
- Plot and save baseline learning curves.
- If enabled, run one-parameter hyperparameter tuning.
- Plot tuning curves and compare the best tuned configuration with the baseline.
- Save numerical summaries and per-episode results.
The code sets random seeds for Python, NumPy, and PyTorch through utils.set_seed. Results may still vary slightly across hardware, operating systems, and PyTorch versions, especially when GPU acceleration is used.
- Running the full default configuration can take a long time because it includes multiple environments, multiple algorithms, three seeds, and several hyperparameter sweeps.
- The
results/directory already contains generated experiment outputs and trained model files. - The
__pycache__/and.ipynb_checkpoints/folders are generated automatically and are not required to understand or run the code.