Пример #1
0
        public static PlayerTask GetBestAction(POGame.POGame game, int iterations)
        {
            TaskNode root = new TaskNode(null, null, game.getCopy());

            for (int i = 0; i < iterations; ++i)
            {
                try
                {
                    TaskNode node = root.SelectNode();
                    node = node.Expand();
                    int r = node.SimulateGames(5);
                    node.Backpropagate(r);
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e.Message);
                    Debug.WriteLine(e.StackTrace);
                }
            }

            TaskNode best = null;

            foreach (TaskNode child in root.Children)
            {
                //Console.WriteLine("visits: " + child.TotNumVisits);
                //Console.WriteLine("wins: " + child.Wins);

                if (best == null || child.TotNumVisits > best.TotNumVisits)
                {
                    best = child;
                }
            }

            //Console.WriteLine("best visits: " + best.TotNumVisits);
            //Console.WriteLine("best wins: " + best.Wins);

            return(best.Action);
        }
Пример #2
0
        /*
         * public static PlayerTask GetBestAction_iteration(POGame.POGame game, int iterations)
         * {
         *      TaskNode root = new TaskNode(null, null, game.getCopy());
         *
         *      for (int i = 0; i < iterations; ++i)
         *      {
         *              try
         *              {
         *                      TaskNode node = root.SelectNode();
         *                      node = node.Expand();
         *                      int r = node.SimulateGames(10);
         *                      node.Backpropagate(r);
         *              }
         *              catch (Exception e)
         *              {
         *                      Debug.WriteLine(e.Message);
         *                      Debug.WriteLine(e.StackTrace);
         *              }
         *      }
         *
         *      TaskNode best = null;
         *
         *      foreach (TaskNode child in root.Children)
         *      {
         *              //Console.WriteLine("visits: " + child.TotNumVisits);
         *              //Console.WriteLine("wins: " + child.Wins);
         *
         *              if (best == null || child.TotNumVisits > best.TotNumVisits)
         *              {
         *                      best = child;
         *              }
         *      }
         *
         *      //Console.WriteLine("best visits: " + best.TotNumVisits);
         *      //Console.WriteLine("best wins: " + best.Wins);
         *
         *      return best.Action;
         * }
         */

        public static PlayerTask GetBestAction_second(POGame.POGame game, double seconds)
        {
            DateTime start = DateTime.Now;
            TaskNode root  = new TaskNode(null, null, game.getCopy());

            int i = 0;

            while (true)
            {
                if (TimeUp(start, seconds - 0.1))
                {
                    break;
                }

                try
                {
                    TaskNode node = root.SelectNode();
                    if (TimeUp(start, seconds))
                    {
                        break;
                    }

                    node = node.Expand();
                    if (TimeUp(start, seconds))
                    {
                        break;
                    }

                    int r = node.SimulateGames(5);                    //預設5
                    if (TimeUp(start, seconds))
                    {
                        break;
                    }

                    node.Backpropagate(r);
                }
                catch (Exception e)
                {
                    //Debug.WriteLine(e.Message);
                    //Debug.WriteLine(e.StackTrace);
                }

                ++i;
            }

            TaskNode best = null;

            //Console.WriteLine($"Iterations: {i}, Time: " + (DateTime.Now-start).TotalMilliseconds + "ms");

            foreach (TaskNode child in root.Children)
            {
                //Console.WriteLine("visits: " + child.TotNumVisits);
                //Console.WriteLine("wins: " + child.Wins);

                if (best == null || child.TotNumVisits > best.TotNumVisits || (child.TotNumVisits == best.TotNumVisits && child.Wins > best.Wins))
                {
                    best = child;
                }
            }

            //Console.WriteLine("best visits: " + best.TotNumVisits);
            //Console.WriteLine("best wins: " + best.Wins);

            if (best == null)
            {
                //Debug.WriteLine("best == null");
                return(game.CurrentPlayer.Options()[0]);
            }

            //Console.WriteLine("best wins: " + best.Wins + " best visits: " + best.TotNumVisits);
            return(best.Action);
        }
