Example #1
0
        public async static Task <MCTSNode> ProcessTree(MCTSNode aRootNode, IGameState aGameState, MCTSConfig aConfig)
        {
            bool            verbose  = aConfig.Verbose;
            Action <string> printfn  = aConfig.PrintFn;
            uint            maxIters = aConfig.MaxIterations;

            MCTSNode root = aRootNode;

            bool done = false;

            int iters = 0;

            do
            {
                MCTSNode node = root;

                // Select
                while (node.IsFullyExpandedAndNonterminal)
                {
                    node = node.SelectChildByPolicy();
                }

                // Expand
                IAction newAction = node.GetRandomUntriedAction();
                if (newAction != null)
                {
                    IGameState             state     = newAction.DoAction();
                    System.Func <MCTSNode> nodeConst = node.GenerateChildFunction(newAction, state);
                    node = node.AddChild(nodeConst);
                }

                // getting state reference for Rollout and Backprop
                IGameState finalState = node.NodeState.Clone() as IGameState;

                // Rollout
                finalState = await finalState.SimulateToTerminal();

                // Backpropagate
                while (node != null)
                {
                    node.UpdateResult(
                        finalState.GetResult(
                            node.ActorJustActed
                            )
                        );
                    node = node.Parent;
                }

                if (++iters >= maxIters)
                {
                    done = true;
                }
            }while (!done);

            MCTSNode bestChild = root.GetBestChild();

            if (verbose)
            {
                printfn(root.DisplayTree(0));

                printfn(root.DisplayMostVisistedChild());
            }

            return(bestChild);
        }
Example #2
0
        public async static Task <IAction> GetAction(MCTSNode aRootNode, IGameState aGameState, MCTSConfig aConfig)
        {
            MCTSNode processedTree = await ProcessTree(aRootNode, aGameState, aConfig);

            return(processedTree.IncomingAction);
        }