Skip to content

Instantly share code, notes, and snippets.

@carlos-aguayo
Last active November 6, 2020 01:47
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/994c93ef6e818e170c7bc1a60273993f to your computer and use it in GitHub Desktop.
Save carlos-aguayo/994c93ef6e818e170c7bc1a60273993f to your computer and use it in GitHub Desktop.
Part A - Select the node with the highest Upper Confidence Bound (UCB)
# https://github.com/suragnair/alpha-zero-general/blob/5156c7fd1d2f3e5fefe732a4b2e0ffc5b272f819/MCTS.py#L105-L121
cur_best = -float('inf')
best_act = -1
# pick the action with the highest upper confidence bound
for a in range(self.game.getActionSize()):
if valids[a]:
if (s, a) in self.Qsa:
u = self.Qsa[(s, a)] + self.args.cpuct * self.Ps[s][a] * math.sqrt(self.Ns[s]) / (
1 + self.Nsa[(s, a)])
else:
u = self.args.cpuct * self.Ps[s][a] * math.sqrt(self.Ns[s] + EPS) # Q = 0 ?
if u > cur_best:
cur_best = u
best_act = a
a = best_act
next_s, next_player = self.game.getNextState(canonicalBoard, 1, a)
next_s = self.game.getCanonicalForm(next_s, next_player)
# Recursively visit the node
v = self.search(next_s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment