0.2.1 • Published 7 years ago

genetic-js-no-ww v0.2.1

Weekly downloads
37
License
BSD
Repository
github
Last release
7 years ago

Genetic.js

Advanced genetic and evolutionary algorithm library written in Javascript by Sub Protocol.

Fork

This fork by framp removes the automatic spinning of a new web worker.

I believe spinning a new web worker should be responsibility of the user and coupling it to an optimisation library just worsen the experience for developers. This fork is perfectly compatible with web workers - you just need to manage it yourself.

Rational

The existing Javascript GA/EP library landscape could collectively be summed up as, meh. All that I required to take over the world was a lightweight, performant, feature-rich, nodejs + browser compatible, unit tested, and easily hackable GA/EP library.

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!

Examples

Install

npm install genetic-js

Population Functions

The genetic-js interface exposes a few simple concepts and primitives, you just fill in the details/features you want to use.

FunctionReturn TypeRequiredDescription
seed()IndividualYesCalled to create an individual, can be of any type (int, float, string, array, object)
fitness(individual)FloatYesComputes a fitness score for an individual
mutate(individual)IndividualOptionalCalled when an individual has been selected for mutation
crossover(mother, father)Son, DaughterOptionalCalled when two individuals are selected for mating. Two children should always returned
optimize(fitness, fitness)BooleanYesDetermines if the first fitness score is better than the second. See Optimizer section below
select1(population)IndividualYesSee Selection section below
select2(population)IndividualOptionalSelects a pair of individuals from a population. Selection
generation(pop, gen, stats)BooleanOptionalCalled for each generation. Return false to terminate end algorithm (ie- if goal state is reached)
notification(pop, gen, stats, isFinished)VoidOptionalRuns in the calling context.

Optimizer

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.

OptimizerDescription
Genetic.Optimize.MinimizerThe smaller fitness score of two individuals is best
Genetic.Optimize.MaximizerThe greater fitness score of two individuals is best

Selection

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.

Select TypeRequiredDescription
select1 (Single)YesSelects a single individual for survival from a population
select2 (Pair-wise)OptionalSelects two individuals from a population for mating/crossover

Selection Operators

Single SelectorsDescription
Genetic.Select1.Tournament2Fittest of two random individuals
Genetic.Select1.Tournament3Fittest of three random individuals
Genetic.Select1.FittestAlways selects the Fittest individual
Genetic.Select1.RandomRandomly selects an individual
Genetic.Select1.RandomLinearRankSelect random individual where probability is a linear function of rank
Genetic.Select1.SequentialSequentially selects an individual
Pair-wise SelectorsDescription
Genetic.Select2.Tournament2Pairs two individuals, each the best from a random pair
Genetic.Select2.Tournament3Pairs two individuals, each the best from a random triplett
Genetic.Select2.RandomRandomly pairs two individuals
Genetic.Select2.RandomLinearRankPairs two individuals, each randomly selected from a linear rank
Genetic.Select2.SequentialSelects adjacent pairs
Genetic.Select2.FittestRandomPairs 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;

// ...

Configuration Parameters

ParameterDefaultRange/TypeDescription
size250Real NumberPopulation size
crossover0.90.0, 1.0Probability of crossover
mutation0.20.0, 1.0Probability of mutation
iterations100Real NumberMaximum number of iterations before finishing
fittestAlwaysSurvivestrueBooleanPrevents losing the best fit between generations
maxResults100Real NumberThe maximum number of best-fit results that will be sent per notification
skip0Real NumberSetting this higher throttles back how frequently genetic.notification gets called in the main thread.

Building

To clone, build, and test Genetic.js issue the following command:

git clone git@github.com:subprotocol/genetic-js.git && make distcheck
CommandDescription
makeAutomatically install dev-dependencies, builds project, places library to js/ folder
make checkRuns test cases
make cleanRemoves files from js/ library
make distcleanRemoves both files from js/ library and dev-dependencies
make distcheckEquivlant to running make distclean && make && check

Contributing

Feel free to open issues and send pull-requests.