Ejemplo n.º 1
0
        public GameRunnerResults RunGames(int gamesToPlay, BasePacman controller, int RandomSeed = 0, List <double> gameParameters = null)
        {
            grr = new GameRunnerResults();

            gs = gameParameters == null ? new GameState(RandomSeed) : new GameState(gameParameters, RandomSeed);

            gs.GameOver += new EventHandler(GameOverHandler);
            gs.StartPlay();

            // Turn off the logging
            if (controller.GetType() == typeof(LucPac))
            {
                LucPac.REMAIN_QUIET = true;
            }

            if (controller.GetType() == typeof(LucPacScripted))
            {
                LucPacScripted.REMAIN_QUIET = true;
            }

            gs.Controller = controller;

            Stopwatch watch      = new Stopwatch();
            int       percentage = -1;
            int       lastUpdate = 0;

            watch.Start();
            while (grr.gamesPlayed < gamesToPlay)
            {
                int newPercentage = (int)Math.Floor(((float)grr.gamesPlayed / gamesToPlay) * 100);
                if (newPercentage != percentage || grr.gamesPlayed - lastUpdate >= 100)
                {
                    lastUpdate = grr.gamesPlayed;
                    percentage = newPercentage;
                }
                // update gamestate
                Direction direction = controller.Think(gs);
                gs.Pacman.SetDirection(direction);

                // update game
                gs.Update();
            }
            watch.Stop();

            // shut down controller
            controller.SimulationFinished();

            return(grr);
        }
