src/GenFx.Components/Lists/SinglePointCrossoverOperator.cs
/// <summary>
/// When overriden in a derived class, generates a crossover based on the parent entities.
/// </summary>
/// <param name="parents">The <see cref="GeneticEntity"/> objects to be operated upon.</param>
/// <returns>
/// Collection of the <see cref="GeneticEntity"/> objects resulting from the crossover.
/// </returns>
When overriden in a derived class, generates a crossover based on the parent entities.
When overriden in a derived class generates a crossover based on the parent entities .
protected override IEnumerable<GeneticEntity> GenerateCrossover(IList<GeneticEntity> parents)
        {
            if (parents == null)
            {
                throw new ArgumentNullException(nameof(parents));
            }
            ListEntityBase listEntity1 = (ListEntityBase)parents[0];
            ListEntityBase listEntity2 = (ListEntityBase)parents[1];
            int entity1Length = listEntity1.Length;
            int entity2Length = listEntity2.Length;
            int crossoverLocus = RandomNumberService.Instance.GetRandomValue(Math.Min(entity1Length, entity2Length));
            IList<GeneticEntity> crossoverOffspring = new List<GeneticEntity>();
            int maxLength = Math.Max(entity1Length, entity2Length);
            if (entity1Length != entity2Length)
            {
                listEntity1.Length = maxLength;
                listEntity2.Length = maxLength;
            }
            for (int i = crossoverLocus; i < maxLength; i++)
            {
                object? tempGeneValue = listEntity1.GetValue(i);
                listEntity1.SetValue(i, listEntity2.GetValue(i));
                listEntity2.SetValue(i, tempGeneValue);
            }
            listEntity1.Length = entity2Length;
            listEntity2.Length = entity1Length;
            crossoverOffspring.Add(listEntity1);
            crossoverOffspring.Add(listEntity2);
            return crossoverOffspring;
        }
protected override IEnumerable < GeneticEntity > GenerateCrossover ( IList < GeneticEntity > parents ) { if ( parents == null ) { throw new ArgumentNullException ( nameof ( parents ) ) ; } ListEntityBase listEntity1 = ( ListEntityBase ) parents [ 0 ] ; ListEntityBase listEntity2 = ( ListEntityBase ) parents [ 1 ] ; int entity1Length = listEntity1 . Length ; int entity2Length = listEntity2 . Length ; int crossoverLocus = RandomNumberService . Instance . GetRandomValue ( Math . Min ( entity1Length , entity2Length ) ) ; IList < GeneticEntity > crossoverOffspring = new List < GeneticEntity > ( ) ; int maxLength = Math . Max ( entity1Length , entity2Length ) ; if ( entity1Length != entity2Length ) { listEntity1 . Length = maxLength ; listEntity2 . Length = maxLength ; } for ( int i = crossoverLocus ; i < maxLength ; i ++ ) { object ? tempGeneValue = listEntity1 . GetValue ( i ) ; listEntity1 . SetValue ( i , listEntity2 . GetValue ( i ) ) ; listEntity2 . SetValue ( i , tempGeneValue ) ; } listEntity1 . Length = entity2Length ; listEntity2 . Length = entity1Length ; crossoverOffspring . Add ( listEntity1 ) ; crossoverOffspring . Add ( listEntity2 ) ; return crossoverOffspring ; }
When overriden in a derived class, generates a crossover based on the parent entities.
When overriden in a derived class generates a crossover based on the parent entities .
// Normalize the lists into a common length // swap the elements of the lists starting at the crossover locus // Set the length based on their swapped length
Collection of the  objects resulting from the crossover.
Collection of the objects resulting from the crossover .
parents
IList<GeneticEntity>
The  objects to be operated upon.
The objects to be operated upon .