Monte Carlo Tree Search (MCTS) is a powerful search algorithm used in AI-driven decision-making, particularly in game-playing, robotics, and planning problems. This repository serves as a complete guide to understanding, implementing, and optimizing MCTS in Python with real-world examples.
📌 Understand the fundamentals of MCTS
📌 Explore the four key MCTS steps: Selection, Expansion, Simulation, Backpropagation
📌 Implement MCTS from scratch in Python
📌 Apply MCTS to board games like Chess, Go, and Tic-Tac-Toe
📌 Optimize MCTS using parallelization & deep learning
- 📖 Detailed explanations of MCTS principles
- 🏗 Step-by-step implementation from scratch
- 🎲 Game-playing AI using MCTS (Tic-Tac-Toe, Chess, Go, etc.)
- ⚡ Performance optimizations (Parallel MCTS, pruning, heuristics)
- 📚 Jupyter notebooks with interactive explanations
- 🏆 Comparisons with Minimax & Alpha-Beta Pruning
Before running the code, make sure you have:
- Python 3.x → Download Here
- Libraries: NumPy, Matplotlib, SciPy
- Jupyter Notebook for interactive learning
git clone https://github.com/saadsalmanakram/MCTS.git
cd MCTS
pip install -r requirements.txt
Launch Jupyter Notebook and explore interactive notebooks:
jupyter notebook
- The Four Phases: Selection, Expansion, Simulation, Backpropagation
- The UCT Formula (Upper Confidence Bound for Trees)
- Balancing exploration vs. exploitation
- Creating a Tree Node structure
- Running Monte Carlo simulations
- Selecting the best move using UCT
- Tic-Tac-Toe (Basic Example)
- Connect 4 (Medium Complexity)
- Chess & Go (Advanced AI)
- Parallel MCTS for speedup
- Pruning strategies
- Using Neural Networks for move evaluation
import math
import random
class Node:
def __init__(self, state, parent=None):
self.state = state
self.parent = parent
self.children = []
self.visits = 0
self.value = 0.0
def select_best_child(self):
return max(self.children, key=lambda child: child.value / (child.visits + 1e-6) + math.sqrt(2 * math.log(self.visits + 1) / (child.visits + 1)))
def expand(self):
new_state = self.state.get_next_state() # Define this in your game logic
child = Node(new_state, parent=self)
self.children.append(child)
return child
def mcts_search(root, iterations=1000):
for _ in range(iterations):
node = root
while node.children:
node = node.select_best_child()
new_node = node.expand()
reward = simulate(new_node.state) # Run a Monte Carlo simulation
backpropagate(new_node, reward)
return max(root.children, key=lambda child: child.visits)
def backpropagate(node, reward):
while node:
node.visits += 1
node.value += reward
node = node.parent
from tic_tac_toe import TicTacToe # Assume TicTacToe class is defined
game = TicTacToe()
root = Node(game)
best_move = mcts_search(root, iterations=500)
print("Best Move:", best_move.state)
📌 Game AI → Chess, Go, Poker, Tic-Tac-Toe
📌 Robotics → Motion planning & decision-making
📌 Optimization Problems → Route planning, scheduling
📌 Autonomous Systems → AI-driven decision-making
Contributions are welcome! 🚀
🔹 Fork the repository
🔹 Create a new branch (git checkout -b feature-name
)
🔹 Commit changes (git commit -m "Added MCTS optimization example"
)
🔹 Push to your branch (git push origin feature-name
)
🔹 Open a pull request
This project is licensed under the MIT License – feel free to use, modify, and share the code.
📧 Email: saadsalmanakram1@gmail.com
🌐 GitHub: SaadSalmanAkram
💼 LinkedIn: Saad Salman Akram
⚡ Master MCTS & Build Smarter AI Systems! ⚡