Skip to content

Instantly share code, notes, and snippets.

@mohsenheydari
Created February 4, 2019 14:33
Show Gist options
  • Save mohsenheydari/55a65083fec995ad21978fbe30a5e367 to your computer and use it in GitHub Desktop.
Save mohsenheydari/55a65083fec995ad21978fbe30a5e367 to your computer and use it in GitHub Desktop.
MCTS - function to find best next action for current state and player
findNextAction(board, player, iterations) {
let root = new Node();
root.state = new NodeState();
root.state.board = board;
root.state.player = 3 - player;//Opponent player is owner of root node
for (let i=0; i < iterations; i++) {
//1.Selection
let node = this.selectPromisingChild(root);
//2.Expansion
if(!node.state.isTerminal && !node.isFullyExpanded) {
//Single node expansion
node = node.expand();
}
//3.Simulation
let result = this.simulate(node);
//4.Update
this.backpropagate(node, result);
}
let winnerNode = root.childWithMostVisits;
return winnerNode.state.action;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment