|
panga
|
Public Types | |
| enum | CrossoverType : uint8_t { CrossoverType::OnePoint = 1, CrossoverType::TwoPoint, CrossoverType::KPoint, CrossoverType::Uniform } |
| enum | MutatorType : uint8_t { MutatorType::Flip = 1 } |
| enum | SelectorType : uint8_t { SelectorType::Rank = 1, SelectorType::Uniform, SelectorType::RouletteWheel, SelectorType::Tournament } |
| enum | MutationRateSchedule : uint8_t { MutationRateSchedule::Constant = 1, MutationRateSchedule::Deterministic, MutationRateSchedule::SelfAdaptive, MutationRateSchedule::Proportional } |
Public Member Functions | |
| GeneticAlgorithm (const GeneticAlgorithm &rhs)=delete | |
| GeneticAlgorithm & | operator= (const GeneticAlgorithm &rhs)=delete |
| Genome & | GetGenome () |
| void | SetCrossoverIgnoreGeneBoundaries (bool crossover_ignore_gene_boundaries) |
| bool | GetCrossoverIgnoreGeneBoundaries () const |
| void | SetAllowSameParentCouples (bool allow_same_parent_couples) |
| bool | GetAllowSameParentCouples () const |
| void | SetTournamentSize (size_t tournament_size) |
| size_t | GetTournamentSize () const |
| void | SetKPointCrossoverPointCount (size_t k_point_crossover_point_count) |
| size_t | GetKPointCrossoverPointCount () const |
| void | SetSelfAdaptiveMutationDiversityFloor (double self_adaptive_mutation_diversity_floor) |
| double | GetSelfAdaptiveMutationDiversityFloor () const |
| void | SetSelfAdaptiveMutationAggressiveRate (double self_adaptive_mutation_aggressive_rate) |
| double | GetSelfAdaptiveMutationAggressiveRate () const |
| void | SetProportionalMutationBitCount (size_t proportional_mutation_bit_count) |
| size_t | GetProportionalMutationBitCount () const |
| void | SetTotalGenerations (size_t total_generations) |
| size_t | GetTotalGenerations () const |
| void | SetMutationRate (double mutation_rate) |
| double | GetMutationRate () const |
| void | SetCrossoverRate (double crossover_rate) |
| double | GetCrossoverRate () const |
| void | SetCrossoverType (CrossoverType crossover_type) |
| CrossoverType | GetCrossoverType () const |
| void | SetSelectorType (SelectorType selector_type) |
| SelectorType | GetSelectorType () const |
| void | SetEliteCount (size_t elite_count) |
| size_t | GetEliteCount () const |
| void | SetMutatedEliteCount (size_t mutated_elite_count) |
| size_t | GetMutatedEliteCount () const |
| void | SetMutatedEliteMutationRate (double mutated_elite_mutation_rate) |
| double | GetMutatedEliteMutationRate () const |
| void | SetMutationRateSchedule (MutationRateSchedule mutation_rate_schedule) |
| MutationRateSchedule | GetMutationRateSchedule () const |
| void | SetMutatorType (MutatorType mutator_type) |
| MutatorType | GetMutatorType () const |
| void | SetUserData (void *user_data) |
| void * | GetUserData () const |
| void | SetFitnessFunction (FitnessFunction fitness_function) |
| FitnessFunction | GetFitnessFunction () const |
| void | SetPopulationSize (size_t population_size) |
| size_t | GetPopulationSize () const |
| size_t | GetCurrentGeneration () const |
| const Population & | GetPopulation () const |
| void | Initialize () |
| void | SetInitialPopulation (const std::vector< BitVector > &initial_population) |
| void | Step () |
| void | Run () |
Protected Member Functions | |
| void | Crossover (const Individual &parent1, const Individual &parent2, Individual *offspring) |
| void | Mutate (Individual *individual, double mutation_percentage) |
| double | GetCurrentMutationRate () |
| void | InitializeSelector (Population *population) |
| std::pair< const Individual &, const Individual & > | SelectParents (const Population &population) |
| const Individual & | SelectOne (const Population &population) |
| Population & | GetCurrentPopulation () |
| Population & | GetLastGenerationPopulation () |
|
strong |
Type of crossover we will perform on selected parents in order to produce offspring for the next generation.
Actual crossover is performed by the Chromosome itself.
| Enumerator | |
|---|---|
| OnePoint | Perform one-point crossover.
|
| TwoPoint | Perform two-point crossover.
|
| KPoint | Perform k-point crossover.
|
| Uniform | Perform uniform crossover.
|
|
strong |
Method by which the mutation rate is adjusted over the course of running the genetic algorithm.
| Enumerator | |
|---|---|
| Constant | The mutation rate is constant and does not vary from generation to generation.
|
| Deterministic | The mutation rate varies by generation.
|
| SelfAdaptive | Attempt to increase mutation when the population converges too much. Uses the population diversity score as a metric.
|
| Proportional | Calculates the mutation rate required to flip a number of bits in the chromosome.
|
|
strong |
The type of mutation we will perform on new individuals when they are added to the next generation population.
| Enumerator | |
|---|---|
| Flip | Perform the flip mutator.
|
|
strong |
Algorithm we want to use when we select individuals from the current population to become parents of an offspring in the next generation population.
Actual selection is done by the Population itself.
| Enumerator | |
|---|---|
| Rank | Rank selector.
|
| Uniform | Uniform selector.
|
| RouletteWheel | Roulette wheel selector. |
| Tournament | Tournament selector.
|
|
protected |
Uses the selected crossover operator to construct |offspring| based on |parent1| and |parent2|.
| size_t panga::GeneticAlgorithm::GetCurrentGeneration | ( | ) | const |
Return the current generation.
The generation increases after every step.
|
protected |
Get the mutation rate we should use for the current generation.
Note: May depend on the previous population being evaluated so the value returned may be undefined when called while the initial population is the current population. ie: This rate may only make sense during generations greater than 0.
|
protected |
We store two populations and alternate between them between generations. In this way, the previous and current generations are always available.
This function will always return the current population.
The current population is the one which is being built, evaluated, and scored during the current generation of the genetic algorithm.
| Genome & panga::GeneticAlgorithm::GetGenome | ( | ) |
Get the writable genome we will use to construct Individuals which make up the populations of each generation. Note: Don't change the genome while the genetic algorithm is running.
|
protected |
We store two populations and alternate between them between generations. In this way, the previous and current generations are always available.
This function will always return the population from the previous generation.
The previous generation population is the one which was built, evaluated, and scored during the previous generation of the genetic algorithm.
| const Population & panga::GeneticAlgorithm::GetPopulation | ( | ) | const |
Get the current genetic algorithm population.
| void panga::GeneticAlgorithm::Initialize | ( | ) |
Initialize the state of the GeneticAlgorithm.
Initializes missing population members randomly.
|
protected |
If the selector we're using requires some initialization based on the population, this function will perform that initialization.
|
protected |
Uses the selected mutation operator to mutate |individual| by |mutation_percentage|.
| void panga::GeneticAlgorithm::Run | ( | ) |
Run the GeneticAlgorithm totalGenerations steps.
|
protected |
Uses the selector to choose one Individual from |population|.
|
protected |
Uses the selector to choose a pair of individuals from |population|.
Respects the allow_same_parent_couples_ flag to enable/disable choosing the same individual for both parents.
| void panga::GeneticAlgorithm::SetAllowSameParentCouples | ( | bool | allow_same_parent_couples | ) |
When we choose parents to use for crossover, should we allow the same Individual to be used as both parents?
This could be useful because crossover returns the same parent if both parents are identical.
If this flag is false, we will not allow the same Individual to be selected as both parents when we select a couple for crossover.
If this flag is true, it is possible that both parents in the couple selected for crossover will be the same Individual. This flag is true by default.
| void panga::GeneticAlgorithm::SetCrossoverIgnoreGeneBoundaries | ( | bool | crossover_ignore_gene_boundaries | ) |
When we perform crossover, should we respect gene boundaries such that all of the bits making up a gene are copied from one parent?
This could be useful if the bits underlying the gene don't make as much sense when they are split up.
If this flag is false, we will respect the boundary of each gene and not split a gene up during crossover.
If this flag is true, we will ignore gene boundaries during crossover and treat the chromosome as a big chunk of binary data which will be split according to the crossover operator chosen.
In general, this flag should be left at the default of true.
| void panga::GeneticAlgorithm::SetCrossoverRate | ( | double | crossover_rate | ) |
Set the crossover rate.
During each step, construct new individuals for the next generation.
We will select two Individuals and perform crossover to produce the offspring in the next generation with a crossover rate chance.
Instead of crossover, we will copy one Individual from the old population over into the new one with (1 - crossover rate) chance.
| void panga::GeneticAlgorithm::SetCrossoverType | ( | CrossoverType | crossover_type | ) |
Set the crossover type.
We will use this type to crossover two parent individuals in order to produce offspring for the next generation.
| void panga::GeneticAlgorithm::SetEliteCount | ( | size_t | elite_count | ) |
When constructing the next generation, always copy some of the best Individuals from the current generation. These elite Individuals are copied as they are and are not crossed-over or mutated.
| eliteCount | Number of elites. |
| void panga::GeneticAlgorithm::SetFitnessFunction | ( | FitnessFunction | fitness_function | ) |
Set the fitness function used to evaluate the fitness of each Individual.
The fitness function takes a pointer to an Individual and a void* which is the user data previously set via GeneticAlgorithm::SetUserData.
| void panga::GeneticAlgorithm::SetInitialPopulation | ( | const std::vector< BitVector > & | initial_population | ) |
Initialize the GeneticAlgorithm population based on |initial_population|.
Note: We need to call this before calling Initialize().
| initial_population | We will construct new population members and interpret these values as binary chromosome data. |
| void panga::GeneticAlgorithm::SetKPointCrossoverPointCount | ( | size_t | k_point_crossover_point_count | ) |
Set the number of points we'll use to cut up the parent chromosomes during k-point crossover.
| void panga::GeneticAlgorithm::SetMutatedEliteCount | ( | size_t | mutated_elite_count | ) |
Mutated elites are the same as ordinary elite Individuals except they are subjected to the mutation operator according to mutatedEliteMutationRate.
| void panga::GeneticAlgorithm::SetMutatedEliteMutationRate | ( | double | mutated_elite_mutation_rate | ) |
Set the rate at which mutated elite individuals will be mutated.
| void panga::GeneticAlgorithm::SetMutationRate | ( | double | mutation_rate | ) |
Set the mutation rate.
After we perform crossover, mutate each offspring Individual by this rate according to the mutation operator.
During execution, the actual mutation rate is calculated based on the mutation rate schedule.
| void panga::GeneticAlgorithm::SetMutationRateSchedule | ( | MutationRateSchedule | mutation_rate_schedule | ) |
Set the mutation rate schedule we will use to determine the mutation rate over the course of many generations.
| void panga::GeneticAlgorithm::SetMutatorType | ( | MutatorType | mutator_type | ) |
Set the mutator type.
We will use the mutator to mutate each new offspring before adding it to the next generation population.
| void panga::GeneticAlgorithm::SetPopulationSize | ( | size_t | population_size | ) |
Each generation, we will construct and evaluate |population_size| Individuals.
| void panga::GeneticAlgorithm::SetProportionalMutationBitCount | ( | size_t | proportional_mutation_bit_count | ) |
Set the number of bits we should attempt to mutate with each mutation operation.
| void panga::GeneticAlgorithm::SetSelectorType | ( | SelectorType | selector_type | ) |
Set the selector type.
We will use the selector specified to select two parent individuals on which to perform crossover to produce offspring for the next generation.
| void panga::GeneticAlgorithm::SetSelfAdaptiveMutationAggressiveRate | ( | double | self_adaptive_mutation_aggressive_rate | ) |
Set the aggressive mutation rate we will switch to when the population diversity falls below the self-adaptive mutation diversity floor.
| void panga::GeneticAlgorithm::SetSelfAdaptiveMutationDiversityFloor | ( | double | self_adaptive_mutation_diversity_floor | ) |
Set the floor value for the population diversity, below which we will more aggressively mutate the population.
| void panga::GeneticAlgorithm::SetTotalGenerations | ( | size_t | total_generations | ) |
| void panga::GeneticAlgorithm::SetTournamentSize | ( | size_t | tournament_size | ) |
Set the number of Individuals we should include in the tournament when using tournament selector.
| void panga::GeneticAlgorithm::SetUserData | ( | void * | user_data | ) |
Set some data which will be passed into the fitness function called to score each Individual.
| void panga::GeneticAlgorithm::Step | ( | ) |
Perform one step of the genetic algorithm:
1.8.17