Exemplo n.º 1
0
        // --------------------------------------------------------------------------------------------
        static void ProcessNode(PositionEvalCache cache, MCTSNode node, float weightEmpirical,
                                bool saveToCache, bool rewriteNodeInTree)
        {
            Span <MCTSNodeStructChild> children = node.Ref.Children;

            // TODO: optimize this away if saveToCache is false
            ushort[] probabilities = new ushort[node.NumPolicyMoves];
            ushort[] indices       = new ushort[node.NumPolicyMoves];

            // Compute empirical visit distribution
            float[] nodeFractions = new float[node.NumPolicyMoves];
            for (int i = 0; i < node.NumChildrenExpanded; i++)
            {
                nodeFractions[i] = (float)node.ChildAtIndex(i).N / (float)node.N;
            }

            // Determine P of first unexpanded node
            // We can't allow any child to have a new P less than this
            // since we need to keep them in order by P and the resorting logic below
            // can only operate over expanded nodes
            float minP = 0;

            if (node.NumChildrenExpanded < node.NumPolicyMoves)
            {
                minP = node.ChildAtIndexInfo(node.NumChildrenExpanded).p;
            }

            // Add each move to the policy vector with blend of prior and empirical values
            for (int i = 0; i < node.NumChildrenExpanded; i++)
            {
                (MCTSNode node, EncodedMove move, FP16 p)info = node.ChildAtIndexInfo(i);
                indices[i] = (ushort)info.move.IndexNeuralNet;

                float newValue = (1.0f - weightEmpirical) * info.p
                                 + weightEmpirical * nodeFractions[i];
                if (newValue < minP)
                {
                    newValue = minP;
                }
                probabilities[i] = CompressedPolicyVector.EncodedProbability(newValue);

                if (rewriteNodeInTree && weightEmpirical != 0)
                {
                    MCTSNodeStructChild thisChild = children[i];
                    if (thisChild.IsExpanded)
                    {
                        ref MCTSNodeStruct childNodeRef = ref thisChild.ChildRef;
                        thisChild.ChildRef.P = (FP16)newValue;
                    }
                    else
                    {
                        node.Ref.ChildAtIndex(i).SetUnexpandedPolicyValues(thisChild.Move, (FP16)newValue);
                    }
                }
            }
Exemplo n.º 2
0
        /// <summary>
        /// Implements the gathering of nodes up to speecified maximum depth and width.
        /// </summary>
        /// <param name="selectorID"></param>
        /// <param name="node"></param>
        /// <param name="maxNodes"></param>
        /// <param name="maxDepth"></param>
        /// <param name="width"></param>
        /// <param name="nodes"></param>
        /// <param name="pThreshold"></param>
        void GatherRootPreloadNodes(int selectorID, MCTSNode node, int maxNodes, int maxDepth, int width, List <MCTSNode> nodes, float pThreshold)
        {
            if (nodes.Count >= maxNodes)
            {
                return;
            }
            if (node.IsTranspositionLinked)
            {
                return;
            }

            // For each top child
            for (int i = 0; i < Math.Min(node.NumPolicyMoves, width); i++)
            {
                if (NumPreloaded >= MAX_PRELOAD_NODES_TOTAL)
                {
                    return;
                }

                (MCTSNode childNode, EncodedMove move, FP16 p) = node.ChildAtIndexInfo(i);

                if (float.IsNaN(pThreshold) || p >= pThreshold)
                {
                    // Add this node if not already extant
                    if (childNode == null)
                    {
                        // Create this child (as cache only) and update in flight
                        childNode            = node.CreateChild(i);
                        childNode.ActionType = MCTSNode.NodeActionType.CacheOnly;
                        if (selectorID == 0)
                        {
                            childNode.Ref.BackupIncrementInFlight(1, 0);
                        }
                        else
                        {
                            childNode.Ref.BackupIncrementInFlight(0, 1);
                        }

                        nodes.Add(childNode);
                        NumPreloaded++;

                        if (nodes.Count == maxNodes)
                        {
                            return;
                        }
                    }
                    else
                    {
                        node.Annotate();

                        if (node.Depth < maxDepth - 1 && childNode.N > 0)
                        {
                            // Recursively descend
                            Debug.Assert(childNode != null);
                            GatherRootPreloadNodes(selectorID, childNode, maxNodes, maxDepth, width, nodes, pThreshold);
                        }
                    }
                }
            }
        }
Exemplo n.º 3
0
        void OutputUCIInfo(MCTSManager manager, MCTSNode searchRootNode, bool isFinalInfo = false)
        {
            BestMoveInfo best = searchRootNode.BestMoveInfo(false);

            if (numPV == 1)
            {
                UCIWriteLine(UCIInfo.UCIInfoString(manager, searchRootNode, best?.BestMoveNode,
                                                   showWDL: showWDL, scoreAsQ: scoreAsQ));
            }
            else
            {
                // Send top move
                UCIWriteLine(UCIInfo.UCIInfoString(manager, searchRootNode, best.BestMoveNode, 1,
                                                   showWDL: showWDL, useParentN: !perPVCounters, scoreAsQ: scoreAsQ));

                // Send other moves visited
                MCTSNode[] sortedN      = searchRootNode.ChildrenSorted(s => - (float)s.N);
                int        multiPVIndex = 2;
                for (int i = 0; i < sortedN.Length && i < numPV; i++)
                {
                    if (!object.ReferenceEquals(sortedN[i], best.BestMoveNode))
                    {
                        UCIWriteLine(UCIInfo.UCIInfoString(manager, searchRootNode, sortedN[i], multiPVIndex,
                                                           showWDL: showWDL, useParentN: !perPVCounters, scoreAsQ: scoreAsQ));
                        multiPVIndex++;
                    }
                }

                // Finally show moves that had no visits
                float  elapsedTimeSeconds = (float)(DateTime.Now - manager.StartTimeThisSearch).TotalSeconds;
                string timeStr            = $"{ elapsedTimeSeconds * 1000.0f:F0}";
                for (int i = multiPVIndex - 1; i < searchRootNode.NumPolicyMoves; i++)
                {
                    (MCTSNode node, EncodedMove move, FP16 p)info = searchRootNode.ChildAtIndexInfo(i);
                    if (info.node == null)
                    {
                        bool        isWhite = searchRootNode.Annotation.Pos.MiscInfo.SideToMove == SideType.White;
                        EncodedMove moveCorrectPerspective = isWhite ? info.move : info.move.Flipped;
                        string      str = $"info depth 0 seldepth 0 time { timeStr } nodes 1 score cp 0 tbhits 0 "
                                          + $"multipv {multiPVIndex} pv {moveCorrectPerspective.AlgebraicStr} ";
                        UCIWriteLine(str);
                        multiPVIndex++;
                    }
                }
            }
            if (verboseMoveStats && (logLiveStats || isFinalInfo))
            {
                OutputVerboseMoveStats(CeresEngine.Search.SearchRootNode);
            }
        }
Exemplo n.º 4
0
        static MCTSNode DescendMovesToNode(MCTSNode rootNode, List <MGMove> moves)
        {
            MCTSNode node = rootNode;

            foreach (MGMove move in moves)
            {
                int?childIndex = node.Ref.ChildIndexWithMove(move);
                if (childIndex is null)
                {
                    throw new Exception($"Move  {move} not found at node {node}");
                }
                else
                {
                    (node, _, _) = node.ChildAtIndexInfo(childIndex.Value);
                }
            }
            return(node);
        }
Exemplo n.º 5
0
        public static List <LC0VerboseMoveStat> BuildStats(MCTSNode searchRootNode)
        {
            List <LC0VerboseMoveStat> stats = new List <LC0VerboseMoveStat>();

            BestMoveInfo best = searchRootNode.BestMoveInfo(false);

            // First process policy moves not yet expanded
            // starting from last one (lowest probability).
            for (int i = searchRootNode.NumPolicyMoves - 1; i >= 0; i--)
            {
                (MCTSNode node, EncodedMove move, FP16 p)info = searchRootNode.ChildAtIndexInfo(i);
                if (info.node == null)
                {
                    LC0VerboseMoveStat stat = BuildStatNotExpanded(searchRootNode, i);
                    stats.Add(stat);
                }
            }

            // Now process moves expanded in order of visit count.
            MCTSNode[] sortedN = searchRootNode.ChildrenSorted(s => (float)s.N + 0.0001f * s.P);
            foreach (MCTSNode node in sortedN)
            {
                if (!object.ReferenceEquals(node, best.BestMoveNode))
                {
                    stats.Add(BuildStatExpanded(node, false));
                }
            }

            // Save the best move for last.
            stats.Add(BuildStatExpanded(best.BestMoveNode, false));

            // Finally, output the search root node.
            stats.Add(BuildStatExpanded(searchRootNode, true));

            return(stats);
        }