Example #1
0
        public override void PerformIteration()
        {
            foreach (NeuroChromosome chromosome in Population)
            {
                for (int j = 0; j < TestGameEnvironment.Agents.Count; j++)
                {
                    TestGameEnvironment.Agents[j] = new EnvironmentAgent(Random.Next(90, 100), Random.Next(90, 100),
                                                                         Random.Next(0, 360), Color.Beige,
                                                                         new NeuralNetwork())
                    {
                        HarvestedFood = 1
                    };
                    TestGameEnvironment.Agents[j].Brain.SetNetworkState(chromosome);
                }

                for (var i = 0; i < 1000; i++)
                {
                    TestGameEnvironment.CalculateAgentsEnvironmentParameters();
                    TestGameEnvironment.GetHarvestedFood();

                    for (int index = 0; index < TestGameEnvironment.Agents.Count; index++)
                    {
                        var agent = TestGameEnvironment.Agents[index];
                        if (TestGameEnvironment.CanMove(agent))
                        {
                            agent.Move();
                        }
                    }
                }

                chromosome.FittnessValue = TestGameEnvironment.Agents.Sum(x => x.HarvestedFood);
            }

            var selection = Selection.Select(Population);
            var crossover = Crossover.Perform(selection);

            foreach (var chromosome in crossover)
            {
                Mutation.Mutate(chromosome);
            }

            Population.Remove(Population.FirstOrDefault(
                                  x => x.FittnessValue == Population.Min(y => y.FittnessValue)));
            Population.Remove(Population.FirstOrDefault(
                                  x => x.FittnessValue == Population.Min(y => y.FittnessValue)));

            Population.AddRange(crossover);
        }