protected override PD_MacroAction MainPlayerActions_Behaviour(
            Random randommness_provider,
            PD_Game game,
            PD_AI_PathFinder pathFinder
            )
        {
            var allMacros = game.CurrentAvailableMacros;

            return(allMacros.GetOneRandom(randommness_provider));
        }
        protected override PD_MacroAction Discarding_AfterDrawing_Behaviour(
            Random randomness_provider,
            PD_Game game,
            PD_AI_PathFinder pathFinder
            )
        {
            var allMacros = game.CurrentAvailableMacros;

            return(allMacros.GetOneRandom(randomness_provider));
        }
        public void Calculate_NumCardsTable_Test()
        {
            Random randomness_provider = new Random();

            List <int> roles_list = new List <int>()
            {
                PD_Player_Roles.Operations_Expert,
                PD_Player_Roles.Researcher,
                PD_Player_Roles.Medic,
                PD_Player_Roles.Scientist
            };

            PD_Game game = PD_Game.Create_Game__AvailableRolesList(
                randomness_provider,
                4,
                0,
                roles_list
                );

            PD_AI_PathFinder pathFinder = new PD_AI_PathFinder();

            int[,] numCardsTable = PD_AI_CardEvaluation_Utilities.NumCardsTable(game);
            int numTypes   = numCardsTable.Height();
            int numPlayers = numCardsTable.Width();

            for (int playerIndex = 0; playerIndex < numPlayers; playerIndex++)
            {
                for (int type = 0; type < numTypes; type++)
                {
                    int numCards = numCardsTable[type, playerIndex];

                    int        player                = game.players[playerIndex];
                    List <int> playerCards           = game.GQ_CityCardsInPlayerHand(player);
                    List <int> playerCardsOfThisType = playerCards.FindAll(
                        x =>
                        game.map.infection_type__per__city[x] == type
                        );

                    Assert.IsTrue(
                        playerCardsOfThisType.Count == numCards
                        );
                }
            }
        }
        /// <summary>
        /// The Policy Based Rolling Horizon Evolution agent,
        /// with optimized parameters.
        /// </summary>
        /// <param name="pathFinder"></param>
        /// <returns></returns>
        public static PD_AI_Agent_Base P_RHE_Agent(PD_AI_PathFinder pathFinder)
        {
            PolicyBased_RHE_Agent agent =
                new PolicyBased_RHE_Agent(
                    (PD_AI_Macro_Agent_Base)HierarchicalPolicyAgent(),
                    (PD_AI_Macro_Agent_Base)RandomPolicyAgent(),

                    // list of game state evaluators
                    new List <PD_GameStateEvaluator>()
            {
                // only one aggregated game state evaluator is used
                new GSE_Aggregated_WinOrDefeat_Adjusted(
                    // list of scores to be aggregated
                    new List <GameStateEvaluationScore>()
                {
                    // gradient cured diseases score
                    new GSES_GradientCuredDiseases(),
                    // multiplied remaining disease cubes score
                    new GSES_MultipliedRemainingDiseaseCubes()
                }
                    )
            },

                    // since only one evaluator is used,
                    // the replacement takes place when the child's evaluation is better than the parent's
                    P_RHE_Replacement_Rule.All_Better,

                    5,    // max genome length
                    100,  // num mutations
                    1.0,  // initial mutation rate
                    0.5,  // final mutation rate
                    5     // num simulations per evaluation
                    );

            return(agent);
        }
