Monte Carlo Tree Search Explained & How To Implement [With Code]

by | Sep 8, 2025 | Data Science

What is Monte Carlo Tree Search?

Monte Carlo Tree Search (MCTS) is a decision-making algorithm that helps an agent figure out the best action when the possible outcomes are too vast to calculate exhaustively. Instead of trying to explore every possible move and consequence—which quickly becomes impossible in complex problems like Go, chess, or real-world planning—MCTS uses clever sampling to approximate which choices are most promising.

At its core, MCTS works by building a search tree:

  • The root of the tree is the current situation or state.
  • Each branch represents a possible action.
  • The algorithm selectively expands this tree, focusing more on actions that appear stronger while still testing out new ones.
Monte Carlo Tree Search

The “Monte Carlo” part comes from using random simulations (like rolling dice many times) to estimate the value of a move. Instead of computing exact results, the algorithm plays out many “what-if” scenarios to see how good or bad a move might be on average.

Think of it as a smart explorer in an unfamiliar city:

  • Instead of visiting every street, they wander randomly, taking notes.
  • Over time, they learn which paths often lead to exciting discoveries.
  • By balancing curiosity (exploring new streets) and experience (returning to known good areas), they gradually find the best routes.

This blend of random exploration and guided improvement is what makes MCTS powerful—it can handle enormous decision spaces without needing perfect information or handcrafted rules.

The Four Steps of Monte Carlo Tree Search

Monte Carlo Tree Search builds its power through a repeating cycle of four key steps. Each iteration makes the search tree a little smarter, gradually zeroing in on the best possible moves.

1. Selection

Starting from the root (the current state), the algorithm walks down the existing tree. At each node, it chooses the next branch using a rule that balances:

  • Exploitation (choosing moves that have performed well so far) and
  • Exploration (trying less-visited moves that might turn out better).
  • The most common rule is the Upper Confidence Bound (UCB1), which adds a small “bonus” for less-explored nodes, encouraging diversity.

2. Expansion

Once the algorithm reaches a node that has unexplored moves, it expands the tree by adding a new child node. This represents a possible new state that hasn’t been tried yet.

3. Simulation (Rollout)

From this new state, the algorithm runs a random simulation (or rollout) until the end of the game or until some stopping condition. The outcome—win, loss, or score—is recorded.

The randomness here is key: instead of planning every possible future, the algorithm “samples” outcomes to get an estimate.

4. Backpropagation

Finally, the result of the simulation is propagated back up the tree. Each node along the path updates its statistics (e.g., number of visits, average reward). This way, the tree as a whole learns which branches tend to lead to good outcomes.

Putting it all together:

These four steps—Selection → Expansion → Simulation → Backpropagation—are repeated thousands or even millions of times. Each cycle makes the search tree more accurate, steadily guiding the algorithm toward the most promising moves.

the four steps of the monte carlo tree search

Balancing Exploration and Exploitation

One of the key challenges in decision-making algorithms like MCTS is deciding between two competing goals:

  1. Exploitation – Choosing moves that have performed well in past simulations.
  2. Exploration – Trying less-visited moves that might turn out to be even better.

If the algorithm only exploited known good moves, it could miss an even better strategy hidden deeper in the tree. On the other hand, if it explored too much, it would waste time on bad options. MCTS solves this dilemma with a clever balancing act.

The Upper Confidence Bound (UCB1) Formula

MCTS uses the UCB1 formula to score each node and decide which branch to follow during selection:

UCB1

Where:

  • Average reward = how well this move has performed so far
  • N = total visits to the parent node
  • n = number of times this child node has been visited
  • C = exploration parameter (higher values favour exploration)

The first term encourages exploitation, while the second term encourages exploration. Nodes with fewer visits get a higher exploration bonus, making them more likely to be tried.

Intuitive Analogy

