To make this API reference more concise and readable, a few "types" have been introduced. Those "types" are just aliases for some plain JavaScript structures.
Type | Translates to | Description |
---|---|---|
Individual |
Any |
Represents a single individual/chromosome. |
Population |
Array<Individual> |
Array of individuals makes a population. |
EvaluatedPopulation |
Array<{ fitness: number, indvidual: Individual }> |
Array of objects containing an individual and its fitness . |
Rng |
() => number |
Function, which returns random values between 0 and 1 (inclusive of 0, but not 1). |
Runs a genetic algorithm. Returns a promise, which resolves to an object:
{
evaluatedPopulation: EvaluatedPopulation,
iteration: number,
logs: object,
getLowestFitnessIndividual: () => ({ fitness: number, indvidual: Individual }),
getHighestFitnessIndividual: () => ({ fitness: number, indvidual: Individual })
}
Genemo.run
takes a single argument - options
. It is an object with the following properties:
-
generateInitialPopulation
- Generates initial population of individuals (chromosomes).
Type:(Rng) => Population
Required: Yes
Available values:Genemo.generateInitialPopulation()
-
selection
- Selects individuals for breeding.
Type:(EvaluatedPopulation, Rng) => EvaluatedPopulation
Required: Yes
Available values:Genemo.selection.tournament()
,Genemo.selection.rank()
,Genemo.selection.roulette()
-
reproduce
- Creates new population from the selected individuals.
Type:(EvaluatedPopulation, Rng, collectLog) => Population
Required: Yes
Available values:Genemo.reproduce()
-
evaluatePopulation
- Maps an array of individuals to an array of fitness values.
Type:(Population, Rng) => Array<number>
Required: Yes
Available values:Genemo.evaluatePopulation()
-
stopCondition
- Returningtrue
terminates the algorithm.
Type:({ evaluatedPopulation, iteration }, Rng) => boolean
Required: Yes
Available values:Genemo.stopCondition()
-
succession
- Creates a new population based on previous (evaluated) population and current (also evaluated) children population (result ofreproduce
function).
Type:({ prevPopulation, childrenPopulation }, Rng) => EvaluatedPopulation
Required: No
Default:({ childrenPopulation }) => childrenPopulation
Available values:Genemo.elitism()
-
iterationCallback
- Callback, which is called in every iteration.
Type:({ evaluatedPopulation, iteration, logs, getLowestFitnessIndividual, getHighestFitnessIndividual }) => undefined
Required: No
Default:() => {}
Available values:Genemo.logIterationData()
-
random
- Custom random number generator. Should return values between 0 and 1 (inclusive of 0, but not 1).
Type:() => number
Required: No
Default:Math.random
-
maxBlockingTime
- Time in milliseconds, after which the next iteration is called asynchronously (as a macrotask).
Type:number
Required: No
Default:Infinity
-
collectLogs
- Indicates if the logs about performance should be collected.
Type:boolean
Required: No
Default:true
Returned function is applicable to: Genemo.run
's generateInitialPopulation
parameter.
Takes an object with the following properties:
-
generateIndividual
- Function which takes a random number generator as an argument and returns a random individual.
Type:(Rng) => Individual
-
size
- Number of individuals in the population.
Type:number
Returned function is applicable to: Genemo.run
's evaluatePopulation
parameter.
Due to performance reasons, this function doesn't support promises. fitnessFunction must be a synchronous.
evaluatePopulation
takes an object with the following properties:
fitnessFunction
- Function which takes an individual as a parameter and returns its fitness.
Type:(Individual) => number
Returned function is applicable to: Genemo.run
's reproduce
parameter.
Takes an object with the following properties:
-
crossover
- Function which takes a pair of parents and a random number generator and returns a pair of children.
Type:([Individual, Individual], Rng) => [Individual, Individual]
-
mutate
- Function which maps an individual to a new individual modified by mutation
Type:(Individual, Rng) => Individual
-
mutationProbability
- Mutation probability for a single individual.
Type:number
Default value:0.01
Returned function is applicable to: Genemo.run
's reproduce
parameter.
Takes an object with the following properties:
-
crossoverAll
- Function which takes an array of pairs of parents and a random number generator and returns an array of pairs of children.
Type:(Array<[Individual, Individual]>, Rng) => Array<[Individual, Individual]>
-
mutateAll
- Function which maps an array of individuals to a new array of individuals modified by mutation. Only individuals selected for mutation will be passed to this function.
Type:(Individual, Rng) => Individual
-
mutationProbability
- Mutation probability for a single individual.
Type:number
Default value:0.01
Returned function is applicable to: Genemo.run
's stopCondition
parameter.
Takes an object with the following properties:
-
minFitness
- stop when one of the individuals has fitness higher or equalminFitness
Type:number
Optional -
maxFitness
- stop when one of the individuals has fitness lower or equalminFitness
.
Type:number
Optional -
maxIterations
- stop aftermaxIterations
iterations.
Type:number
Optional
Returns a function with a signature matching that of stopCondition
property of Genemo.run
options object. Use minFitness
for maximization problems and maxFitness
for minimization.
Returns a function which logs iteration data (mostly performance data).
Returned function is applicable to: Genemo.run
's iterationCallback
parameter.
Takes an object with the following properties:
-
include
- object indicating what should be logged
Type:object
Properties (all optional):iteration
- Signature:
{ show: boolean, formatter: (key, value) => string }
- Signature:
minFitness
- Signature:
{ show: boolean, formatter: (key, value) => string }
- Signature:
maxFitness
- Signature:
{ show: boolean, formatter: (key, value) => string }
- Signature:
avgFitness
- Signature:
{ show: boolean, formatter: (key, value) => string }
- Signature:
logsKeys
- Signature:
Array<{ key: string, formatter: (key, value) => string }>
- Available keys for
Genemo.run
:lastIteration
,selection
,reproduce
,evaluatePopulation
,succession
,stopCondition
,iterationCallback
- Available keys for
Genemo.reproduce
:reproduce.crossover
,reproduce.mutation
- Signature:
Formatters are optional.
-
customLogger
- logs provided string value
Type:(string) => undefined
Optional
Default value:console.log
Returned function is applicable to: Genemo.run
's selection
parameter.
Takes an object with the following properties:
minimizeFitness
- if true, selection's purpose will be to minimize fitness.
Type:boolean
Returned function is applicable to: Genemo.run
's selection
parameter.
Takes an object with the following properties:
minimizeFitness
- if true, selection's purpose will be to minimize fitness.
Type:boolean
Returned function is applicable to: Genemo.run
's selection
parameter.
Takes an object with the following properties:
-
size
- tournament size
Type:number
-
minimizeFitness
- if true, selection's purpose will be to minimize fitness.
Type:boolean
Returns a function which takes a random number generator and returns an array of random elements from valuesSet
of length equal to length
.
Returned function is applicable to: Genemo.generateInitialPopulation
's generateIndividual
parameter.
Returns a function which takes a random number generator and returns a random permutation of elements from valuesSet
.
Returned function is applicable to: Genemo.generateInitialPopulation
's generateIndividual
parameter.
Succession strategy, which keeps best individuals regerdless of reproduce's outcome.
Returned function is applicable to: Genemo.run
's succession
parameter.
Takes an object with the following properties:
-
keepFactor
- tournament size
Type:number
-
minimizeFitness
- if true, succession's purpose will be to minimize fitness.
Type:boolean
Single-point crossover.
Returned function is applicable to: Genemo.reproduce
's crossover
parameter.
Two-point crossover.
Returned function is applicable to: Genemo.reproduce
's crossover
parameter.
K-point crossover.
Returned function is applicable to: Genemo.reproduce
's crossover
parameter.
Takes one argument:
k
- number of crosover points.
Type:number
Order 1 crossover.
Returned function is applicable to: Genemo.reproduce
's crossover
parameter.
Partially-mapped crossover.
Returned function is applicable to: Genemo.reproduce
's crossover
parameter.
Uniform crossover. Offsprings are created by selecting each gene from one of the parents with equal probability.
Returned function is applicable to: Genemo.reproduce
's crossover
parameter.
Edge recombination crossover. It is an order-based crossover.
Returned function is applicable to: Genemo.reproduce
's crossover
parameter.
Takes an optional object with a single property:
hashGene
- Function which returns an identificator of a given gene for comparison purposes. It is used to distinguish genes when using===
comparison and structures likeMap
andSet
. If you represent a gene by a value of a simple type (for example string or number), then the default value ofhashGene
will work just fine.
Type:gene => geneHash
Optional
Default value:gene => gene
Transforms random gene from individual. Individual must be represented as an array of genes.
Returned function is applicable to: Genemo.reproduce
's mutate
parameter.
Takes one argument:
transformFunc
- function which transforms one gene.
Type:(gene, Rng) => mutatedGene
Negates value of a random gene (array element) of an individual.
Returned function is applicable to: Genemo.reproduce
's transformRandomGene
parameter.
Swaps places of two randomly chosen genes (array elements) of an individual.
Returned function is applicable to: Genemo.reproduce
's transformRandomGene
parameter.