示例#5
0
        static void Main(string[] args)
        {
            DateTime   dateTimeOfExperiment = DateTime.UtcNow;
            int        number_of_games      = 1000;
            List <int> roles_list           = new List <int>()
            {
                PD_Player_Roles.Operations_Expert,
                PD_Player_Roles.Researcher,
                PD_Player_Roles.Medic,
                PD_Player_Roles.Scientist
            };

            Random randomness_provider = new Random(1001);

            var watch = System.Diagnostics.Stopwatch.StartNew();

            string report_file_name = "report_" + dateTimeOfExperiment.Ticks.ToString() + ".txt";
            string report_file_path = Path.Combine(
                Directory.GetCurrentDirectory(),
                report_file_name
                );

            PD_IO_Utilities.CreateFile(report_file_path, false, true);

            string report_part = "Date / time: " + dateTimeOfExperiment.ToString();

            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");

            //////////////////////////////////////////////////////////////////////////////
            /// 1.1 Generate the games, from data
            //////////////////////////////////////////////////////////////////////////////
            watch.Restart();
            List <PD_Game> games = new List <PD_Game>();

            for (int i = 0; i < number_of_games; i++)
            {
                games.Add(
                    PD_Game.Create_Game__AvailableRolesList(
                        randomness_provider,
                        4,
                        0,
                        roles_list
                        )
                    );
            }
            watch.Stop();
            long time_to_generate_games = watch.ElapsedMilliseconds;

            report_part = String.Format(
                "Generate {0} games from data and perform game setup: {1}",
                number_of_games,
                time_to_generate_games
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");

            //////////////////////////////////////////////////////////////////////////////
            /// 1.3 Run all games until the end, by selecting random actions
            //////////////////////////////////////////////////////////////////////////////
            watch.Restart();
            foreach (var game in games)
            {
                while (PD_Game_Queries.GQ_Is_Ongoing(game))
                {
                    game.Apply_Action(
                        randomness_provider,
                        game.CurrentAvailablePlayerActions.GetOneRandom(randomness_provider)
                        );
                }
            }
            watch.Stop();
            long time_to_play_until_the_end_using_random_actions = watch.ElapsedMilliseconds;

            report_part = String.Format(
                "Play {0} games until the end, using random actions: {1}",
                number_of_games,
                time_to_play_until_the_end_using_random_actions
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");

            //////////////////////////////////////////////////////////////////////////////
            /// 1.4 Make temporary copies of all games...
            //////////////////////////////////////////////////////////////////////////////
            watch.Restart();
            List <PD_Game> game_copies = new List <PD_Game>();

            foreach (var game in games)
            {
                PD_Game game_copy = game.GetCustomDeepCopy();
                game_copies.Add(game_copy);
            }
            watch.Stop();
            long time_to_copy_all_finished_games = watch.ElapsedMilliseconds;

            report_part = String.Format(
                "Copy {0} finished games: {1}",
                number_of_games,
                time_to_copy_all_finished_games
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");

            // clear the games' list..
            games.Clear();
            game_copies.Clear();

            //////////////////////////////////////////////////////////////////////////////
            /// 2.1 Generate games and automatically perform setup...
            //////////////////////////////////////////////////////////////////////////////
            watch.Restart();
            for (int i = 0; i < number_of_games; i++)
            {
                games.Add(
                    PD_Game.Create_Game__AvailableRolesList(
                        randomness_provider,
                        4,
                        0,
                        roles_list
                        )
                    );
            }
            watch.Stop();
            long time_to_generate_and_setup_games = watch.ElapsedMilliseconds;

            report_part = String.Format(
                "Generate {0} games from data and perform set up: {1}",
                number_of_games,
                time_to_generate_and_setup_games
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");

            //////////////////////////////////////////////////////////////////////////////
            /// 2.2. Run all games until the end, by selecting random macro actions
            //////////////////////////////////////////////////////////////////////////////
            PD_AI_PathFinder pf = new PD_AI_PathFinder();

            watch.Restart();
            foreach (var game in games)
            {
                while (PD_Game_Queries.GQ_Is_Ongoing(game))
                {
                    var available_macros = game.GetAvailableMacros(pf);
                    game.Apply_Macro_Action(
                        randomness_provider,
                        available_macros.GetOneRandom(randomness_provider)
                        );
                }
            }
            watch.Stop();
            long time_to_play_until_the_end_using_random_macro_actions = watch.ElapsedMilliseconds;

            report_part = String.Format(
                "Play {0} games until the end, using random macro actions: {1}",
                number_of_games,
                time_to_play_until_the_end_using_random_macro_actions
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");

            //////////////////////////////////////////////////////////////////////////////
            /// 1.4 Make temporary copies of all games...
            //////////////////////////////////////////////////////////////////////////////
            watch.Restart();
            foreach (var game in games)
            {
                PD_Game game_copy = game.GetCustomDeepCopy();
                game_copies.Add(game_copy);
            }
            watch.Stop();
            long time_to_copy_all_macro_finished_games = watch.ElapsedMilliseconds;

            report_part = String.Format(
                "Copy {0} finished games: {1}",
                number_of_games,
                time_to_copy_all_macro_finished_games
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");


            // clear the games' list..
            games.Clear();
            game_copies.Clear();

            //////////////////////////////////////////////////////////////////////////////
            /// 3.1 Generate games and automatically perform setup...
            //////////////////////////////////////////////////////////////////////////////
            watch.Restart();
            for (int i = 0; i < number_of_games; i++)
            {
                games.Add(
                    PD_Game.Create_Game__AvailableRolesList(
                        randomness_provider,
                        4,
                        0,
                        roles_list
                        )
                    );
            }
            watch.Stop();
            time_to_generate_and_setup_games = watch.ElapsedMilliseconds;
            report_part = String.Format(
                "Generate {0} games from data and perform set up: {1}",
                number_of_games,
                time_to_generate_and_setup_games
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");

            //////////////////////////////////////////////////////////////////////////////
            /// 3.2. Run all games until the end, by selecting random macro actions
            //////////////////////////////////////////////////////////////////////////////
            //PD_AI_PathFinder pf = new PD_AI_PathFinder();
            PD_AI_MacroAgent_HierarchicalPolicy agent = new PD_AI_MacroAgent_HierarchicalPolicy(
                new List <PD_AI_MacroAgent_MainPolicy_Base>()
            {
                new PD_AI_MacroAgent_Policy_Cure_ASAP(),
                new PD_AI_MacroAgent_Policy_Treat_MinSameCubes_Now_Smart(3),
                new PD_AI_MacroAgent_Policy_ShareKnowledge_1_Variation_A(),
                new PD_AI_MacroAgent_Policy_BuildRS_MaxTotal_MinDistance(6, 3),
                new PD_AI_MacroAgent_Policy_Treat_MinSameCubes_Now_Smart(2),
                new PD_AI_MacroAgent_Policy_Treat_MinSameCubes_Now_Smart(1),
                new PD_AI_MacroAgent_Policy_WalkAway()
            },
                new List <PD_AI_MacroAgent_DiscardPolicy_Base>()
            {
                new PD_AI_MacroAgent_Policy_CardDiscard_Smart()
            }
                );

            watch.Restart();
            foreach (var game in games)
            {
                while (PD_Game_Queries.GQ_Is_Ongoing(game))
                {
                    var macro = agent.GetNextMacroAction(randomness_provider, game, pf);
                    game.Apply_Macro_Action(
                        randomness_provider,
                        macro
                        );
                }
            }
            watch.Stop();
            long time_played = watch.ElapsedMilliseconds;

            report_part = String.Format(
                "Play {0} games until the end, using HPA: {1}",
                number_of_games,
                time_played
                );
            Console.WriteLine(report_part);
            PD_IO_Utilities.AppendToFile(report_file_path, report_part + "\n");
        }
        public void Deserialize_ExistingGame_NewRepresentation_Test()
        {
            Random randomness_provider = new Random(1001);

            string saved_games_path = System.IO.Path.Combine(
                System.IO.Directory.GetCurrentDirectory(),
                "ParameterTuning_TestBed_NewRepresentation"
                );
            var game_file_paths = PD_IO_Utilities.GetFilePathsInFolder(saved_games_path);

            // play the games until the end, using single actions
            foreach (var game_file_path in game_file_paths)
            {
                string serialized_mini_game = PD_IO_Utilities.ReadFile(game_file_path);

                PD_MiniGame deserialized_mini_game =
                    JsonConvert.DeserializeObject <PD_MiniGame>(serialized_mini_game);

                PD_Game game = deserialized_mini_game.Convert_To_Game();

                // play the game until the end...
                while (game.GQ_Is_Ongoing())
                {
                    var available_actions = game.CurrentAvailablePlayerActions;
                    var random_action     = available_actions.GetOneRandom(randomness_provider);
                    game.Apply_Action(
                        randomness_provider,
                        random_action
                        );
                }

                Assert.IsTrue(game.GQ_Is_Ongoing() == false);
            }

            // play the games until the end, using single actions
            foreach (var game_file_path in game_file_paths)
            {
                PD_Game game = PD_IO_Utilities.DeserializeGame_From_MiniGameJsonFile(game_file_path);

                // play the game until the end...
                while (game.GQ_Is_Ongoing())
                {
                    var available_actions = game.CurrentAvailablePlayerActions;
                    var random_action     = available_actions.GetOneRandom(randomness_provider);
                    game.Apply_Action(
                        randomness_provider,
                        random_action
                        );
                }

                Assert.IsTrue(game.GQ_Is_Ongoing() == false);
            }

            // play the games until the end, using macro actions
            PD_AI_PathFinder pathFinder = new PD_AI_PathFinder();

            foreach (var game_file_path in game_file_paths)
            {
                string serialized_mini_game = PD_IO_Utilities.ReadFile(game_file_path);

                PD_MiniGame deserialized_mini_game =
                    JsonConvert.DeserializeObject <PD_MiniGame>(serialized_mini_game);

                PD_Game game = deserialized_mini_game.Convert_To_Game();

                // play the game until the end...
                while (game.GQ_Is_Ongoing())
                {
                    var available_macro_actions = game.GetAvailableMacros(pathFinder);
                    var random_macro_action     = available_macro_actions.GetOneRandom(randomness_provider);
                    game.Apply_Macro_Action(
                        randomness_provider,
                        random_macro_action
                        );
                }

                Assert.IsTrue(game.GQ_Is_Ongoing() == false);
            }

            // play the games until the end, using macro actions
            foreach (var game_file_path in game_file_paths)
            {
                PD_Game game = PD_IO_Utilities.DeserializeGame_From_MiniGameJsonFile(game_file_path);

                // play the game until the end...
                while (game.GQ_Is_Ongoing())
                {
                    var available_macro_actions = game.GetAvailableMacros(pathFinder);
                    var random_macro_action     = available_macro_actions.GetOneRandom(randomness_provider);
                    game.Apply_Macro_Action(
                        randomness_provider,
                        random_macro_action
                        );
                }

                Assert.IsTrue(game.GQ_Is_Ongoing() == false);
            }
        }
示例#7
0
        public static void Exp_1__RandomGames__RandomActionAgent()
        {
            Random randomness_provider = new Random();

            // testbed - settings...
            int number_of_games_to_generate = 1000;
            int number_of_players           = 4;
            int game_difficulty             = 0; // easy

            // generate the random games (test-bed)
            List <PD_Game> games = Generate_Random_Games(
                randomness_provider,
                number_of_games_to_generate,
                number_of_players,
                game_difficulty
                );

            // experiment - settings
            int  number_of_repetitions_per_game = 10;
            bool save_initial_game_states       = false;
            bool keep_game_stats_report         = false;
            bool keep_trace = false;

            // debugging settings
            bool display_actions   = true;
            bool display_end_state = true;

            // define the agent and the dictionary for the experiment runner
            PD_AI_ActionAgent_Random randomActionAgent             = new PD_AI_ActionAgent_Random();
            Dictionary <PD_AI_Agent_Base, string> agentsDictionary =
                new Dictionary <PD_AI_Agent_Base, string>()
            {
                { randomActionAgent, "random_action_agent" }
            };

            // prepare the experiment results directory
            string experimentResults_Directory = Directory.GetCurrentDirectory() + "\\ExperimentResults";

            if (PD_IO_Utilities.FolderExists(experimentResults_Directory) == false)
            {
                PD_IO_Utilities.CreateFolder(experimentResults_Directory, true);
            }

            // initialize the experiment
            PD_AI_PathFinder    pathFinder = new PD_AI_PathFinder();
            Pandemic_Experiment experiment = new Pandemic_Experiment(
                games,
                save_initial_game_states,
                agentsDictionary,
                pathFinder,
                number_of_repetitions_per_game,
                experimentResults_Directory,
                keep_game_stats_report,
                keep_trace
                );

            experiment.RunExperiment(
                randomness_provider,
                pathFinder,
                display_actions,
                display_end_state
                );
        }