Imagine you’re picking a restaurant:

  • You have a favourite, you know, it’s good (exploitation).
  • There’s also a new place you haven’t tried yet (exploration).
  • You might try the new place occasionally, but mostly stick to your favourite. Over time, this strategy helps you discover the best option without risking too much.

By carefully balancing exploration and exploitation, MCTS gradually builds a more accurate picture of which moves are genuinely the best, even in vast and complex decision spaces.

Strengths of Monte Carlo Tree Search

Monte Carlo Tree Search has become a go-to algorithm for AI decision-making because of several key strengths that set it apart from traditional search methods.

1. Works Well with Little Domain Knowledge

Unlike classical AI algorithms that often require handcrafted rules or heuristics, MCTS can operate with minimal prior knowledge about the problem. It learns which moves are promising purely through simulations.

2. Handles Large and Complex Decision Spaces

In games like Go or real-world planning problems, the number of possible states grows exponentially. MCTS doesn’t need to explore all possibilities; it selectively samples the most promising moves, making it scalable.

3. Balances Exploration and Exploitation Naturally

Through the UCB1 formula and its iterative process, MCTS automatically balances trying new strategies (exploration) with sticking to known good moves (exploitation), which helps it avoid getting stuck in suboptimal choices.

4. Improves with More Computation

MCTS is an anytime algorithm: the more iterations or simulations you run, the better the estimates of move quality become. This makes it flexible for both real-time applications and offline analysis.

5. Versatile Across Domains

MCTS isn’t limited to board games. Its principles apply to:

  • Video games and AI opponents
  • Robotics and path planning
  • Scheduling and logistics
  • Decision-making under uncertainty

In short, MCTS shines in situations where the problem is too complex for exhaustive search, but you can still learn by sampling outcomes intelligently.

Limitations of Monte Carlo Tree Search

While Monte Carlo Tree Search is powerful, it’s not a silver bullet. There are several challenges and limitations to be aware of:

1. Computationally Expensive for Large or Real-Time Problems

MCTS relies on running many simulations to make accurate decisions. For vast decision spaces or situations requiring instant responses, the computation can become a bottleneck.

2. Quality of Simulations Matters

The algorithm often uses random simulations (rollouts) to estimate the value of moves. If the random playouts are too simplistic or unrealistic, the evaluations can be inaccurate, leading to poor decisions. Adding domain knowledge or heuristics can improve this, but then the algorithm is no longer completely domain-agnostic.

3. Memory Usage Can Grow Quickly

As MCTS builds a search tree, it stores statistics for each node (number of visits, average rewards, etc.). For very deep or wide trees, this can consume significant memory.

4. Performance Sensitive to Parameters

Parameters like the exploration constant in the UCB1 formula can significantly affect performance. Choosing the right balance between exploration and exploitation often requires experimentation.

5. Not Always Optimal

MCTS provides approximate solutions rather than guaranteed optimal ones. In domains where exact solutions are required, other algorithms might be more appropriate.

Despite these limitations, MCTS remains one of the most flexible and powerful methods for decision-making in uncertain and complex environments. Its strengths often outweigh the drawbacks, especially when combined with enhancements like heuristics, parallelisation, or neural network guidance.

Applications of Monte Carlo Tree Search Beyond Games

While MCTS gained fame for its success in board games like Go and Chess, its underlying principles make it valuable in a wide range of real-world problems.

1. Board Games and Video Games

  • Go, Chess, and Shogi: MCTS allows AI to explore possible moves efficiently, even in games with enormous branching factors.
  • Video games: AI agents use MCTS to plan actions, optimise strategies, and handle dynamic environments.

2. Robotics and Path Planning

  • MCTS helps robots navigate complex spaces, avoiding obstacles while finding efficient paths.
  • It can be applied to motion planning, multi-robot coordination, and autonomous vehicle decision-making.

3. Operations Research and Scheduling

Tasks like job scheduling, resource allocation, and logistics optimisation benefit from MCTS’s ability to explore many options without exhaustive enumeration.

