Provide a config object to the evolve method, After which the run method can be used to create a new generation of candidate solutions. All configuration parameters are optional and have defaults provided for them, except for the Fitness Function.
When running the algorithm for the first time, it will generate a random population of chromosomes and determine their fitness value. Each consecutive run will select the fittest chromosomes from the population, use them to generate offspring, mutate the offspring, and add the surviving population and offspring together to create the new population. Afterwards the fitness values of the new population will be calculated.
Configuration
import{evolve}from"evolve-ga";
constalgorithm=evolve({
populationSize:10000,
chromosomeLength:100,
possibleGenes:[0,1,2,3],
mutationChance:0.1,
fitnessFunction:fitnessFunction,
selectionFunction:selectionFunction,
crossOverFunction:crossOverFunction,
mutationFunction:mutationFunction
});
Population Size: The amount of chromosomes the initial population should consist of.
Chromosome Length: The amount of genes a chromosome should contain.
Possible Genes: An array of strings or numbers. These values are used to generate and mutate chromosomes.
Mutation Chance: A number between 0 and 1. The higher the number, the higher the chance of a mutation occuring.
Fitness Function
The function that's used to determine a chromosomes fitness value. It receives a chromosome as parameter and returns a number (fitness value).
Signature:
(chromosome:Chromosome)=>number
Selection Function
The function that's used for selecting chromosomes to keep for the next generation and to generate offspring with. It receives the current population and returns an array of the fittest chromosomes.
The function that's used for generating offspring from selected chromosomes. It receives the chromosomes that resulted from the Selection Function and returns an array of offspring.
The function that's used for mutating chromosomes. It receives chromosomes that result from the CrossOver Function and trigger a mutation, and returns a mutated chromosome.