src/GenFx.Components/Algorithms/MultiDemeGeneticAlgorithm.cs
/// <summary>
/// Migrates the <see cref="GeneticEntity"/> objects with the best fitness between populations.
/// </summary>
Migrates the  objects with the best fitness between populations.
Migrates the objects with the best fitness between populations .
protected virtual void OnMigrate()
        {
            this.AssertIsInitialized();
            IList<Population> populations = this.Environment!.Populations;
            List<GeneticEntity> migrantGeneticEntities = new List<GeneticEntity>(this.MigrantCount);
            for (int i = 0; i < this.MigrantCount; i++)
            {
                GeneticEntity[] sortedEntities = populations[0].Entities.GetEntitiesSortedByFitness(
                    this.SelectionOperator!.SelectionBasedOnFitnessType,
                    this.FitnessEvaluator!.EvaluationMode).ToArray();
                migrantGeneticEntities.Add(sortedEntities[sortedEntities.Length - i - 1]);
            }
            for (int populationIndex = 1; populationIndex < populations.Count; populationIndex++)
            {
                Population population = populations[populationIndex];
                List<GeneticEntity> sortedEntities = population.Entities.GetEntitiesSortedByFitness(
                    this.SelectionOperator!.SelectionBasedOnFitnessType,
                    this.FitnessEvaluator!.EvaluationMode).ToList();
                for (int entityIndex = 0; entityIndex < this.MigrantCount; entityIndex++)
                {
                    population.Entities.Add(migrantGeneticEntities[entityIndex]);
                    sortedEntities.Add(migrantGeneticEntities[entityIndex]);
                    GeneticEntity migrant = sortedEntities[sortedEntities.Count - entityIndex - 2];
                    migrantGeneticEntities[entityIndex] = migrant;
                    population.Entities.Remove(migrant);
                    sortedEntities.Remove(migrant);
                }
            }
            Population firstPopulation = populations[0];
            List<GeneticEntity> firstPopulationSortedEntities = firstPopulation.Entities.GetEntitiesSortedByFitness(
                this.SelectionOperator!.SelectionBasedOnFitnessType,
                this.FitnessEvaluator!.EvaluationMode).ToList();
            for (int entityIndex = 0; entityIndex < this.MigrantCount; entityIndex++)
            {
                firstPopulation.Entities.Add(migrantGeneticEntities[entityIndex]);
                firstPopulationSortedEntities.Add(migrantGeneticEntities[entityIndex]);
                GeneticEntity replacedEntity = firstPopulationSortedEntities[firstPopulationSortedEntities.Count - entityIndex - 2];
                firstPopulation.Entities.Remove(replacedEntity);
                firstPopulationSortedEntities.Remove(replacedEntity);
            }
        }
protected virtual void OnMigrate ( ) { this . AssertIsInitialized ( ) ; IList < Population > populations = this . Environment ! . Populations ; List < GeneticEntity > migrantGeneticEntities = new List < GeneticEntity > ( this . MigrantCount ) ; for ( int i = 0 ; i < this . MigrantCount ; i ++ ) { GeneticEntity [ ] sortedEntities = populations [ 0 ] . Entities . GetEntitiesSortedByFitness ( this . SelectionOperator ! . SelectionBasedOnFitnessType , this . FitnessEvaluator ! . EvaluationMode ) . ToArray ( ) ; migrantGeneticEntities . Add ( sortedEntities [ sortedEntities . Length - i - 1 ] ) ; } for ( int populationIndex = 1 ; populationIndex < populations . Count ; populationIndex ++ ) { Population population = populations [ populationIndex ] ; List < GeneticEntity > sortedEntities = population . Entities . GetEntitiesSortedByFitness ( this . SelectionOperator ! . SelectionBasedOnFitnessType , this . FitnessEvaluator ! . EvaluationMode ) . ToList ( ) ; for ( int entityIndex = 0 ; entityIndex < this . MigrantCount ; entityIndex ++ ) { population . Entities . Add ( migrantGeneticEntities [ entityIndex ] ) ; sortedEntities . Add ( migrantGeneticEntities [ entityIndex ] ) ; GeneticEntity migrant = sortedEntities [ sortedEntities . Count - entityIndex - 2 ] ; migrantGeneticEntities [ entityIndex ] = migrant ; population . Entities . Remove ( migrant ) ; sortedEntities . Remove ( migrant ) ; } } Population firstPopulation = populations [ 0 ] ; List < GeneticEntity > firstPopulationSortedEntities = firstPopulation . Entities . GetEntitiesSortedByFitness ( this . SelectionOperator ! . SelectionBasedOnFitnessType , this . FitnessEvaluator ! . EvaluationMode ) . ToList ( ) ; for ( int entityIndex = 0 ; entityIndex < this . MigrantCount ; entityIndex ++ ) { firstPopulation . Entities . Add ( migrantGeneticEntities [ entityIndex ] ) ; firstPopulationSortedEntities . Add ( migrantGeneticEntities [ entityIndex ] ) ; GeneticEntity replacedEntity = firstPopulationSortedEntities [ firstPopulationSortedEntities . Count - entityIndex - 2 ] ; firstPopulation . Entities . Remove ( replacedEntity ) ; firstPopulationSortedEntities . Remove ( replacedEntity ) ; } }
Migrates the  objects with the best fitness between populations.
Migrates the objects with the best fitness between populations .
// Build a list of migrant genetic entities from the first population // Migrate genetic entities between populations // Add new migrant // Set entity to be replaced as a new migrant for next population. // Remove the replaced entity. // Use the migrants from the last population and migrate them to the first population. // Add new migrant // Remove the replaced entity.