public override void Evolve()
        {
            int individualsToEliminate = (int)(_generationGap * _populationSize);

            // A single generation is when GenerationGap % of population is eliminated and replaced
            IList <T> offspringPopulation = new List <T>();

            while (offspringPopulation.Count < individualsToEliminate)
            {
                IList <T> matingPool = new List <T>(CrossoverOperator.NumberOfParents);
                for (int j = 0; j < CrossoverOperator.NumberOfParents; j++)
                {
                    T mate = SelectionOperator.Execute(_population);
                    matingPool.Add(mate);
                }

                IList <T> children = CrossoverOperator.Execute(matingPool);

                for (int j = 0; j < children.Count; j++)
                {
                    MutationOperator.Execute(children[j]);
                    _problem.Evaluate(children[j]);
                    offspringPopulation.Add(children[j]);
                }
            }

            offspringPopulation = offspringPopulation.Take(individualsToEliminate).ToList();

            _population = ReplacementOperator.Execute(_population, offspringPopulation);

            UpdateState();
        }
Ejemplo n.º 2
0
        public override void Evolve()
        {
            IList <T> offspringPopulation = new List <T>();

            // Let elites survive to next generation
            int       numberOfElites = (int)(_elitismRate * _populationSize);
            IList <T> elites         = _population
                                       .OrderByDescending(c => c, _problem.FitnessComparer)
                                       .Take(numberOfElites).ToList();

            foreach (T individual in elites)
            {
                offspringPopulation.Add(individual);
            }

            while (offspringPopulation.Count < _populationSize)
            {
                IList <T> matingPool = new List <T>(CrossoverOperator.NumberOfParents);
                for (int i = 0; i < CrossoverOperator.NumberOfParents; i++)
                {
                    matingPool.Add(SelectionOperator.Execute(_population));
                }

                IList <T> children = CrossoverOperator.Execute(matingPool);

                for (int i = 0; i < children.Count; i++)
                {
                    MutationOperator.Execute(children[i]);
                    _problem.Evaluate(children[i]);
                    offspringPopulation.Add(children[i]);
                }
            }

            // When there is some excess chromosomes, discard them by taking only the needed ones
            _population = offspringPopulation.Take(_populationSize).ToList();

            UpdateState();
        }