A lightweight genetic algorithm library.
- Added support for custom configuration, ex. custom randomizer function
For a more detailed list of updates, see CHANGELOG.
Add to existing project using npm or yarn.
npm install genetic-lab --save
yarn add genetic-lab
Import the library to your project.
const GeneticLab = require("genetic-lab");
// using ES6 import
// import GeneticLab from 'genetic-lab';
const { Selection, Mutation, Crossover } = GeneticLab;
const {
createRoulleteWheelSelection,
createStochasticUniversalSampling,
createRankSelection,
createTournamentSelection
} = Selection;
const { pointCrossover, uniquePointCrossover } = Crossover;
const { mutateOrder, createMutation } = Mutation;
Check the examples for additional reference on using this library.
A fitness function should check the fitness of a single individual and return a number based on it's fitness score.
For example:
// A fitness function that optimizes for highest binary string value
function getFitness(individual) {
return parseInt(individual, 2);
}
getFitness("010111"); // => 23
Creates a selection function that uses Roullete Wheel method.
const population = ["000", "111"];
const selection = createRoulleteWheelSelection(getFitness);
selection(population); // => ['111']
- randomizer?:
() => number
; // function that replacesMath.random()
Creates a selection function that uses Stochastic Universal Sampling method. Number of individuals selected is based on count.
const population = ["000", "111", "101"];
const selection = createStochasticUniversalSampling(getFitness, { count: 2 });
selection(population); // => ['111', '101'] or ['101', '111']
- randomizer?:
() => number
; // function that replacesMath.random()
- count:
number
;
Creates a selection function that uses Rank Selection method. Number of individuals selected is based on count.
const population = ["000", "111", "101"];
const selection = createRankSelection(getFitness, { count: 2 });
selection(population); // => ['111', '101']
- randomizer?:
() => number
; // function that replacesMath.random()
- count:
number
;
Creates a selection function that uses Tournament Selection method. Size of tournament is dependent on tournametSize.
const population = ["000", "111", "101"];
const selection = createTournamentSelection(getFitness, 2);
selection(population); // => ['111'] or ['101']
- randomizer?:
() => number
; // function that replacesMath.random()
Swaps two genes randomly.
mutateOrder("ABC"); // => 'BAC' or 'CBA' or 'BCA'
mutateOrder("10"); // => '01'
Creates a mutation function that randomly changes a gene based on sample.
const sample = ["1", "0"];
const mutateBinary = createMutation(sample);
mutateBinary("10"); // => '11' or '00'
Creates two offspring based on parentA and parentB using index as point of crossover. If no index is provided, it would be assigned randomly.
const pair = ["ABCD", "DCBA"];
pointCrossover(...pair, 2); // => ['ABBA', 'DCCD']
Creates two unique chromosome offsprings based on unique chromosomes of parentA and parentB using index as point of crossover. If no index is provided, it would be assigned randomly.
const pair = ["ABCD", "DCBA"];
uniquePointCrossover(...pair, 2); // => ['ABDC', 'DCAB']
This project is licensed under the MIT License - see the LICENSE file for details.