Ejemplo n.º 2
0
        public static void RunGameLinear(GameData CustomData)
        {
            Reset();
            try
            {
                // Set the new count of games that we want to simulate.
                gamesToPlay = CustomData.GamesToPlay;


                // Get some strange invocation error here.
                // tryLoadController(_agentName);

                BasePacman controller = null;

                if (CustomData.EvolvedValues.Count < 25 && CustomData.EvolvedValues.Count > 0)
                {
                    gs = new GameState(CustomData.EvolvedValues, CustomData.RandomSeed);
                }
                else
                {
                    gs = new GameState(CustomData.RandomSeed);
                }

                //BasePacman controller = new TestPac();

                controller = GetNewController(CustomData);

                /*if(CustomData.AIToUse.Equals("LucPacScripted"))
                 * {
                 *  controller = new LucPacScripted();
                 *  LucPacScripted.REMAIN_QUIET = true;
                 * }
                 * if (CustomData.AIToUse.Equals("MMPac"))
                 * {
                 *  //use CustomData.NeuralNetwork in constructor
                 *  controller = new MMPac.MMPac(CustomData.MapData.EvolvedValues);
                 * }
                 * if (CustomData.AIToUse.Equals("MMLocPac"))
                 * {
                 *  //use CustomData.NeuralNetwork in constructor
                 *  if (CustomData.MapData.EvolvedValues.Count < 25)
                 *      controller = new MMPac.MMLocPac("NeuralNetworkLocPac.nn");
                 *  else
                 *      controller = new MMPac.MMLocPac(CustomData.MapData.EvolvedValues);
                 * }
                 * if(CustomData.AIToUse.Equals("LucPac"))
                 * {
                 *  controller = new LucPac();
                 *  LucPac.REMAIN_QUIET = true;
                 * }*/


                gs.GameOver += new EventHandler(GameOverHandler);
                gs.StartPlay();

                gs.Controller = controller;

                Stopwatch watch           = new Stopwatch();
                int       percentage      = -1;
                int       lastUpdate      = 0;
                int       lastGamesPlayed = 0;
                watch.Start();
                while (gamesPlayed < gamesToPlay)
                {
                    int newPercentage = (int)Math.Floor(((float)gamesPlayed / gamesToPlay) * 100);
                    if (newPercentage != percentage || gamesPlayed - lastUpdate >= 100)
                    {
                        lastUpdate = gamesPlayed;
                        percentage = newPercentage;
                        Console.Clear();

                        /*Console.Write("Current parameter set: ");
                         * foreach(var X in CustomData.MapData.EvolvedValues)
                         * {
                         *  Console.Write(X + ", ");
                         * }*/
                        Console.WriteLine();
                        Console.WriteLine("Simulating ... " + percentage + "% (" + gamesPlayed + " : " + gamesToPlay + ")");
                        //Console.WriteLine(" - Elapsed: " + formatSeconds((watch.ElapsedMilliseconds / 1000.0) + "") + "s, Estimated total: " + formatSeconds(((watch.ElapsedMilliseconds / 1000.0) / percentage * 100) + "") + "s");
                        Console.WriteLine(" - Current best: " + highestScore);
                        Console.WriteLine(" - Current worst: " + lowestScore);
                        if (gamesPlayed > 0)
                        {
                            Console.WriteLine(" - Current avg.: " + (totalScore / gamesPlayed));
                        }

                        /*for (int i = scores.Count - 1; i >= 0 && i > scores.Count - 100; i--)
                         * {
                         *  Console.Write(scores[i] + ",");
                         * }*/
                    }
                    // update gamestate
                    Direction direction = controller.Think(gs);
                    gs.Pacman.SetDirection(direction);

                    // update game
                    gs.Update();
                    ms += GameState.MSPF;

                    if (lastGamesPlayed != gamesPlayed)
                    {
                        //Game finished, recreate controller
                        controller    = GetNewController(CustomData);
                        gs.Controller = controller;

                        lastGamesPlayed = gamesPlayed;
                    }
                    else
                    {
                        if (ms - lastMs > 3600000)
                        {
                            gs.InvokeGameOver();
                        }
                    }
                }
                watch.Stop();

                // shut down controller
                controller.SimulationFinished();

                JArray o            = JArray.FromObject(Scores);
                JArray JGameLengths = JArray.FromObject(GameLengths);
                JArray JPowerPills  = JArray.FromObject(PowerPillsEaten);

                JArray JPills  = JArray.FromObject(PillsEaten);
                JArray JGhosts = JArray.FromObject(GhostsEaten);

                Dictionary <string, object> collection = new Dictionary <string, object>()
                {
                    { "Scores", o },
                    { "Lengths", JGameLengths },
                    { "PowerPills", JPowerPills },
                    { "Pills", JPills },
                    { "Ghosts", JGhosts }
                };

                JObject Result = new JObject(
                    new JProperty("metrics",
                                  JObject.FromObject(collection)
                                  )
                    );

                Results = Result.ToString();

                // output results
                Console.Clear();
                long seconds = ms / 1000;
                Console.WriteLine("Games played: " + gamesPlayed);
                Console.WriteLine("Avg. score: " + (totalScore / gamesPlayed));
                Console.WriteLine("Highest score: " + highestScore + " points");
                Console.WriteLine("Lowest score: " + lowestScore + " points");
                Console.WriteLine("Max Pills Eaten: " + maxPillsEaten);
                Console.WriteLine("Min Pills Eaten: " + minPillsEaten);
                Console.WriteLine("Average Pills Eaten: " + pillsEatenTotal / gamesPlayed);
                Console.WriteLine("Max Ghosts Eaten: " + maxGhostsEaten);
                Console.WriteLine("Min Ghosts Eaten: " + minGhostsEaten);
                Console.WriteLine("Average Ghosts Eaten: " + totalGhostsEaten / gamesPlayed);
                Console.WriteLine("Longest game: " + ((float)longestGame / 1000.0f) + " seconds");
                Console.WriteLine("Total simulated time: " + (seconds / 60 / 60 / 24) + "d " + ((seconds / 60 / 60) % 24) + "h " + ((seconds / 60) % 60) + "m " + (seconds % 60) + "s");
                Console.WriteLine("Avg. simulated time pr. game: " + ((float)ms / 1000.0f / gamesPlayed) + " seconds");
                Console.WriteLine("Simulation took: " + (watch.ElapsedMilliseconds / 1000.0f) + " seconds");
                //Console.WriteLine("Speed: " + (ms / watch.ElapsedMilliseconds) + " (" + ((ms / watch.ElapsedMilliseconds) / 60) + "m " + ((ms / watch.ElapsedMilliseconds) % 60) + " s) simulated seconds pr. second");
                Console.WriteLine("For a total of: " + gamesPlayed / (watch.ElapsedMilliseconds / 1000.0f) + " games pr. second");
            }
            catch (Exception e)
            {
                // Log error.
                Console.WriteLine("Error happened. " + e.Message);
            }
            //return Results;
        }