Skip to content
saraelsayed13Public

About

A reinforcement learning zoo management agent trained with PER-DQN and PPO using a custom Gymnasium environment.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦁 Zoo Keeper — Reinforcement Learning Agent

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.


📁 Project Structure

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

🌍 Environment — ZooEnv

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

🤖 Algorithms

1. PER-DQN (PER-DQN.py)

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

2. PPO (PPO.py)

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

🚀 Getting Started

Prerequisites

pip install torch gymnasium numpy matplotlib

Train DQN

python PER-DQN.py

Logs and models are saved to ./outputs/.

Train PPO

python PPO.py

Logs and checkpoints are saved to ./runs/ppo_best/.

Visualize Results

python Vis.py

📊 Logged Metrics

Both 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

🎬 GIF Snapshots

Both agents save .gif animations of the agent's behavior at regular intervals during training, letting you visually track policy improvement over time.


📦 Saved Models

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

📄 License

MIT

About

A reinforcement learning zoo management agent trained with PER-DQN and PPO using a custom Gymnasium environment.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages