示例#1
0
        static void SearchBias(int visits, int _2048Visits, int k = 10)
        {
            double           c     = Math.Pow(2, k);
            var              _lock = new Object();
            MCTS <RangeNode> root  = null;

            root = new MCTS <RangeNode>(
                new RangeNode(
                    -0.75,
                    0.25,
                    (x) =>
            {
                var biasExponent = Math.Pow(2, x * c);
                var _2048Root    = new MCTS <GameNode>(
                    new GameNode(
                        new _2048Model()
                        ),
                    biasExponent
                    );
                while (_2048Root.TryMove(_2048Visits, out _2048Root))
                {
                    ;
                }
                lock (_lock)
                {
                    Console.WriteLine(string.Format("biasExponent: {0}", biasExponent));
                    Console.WriteLine(string.Format("score: {0}", _2048Root.Node.Game.Score));
                    Console.Write(((_2048Model)_2048Root.Node.Game).Matrix.ToDebugString(5));
                    Console.WriteLine(string.Format("best biasExponent: {0} score: {1}", Math.Pow(2, c * root.GetBestLeaf().Node.Middle), root.GetBestLeaf().WinRate));
                }
                return(_2048Root.Node.Game.Score);
            }
                    )
                );
            root.Execute(visits);
            var bestBiasExponent = Math.Pow(2, c * root.GetBestLeaf().Node.Middle);
            var statistics       = new Statistics.Statistics();

            Parallel.For(0, visits / 4, () => new Statistics.Statistics(),
                         (i, loop, _statistics) =>
            {
                var _2048Root2 = new MCTS <GameNode>(
                    new GameNode(
                        new _2048Model()
                        ),
                    bestBiasExponent
                    );
                while (_2048Root2.TryMove(_2048Visits, out _2048Root2))
                {
                    ;
                }
                _statistics.Add(_2048Root2.Node.Game.Score);
                Console.WriteLine(string.Format("best biasExponent: {0} score: {1} (s: {2}, c: {3})", bestBiasExponent, _statistics.Mean, _statistics.SampleStandardDeviation, _statistics.Count));
                return(_statistics);
            },
                         (_statistics) => statistics.Add(_statistics)
                         );
            Console.WriteLine(string.Format("best biasExponent: {0} score: {1} (s: {2}, c: {3})", bestBiasExponent, statistics.Mean, statistics.SampleStandardDeviation, statistics.Count));
        }
示例#2
0
 public static bool TryMove(this MCTS <GameNode> root, int iterations, out MCTS <GameNode> newRoot, bool preferAutoMove = true)
 {
     if (preferAutoMove && root.TryAutoMove(out newRoot))
     {
         return(true);
     }
     else
     {
         root.Execute(iterations);
         if (root.GetBestMove() < 0)
         {
             newRoot = root;
             return(false);
         }
         else
         {
             newRoot = root.GetChildByMove(root.GetBestMove());
             return(true);
         }
     }
 }