JSGA is an ES6 genetic algorithm library.
bower install jsga-feklee
yarn add jsga-feklee
Or you can download
jsga.js from the GitHub repo.
JSGA operates on chromosomes --- lists of numbers. It generates successive generations of chromosomes by mixing the chromosomes of randomly selected parents, and by randomly mutating chromosomes. It evaluates each individual by using a user-provided fitness function. Only the fittest individuals from each generation are carried over to the next.
Start by creating a JSGA object:
let algorithm =;
Notes on the parameters:
lengthis the length of the chromosome.
radixis the 'base' used to encode the data. Each number in the chromosome will have a range from
fitnessis the fitness function used to evaluate individuals. It is passed a chromosome (array of numbers) and expects a number to be returned. JSGA will try to maximize the fitness function.
sizeis the number of individuals in the population. This must be an even number.
seedis used to fill the first individuals of the initial population. The remaining individuals are random.
childrenis the number of offsping each pair of parents will produce. The default is 4.
mutationRateis the rate of random mutations, in mutations/number. That is, for the default mutation rate of 0.05, around one in every twenty numbers in each chromosome will be randomly mutated.
crossoversis the number of times to randomly cross over the parents chromosomes. This is the primary source of variation. The default is 1.
To run the algorithm, simply call the
run method with the number of
generations to run the algorithm. Pass
-1 to make the algorithm run forever.
run is a generator which yields an object containing information about the
for let generation of algorithmconsole;console;console;console;