Пример #3
0
        public static int simulation_num = 5; //模擬次數,預設5

        /*
         * public static PlayerTask GetBestAction_iteration(POGame.POGame game, int iterations)
         * {
         *      TaskNode root = new TaskNode(null, null, game.getCopy());
         *
         *      for (int i = 0; i < iterations; ++i)
         *      {
         *              try
         *              {
         *                      TaskNode node = root.SelectNode();
         *                      node = node.Expand();
         *                      int r = node.SimulateGames(simulation_num);
         *                      node.Backpropagate(r);
         *              }
         *              catch (Exception e)
         *              {
         *                      Debug.WriteLine(e.Message);
         *                      Debug.WriteLine(e.StackTrace);
         *              }
         *      }
         *
         *      TaskNode best = null;
         *
         *      foreach (TaskNode child in root.Children)
         *      {
         *              Console.WriteLine("visits: " + child.TotNumVisits);
         *              Console.WriteLine("wins: " + child.Wins);
         *
         *              if (best == null || child.TotNumVisits > best.TotNumVisits)
         *              {
         *                      best = child;
         *              }
         *      }
         *
         *      //Console.WriteLine("best visits: " + best.TotNumVisits);
         *      //Console.WriteLine("best wins: " + best.Wins);
         *
         *      return best.Action;
         * }
         */

        public static PlayerTask GetBestAction_second(POGame.POGame game, double seconds)
        {
            DateTime start = DateTime.Now;
            TaskNode root  = new TaskNode(null, null, game.getCopy());

            int i = 0;



            while (true)
            {
                if (TimeUp(start, seconds - 0.1))
                {
                    break;
                }
                //Board_Analysis(game);//場面分析函式
                Controller my = game.CurrentPlayer;
                Controller op = game.CurrentOpponent;
                //分配數值
                num_my_board       = my.BoardZone.Count;
                num_op_board       = op.BoardZone.Count;
                num_my_hand        = my.HandZone.Count;
                num_op_hand        = op.HandZone.Count;
                num_my_hero        = my.Hero.Health;
                num_op_hero        = op.Hero.Health;
                num_my_deck        = my.DeckZone.Count;
                num_op_deck        = op.DeckZone.Count;
                num_remaining_mana = my.RemainingMana;

                TreeScore.treescore.tree_score.tree_node = SabberStoneCoreAi.src.Program.main.node_string;


                c = Math.Round(TreeScore.treescore.tree_score.Node_Evaluation(num_my_board, num_op_board, num_my_hand, num_op_hand,
                                                                              num_my_hero, num_op_hero, num_my_deck, num_op_deck, num_remaining_mana), 2, MidpointRounding.AwayFromZero);
                //Console.WriteLine(num_my_board+"  "+ num_op_board + "  " + num_my_hand + "  " + num_op_hand + "  " +
                //	num_my_hero + "  " + num_op_hero + "  " + num_my_deck + "  " + num_op_deck + "  " + num_remaining_mana);

                if (c < 0)
                {
                    c = 0;
                }
                //Console.WriteLine("c=" + c);
                //simulation_num = (int) Math.Round(TreeScore.treescore.tree_score.Node_Evaluation(num_my_board, num_op_board, num_my_hand, num_op_hand,
                //	num_my_hero, num_op_hero, num_my_deck, num_op_deck), 0, MidpointRounding.AwayFromZero);

                //if (simulation_num < 0)
                //{
                //	simulation_num = 1;
                //}

                //Console.WriteLine("模擬次數為:" + simulation_num);

                try
                {
                    TaskNode node = root.SelectNode();
                    if (TimeUp(start, seconds))
                    {
                        break;
                    }

                    node = node.Expand();
                    if (TimeUp(start, seconds))
                    {
                        break;
                    }

                    int r = node.SimulateGames(simulation_num);                    //預設5
                    if (TimeUp(start, seconds))
                    {
                        break;
                    }

                    node.Backpropagate(r);
                }
                catch (Exception e)
                {
                    //Debug.WriteLine(e.Message);
                    //Debug.WriteLine(e.StackTrace);
                }

                ++i;
            }

            TaskNode best = null;

            //Console.WriteLine($"Iterations: {i}, Time: " + (DateTime.Now-start).TotalMilliseconds + "ms");

            foreach (TaskNode child in root.Children)
            {
                //Console.WriteLine("visits: " + child.TotNumVisits);//刪除註解
                //Console.WriteLine("wins: " + child.Wins);

                if (best == null || child.TotNumVisits > best.TotNumVisits || (child.TotNumVisits == best.TotNumVisits && child.Wins > best.Wins))
                {
                    best = child;
                }
            }

            //Console.WriteLine("best visits: " + best.TotNumVisits);
            //Console.WriteLine("best wins: " + best.Wins);

            if (best == null)
            {
                //Debug.WriteLine("best == null");
                return(game.CurrentPlayer.Options()[0]);
            }

            //Console.WriteLine("best wins: " + best.Wins + " best visits: " + best.TotNumVisits);
            return(best.Action);
        }