Ejemplo n.º 1
0
        public SearchPrincipalVariation(MCTSNode root)
        {
            Nodes = new List <MCTSNode>();

            MCTSNode node = root;

            do
            {
                root.Context.Tree.Annotate(node);

                Nodes.Add(node);

                if (node.NumPolicyMoves > 0)
                {
                    if (node.IsRoot)
                    {
                        // Apply special logic at root for best move
                        node = node.BestMove(false);
                    }
                    else
                    {
                        // Non-root nodes follow visits with maximum number of visits.
                        node = node.ChildWithLargestValue(n => n.N);
                    }
                }
                else
                {
                    node = null;
                }
            } while (node != null);
        }
Ejemplo n.º 2
0
        public static void DumpPV(PositionWithHistory priorMoves, MCTSNode node,
                                  bool fullDetail, List <MGMove> subvariation = null)
        {
            if (subvariation != null)
            {
                List <MGMove> allMoves = new List <MGMove>();
                allMoves.AddRange(priorMoves.Moves);
                allMoves.AddRange(subvariation);
                DumpPV(new PositionWithHistory(priorMoves.InitialPosMG, allMoves),
                       DescendMovesToNode(node, subvariation), fullDetail);
            }

            Console.WriteLine();
            WriteHeaders(fullDetail);


            List <Position> seenPositions = new List <Position>();

            int CountDuplicatePos(Position pos)
            {
                int count = 0;

                foreach (Position priorPos in seenPositions)
                {
                    if (pos.EqualAsRepetition(priorPos))
                    {
                        count++;
                    }
                }

                return(count);
            }

            int depth = 0;

            while (true)
            {
                node.Context.Tree.Annotate(node);
                seenPositions.Add(node.Annotation.Pos);
                int countSeen = CountDuplicatePos(node.Annotation.Pos);

                DumpNodeStr(priorMoves, node, depth, countSeen, fullDetail);

                if (node.NumChildrenVisited == 0)
                {
                    return;
                }
                node = node.BestMove(false);

                depth++;
            }
        }
    /// <summary>
    /// Run a thousand simulations,
    /// then return the move of the child with the most simulations
    /// </summary>
    /// <returns></returns>
    public override Spot BestMove()
    {
        Stopwatch stopwatch    = new Stopwatch();
        float     seconds      = time;
        float     milliseconds = seconds * 1000;

        stopwatch.Start();
        while (stopwatch.ElapsedMilliseconds < milliseconds)
        {
            root.ChooseChild();
        }
        return(root.BestMove());
    }
Ejemplo n.º 4
0
        public static void DumpPV(MCTSNode node, bool fullDetail, TextWriter writer = null)
        {
            writer = writer ?? Console.Out;

            writer.WriteLine();
            WriteHeaders(fullDetail, writer);

            List <Position> seenPositions = new List <Position>();

            int CountDuplicatePos(Position pos)
            {
                int count = 0;

                foreach (Position priorPos in seenPositions)
                {
                    if (pos.EqualAsRepetition(priorPos))
                    {
                        count++;
                    }
                }

                return(count);
            }

            int      depth          = 0;
            MCTSNode searchRootNode = node;

            while (true)
            {
                node.Context.Tree.Annotate(node);
                seenPositions.Add(node.Annotation.Pos);
                int countSeen = CountDuplicatePos(node.Annotation.Pos);

                DumpNodeStr(searchRootNode, node, countSeen, fullDetail, writer);

                if (node.NumChildrenVisited == 0)
                {
                    return;
                }
                node = node.BestMove(false);

                depth++;
            }
        }