Exemplo n.º 1
0
        private void StartButton_Click(object sender, EventArgs e)
        {
            ab        = new AlphaBeta();
            uct       = new UCT(Math.Sqrt(2), 123, 100000);
            puct      = new PUCT(Math.Sqrt(2), 123, 100000);
            ucb1tuned = new UCB1TUNED(1, 123, 100000);
            if (playerRB.Checked)
            {
                vs = VS.Player;
            }
            else if (abRB.Checked)
            {
                vs = VS.AlphaBeta;
            }
            else
            {
                vs = VS.MCTS;
            }

            if (puctRB.Checked)
            {
                algorithm = puct;
            }
            if (uctRB.Checked)
            {
                algorithm = uct;
            }
            if (tunedRB.Checked)
            {
                algorithm = ucb1tuned;
            }

            yourTurn    = youStartBox.Checked;
            board       = new Board();
            firstPlayer = play = start = true;
            MainPanel.Invalidate();
            if (!yourTurn)
            {
                PlayMove();
                CheckResult();
                firstPlayer = !firstPlayer;
                MainPanel.Invalidate();
            }
        }
Exemplo n.º 2
0
        static void UCTvsTUNED()
        {
            var gameCount    = 30;
            var rolloutCount = 25000;

            var ResultWinDict = new Dictionary <string, int>()
            {
                { "UCTfirst", 0 },
                { "UCTsecond", 0 },
                { "TUNEDfirst", 0 },
                { "TUNEDsecond", 0 }
            };

            for (int i = 0; i < gameCount; i++)
            {
                IAlgorithmInterface firstPlayer;
                IAlgorithmInterface secondPlayer;
                var uct        = new UCT(Math.Sqrt(2), i, rolloutCount);
                var tuned      = new UCB1TUNED(Math.Sqrt(2), i, rolloutCount);
                var tunedFirst = false;
                if (i < gameCount / 2)
                {
                    tunedFirst   = true;
                    firstPlayer  = tuned;
                    secondPlayer = uct;
                }
                else
                {
                    tunedFirst   = false;
                    firstPlayer  = uct;
                    secondPlayer = tuned;
                }
                var board        = new Board();
                var activePlayer = firstPlayer;
                while (board.Result == Result.None)
                {
                    board.PutToken(activePlayer.SelectMove(board));
                    if (activePlayer == firstPlayer)
                    {
                        activePlayer = secondPlayer;
                    }
                    else
                    {
                        activePlayer = firstPlayer;
                    }
                }


                if (board.Result == Result.FirstWon)
                {
                    if (tunedFirst)
                    {
                        ResultWinDict["TUNEDfirst"]++;
                    }
                    else
                    {
                        ResultWinDict["UCTfirst"]++;
                    }
                }
                else
                {
                    if (tunedFirst)
                    {
                        ResultWinDict["UCTsecond"]++;
                    }
                    else
                    {
                        ResultWinDict["TUNEDsecond"]++;
                    }
                }
            }
            Console.WriteLine("UCT vs UCB1TUNED");
            foreach (var kvp in ResultWinDict)
            {
                Console.WriteLine($"{kvp.Key}: {kvp.Value}");
            }
            Console.WriteLine();
        }
