Lesson 1: Python Environment Setup
Before implementing Q-Learning, you need to set up your Python environment with all the necessary libraries and tools.
๐ฏ Learning Objectivesโ
By the end of this lesson, you'll have:
- A properly configured Python environment
- All required libraries installed
- Unity-Python communication working
- A basic project structure
๐ Prerequisitesโ
Before starting, ensure you have:
- Python 3.8+ installed
- Unity project from GameDev Unity section
- Basic understanding of Python and virtual environments
๐ ๏ธ Step-by-Step Setupโ
1. Create Virtual Environmentโ
# Create a new virtual environment
python -m venv ants-saga-env
# Activate the environment
# On Windows:
ants-saga-env\Scripts\activate
# On macOS/Linux:
source ants-saga-env/bin/activate
2. Install Required Packagesโ
# Install core ML libraries
pip install numpy pandas matplotlib seaborn
# Install RL libraries
pip install gym gymnasium
# Install Unity communication
pip install mlagents
# Install additional utilities
pip install jupyter notebook tqdm
3. Verify Installationโ
Create a test script test_installation.py:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import gym
import mlagents
print("โ
All packages installed successfully!")
print(f"NumPy version: {np.__version__}")
print(f"Pandas version: {pd.__version__}")
print(f"Gym version: {gym.__version__}")
Run the test:
python test_installation.py
๐๏ธ Project Structureโ
Create the following directory structure:
ants-saga-rl/
โโโ environments/
โ โโโ __init__.py
โ โโโ ants_saga_env.py
โ โโโ unity_connector.py
โโโ agents/
โ โโโ __init__.py
โ โโโ q_learning.py
โ โโโ base_agent.py
โโโ utils/
โ โโโ __init__.py
โ โโโ visualization.py
โ โโโ metrics.py
โโโ config/
โ โโโ hyperparameters.py
โ โโโ environment_config.py
โโโ notebooks/
โ โโโ exploration.ipynb
โโโ models/
โ โโโ .gitkeep
โโโ logs/
โ โโโ .gitkeep
โโโ main.py
๐ง Unity-Python Connectionโ
1. Configure Unity for ML-Agentsโ
In your Unity project:
- Go to
Window > Package Manager - Install ML-Agents package
- Add the ML-Agents component to your agent GameObject
2. Test Connectionโ
Create a simple connection test:
# test_unity_connection.py
import mlagents
from mlagents_envs.environment import UnityEnvironment
def test_unity_connection():
try:
# Connect to Unity environment
env = UnityEnvironment(file_name="path/to/your/unity/build")
print("โ
Unity connection successful!")
env.close()
except Exception as e:
print(f"โ Unity connection failed: {e}")
if __name__ == "__main__":
test_unity_connection()
โ Verification Checklistโ
- Virtual environment created and activated
- All packages installed without errors
- Test script runs successfully
- Project structure created
- Unity connection working (if Unity is available)
๐ฏ Next Stepsโ
Once your environment is set up:
- Proceed to Lesson 2: Q-Learning Agent
- Review Getting Started for troubleshooting
- Check Glossary for any unfamiliar terms
๐ก Tipsโ
- Use virtual environments: Always isolate your project dependencies
- Pin versions: Create a
requirements.txtfile with specific versions - Test early: Verify each component works before moving on
- Document issues: Keep notes of any problems and solutions
๐ Requirements Fileโ
Create a requirements.txt file:
numpy==1.24.3
pandas==2.0.3
matplotlib==3.7.2
seaborn==0.12.2
gym==0.29.1
gymnasium==0.29.0
mlagents==0.30.0
jupyter==1.0.0
tqdm==4.65.0
Perfect! Your Python environment is ready. Let's implement Q-Learning!