A reinforcement learning project where an agent learns to manage a zoo: feeding animals, preventing escapes, and maximizing survival. Two algorithms are implemented and compared — PER-DQN and PPO.
zoo-rl/
│
├── zoo_env.py # Custom Gymnasium environment (ZooEnv)
├── PER-DQN.py # Prioritized Experience Replay DQN agent + training loop
├── PPO.py # Proximal Policy Optimization agent + training loop
├── Vis.py # Visualization and plotting utilities
│
├── runs/
│ └── ppo_best/
│ ├── best.pt # Best PPO checkpoint
│ ├── final.pt # Final PPO checkpoint
│ └── log.csv # PPO training log
│
├── outputs/
│ ├── best_model.pth # Best DQN model
│ ├── final_model.pth # Final DQN model
│ ├── training_log.csv # DQN training log
│ └── progress_step_*.gif # Training animation snapshots
│
└── README.md
A custom Gymnasium environment simulating a zoo grid.
| Property | Value |
|---|---|
| Observation space | Box(24,) — normalized animal positions, hunger levels, agent position |
| Action space | Discrete(5) — UP, DOWN, LEFT, RIGHT, FEED |
| Max steps per episode | 125 |
| Animals | 6 (configurable) |
Reward signal:
- ✅ Positive reward for feeding a hungry animal
- ❌ Penalty for animal escapes
- Episode ends on terminal conditions
Deep Q-Network with Prioritized Experience Replay.
| Hyperparameter | Value |
|---|---|
| Replay buffer | 100,000 |
| Batch size | 128 |
| Gamma | 0.99 |
| Learning rate | 3e-4 |
| Epsilon decay | 0.99998 → 0.05 |
| Target update | Every 1,000 steps |
| Network | Linear(24→256→256→5) |
| Training episodes | 24,000 |
Proximal Policy Optimization with vectorized environments, GAE, and running normalization.
| Hyperparameter | Value |
|---|---|
| Parallel envs | 8 |
| Steps per rollout | 256 |
| Total timesteps | 3,000,000 |
| Epochs per update | 10 |
| Minibatch size | 256 |
| Gamma / GAE λ | 0.99 / 0.95 |
| Clip ε | 0.2 |
| Learning rate | 3e-4 (annealed) |
| Network | Linear(24→256→256) + Actor/Critic heads |
pip install torch gymnasium numpy matplotlibpython PER-DQN.pyLogs and models are saved to ./outputs/.
python PPO.pyLogs and checkpoints are saved to ./runs/ppo_best/.
python Vis.pyBoth training scripts produce a .csv log with:
| Column | Description |
|---|---|
update |
Log interval index |
steps |
Total environment steps |
mean_rew |
Average reward over last N episodes |
mean_esc / avg_escapes |
Average animal escapes |
loss / pl / vl |
Loss values |
epsilon (DQN) |
Exploration rate |
ent, kl (PPO) |
Entropy and KL divergence |
Both agents save .gif animations of the agent's behavior at regular intervals during training, letting you visually track policy improvement over time.
| File | Description |
|---|---|
outputs/best_model.pth |
Best DQN model (full torch.save) |
outputs/final_model.pth |
Final DQN model |
runs/ppo_best/best.pt |
Best PPO checkpoint (state dicts + normalizer) |
runs/ppo_best/final.pt |
Final PPO checkpoint |
MIT