Skip to content

Instantly share code, notes, and snippets.

@inoryy
Last active January 25, 2019 10:29
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 inoryy/7e96f252e2924ddbcc976b458db9814c to your computer and use it in GitHub Desktop.
Save inoryy/7e96f252e2924ddbcc976b458db9814c to your computer and use it in GitHub Desktop.
Node* mcts(Node* root) {
save();
while (true) {
if (TIME >= TIME_LIMIT) break;
// select
Node* node = root;
while (!isTerminal() && node->isExpanded()) {
node = node->uctChild();
apply(node->action);
}
// expand
if (!isTerminal() && !node->isExpanded()) {
node = node->children[node->explored++];
apply(node->action);
node->init();
}
// simulate
while (!isTerminal()) {
auto acts = getActions();
apply(acts[rnd(acts.size())]);
}
// backprop
double score = getScore();
while (node) {
node->update(score);
node = node->parent;
}
load();
}
return root->bestChild();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment