AForge.Genetic.Population.Migrate C# (CSharp) Метод

Migrate() публичный Метод

Perform migration between two populations.

The method performs migration between two populations - current and the specified one. During migration specified number of chromosomes is choosen from each population using specified selection algorithms and put into another population replacing worst members there.

public Migrate ( Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector ) : void
anotherPopulation Population Population to do migration with.
numberOfMigrants int Number of chromosomes from each population to migrate.
migrantsSelector ISelectionMethod Selection algorithm used to select chromosomes to migrate.
Результат void
        public void Migrate( Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector )
        {
            int currentSize = this.size;
            int anotherSize = anotherPopulation.Size;

            // create copy of current population
            List<IChromosome> currentCopy = new List<IChromosome>( );

            for ( int i = 0; i < currentSize; i++ )
            {
                currentCopy.Add( population[i].Clone( ) );
            }

            // create copy of another population
            List<IChromosome> anotherCopy = new List<IChromosome>( );

            for ( int i = 0; i < anotherSize; i++ )
            {
                anotherCopy.Add( anotherPopulation.population[i].Clone( ) );
            }

            // apply selection to both populations' copies - select members to migrate
            migrantsSelector.ApplySelection( currentCopy, numberOfMigrants );
            migrantsSelector.ApplySelection( anotherCopy, numberOfMigrants );

            // sort original populations, so the best chromosomes are in the beginning
            population.Sort( );
            anotherPopulation.population.Sort( );

            // remove worst chromosomes from both populations to free space for new members
            population.RemoveRange( currentSize - numberOfMigrants, numberOfMigrants );
            anotherPopulation.population.RemoveRange( anotherSize - numberOfMigrants, numberOfMigrants );

            // put migrants to corresponding populations
            population.AddRange( anotherCopy );
            anotherPopulation.population.AddRange( currentCopy );

            // find best chromosomes in each population
            FindBestChromosome( );
            anotherPopulation.FindBestChromosome( );
        }