src/GenFx/GeneticAlgorithm.cs
/// <summary>
/// Applies the <see cref="GenFx.CrossoverOperator"/>, if one is set, to the genetic entities.
/// </summary>
/// <param name="population">Current population.</param>
/// <param name="parents">Parents to which to apply the crossover operation.</param>
/// <returns>List of entities after the crossover was applied.</returns>
Applies the , if one is set, to the genetic entities.
Applies the if one is set to the genetic entities .
protected IList<GeneticEntity> ApplyCrossover(Population population, IList<GeneticEntity> parents)
        {
            if (population == null)
            {
                throw new ArgumentNullException(nameof(population));
            }
            if (parents == null)
            {
                throw new ArgumentNullException(nameof(parents));
            }
            IList<GeneticEntity> childEntities = new List<GeneticEntity>();
            if (this.CrossoverOperator != null)
            {
                Queue<GeneticEntity> parentQueue = new Queue<GeneticEntity>(parents);
                while (childEntities.Count < population.MinimumPopulationSize)
                {
                    if (this.CrossoverOperator.RequiredParentCount > parentQueue.Count)
                    {
                        childEntities.AddRange(parentQueue);
                        parentQueue.Clear();
                        break;
                    }
                    else
                    {
                        List<GeneticEntity> parentSubset = new List<GeneticEntity>();
                        for (int i = 0; i < this.CrossoverOperator.RequiredParentCount; i++)
                        {
                            parentSubset.Add(parentQueue.Dequeue());
                        }
                        childEntities.AddRange(this.CrossoverOperator.Crossover(parentSubset));
                    }
                }
            }
            else
            {
                childEntities = parents;
            }
            return childEntities;
        }
protected IList < GeneticEntity > ApplyCrossover ( Population population , IList < GeneticEntity > parents ) { if ( population == null ) { throw new ArgumentNullException ( nameof ( population ) ) ; } if ( parents == null ) { throw new ArgumentNullException ( nameof ( parents ) ) ; } IList < GeneticEntity > childEntities = new List < GeneticEntity > ( ) ; if ( this . CrossoverOperator != null ) { Queue < GeneticEntity > parentQueue = new Queue < GeneticEntity > ( parents ) ; while ( childEntities . Count < population . MinimumPopulationSize ) { if ( this . CrossoverOperator . RequiredParentCount > parentQueue . Count ) { childEntities . AddRange ( parentQueue ) ; parentQueue . Clear ( ) ; break ; } else { List < GeneticEntity > parentSubset = new List < GeneticEntity > ( ) ; for ( int i = 0 ; i < this . CrossoverOperator . RequiredParentCount ; i ++ ) { parentSubset . Add ( parentQueue . Dequeue ( ) ) ; } childEntities . AddRange ( this . CrossoverOperator . Crossover ( parentSubset ) ) ; } } } else { childEntities = parents ; } return childEntities ; }
Applies the , if one is set, to the genetic entities.
Applies the if one is set to the genetic entities .
// If there are not enough parents left to perform a crossover with, then // just skip the crossover and add the parents to the child list.
List of entities after the crossover was applied.
List of entities after the crossover was applied .
population
Population
None
parents
IList<GeneticEntity>
Parents to which to apply the crossover operation.
Parents to which to apply the crossover operation .