CoolFace
Apppublic

Ab-Romia/grid-world-rl-game

sourceHugging Faceupdated 11mo agoView on Hugging Face
0likes
App README

๐ŸŽฎ Grid World RL Game

An interactive reinforcement learning game where you can watch an AI agent learn to navigate a grid world in real-time! Built with Gradio and deployable on Hugging Face Spaces.

๐ŸŒŸ Features

  • โ€”Interactive Web Interface: No coding required - just adjust parameters and watch the agent learn
  • โ€”Real-time Visualization: See the agent's policy, value function, and decision-making process
  • โ€”Multiple Algorithms: Compare Value Iteration and Policy Iteration
  • โ€”Customizable Environment: Adjust grid size, rewards, and discount factors
  • โ€”Live Simulations: Watch trained agents navigate the environment step-by-step
  • โ€”Educational: Perfect for learning RL concepts visually

๐Ÿš€ Quick Start

Local Installation

bash
# Clone the repository
git clone https://github.com/Ab-Romia/Reinforcement_Learning.git
cd Reinforcement_Learning

# Install dependencies
pip install -r requirements.txt

# Run the app
python app.py

The app will open in your browser at http://localhost:7860

Hugging Face Spaces

This app is ready to deploy on Hugging Face Spaces:

  1. 1.Create a new Space on Hugging Face
  2. 2.Choose "Gradio" as the SDK
  3. 3.Upload all files from this repository
  4. 4.The app will automatically deploy!

๐ŸŽฏ How It Works

The Environment

The agent navigates a grid world with:

  • โ€”Terminal States (โ˜…): Episodes end here with big rewards or penalties
  • โ€”Top-right corner: +100 reward
  • โ€”Bottom-left corner: -50 penalty
  • โ€”Reward Cells (โ—†): Intermediate rewards the agent can collect
  • โ€”Regular Cells: Small negative reward (-1) to encourage efficiency
  • โ€”Stochastic Movement: 70% chance of intended direction, 15% each perpendicular

The Learning Process

The agent uses Reinforcement Learning algorithms to find the optimal policy:

  1. 1.Value Iteration:
  2. 2.Iteratively computes the value of each state
  3. 3.Converges to optimal policy
  4. 4.Uses the Bellman optimality equation
  1. 1.Policy Iteration:
  2. 2.Alternates between policy evaluation and improvement
  3. 3.Often converges faster than value iteration
  4. 4.Guaranteed to find optimal policy

Key Concepts

  • โ€”Policy: The agent's strategy - which action to take in each state
  • โ€”Value Function: Expected total reward from each state
  • โ€”Discount Factor (ฮณ): How much the agent values future vs immediate rewards
  • โ€”Stochastic Transitions: Movement isn't always predictable, like in real life!

๐ŸŽฎ Using the Interface

Training Tab

  1. 1.Configure Parameters:
  2. 2.Grid Size: 3x3 to 8x8
  3. 3.Center Reward: -50 to +50
  4. 4.Discount Factor: 0.8 to 0.99
  5. 5.Algorithm: Value or Policy Iteration
  1. 1.Click "Train Agent" to run the learning algorithm
  1. 1.View Results:
  2. 2.Learned policy with arrows showing optimal actions
  3. 3.Value function as background colors
  4. 4.Convergence statistics

Simulation Tab

Watch a trained agent navigate the environment:

  • โ€”Stochastic movement (more realistic)
  • โ€”Step-by-step visualization
  • โ€”Total reward tracking

๐Ÿ”ฌ Experiment Ideas

Try these configurations to see different behaviors:

  1. 1.Risk Taker vs Risk Avoider:
  2. 2.High ฮณ (0.99): Values future rewards, takes longer paths
  3. 3.Low ฮณ (0.85): Prefers quick rewards, takes shortcuts
  1. 1.Reward Attraction:
  2. 2.Positive center reward (+30): Agent seeks it out
  3. 3.Negative center reward (-30): Agent avoids it
  1. 1.Grid Complexity:
  2. 2.Small grid (3x3): Simple, fast learning
  3. 3.Large grid (8x8): Complex, more interesting policies
  1. 1.Penalty Sensitivity:
  2. 2.Adjust bottom-left penalty and see how the agent's path changes

๐Ÿ“ Project Structure

Reinforcement_Learning/
โ”œโ”€โ”€ app.py              # Gradio web interface
โ”œโ”€โ”€ game.py             # Grid World game logic
โ”œโ”€โ”€ flame.py            # Original MDP implementation
โ”œโ”€โ”€ requirements.txt    # Python dependencies
โ””โ”€โ”€ README.md          # This file

๐Ÿง  Technical Details

Algorithms

Value Iteration:

V(s) โ† max_a ฮฃ P(s'|s,a)[R(s) + ฮณV(s')]

Policy Iteration:

1. Policy Evaluation: Compute V^ฯ€(s)
2. Policy Improvement: ฯ€'(s) โ† argmax_a Q(s,a)
3. Repeat until convergence

Transition Model

For action a from state s:

  • โ€”70% probability: intended direction
  • โ€”15% probability: perpendicular left
  • โ€”15% probability: perpendicular right
  • โ€”If move would exit grid: stay in current state

Rewards

  • โ€”Terminal positive: +100
  • โ€”Terminal negative: -50
  • โ€”Center cell: configurable
  • โ€”Step cost: -1 (encourages efficiency)

๐ŸŽจ Visualization Features

  • โ€”Color-coded values: Warmer colors = higher values
  • โ€”Policy arrows: Clear direction indicators
  • โ€”Special state markers:
  • โ€”โ˜… Terminal states
  • โ€”โ—† Reward cells
  • โ€”๐Ÿค– Agent position
  • โ€”Real-time updates: See learning progress

๐Ÿ”ง Advanced Usage

Programmatic Access

python
from game import GridWorldGame

# Create custom environment
game = GridWorldGame(r_value=20, gamma=0.95, grid_size=6)

# Train agent
values, iterations = game.value_iteration()
policy = game.extract_policy(values)

# Run episode
trajectory, reward = game.run_episode(policy)
print(f"Total reward: {reward}")

# Visualize
fig = game.visualize_grid(values, policy)

Custom Environments

Modify game.py to create your own environments:

  • โ€”Change terminal state positions
  • โ€”Add more reward cells
  • โ€”Adjust transition probabilities
  • โ€”Implement obstacles

๐Ÿ“š Learning Resources

Great for understanding:

  • โ€”Markov Decision Processes (MDPs)
  • โ€”Dynamic Programming in RL
  • โ€”Value and Policy Iteration
  • โ€”Stochastic environments
  • โ€”Discount factors and their effects

๐Ÿค Contributing

This is an educational project. Feel free to:

  • โ€”Add new RL algorithms (Q-learning, SARSA, etc.)
  • โ€”Improve visualizations
  • โ€”Add new environment features
  • โ€”Create tutorials

๐Ÿ“„ License

MIT License - feel free to use for learning and teaching!

๐Ÿ™ Acknowledgments

Built with:

  • โ€”Gradio - Easy ML web interfaces
  • โ€”NumPy - Numerical computing
  • โ€”Matplotlib - Visualization

Inspired by classic RL textbooks:

  • โ€”Sutton & Barto - "Reinforcement Learning: An Introduction"
  • โ€”Russell & Norvig - "Artificial Intelligence: A Modern Approach"

๐Ÿ“ง Contact

Questions or suggestions? Open an issue or reach out!


Have fun learning Reinforcement Learning! ๐Ÿš€

Try different configurations, observe how the agent learns, and build intuition for RL concepts. The best way to learn is by experimenting!