C# Class AForge.Genetic.Population

Population of chromosomes.

The class represents population - collection of individuals (chromosomes) and provides functionality for common population's life cycle - population growing with help of genetic operators and selection of chromosomes to new generation with help of selection algorithm. The class may work with any type of chromosomes implementing IChromosome interface, use any type of fitness functions implementing IFitnessFunction interface and use any type of selection algorithms implementing ISelectionMethod interface.

Show file Open project: holisticware-admin/MonoVersal.AForgeNET Class Usage Examples

Public Methods

Method Description
AddChromosome ( IChromosome chromosome ) : void

Add chromosome to the population.

The method adds specified chromosome to the current population. Manual adding of chromosome maybe useful, when it is required to add some initialized chromosomes instead of random.

Adding chromosome manually should be done very carefully, since it may break the population. The manually added chromosome must have the same type and initialization parameters as the ancestor passed to constructor.

Crossover ( ) : void

Do crossover in the population.

The method walks through the population and performs crossover operator taking each two chromosomes in the order of their presence in the population. The total amount of paired chromosomes is determined by crossover rate.

Migrate ( Population anotherPopulation, int numberOfMigrants, ISelectionMethod migrantsSelector ) : void

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.

Mutate ( ) : void

Do mutation in the population.

The method walks through the population and performs mutation operator taking each chromosome one by one. The total amount of mutated chromosomes is determined by mutation rate.

Population ( int size, IChromosome ancestor, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod ) : System

Initializes a new instance of the Population class.

Creates new population of specified size. The specified ancestor becomes first member of the population and is used to create other members with same parameters, which were used for ancestor's creation.

Regenerate ( ) : void

Regenerate population.

The method regenerates population filling it with random chromosomes.

Resize ( int newPopulationSize ) : void

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 population's selection method is used to reduce the population.

Resize ( int newPopulationSize, ISelectionMethod membersSelector ) : void

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.

RunEpoch ( ) : void

Run one epoch of the population.

The method runs one epoch of the population, doing crossover, mutation and selection by calling Crossover, Mutate and Selection.

Selection ( ) : void

Do selection.

The method applies selection operator to the current population. Using specified selection algorithm it selects members to the new generation from current generates and adds certain amount of random members, if is required (see RandomSelectionPortion).

Shuffle ( ) : void

Shuffle randomly current population.

Population shuffling may be useful in cases when selection operator results in not random order of chromosomes (for example, after elite selection population may be ordered in ascending/descending order).

this ( int index ) : IChromosome

Get chromosome with specified index.

Allows to access individuals of the population.

Private Methods

Method Description
FindBestChromosome ( ) : void

Method Details

AddChromosome() public method

Add chromosome to the population.

The method adds specified chromosome to the current population. Manual adding of chromosome maybe useful, when it is required to add some initialized chromosomes instead of random.

Adding chromosome manually should be done very carefully, since it may break the population. The manually added chromosome must have the same type and initialization parameters as the ancestor passed to constructor.

public AddChromosome ( IChromosome chromosome ) : void
chromosome IChromosome Chromosome to add to the population.
return void

Crossover() public method

Do crossover in the population.
The method walks through the population and performs crossover operator taking each two chromosomes in the order of their presence in the population. The total amount of paired chromosomes is determined by crossover rate.
public Crossover ( ) : void
return void

Migrate() public method

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.
return void

Mutate() public method

Do mutation in the population.
The method walks through the population and performs mutation operator taking each chromosome one by one. The total amount of mutated chromosomes is determined by mutation rate.
public Mutate ( ) : void
return void

Population() public method

Initializes a new instance of the Population class.
Creates new population of specified size. The specified ancestor becomes first member of the population and is used to create other members with same parameters, which were used for ancestor's creation.
Too small population's size was specified. The /// exception is thrown in the case if is smaller than 2.
public Population ( int size, IChromosome ancestor, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod ) : System
size int Initial size of population.
ancestor IChromosome Ancestor chromosome to use for population creatioin.
fitnessFunction IFitnessFunction Fitness function to use for calculating /// chromosome's fitness values.
selectionMethod ISelectionMethod Selection algorithm to use for selection /// chromosome's to new generation.
return System

Regenerate() public method

Regenerate population.
The method regenerates population filling it with random chromosomes.
public Regenerate ( ) : void
return void

Resize() public method

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 population's 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 ) : void
newPopulationSize int New size of population.
return void

Resize() public method

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.
return void

RunEpoch() public method

Run one epoch of the population.
The method runs one epoch of the population, doing crossover, mutation and selection by calling Crossover, Mutate and Selection.
public RunEpoch ( ) : void
return void

Selection() public method

Do selection.
The method applies selection operator to the current population. Using specified selection algorithm it selects members to the new generation from current generates and adds certain amount of random members, if is required (see RandomSelectionPortion).
public Selection ( ) : void
return void

Shuffle() public method

Shuffle randomly current population.

Population shuffling may be useful in cases when selection operator results in not random order of chromosomes (for example, after elite selection population may be ordered in ascending/descending order).

public Shuffle ( ) : void
return void

this() public method

Get chromosome with specified index.
Allows to access individuals of the population.
public this ( int index ) : IChromosome
index int Chromosome's index to retrieve.
return IChromosome