Skip to content

Instantly share code, notes, and snippets.

@carlos-aguayo
Last active November 4, 2020 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlos-aguayo/681bcbc123c0de0417331521b326b0df to your computer and use it in GitHub Desktop.
Save carlos-aguayo/681bcbc123c0de0417331521b326b0df to your computer and use it in GitHub Desktop.
Run a Monte Carlo Tree Search (MCTS) Simulation
# https://github.com/suragnair/alpha-zero-general/blob/5156c7fd1d2f3e5fefe732a4b2e0ffc5b272f819/MCTS.py#L37-L48
for i in range(self.args.numMCTSSims): # self.args.numMCTSSims, the number of MCTS simulations to compute
self.search(canonicalBoard) # "search" is a MCTS simulations
s = self.game.stringRepresentation(canonicalBoard)
# Count how many times we have visited each node
counts = [self.Nsa[(s, a)] if (s, a) in self.Nsa else 0 for a in range(self.game.getActionSize())]
if temp == 0:
# Pick the node that was visited the most
bestAs = np.array(np.argwhere(counts == np.max(counts))).flatten()
bestA = np.random.choice(bestAs)
probs = [0] * len(counts)
probs[bestA] = 1
return probs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment