private Node SelectPromisingNode(Node rootNode) { Node node = rootNode; while (node.ChildArray.Count() != 0) { node = UCT.FindBestNodeWithUCT(node); } return(node); }
public override String ToString() { List <int> lastMoves = this.State.Game.LastMoves; String rc = "Move: " + ((lastMoves.Count == 0) ? "-" : lastMoves[lastMoves.Count - 1].ToString()) + " | "; rc += "Score: " + this.State.WinScore + " | "; rc += "VisitCount: " + this.State.VisitCount + " | "; rc += "UCT:" + UCT.UCT_Value(this).ToString("0.000"); return(rc); }
public Game FindNextMove(Game game) { Game winGame = CheckWinMove(game); if (winGame != null) { Console.WriteLine(winGame.GameState); return(winGame); } Console.WriteLine("Running mcts..."); Tree tree = new Tree(); Node rootNode = tree.Root; rootNode.State.Game = game; int count = 0; while (count < iterations) { count++; // Phase 1 - Selection Node promisingNode = SelectPromisingNode(rootNode); // Phase 2 - Expansion if (promisingNode.State.Game.GameState.Done == false && (promisingNode == tree.Root || promisingNode.State.VisitCount >= 10)) { ExpandNode(promisingNode); } // Phase 3 - Simulation Node nodeToExplore = promisingNode; if (promisingNode.ChildArray.Count() > 0) { Node answerNode = promisingNode.ChildArray.Where(m => m.State.Game.GameState.Done).FirstOrDefault(); if (answerNode != null) { nodeToExplore = answerNode; } else { nodeToExplore = promisingNode.RandomChildNode; } } int playoutResult = SimulateRandomPlayout(nodeToExplore, rootNode); // Phase 4 - Update BackPropagation(nodeToExplore, playoutResult); if (count % (iterations * 0.2) == 0) { Console.WriteLine("Iterations : " + count + " out of " + iterations); } } Node winnerNode = rootNode.ChildWithMaxScore; List <Node> resultNodes = rootNode.ChildArray.OrderByDescending(m => UCT.UCT_Value(m)).ToList(); for (int i = 0; i <= resultNodes.Count - 1; i++) { Console.WriteLine(resultNodes[i]); Debug.WriteLine(resultNodes[i]); } Console.WriteLine(winnerNode.State.Game.GameState); Debug.WriteLine(winnerNode.State.Game.GameState); Debug.WriteLine("Last moves: " + winnerNode.State.Game); return(winnerNode.State.Game); }