Install using npm
:
npm install neuroevolution
or Yarn yarn
:
yarn add neuroevolution
The first step should be to initialize a new population.
A population accepts 3 main parameters:
populationSize
Total size of the genomes populationnbInput
Number of input nodesnbOutput
Number of output node
const { Population } = require('neuroevolution')
const population = new Population(100, 2, 4)
This will create a population of 100 neural networks with 2 inputs and 4 outputs.
You can start evolving the population using evolve
with 2 parameters :
iterations
How many iterations to evolvefitnessFunction
You fitness function that has access to your genome
population.evolve(40, genome => {
const network = genome.generateNetwork()
const prediction = network.predict(input)
// ... return a fitness score according to the accuracy of prediction
})
This will evolve the population, keeping the fittest neural networks according to your fitness function accross 40 generations. The number of iterations is cumulative, this means if your population current generation is 12, evolving with 40 iterations will transform the population to generation 52.
To run this example :
node examples/xor
Sample code :
const { Population } = require('neuroevolution')
const population = new Population(50, 2, 1, false)
const xor = [
[[0, 0], 0],
[[0, 1], 1],
[[1, 0], 1],
[[1, 1], 0],
]
population.evolve(1000, genome => {
const network = genome.generateNetwork()
let error = 0;
for (const [input, output] of xor) {
const [prediction] = network.predict(input)
error += Math.abs(prediction - output)
}
return 1 - (error / xor.length)
})
Neuroevolution is MIT licensed.