Ejemplo n.º 1
0
        /// <summary>
        /// Show replay of the given agent.
        /// </summary>
        /// <param name="agentToShow"></param>
        /// <param name="geneticSettings"></param>
        public void ViewAgent(Agent agentToShow, GeneticSettings geneticSettings)
        {
            networkSettings = (geneticSettings.FitnessCalculator as SnakeFitnessCalculator).NetworkSettings;
            snakeSettings   = (geneticSettings.FitnessCalculator as SnakeFitnessCalculator).SnakeSettings;
            SnakeFitnessCalculator fitnessCalculator = geneticSettings.FitnessCalculator as SnakeFitnessCalculator;

            FitnessCalculatorRecording recording = fitnessCalculator.RecordCalculation(agentToShow);

            MilisecondsForm milisecondsForm = new MilisecondsForm();

            milisecondsForm.ShowDialog();
            speedMS = milisecondsForm.speedMS;

            ShowPlay(recording);
        }
Ejemplo n.º 2
0
        private void ShowPlay(FitnessCalculatorRecording recorder)
        {
            List <FitnessRoundInfo> fitnessRoundInfoList = recorder.FitnessRoundInfoList;

            int  score   = recorder.InitialFitnessRoundInfo.Score;
            bool isAlive = recorder.InitialFitnessRoundInfo.IsAlive;

            UpdateCurrentGridInitial(recorder.InitialFitnessRoundInfo.ChangedFields);

            SnakeGameGUI snakeGameGUI = new SnakeGameGUI(snakeSettings, currentGrid, score, isAlive);

            // START GUI
            snakeGameGUI.OpenGameWindow();

            // Loop throug frames.
            for (int i = 0; i < fitnessRoundInfoList.Count; i++)
            {
                UpdateCurrentGrid(fitnessRoundInfoList[i].ChangedFields);
                snakeGameGUI.UpdateView(currentGrid, fitnessRoundInfoList[i].Score, fitnessRoundInfoList[i].IsAlive, fitnessRoundInfoList[i].SnakeHeadPoint);
                Thread.Sleep(speedMS);
            }
        }
Ejemplo n.º 3
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);
        }