4. Healthcare and Medical Decision-Making

  • MCTS can help in planning treatment strategies by simulating potential outcomes of different interventions.
  • It’s helpful in personalised medicine, where the decision space is vast and uncertain.

5. Defence and Strategic Planning

  • In military simulations and planning, MCTS evaluates possible courses of action under uncertainty.
  • It can optimise decision-making in resource deployment, mission planning, and risk assessment.

6. Financial Modelling and Investment

Portfolio management and trading strategies can leverage MCTS to simulate different market scenarios and evaluate decisions under uncertainty.

MCTS excels in complex, uncertain decision spaces that are too large to analyse exhaustively. Its ability to learn from simulations rather than requiring complete knowledge makes it highly versatile across domains.

Monte Carlo Tree Search Variants and Improvements

Over the years, researchers have developed several variants and enhancements to make Monte Carlo Tree Search more efficient, accurate, and applicable to complex problems.

1. RAVE (Rapid Action Value Estimation)

RAVE speeds up learning by sharing information between similar moves across different parts of the tree.

This reduces the number of simulations needed for the algorithm to identify promising actions, especially in large games like Go.

2. Parallel MCTS

By running multiple simulations simultaneously on different processors or threads, parallel MCTS significantly reduces computation time.

Care must be taken to manage shared tree structures and avoid conflicts, but it allows real-time performance in demanding applications.

3. Progressive Bias / Heuristics

Incorporating domain-specific knowledge or heuristics can guide the algorithm to more promising areas of the search space.

For example, in games, moves that are known to be strong can be prioritised during selection.

4. Integration with Deep Learning (AlphaZero Approach)

Modern AI systems like AlphaZero combine MCTS with neural networks:

  • Policy networks suggest promising moves.
  • Value networks estimate the expected outcome of a state.

This combination dramatically improves both efficiency and decision quality.

5. Anytime and Adaptive Variants

Some MCTS implementations dynamically adjust exploration parameters or simulation depth based on available computation time.

This flexibility is useful in real-time environments where decisions must be made quickly.

These enhancements address many of the limitations of vanilla MCTS, such as slow convergence, excessive randomness, and high computational cost. By adapting MCTS with parallelisation, heuristics, or deep learning, it becomes even more powerful and widely applicable.

Getting Started with Monte Carlo Tree Search Yourself

Monte Carlo Tree Search is surprisingly approachable for beginners. With just a basic understanding of programming, you can implement a simple MCTS agent and experiment with games or decision problems.

1. Simple Python Pseudo-Code

class Node:
    def __init__(self, state, parent=None):
        self.state = state
        self.parent = parent
        self.children = []
        self.visits = 0
        self.value = 0

def MCTS(root, iterations):
    for _ in range(iterations):
        node = select(root)
        if not node.is_terminal():
            node = expand(node)
        reward = simulate(node)
        backpropagate(node, reward)
    return best_child(root)

# Core steps: select → expand → simulate → backpropagate

This pseudo-code captures the essence of MCTS:

  • select: traverse the tree using UCB1 to balance exploration and exploitation
  • expand: add new possible moves to the tree
  • simulate: run random playouts to estimate outcomes
  • backpropagate: update statistics along the path

2. Beginner-Friendly Projects

Start small to understand how MCTS works:

  • Tic-Tac-Toe: Classic, easy to implement, and fast to simulate.
  • Connect Four: A slightly larger game that demonstrates deeper search trees.
  • Simple Grid Maze Navigation: Use MCTS for pathfinding in a 2D maze.

3. Libraries and Resources

  • Python libraries: anytree, pygame for visualisation, or custom MCTS scripts.
  • Tutorials: Online resources often include step-by-step MCTS implementations for games.
  • Further reading: Papers like AlphaGo and AlphaZero for advanced MCTS integration with deep learning.

4. Tips for Experimenting

  • Start with a small number of simulations, then gradually increase.
  • Visualise the tree to understand which moves are explored more often.
  • Experiment with the exploration parameter in UCB1 to see its effect.

