social_learning.World.Step C# (CSharp) Method

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
        public void Step()
        {
            // If no one set a lookup table yet, generate one.
            if (_sensorDictionary == null)
                _sensorDictionary = new SensorDictionary((int)AgentHorizon, Height, Width);

            // Advance each teacher by one step
            foreach (var agent in Agents)
            {
                var sensors = calculateForagingAgentSensors(agent);

                agent.Step(sensors);
                applyToroidalAgentLocationRules(agent);
            }

            // Advance each predator by one step
            foreach (var predator in Predators)
            {
                var sensors = calculatePredatorSensors(predator);
                predator.Step(sensors);
                applyToroidalAgentLocationRules(predator);
            }

            // Make a separate pass over all the agents, now that they're in new locations
            // and determine who is on top of a plant and who is on top of a predator.
            foreach (var agent in Agents)
            {
                foreach (var predator in Predators)
                {
                    if (agent.HidingMode != predator.AttackType &&
                        _sensorDictionary.getDistanceAndOrientation((int)agent.X, (int)agent.Y, (int)predator.X, (int)predator.Y)[0]
                        < agent.Radius && !agent.EatenByRecently(_step, AGENT_GHOST_TIME, predator))
                    {
                        // Eat the agent
                        agent.EatenBy(predator, _step);

                        // Notify the predator that it received a reward
                        predator.ReceiveReward(agent.Reward);

                        // Notify the agent that it's been eaten.
                        agent.ReceiveReward(-agent.Reward);

                        // Notify listeners that we gobbled up this agent
                        onAgentEaten(predator, agent);
                    }
                }
                foreach (var plant in Plants)
                    if (_sensorDictionary.getDistanceAndOrientation((int)agent.X, (int)agent.Y, (int)plant.X, (int)plant.Y)[0]
                        < plant.Radius && plant.AvailableForEating(agent))
                    {
                        // Eat the plant
                        plant.EatenBy(agent, _step);

                        // Notify the teacher that it received a reward
                        agent.ReceiveReward(plant.Reward);

                        // Notify listeners that someone has eaten a plant.
                        onPlantEaten(agent, plant);
                    }

                agent.Fitness += StepReward;
            }

            // Notify listeners that the world has stepped and changed.
            onStepped(EventArgs.Empty);
            onChanged(EventArgs.Empty);
            _step++;
        }

Usage Example

Example #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;
        }