Ejemplo n.º 1
0
 /// <summary>
 /// Copies recieved information to the properties of the class.
 /// </summary>
 public PlaytestRoundInfo(SnakeGame snakeGame, List <Field> changedFields)
 {
     Score            = snakeGame.Score;
     IsAlive          = snakeGame.Snake.IsAlive;
     CurrentDirection = snakeGame.Snake.Head.Direction;
     SnakeHeadPoint   = snakeGame.Snake.Head.Point;
     ChangedFields    = changedFields;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Saves information about the snake game during the simulation round.
        /// </summary>
        /// <param name="snakeGame"></param>
        public void TakeSnapShotRound(SnakeGame snakeGame)
        {
            List <Field> changedFields = new List <Field>();

            changedFields = GetChangedFields(snakeGame.Grid);
            PlaytestRoundInfoList.Add(new PlaytestRoundInfo(snakeGame, changedFields));
            previousGrid = snakeGame.Grid.GetCopy();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Saves information about the snake game before the simulation round begins.
        /// </summary>
        /// <param name="snakeGame"></param>
        public void TakeSnapShotInitial(SnakeGame snakeGame)
        {
            List <Field> changedFields = new List <Field>();

            changedFields            = GetChangedFieldsInitial(snakeGame.Grid);
            InitialPlaytestRoundInfo = new PlaytestRoundInfo(snakeGame, changedFields);
            previousGrid             = snakeGame.Grid.GetCopy();
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the class <see cref="EndGameInfo"/>
 /// which contains the given information and exposes it in its properties.
 /// </summary>
 /// <param name="snakegame"></param>
 /// <param name="fitness"></param>
 /// <param name="averageMovesPerPoint"></param>
 /// <param name="movesTotal"></param>
 public EndGameInfo(SnakeGame snakegame, double fitness, double averageMovesPerPoint, int movesTotal)
 {
     Fitness                  = fitness;
     this.FoodEaten           = snakegame.FoodEaten;
     this.Score               = snakegame.Score;
     this.AverageMovesPerFood = averageMovesPerPoint;
     MovesTotal               = movesTotal;
     SnakeCauseOfDeath        = GetCauseOfDeath(snakegame.Snake);
 }
Ejemplo n.º 5
0
 private void initWindow()
 {
     snake          = new SnakeGame(true);
     snake.Width    = 500;
     snake.Height   = 500;
     pnlMain.Width  = snake.Width;
     pnlMain.Height = snake.Height;
     pnlMain.Controls.Add(snake);
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Saves information during in the fitness calculation round.
        /// </summary>
        /// <param name="snakeGame"></param>
        /// <param name="movesSincePoint"></param>
        /// <param name="totalMoves"></param>
        /// <param name="inputNeuralNetwork"></param>
        /// <param name="neuralNetwork"></param>
        public void TakeSnapShotRound(SnakeGame snakeGame, int movesSincePoint, int totalMoves, double[] inputNeuralNetwork,
                                      NeuralNetwork neuralNetwork)
        {
            List <Field> changedFields = new List <Field>();

            changedFields = GetChangedFields(snakeGame.Grid);
            FitnessRoundInfoList.Add(new FitnessRoundInfo(snakeGame, movesSincePoint, totalMoves, inputNeuralNetwork, neuralNetwork, changedFields));
            previousGrid = snakeGame.Grid.GetCopy();
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Copies recieved information to its properties.
        /// </summary>
        public FitnessRoundInfo(SnakeGame snakeGame, int movesSincePoint, int totalMoves, double[] inputNeuralNetwork,
                                NeuralNetwork neuralNetwork, List <Field> changedFields)
        {
            Score            = snakeGame.Score;
            IsAlive          = snakeGame.Snake.IsAlive;
            CurrentDirection = snakeGame.Snake.Head.Direction;
            MovesSincePoint  = movesSincePoint;
            TotalMoves       = totalMoves;
            SnakeHeadPoint   = snakeGame.Snake.Head.Point;

            InputNeuralNetwork = new double[inputNeuralNetwork.Length];
            Array.Copy(inputNeuralNetwork, InputNeuralNetwork, inputNeuralNetwork.Length);
            OutputNeuralNetwork = neuralNetwork.GetOutputValues();

            ChangedFields = changedFields;
        }
Ejemplo n.º 8
0
        private void Form1_Load(object sender, EventArgs e)
        {
            /*
             * snake.Width = 400;
             * snake.Height = 400;
             * this.Controls.Add(snake);
             *
             * for (int i = 0; i < 1; i++) {
             *  snake.moveLeft();
             *  this.Refresh();
             *  System.Threading.Thread.Sleep(1000);
             * }
             */
            snakes   = new SnakeGame[NETWORKCNT];
            networks = new NNFeedForwardNetwork[NETWORKCNT];
            int x = 0;
            int y = 0;

            for (int i = 0; i < snakes.Length; i++)
            {
                snakes[i]        = new SnakeGame();
                snakes[i].Width  = 100;
                snakes[i].Height = 100;
                snakes[i].Left   = snakes[i].Width * x;
                snakes[i].Top    = snakes[i].Height * y;
                snakes[i].moveLeft();
                snakes[i].Refresh();
                snakes[i].MouseUp += new MouseEventHandler(Snakes_MouseUp);
                snakes[i].Tag      = i;
                pnlMain.Controls.Add(snakes[i]);
                x++;
                if (x >= 10)
                {
                    x = 0;
                    y++;
                }
            }

            initialize();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Calculates the resulting fitness value by using the passed weights
        /// and returns the value in a FitnessInfo class together with other relevant info about the calculation.
        /// </summary>
        public IFitnessInfo CalculateFitness(double[] weightsForNeuralNetwork)
        {
            int    action              = 0;
            int    currentScore        = 0;
            int    movesSincePoint     = 0;
            int    movesTotal          = 0;
            double fitness             = 0;
            double averageMovesPerFood = 0;

            double[]    input = new double[ProgramSettings.NUMBER_OF_INPUT_NEURONS];
            EndGameInfo endGameInfo;

            // Setup new snake game and neural network
            neuralNetwork = new NeuralNetwork(NetworkSettings, weightsForNeuralNetwork);
            snakeGame     = new SnakeGame(SnakeSettings);

            // Make recorder
            if (record)
            {
                recorder = new FitnessCalculatorRecording(agentToRecord, NetworkSettings, SnakeSettings);
                recorder.TakeSnapShotInitial(snakeGame, movesSincePoint, movesTotal, input, neuralNetwork);
            }

            // Simulation begins
            do
            {
                input  = ConvertGridToInput(snakeGame.Grid, snakeGame.Snake, snakeGame.Food);
                action = neuralNetwork.CalculateOutput(input);
                snakeGame.UpdateDirection(action);
                // Check if got point
                if (snakeGame.Score != currentScore)
                {
                    movesSincePoint = 0;
                    currentScore    = snakeGame.Score;
                }
                else
                {
                    movesSincePoint++;
                }
                movesTotal++;
                if (record) // Save round info.
                {
                    recorder.TakeSnapShotRound(snakeGame, movesSincePoint, movesTotal, input, neuralNetwork);
                }
                FitnessRoundsCount++;
            } while(snakeGame.Snake.IsAlive && movesSincePoint < GetMaxMoves(snakeGame.Snake.Lenght));

            if (snakeGame.FoodEaten != 0)
            {
                averageMovesPerFood = movesTotal / (double)snakeGame.FoodEaten;
            }

            fitness     = snakeGame.Score;
            endGameInfo = new EndGameInfo(snakeGame, fitness, averageMovesPerFood, movesTotal);

            if (record)
            {
                recorder.TakeSnapShotEndGame(endGameInfo);
            }
            return(endGameInfo);
        }
Ejemplo n.º 10
0
 public EndTestInfo(SnakeGame snakegame, int movesTotal)
 {
     Score      = snakegame.Score;
     MovesTotal = movesTotal;
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Runs a play test simulation of the saved agent and returns the result.
        /// </summary>
        /// <param name="savedAgent"></param>
        /// <param name="testType"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public PlaytestResult RunSimulation(SavedAgent savedAgent, TestType testType, bool record)
        {
            int               action                    = 0;
            int               movesTotal                = 0;
            int               currentScore              = 0;
            int               movesSincePoint           = 0;
            int               maxStepsBeforeTerminating = 1000;
            double            averageMovesPerFood       = 0;
            bool              isInBounds                = true;
            PlaytestRecording recorder                  = null;
            TestResult        testResult                = TestResult.Failed;

            NetworkSettings networkSettings = (savedAgent.geneticSettings.FitnessCalculator as SnakeFitnessCalculator).NetworkSettings;
            SnakeSettings   snakeSettings   = (savedAgent.geneticSettings.FitnessCalculator as SnakeFitnessCalculator).SnakeSettings;

            // Setup new snake game and neural network
            NeuralNetwork neuralNetwork = new NeuralNetwork(networkSettings, savedAgent.agent.Chromosome.ToArray());

            double[]  input     = new double[networkSettings.numberOfInputNeurons];
            SnakeGame snakeGame = new SnakeGame(snakeLevel);

            // Make recorder
            if (record)
            {
                recorder = new PlaytestRecording(savedAgent.agent, networkSettings, snakeSettings);
                recorder.TakeSnapShotInitial(snakeGame);
            }

            if (record) // Save round info.
            {
                recorder.TakeSnapShotRound(snakeGame);
            }

            // Check within bounds.
            if (testType == TestType.WithinBounds)
            {
                if (snakeGame.Grid.PointWithinGrid(snakeGame.Snake.Head.Point))
                {
                    testResult = TestResult.Passed;
                }
                else
                {
                    testResult = TestResult.Failed;
                    isInBounds = false;
                }
            }

            if (isInBounds)
            {
                do
                {
                    input  = ConvertGridToInput(snakeGame.Grid, snakeGame.Snake, snakeGame.Food);
                    action = neuralNetwork.CalculateOutput(input);
                    snakeGame.UpdateDirection(action);

                    // Check if got point
                    if (snakeGame.Score != currentScore)
                    {
                        movesSincePoint = 0;
                        currentScore    = snakeGame.Score;
                    }
                    else
                    {
                        movesSincePoint++;
                    }
                    movesTotal++;

                    if (record) // Save round info.
                    {
                        recorder.TakeSnapShotRound(snakeGame);
                    }
                    // Test within bounds.
                    if (testType == TestType.WithinBounds)
                    {
                        if (snakeGame.Grid.PointWithinGrid(snakeGame.Snake.Head.Point))
                        {
                            testResult = TestResult.Passed;
                        }
                        else
                        {
                            testResult = TestResult.Failed;
                            isInBounds = false;
                        }
                    }
                } while(snakeGame.Snake.IsAlive && movesSincePoint < maxStepsBeforeTerminating && isInBounds);
            }

            // Check if snake completed game.
            if (testType == TestType.CanComplete)
            {
                if (snakeGame.MaxScore != snakeGame.Score)
                {
                    testResult = TestResult.Failed;
                }
                else
                {
                    testResult = TestResult.Passed;
                }
            }

            // Get avg. moves per food.
            if (snakeGame.FoodEaten != 0)
            {
                averageMovesPerFood = movesTotal / (double)snakeGame.FoodEaten;
            }
            EndTestInfo endTestInfo = new EndTestInfo(snakeGame, movesTotal);

            if (record)
            {
                recorder.TakeSnapShotEndTest(endTestInfo);
            }

            PlaytestResult playtestResult = new PlaytestResult(testResult, recorder);

            return(playtestResult);
        }