Genetic algorithm library written in JavaScript
The library lets you define specific functions for your particular application
Define how to generate an individual. Function must return the constructed individual.
Example:
function generateIndividual() {
let array = [];
for(let i = 0; i < 10; i++) {
array.push(Math.round(Math.random()));
}
return array;
}
Define the fitness function. Function takes in an individual and returns its fitness value.
Example:
function getFitness(indv) {
let fitness = 0;
for(let i = 0; i < indv.length; i++) {
fitness += indv[i] == 1 ? 1 : 0;
}
return fitness;
}
Define how to mutate an individual. Function takes in an individual, mutates, and returns it.
Example:
function mutate(indv) {
let mutatedIndex = Math.floor(Math.random() * indv.length);
indv[mutatedIndex] = indv[mutatedIndex] == 1 ? 0 : 1;
return indv;
}
Define how to breed two individuals and produce a newborn. Function takes in two individuals and returns a new individual produced from both individuals
function breedFunction(parent0, parent1) {
let newborn = parent0.slice();
let randIndex = Math.round(Math.random() * parent1.length);
newborn[randIndex] = parent1[randIndex];
return newborn;
}
Create a toolbox that will hold your created functions
let toolbox = new Toolbox();
toolbox.genIndv = generateIndividual;
toolbox.getFitness = getFitness;
toolbox.mutate = mutate;
Set the goal fitness. If a larger fitness is desired then use Toolbox.fitnessMax; otherwise use Toolbox.fitnessMin.
toolbox.goalFitness = Toolbox.fitnessMax;
let populationSize = 100;
let mutationProbability = .1;
let generations = 100;
Create Genetic Algorithm object and run evolution
let gen = new GeneticAlgorithm(toolbox, populationSize, mutationProbability, breedFunction);
let results = gen.evolve(generations);
// Do something with the results