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

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

Resize population to the new specified size.

The method does resizing of population. In the case if population should grow, it just adds missing number of random members. In the case if population should get smaller, the specified selection method is used to reduce the population.

Too small population's size was specified. The /// exception is thrown in the case if is smaller than 2.
public Resize ( int newPopulationSize, ISelectionMethod membersSelector ) : void
newPopulationSize int New size of population.
membersSelector ISelectionMethod Selection algorithm to use in the case /// if population should get smaller.
Результат void
        public void Resize( int newPopulationSize, ISelectionMethod membersSelector )
        {
            if ( newPopulationSize < 2 )
                throw new ArgumentException( "Too small new population's size was specified." );

            if ( newPopulationSize > size )
            {
                // population is growing, so add new rundom members

                // Note: we use population.Count here instead of "size" because
                // population may be bigger already after crossover/mutation. So
                // we just keep those members instead of adding random member.
                int toAdd = newPopulationSize - population.Count;

                for ( int i = 0; i < toAdd; i++ )
                {
                    // create new chromosome
                    IChromosome c = population[0].CreateNew( );
                    // calculate it's fitness
                    c.Evaluate( fitnessFunction );
                    // add it to population
                    population.Add( c );
                }
            }
            else
            {
                // do selection
                membersSelector.ApplySelection( population, newPopulationSize );
            }

            size = newPopulationSize;
        }

Same methods

Population::Resize ( int newPopulationSize ) : void