Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 73

Monte Carlo Tree Search

Search ahead by selecting, expanding, simulating, and backing up values, guided by an exploration bonus. One example is on this page.

Intermediate12 min
Monte Carlo Tree Search MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

Monte Carlo tree search grows a tree of future actions. Selection walks toward actions with a high value plus an exploration bonus. A new leaf is expanded, a rollout estimates it, and the result is backed up to the root. The bonus shrinks as a child is visited more often.

Example

python
import math

parent_visits = 10
child_visits = 2
value = 0.6
bonus = 1.4 * math.sqrt(math.log(parent_visits) / child_visits)
print(f"ucb: {value + bonus:.2f}")

Output:

text
ucb: 2.10

When to use it

  • Use it when you can simulate future moves, as in games.
  • The score adds the child's average value to a bonus that favors less-visited moves.
  • More simulations sharpen the root action. They do not replace a learned value in huge games.

Loading playground…