Until now, no such thing existed. Now you can have my cake, and optimize it too. Is it perfect? Probably. Regardless, this library is my gift to you.
Have fun optimizing all your optimizations!
npm install genetic-js
The genetic-js interface exposes a few simple concepts and primitives, you just fill in the details/features you want to use.
|seed()||Individual||Yes||Called to create an individual, can be of any type (int, float, string, array, object)|
|fitness(individual)||Float||Yes||Computes a fitness score for an individual|
|mutate(individual)||Individual||Optional||Called when an individual has been selected for mutation|
|crossover(mother, father)||[Son, Daughter]||Optional||Called when two individuals are selected for mating. Two children should always returned|
|optimize(fitness, fitness)||Boolean||Yes||Determines if the first fitness score is better than the second. See Optimizer section below|
|select1(population)||Individual||Yes||See Selection section below|
|select2(population)||Individual||Optional||Selects a pair of individuals from a population. Selection|
|generation(pop, gen, stats)||Boolean||Optional||Called for each generation. Return false to terminate end algorithm (ie- if goal state is reached)|
|notification(pop, gen, stats, isFinished)||Void||Optional||Runs in the calling context. All functions other than this one are run in a web worker.|
The optimizer specifies how to rank individuals against each other based on an arbitrary fitness score. For example, minimizing the sum of squared error for a regression curve
Genetic.Optimize.Minimize would be used, as a smaller fitness score is indicative of better fit.
|Genetic.Optimize.Minimizer||The smaller fitness score of two individuals is best|
|Genetic.Optimize.Maximizer||The greater fitness score of two individuals is best|
An algorithm can be either genetic or evolutionary depending on which selection operations are used. An algorithm is evolutionary if it only uses a Single (select1) operator. If both Single and Pair-wise operations are used (and if crossover is implemented) it is genetic.
|select1 (Single)||Yes||Selects a single individual for survival from a population|
|select2 (Pair-wise)||Optional||Selects two individuals from a population for mating/crossover|
|Genetic.Select1.Tournament2||Fittest of two random individuals|
|Genetic.Select1.Tournament3||Fittest of three random individuals|
|Genetic.Select1.Fittest||Always selects the Fittest individual|
|Genetic.Select1.Random||Randomly selects an individual|
|Genetic.Select1.RandomLinearRank||Select random individual where probability is a linear function of rank|
|Genetic.Select1.Sequential||Sequentially selects an individual|
|Genetic.Select2.Tournament2||Pairs two individuals, each the best from a random pair|
|Genetic.Select2.Tournament3||Pairs two individuals, each the best from a random triplett|
|Genetic.Select2.Random||Randomly pairs two individuals|
|Genetic.Select2.RandomLinearRank||Pairs two individuals, each randomly selected from a linear rank|
|Genetic.Select2.Sequential||Selects adjacent pairs|
|Genetic.Select2.FittestRandom||Pairs the most fit individual with random individuals|
var genetic = Genetic.create(); // more likely allows the most fit individuals to survive between generations genetic.select1 = Genetic.Select1.RandomLinearRank; // always mates the most fit individual with random individuals genetic.select2 = Genetic.Select2.FittestRandom; // ...
|size||250||Real Number||Population size|
|crossover||0.9||[0.0, 1.0]||Probability of crossover|
|mutation||0.2||[0.0, 1.0]||Probability of mutation|
|iterations||100||Real Number||Maximum number of iterations before finishing|
|fittestAlwaysSurvives||true||Boolean||Prevents losing the best fit between generations|
|maxResults||100||Real Number||The maximum number of best-fit results that webworkers will send per notification|
|webWorkers||true||Boolean||Use Web Workers (when available)|
|skip||0||Real Number|| Setting this higher throttles back how frequently
To clone, build, and test Genetic.js issue the following command:
git clone email@example.com:subprotocol/genetic-js.git && make distcheck
|make||Automatically install dev-dependencies, builds project, places library to js/ folder|
|make check||Runs test cases|
|make clean||Removes files from js/ library|
|make distclean||Removes both files from js/ library and dev-dependencies|
|make distcheck|| Equivlant to running
Feel free to open issues and send pull-requests.