A (more) functional genetic algorithm library. Designed to make as few assumptions as possible about how you want your genetic algorithm library to run.
Warning: I wrote this in a day without tests in a kinda functional style. So it's probably buggy and not as performant as it could be. Use at your own risk.
npm install evolutionary
var Evolutionary = require('evolutionary')
var Select1 = require('evolutionary/select1')
var Select2 = require('evolutionary/select2')
// Initialize it the way you want
var evolve = Evolutionary({
optimize: Math.max,
select1: Select1.bestOf2,
select2: Select2.bestOf2,
mutate: x => x,
crossover: (a, b) => [a, b],
seed: Math.random,
fitness: (a) => a * 2
populationSize: 250,
crossoverChance: 0.9,
mutateChance: 0.2,
fittestAlwaysSurvives: true
})
var population = evolve()
var nextPopulation = evolve(population)
// Population is sorted by fitness
var fittest = population[0]
// keep evolving to your heart's content
The only required option is seed
and fitness
. But this probably isn't going to be useful if you don't define at least mutate
and crossover
.
A function that returns a random individual when called.
A function that accepts an individual and returns that individual's fitness score.
A function that accepts two fitness scores, and returns whichever one is the better one. Defaults to Math.max
.
A function that accepts an individual, and returns a mutated version of that individual. Defaults to x => x
, so you should probably change it.
A function that accepts two parent individuals, and returns an array containing two children individuals created from those parents. Defaults to (a, b) => [a, b]
, so you should probably change it.
A function that selects a single individual out of a population. See the Selection section below. Defaults to Select1.bestOf2
.
A function that selects a two individuals out of a population. See the Selection section below. Defaults to Select2.bestOf2
.
How many individuals there should be in the population. Defaults to 250
.
The chance for a crossover to happen. Defaults to 0.9
.
The chance for a mutation to happen. Defaults to 0.2
.
Whether or not the fittest individual should always move on to the next generation. Defaults to true
.
There are a number of selection behaviors pre-written if the default isn't what you want.
const Select1 = reqiure('evolutionary/select1')
const Select2 = reqiure('evolutionary/select2')
Selects a random individual from the population.
Picks two random individuals from the population, and selects the fitter one.
Picks three random individuals from the population, and selects the fittest one.
Picks n
random individuals from the population, and selects the fittest one.
Selects the fittest individual from the population.
Selects two random individuals from the population.
Selects two individuals using the Select1.bestOf2
selection behavior.
Selects two individuals using the Select1.bestOf3
selection behavior.
Selects two individuals using the Select1.bestOfN(n)
selection behavior.
Selects the fittest individual and a random individual from the population.
This package also includes a convenience function to wrap your genetic algorithm as a generator:
const Evolutionary = require('evolutionary')
const makeGenerator = require('evolutionary/generator')
const evolve = Evolutionary({ ... })
const done = (pop) => pop[0] == desiredSolution
const generator = makeGenerator(evolve, done)
generator.next()
generator.next()
generator.next()
generator.next()
// ...