Exemplo n.º 3
0
        static void IsBetterMoveChoiceImpactful()
        {
            var gameCount    = 30;
            var rolloutCount = 25000;

            var ResultWinDict = new Dictionary <string, int>()
            {
                { "UCTfirst", 0 },
                { "UCTsecond", 0 },
                { "OneAheadfirst", 0 },
                { "OneAheadsecond", 0 }
            };

            for (int i = 0; i < gameCount; i++)
            {
                IAlgorithmInterface firstPlayer;
                IAlgorithmInterface secondPlayer;
                var uct           = new UCB1TUNED(1, i, 5 * rolloutCount, MoveEvaluation.Random);
                var oneAhead      = new UCB1TUNED(1, i, rolloutCount, MoveEvaluation.OneAhead);
                var oneAheadFirst = false;
                if (i < gameCount / 2)
                {
                    oneAheadFirst = true;
                    firstPlayer   = oneAhead;
                    secondPlayer  = uct;
                }
                else
                {
                    oneAheadFirst = false;
                    firstPlayer   = uct;
                    secondPlayer  = oneAhead;
                }
                var board        = new Board();
                var activePlayer = firstPlayer;
                while (board.Result == Result.None)
                {
                    board.PutToken(activePlayer.SelectMove(board));
                    if (activePlayer == firstPlayer)
                    {
                        activePlayer = secondPlayer;
                    }
                    else
                    {
                        activePlayer = firstPlayer;
                    }
                }


                if (board.Result == Result.FirstWon)
                {
                    if (oneAheadFirst)
                    {
                        ResultWinDict["OneAheadfirst"]++;
                    }
                    else
                    {
                        ResultWinDict["UCTfirst"]++;
                    }
                }
                else
                {
                    if (oneAheadFirst)
                    {
                        ResultWinDict["UCTsecond"]++;
                    }
                    else
                    {
                        ResultWinDict["OneAheadsecond"]++;
                    }
                }
            }
            Console.WriteLine("UCT random vs UCT OneAhead");
            foreach (var kvp in ResultWinDict)
            {
                Console.WriteLine($"{kvp.Key}: {kvp.Value}");
            }
            Console.WriteLine();
        }
Exemplo n.º 4
0
        static void TuneParameterUCT()
        {
            var gameCount    = 30;
            var abDepth      = 5;
            var rolloutCount = 200000;
            var paramList    = new List <double>
            {
                1
            };

            foreach (var param in paramList)
            {
                var ResultWinDict = new Dictionary <string, int>()
                {
                    { "UCTfirst", 0 },
                    { "UCTsecond", 0 },
                    { "ABfirst", 0 },
                    { "ABsecond", 0 }
                };

                var ResultDrawDict = new Dictionary <string, int>()
                {
                    { "UCTfirstDraw", 0 },
                    { "ABfirstDraw", 0 },
                };

                for (int i = 0; i < gameCount; i++)
                {
                    IAlgorithmInterface firstPlayer;
                    IAlgorithmInterface secondPlayer;
                    var uct     = new UCB1TUNED(param, i, rolloutCount);
                    var abFirst = false;
                    if (i < gameCount / 2)
                    {
                        abFirst = true;
                        var ab = new AlphaBeta(abFirst, abDepth);
                        firstPlayer  = ab;
                        secondPlayer = uct;
                    }
                    else
                    {
                        abFirst = false;
                        var ab = new AlphaBeta(abFirst, abDepth);
                        firstPlayer  = uct;
                        secondPlayer = ab;
                    }
                    var board        = new Board();
                    var activePlayer = firstPlayer;
                    while (board.Result == Result.None)
                    {
                        board.PutToken(activePlayer.SelectMove(board));
                        if (activePlayer == firstPlayer)
                        {
                            activePlayer = secondPlayer;
                        }
                        else
                        {
                            activePlayer = firstPlayer;
                        }
                    }

                    if (board.Result == Result.Draw)
                    {
                    }
                    else
                    {
                        if (board.Result == Result.FirstWon)
                        {
                            if (abFirst)
                            {
                                ResultWinDict["ABfirst"]++;
                            }
                            else
                            {
                                ResultWinDict["UCTfirst"]++;
                            }
                        }
                        else
                        {
                            if (abFirst)
                            {
                                ResultWinDict["UCTsecond"]++;
                            }
                            else
                            {
                                ResultWinDict["ABsecond"]++;
                            }
                        }
                    }
                }
                Console.WriteLine($"Param {param}:");
                foreach (var kvp in ResultWinDict)
                {
                    Console.WriteLine($"{kvp.Key}: {kvp.Value}");
                }
                Console.WriteLine();
            }
        }