Ejemplo n.º 1
0
        /// <summary>
        /// Main process loop for performing a crossover on a population.
        /// </summary>
        protected void Process()
        {
            int maxLoop    = 100;
            int eliteCount = 0;

            //reset the number of evaluations
            _evaluations = 0;

            //now that we know how many children we are to be creating, in the case of 'Delete Last'
            //we copy the current generation across and add new children where they are greater than
            //the worst solution.
            if (_replacementMethodS == ReplacementMethod.DeleteLast)
            {
                //copy everything accross including the elites
                NewPopulation.Solutions.Clear();
                NewPopulation.Solutions.AddRange(CurrentPopulation.Solutions);
            }
            else
            {
                //just copy the elites, this will take all elites

                //TODO: Sort out what we do if we overfill the population with elites
                var elites = CurrentPopulation.GetElites();
                eliteCount = elites.Count();
                if (elites != null && eliteCount > 0)
                {
                    NewPopulation.Solutions.AddRange(elites);
                }
            }
            _currentPopulationSize = CurrentPopulation.Solutions.Count;

            _numberOfChildrenToGenerate =
                _currentPopulationSize - eliteCount;

            while (_numberOfChildrenToGenerate > 0)
            {
                //emergency exit
                if (maxLoop <= 0)
                {
                    throw new ChromosomeException("Unable to create a suitable child. If duplicates have been disallowed then consider increasing the chromosome length or increasing the number of elites.");
                }

                //these will hold the children
                Chromosome c1 = null;
                Chromosome c2 = null;

                //select some parents
                var parents = CurrentPopulation.SelectParents();
                var p1      = parents[0];
                var p2      = parents[1];

                //crossover
                PerformCrossover(p1, p2, CrossoverProbability, out c1, out c2);

                if (AddChild(c1))
                {
                    _numberOfChildrenToGenerate--;
                }
                else
                {
                    //unable to create child
                    maxLoop--;
                }

                //see if we can add the secomd
                if (_numberOfChildrenToGenerate > 0)
                {
                    if (AddChild(c2))
                    {
                        _numberOfChildrenToGenerate--;
                    }
                    else
                    {
                        //unable to create child
                        maxLoop--;
                    }
                }
            }
        }