Ejemplo n.º 1
0
        public void RunExperiment(
            Random randomness_provider,
            PD_AI_PathFinder pathFinder,
            bool displayActions,
            bool displayEndState
            )
        {
            List <PD_AI_Agent_Base> allAgents = NamePerAgent.Keys.ToList();

            for (int agentIndex = 0; agentIndex < allAgents.Count; agentIndex++)
            {
                for (int gameIndex = 0; gameIndex < AllGames.Count; gameIndex++)
                {
                    for (int repetitionIndex = 0; repetitionIndex < NumRepetitionsPerAgent; repetitionIndex++)
                    {
                        var gameCopy  = AllGames[gameIndex].GetCustomDeepCopy();
                        var thisAgent = allAgents[agentIndex];

                        Console.WriteLine(
                            "game: " + gameIndex
                            + " | agent: " + allAgents[agentIndex].GetType().ToString()
                            + " | repetition: " + repetitionIndex
                            );

                        if (thisAgent.GetType().IsSubclassOf(typeof(PD_AI_Macro_Agent_Base)))
                        {
                            gameCopy.OverrideStartTime();
                            while (gameCopy.GQ_Is_Ongoing())
                            {
                                if (Keep_Trace)
                                {
                                    string filePath_ToAppend = Report_FilePath_PerAgent[thisAgent];

                                    PD_IO_Utilities.AppendToFile(
                                        filePath_ToAppend,
                                        Get_Record_Row(
                                            gameCopy,
                                            pathFinder,
                                            thisAgent,
                                            repetitionIndex
                                            )
                                        );
                                }

                                var nextMacro = ((PD_AI_Macro_Agent_Base)thisAgent).GetNextMacroAction(
                                    randomness_provider,
                                    gameCopy.Request_Randomized_Copy(randomness_provider),
                                    pathFinder
                                    );

                                if (displayActions)
                                {
                                    Console.WriteLine(nextMacro.GetDescription());
                                }

                                foreach (var playerAction in nextMacro.Actions_Executable_Now)
                                {
                                    if (gameCopy.CurrentAvailablePlayerActions.Contains(playerAction))
                                    {
                                        gameCopy.Apply_Action(
                                            randomness_provider,
                                            playerAction
                                            );
                                    }
                                }
                            }

                            if (displayEndState)
                            {
                                Console.WriteLine(gameCopy.GQ_Is_Won() ? "won" : "lost");
                            }

                            string filePath = Report_FilePath_PerAgent[thisAgent];

                            PD_IO_Utilities.AppendToFile(
                                filePath,
                                Get_Record_Row(
                                    gameCopy,
                                    pathFinder,
                                    thisAgent,
                                    repetitionIndex
                                    )
                                );
                        }
                        else if (thisAgent.GetType().IsSubclassOf(typeof(PD_AI_Action_Agent_Base)))
                        {
                            gameCopy.OverrideStartTime();
                            while (gameCopy.GQ_Is_Ongoing())
                            {
                                var nextAction =
                                    ((PD_AI_Action_Agent_Base)thisAgent).GetNextAction(
                                        randomness_provider,
                                        gameCopy.Request_Randomized_Copy(randomness_provider)
                                        );

                                if (displayActions)
                                {
                                    Console.WriteLine(nextAction.GetDescription());
                                }

                                gameCopy.Apply_Action(
                                    randomness_provider,
                                    nextAction
                                    );
                            }

                            if (displayEndState)
                            {
                                Console.WriteLine(gameCopy.GQ_Is_Won() ? "won" : "lost");
                            }

                            string filePath = Report_FilePath_PerAgent[thisAgent];
                            PD_IO_Utilities.AppendToFile(filePath,
                                                         Get_Record_Row(
                                                             gameCopy,
                                                             pathFinder,
                                                             thisAgent,
                                                             repetitionIndex
                                                             )
                                                         );
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public Pandemic_Experiment(
            List <PD_Game> allGames,
            bool saveInitialGameStates,
            Dictionary <PD_AI_Agent_Base, string> namePerAgent,
            PD_AI_PathFinder pathFinder,
            int numRepetitionsPerAgent,
            string experimentResults_FolderPath,
            bool keep_GameStats_Report,
            bool keep_Trace
            )
        {
            AllGames = allGames;
            SaveInitialGameStates = saveInitialGameStates;

            NamePerAgent           = namePerAgent;
            NumRepetitionsPerAgent = numRepetitionsPerAgent;

            GameState_Report = new PD_GameState_Report(AllGames[0], pathFinder);
            GameStats_Report = new PD_GameStats_Report(AllGames[0], pathFinder);

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

            Keep_GameStats_Report = keep_GameStats_Report;
            Keep_Trace            = keep_Trace;

            string thisExperiment_FolderName = DateTime.UtcNow.Ticks.ToString();

            ThisExperiment_FolderPath = Path.Combine(ExperimentResults_FolderPath, thisExperiment_FolderName);
            if (PD_IO_Utilities.FolderExists(ThisExperiment_FolderPath) == false)
            {
                PD_IO_Utilities.CreateFolder(ThisExperiment_FolderPath, true);
            }

            if (SaveInitialGameStates)
            {
                foreach (var game in AllGames)
                {
                    string gameFileName = "game_" + game.unique_id.ToString() + ".json";
                    string gameFilePath = Path.Combine(ThisExperiment_FolderPath, gameFileName);

                    PD_IO_Utilities.SerializeGameToJsonAndSave(
                        game,
                        false,
                        gameFilePath,
                        false,
                        false
                        );
                }
            }

            Report_FilePath_PerAgent = new Dictionary <PD_AI_Agent_Base, string>();

            List <PD_AI_Agent_Base> allAgents = NamePerAgent.Keys.ToList();

            foreach (var agent in allAgents)
            {
                string agentName = NamePerAgent[agent];

                string agent_Report_FileName = agentName + ".csv";
                string agent_Report_FilePath = Path.Combine(ThisExperiment_FolderPath, agent_Report_FileName);
                Report_FilePath_PerAgent.Add(agent, agent_Report_FilePath);

                string agent_Description_FileName = agentName + ".json";
                string agent_Description_FilePath = Path.Combine(ThisExperiment_FolderPath, agent_Description_FileName);
                PD_IO_Utilities.SerializeToJsonAndSave(agent, agent_Description_FilePath, true, false);
            }

            int counter = 0;

            foreach (var agent in allAgents)
            {
                PD_IO_Utilities.CreateFile(Report_FilePath_PerAgent[agent], false, false);

                PD_IO_Utilities.AppendToFile(
                    Report_FilePath_PerAgent[agent],
                    Get_Record_Header(AllGames[0], pathFinder, agent)
                    );

                counter++;
            }
        }
        public PD_Experiment_Tournament(
            List <PD_Game> allGames,
            Dictionary <PD_AI_Agent_Base, string> namePerAgent,
            PD_AI_PathFinder pathFinder,
            int numRepetitionsPerAgent,
            string experimentResults_ParentFolder_Path,
            bool keep_GameStats_Report,
            bool keep_MacroActions_Report,
            bool keep_Trace
            )
        {
            PathFinder = new PD_AI_PathFinder();

            AllGames               = allGames;
            NamePerAgent           = namePerAgent;
            NumRepetitionsPerAgent = numRepetitionsPerAgent;

            GameState_Report           = new PD_GameState_Report(AllGames[0], pathFinder);
            GameStats_Report           = new PD_GameStats_Report(AllGames[0], pathFinder);
            MacroActions_Report        = new PD_MacroActions_Report(AllGames[0], pathFinder);
            RollingHorizonAgent_Report = new PD_RollingHorizon_Agent_Report();

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

            Keep_GameStats_Report    = keep_GameStats_Report;
            Keep_MacroActions_Report = keep_MacroActions_Report;
            Keep_Trace = keep_Trace;

            string thisTournament_Folder_Name = DateTime.UtcNow.Ticks.ToString();

            ThisTournament_Folder_Path = Path.Combine(ExperimentResults_ParentFolder_Path, thisTournament_Folder_Name);
            if (PD_IO_Utilities.FolderExists(ThisTournament_Folder_Path) == false)
            {
                PD_IO_Utilities.CreateFolder(ThisTournament_Folder_Path, true);
            }

            FolderPath_Per_Agent     = new Dictionary <PD_AI_Agent_Base, string>();
            Report_FilePath_PerAgent = new Dictionary <PD_AI_Agent_Base, string>();

            List <PD_AI_Agent_Base> allAgents = NamePerAgent.Keys.ToList();

            foreach (var agent in allAgents)
            {
                string agentName = NamePerAgent[agent];

                string agent_FolderName = agentName;
                string agent_FolderPath = Path.Combine(ThisTournament_Folder_Path, agent_FolderName);
                FolderPath_Per_Agent.Add(agent, agent_FolderPath);
                if (PD_IO_Utilities.FolderExists(agent_FolderPath) == false)
                {
                    PD_IO_Utilities.CreateFolder(agent_FolderPath, true);
                }

                string agent_Report_FileName = agentName + ".csv";
                string agent_Report_FilePath = Path.Combine(agent_FolderPath, agent_Report_FileName);
                Report_FilePath_PerAgent.Add(agent, agent_Report_FilePath);

                string agent_Description_FileName = agentName + ".json";
                string agent_Description_FilePath = Path.Combine(agent_FolderPath, agent_Description_FileName);
                PD_IO_Utilities.SerializeToJsonAndSave(agent, agent_Description_FilePath, true, false);
            }

            int counter = 0;

            foreach (var agent in allAgents)
            {
                PD_IO_Utilities.CreateFile(Report_FilePath_PerAgent[agent], false, false);
                //PD_GameStats_Report tempGameReport = new PD_GameStats_Report(AllGames[0], pathFinder);

                PD_IO_Utilities.AppendToFile(
                    Report_FilePath_PerAgent[agent],
                    Get_Record_Header(AllGames[0], pathFinder, agent)
                    );

                counter++;
            }
        }