Genetic Algorithm
A naive try at creating a genetic algorithm in ES6
Installation
npm install genetalg
Usage
const populationSize = 200const chromosomeLength = 40const genesPool = 0 1const population = populationSize chromosomeLength genesPool const nbGenerations = 10000const fitnessFunction = Math // => Add fitness function here instead // Should return a fitness score between 0 and 1population const bestChromosome = populationcurrentPopulation0consoleconsole
Example
Trivial example to evolve a population that will have I AM A ROBOT AND I LIKE TO EAT BOLTS
written in their DNA
const alphabet = ' ' 'A' 'B' 'C' 'D' 'E' 'F' 'G' 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' 'X' 'Y' 'Z' const target = 'I AM A ROBOT AND I LIKE TO EAT BOLTS' // This fitness function will check which percentage// of letters are valid in the generated sentenceconst fitnessFunction = { return chromosomedna / targetlength} const population = 30 targetlength alphabetpopulation // Should be `I AM A ROBOT AND I LIKE TO EAT BOLTS` (or pretty close)console