Hands-on experimentation is the best way to internalise how MCTS balances exploration, exploitation, and learning from simulations.

Conclusion

Monte Carlo Tree Search is a powerful and versatile algorithm that has transformed how AI tackles complex decision-making problems. By intelligently balancing exploration and exploitation and learning from repeated simulations, MCTS can find strong strategies in enormous decision spaces where traditional methods struggle.

From board games like Go and Chess to robotics, logistics, healthcare, and finance, MCTS demonstrates remarkable flexibility. Its ability to improve with more computation and to incorporate enhancements like heuristics, parallelisation, or deep learning makes it a cornerstone of modern AI research.

Key Takeaways:

  • MCTS is intuitive yet powerful, using random simulations to guide decision-making.
  • It shines in situations with large, uncertain, or complex decision spaces.
  • Its flexibility allows it to be applied across domains, from games to real-world planning problems.

For readers looking to dive in, start small with a simple game, experiment with different parameters, and explore the many improvements and variants of MCTS. With hands-on practice, you’ll see firsthand how this algorithm can navigate uncertainty and make smarter decisions—one simulation at a time.

About the Author

Neri Van Otten

Neri Van Otten

Neri Van Otten is the founder of Spot Intelligence, a machine learning engineer with over 12 years of experience specialising in Natural Language Processing (NLP) and deep learning innovation. Dedicated to making your projects succeed.

Recent Articles

the four steps of the monte carlo tree search

Monte Carlo Tree Search Explained & How To Implement [With Code]

What is Monte Carlo Tree Search? Monte Carlo Tree Search (MCTS) is a decision-making algorithm that helps an agent figure out the best action when the possible outcomes...

fibonacci numbers

Dynamic Programming Explained & How To Tutorial In Python

What is Dynamic Programming? Dynamic Programming (DP) is a powerful algorithmic technique used to solve complex problems by breaking them down into simpler, overlapping...

Temporal Difference Learning Made Simple With Example & Alternatives

What is Temporal Difference Learning? Temporal Difference (TD) Learning is a core idea in reinforcement learning (RL), where an agent learns to make better decisions by...

interdependent variables

Understanding Interdependent Variables: The Hidden Web Of Cause And Effect

Have you ever wondered why raising interest rates slows down inflation, or why cutting down forests affects rainfall patterns? These everyday phenomena are driven by a...

Q-learning frozen lake problem

Deep Deterministic Policy Gradient Made Simple & How To Tutorial In Python

Introduction Reinforcement Learning (RL) has seen explosive growth in recent years, powering breakthroughs in robotics, game playing, and autonomous control. While...

multi-agent reinforcement learning marl

Multi-Agent Reinforcement Learning Made Simple, Top Approaches & 9 Tools

Introduction Imagine a group of robots cleaning a warehouse, a swarm of drones surveying a disaster zone, or autonomous cars navigating through city traffic. In each of...

viterbi algorithm example

Viterbi Algorithm Made Simple [How To & Worked-Out Examples]

Introduction Imagine trying to understand what someone said over a noisy phone call or deciphering a DNA sequence from partial biological data. In both cases, you're...

link prediction in graphical neural networks

Structured Prediction In Machine Learning: What Is It & How To Do It

What is Structured Prediction? In traditional machine learning tasks like classification or regression a model predicts a single label or value for each input. For...

q-learning explained witha a mouse navigating a maze and updating it's internal staate

Policy Gradient [Reinforcement Learning] Made Simple In An Elaborate Guide

Introduction Reinforcement Learning (RL) is a powerful framework that enables agents to learn optimal behaviours through interaction with an environment. From mastering...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

nlp trends

2025 NLP Expert Trend Predictions

Get a FREE PDF with expert predictions for 2025. How will natural language processing (NLP) impact businesses? What can we expect from the state-of-the-art models?

Find out this and more by subscribing* to our NLP newsletter.

You have Successfully Subscribed!