예제 #1
0
 private SMBMemoryMapper.PlayerStats GetPlayerStats(SMBMemoryMapper mapper)
 {
     SMBMemoryMapper.PlayerStats playerStats = (SMBMemoryMapper.PlayerStats)mapper.FetchPlayerStats();
     return(playerStats);
 }
예제 #2
0
        /// <summary>
        /// Evaluate the provided IBlackBox against the SMB Neat player and return its fitness score.
        /// </summary>
        public FitnessInfo Evaluate(IBlackBox box)
        {
            // How many frames the NN should wait before making another move (Game runs at 60 frames/second)
            int playerSpeed = 2;

            // The amount of seconds that can pass with Mario not making progress in Seconds
            int idleTime = 3;

            idleTime = idleTime * (60 / playerSpeed);

            // Create a fresh clone of the state
            Emulator emu = LoadState(_stateFile);

            // Create a new controller for the Emulator
            emu.Controller = new NES001Controller();

            // Create a new SMB Mapper object for easy access to the NN inputs.
            SMBMemoryMapper mapper = new SMBMemoryMapper(ref emu);

            // Create a new Neat Player
            SMBNeatPlayer neatPlayer = new SMBNeatPlayer(box, ref emu.Controller);

            // Until Mario becomes stuck or dies feed each input frame into the neural network and make a move!
            int levelX = GetPlayerStats(mapper).PlayerX;

            while (GetGameStats(mapper).Lives >= 2 && idleTime >= 0)
            {
                for (int i = 1; i <= 60; i++)
                {
                    // Process 1 frame of the game
                    emu.PPU.ProcessFrame();

                    if (i % playerSpeed == 0)
                    {
                        // Refresh the memory mapper
                        mapper.Refresh();
                        int currLevelX = GetPlayerStats(mapper).PlayerX;

                        // Evaluate inputs and make a moves
                        neatPlayer.MakeMove(mapper.FetchInputs());

                        // Check whether Mario is advancing
                        if (levelX >= currLevelX)
                        {
                            idleTime--;
                        }

                        // Update marios X progress
                        levelX = currLevelX;

                        //Console.WriteLine("levelX: " + levelX);
                    }
                }

                // Refresh the memory mapper before evalutating while loop
                mapper.Refresh();
            }

            // Assign fitness eval vars
            int    X       = GetPlayerStats(mapper).PlayerX;
            int    S       = GetGameStats(mapper).Score;
            int    T       = GetGameStats(mapper).Time;
            double fitness = 0;

            // Update eval count ?? what is this even doing?
            _evalCount++;

            // Get the fitness score of the run
            fitness = CalculateFinalFitness(X, S, T);

            Console.WriteLine("X: " + X + "\t S: " + S + "\t T: " + T + "\t fitness: " + fitness);

            // Return the fitness score
            return(new FitnessInfo(fitness, fitness));
        }
예제 #3
0
 private SMBMemoryMapper.GameStats GetGameStats(SMBMemoryMapper mapper)
 {
     SMBMemoryMapper.GameStats gameStats = (SMBMemoryMapper.GameStats)mapper.FetchGameStats();
     return(gameStats);
 }