Ejemplo n.º 1
0
        /// <summary>
        /// Runs a benchmark comparing two different AIs against each other on a fixed map.
        /// </summary>
        public static void CompareAi()
        {
            var dna = new DNA(2, 2);

            dna.Randomize();

            var map = Map.Load(@"data/map.json");

            var game = GameSetup.GenerateFromDna(dna, dna, map);

            game.PrepareEverything();

            GameInvariants.AssertMobsNotStandingOnEachother(game);

            IMobController c1, c2;

            switch (Constants.MctsBenchType)
            {
            case 0:
                c1 = new MctsController(game, Constants.MctsBenchIterations);
                c2 = new AiRandomController(game);
                break;

            case 1:
                c1 = new MctsController(game, Constants.MctsBenchIterations);
                c2 = new AiRuleBasedController(game);
                break;

            case 2:
                c1 = new AiRuleBasedController(game);
                c2 = new AiRandomController(game);
                break;

            default:
                throw new ArgumentException($"Invalid value of {Constants.MctsBenchType} for --MctsBenchType");
            }

            var iterationStopwatch = new Stopwatch();

            int c1Wins = 0;
            int c2Wins = 0;

            for (int i = 0; i < 1000; i++)
            {
                dna.Randomize();
                iterationStopwatch.Restart();

                GameSetup.OverrideGameDna(game, dna, dna);
                var r1 = GameEvaluator.Playout(game, c1, c2);

                GameSetup.OverrideGameDna(game, dna, dna);
                var r2 = GameEvaluator.Playout(game, c2, c1);

                iterationStopwatch.Stop();

                Console.WriteLine(Accounting.GetStats());

                c1Wins += r1.RedWins + r2.BlueWins;
                c2Wins += r1.BlueWins + r2.RedWins;

                Console.WriteLine($"{i.ToString("0000")} STATS: M2: {c1Wins}, M5: {c2Wins}, winrate: {((double)c1Wins/(c1Wins+c2Wins)).ToString("0.000")}");
            }
        }