Step() public method

Moves all agents in the world forward by one step and collects food for any agents on top of a plant.
public Step ( ) : void
return void
Exemplo n.º 1
0
        /// <summary>
        /// Main genome evaluation loop with no phenome caching (decode on each evaluation).
        /// </summary>
        public void Evaluate(IList <TGenome> genomeList)
        {
            _genomeList           = (IList <NeatGenome>)genomeList;
            _agents               = new ForagingAgent[genomeList.Count];
            UpdatesThisGeneration = 0;

            if (TeachParadigm == TeachingParadigm.SubcultureRewardFiltering)
            {
                _rewards         = new List <int>();
                _rewardThreshold = 0;
            }

            if (TeachParadigm == TeachingParadigm.EgalitarianEvolvedAcceptability)
            {
                // Creates acceptability functions from each genome
                GenomesToAcceptability(genomeList);
            }
            else
            {
                // Creates policy networks from each genome
                GenomesToPolicies(genomeList);
            }

            // Divides the population into subcultures
            CreateSubcultures();

            // Logs the diversity of the population before evaluation
            if (LogDiversity)
            {
                writeDiversityStats(true);
            }

            // If we're in a student-teacher model, pre-train the agents
            if (TeachParadigm == TeachingParadigm.GenerationalChampionOfflineTraining)
            {
                trainPopulationUsingGenerationalChampion();
            }

            _world.Agents = _agents;

            CreatePredators();

            _world.Reset();

            for (CurrentTimeStep = 0; CurrentTimeStep < MaxTimeSteps; CurrentTimeStep++)
            {
                // Move the world forward one step
                _world.Step();
            }

            // Set the fitness of each genome to the fitness its phenome earned in the world.
            for (int i = 0; i < _agents.Length; i++)
            {
                // NEAT requires fitness to be >= 0, so if the teacher had negative fitness, we cap it at 0.
                _genomeList[i].EvaluationInfo.SetFitness(Math.Max(0, _agents[i].Fitness));

                // This alternate fitness is purely for logging purposes, so we use the actual fitness
                _genomeList[i].EvaluationInfo.AlternativeFitness = _agents[i].Fitness;
            }

            // Analyze diversity after evaluation
            if (LogDiversity)
            {
                writeDiversityStats(false);
            }

            // Lamarkian Evolution
            if (TeachParadigm == TeachingParadigm.EgalitarianEvolvedAcceptability)
            {
                PerformLamarkianEvolution(genomeList, a => (FastCyclicNetwork)((RecurrentNeuralAcceptability)((SocialAgent)a).AcceptabilityFn).Brain);
            }
            else if (EvoParadigm == EvolutionParadigm.Lamarkian)
            {
                PerformLamarkianEvolution(genomeList);
            }

            // If enabled and it's time, grow the size of the agents' memory window.
            if (MemParadigm == MemoryParadigm.IncrementalGrowth &&
                _generations % GenerationsPerMemorySize == 0 &&
                CurrentMemorySize < MaxMemorySize)
            {
                CurrentMemorySize++;
            }

            _generations++;
            _evaluationCount += (ulong)genomeList.Count;
        }