Skip to main content

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:

  1. Go to Window > Package Manager
  2. Install ML-Agents package
  3. 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:

  1. Proceed to Lesson 2: Q-Learning Agent
  2. Review Getting Started for troubleshooting
  3. Check Glossary for any unfamiliar terms

๐Ÿ’ก Tipsโ€‹

  • Use virtual environments: Always isolate your project dependencies
  • Pin versions: Create a requirements.txt file 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!