How kaynup/tic-tac-toe-q-learning works
Project type and purpose
• This is an educational, open-source command-line tool demonstrating tabular Q-Learning on a Tic-Tac-Toe environment.
• It provides two primary UIs: a training script (train.py) to learn a Q-table, and an interactive play script (play.py) for human vs AI games.
File structure
• Top-level scripts and settings:
• train.py – orchestrates training loops, evaluation, plotting.
• play.py – handles CLI prompts, loads a trained agent, and runs games.
• config.yaml – global hyperparameters (learning rate, gamma, epsilon schedule, seeds).
• requirements.txt – NumPy and Matplotlib dependencies.
• Package “tictactoe/”: core modules:
• agent.py – Q-Learning agent logic (action selection, Q-table updates).
• env.py – game environment (state representation, legal moves, reward logic, random opponent).
• utils.py – helper functions (state hashing, plotting routines, statistics).
• Generated artifacts: q_table.pkl (serialized Q-table), training plots (e.g., PNG) via Matplotlib.
README for additional insights
• Describes Q-Learning formula, training vs evaluation modes, CLI flags (--episodes, --load, --level).
• Emphasizes reproducibility, configurability, and live stats.
a. Main components (nodes):
• CLI Layer:
– Train Controller (train.py)
– Play Controller (play.py)
• Core Package (“tictactoe”):
– Q-Learning Agent
– Game Environment
– Utilities (state hashing, plotting)
• Configuration Loader (reads config.yaml)
• Persistence Layer (read/write q_table.pkl)
• Visualization Engine (Matplotlib for plots)
• External Libraries: NumPy for arrays, random opponent logic
1. CLI → Configuration Loader (loads hyperparameters)
2. CLI → Core Package:
– Train Controller instantiates Environment and Agent
– Play Controller instantiates Environment and Agent with loaded Q-table
3. Q-Learning Agent ↔ Game Environment:
– Agent requests legal actions from Environment
– Environment returns next state and reward
– Agent updates Q-table
4. Utilities used by both Agent and Environment (e.g., hashing states, plotting)
5. Persistence Layer ←→ Agent (save/load q_table.pkl)
c. Architectural patterns & design principles:
• Modular separation: CLI controllers vs core logic
• Single-process, in-memory Q-table (tabular RL) persisted to disk
• Config-driven behavior for reproducibility
• Loose coupling via utility functions
d. Key technologies & frameworks:
• Python 3.8+
• NumPy (array operations)
• Matplotlib (training plots)
• YAML (configuration)
• Pickle (Q-table serialization)