romainsimon / neuroevolution

:hatching_chick: NeuroEvolution of Augmenting Topologies (NEAT) implementation with Tensorflow.js (still WIP)
MIT License
18 stars 2 forks source link
genetic-algorithm neat tensorflowjs

NeuroEvolution of Augmenting Topologies (NEAT)

:hatching_chick: Evolving a population of Neural Networks using Tensorflow.js and Genetic Algorithm in ES6. This is an implementation of NEAT (Neuro Evolution of Augmenting topologies)


Build Status Test Coverage Standard

Table of Contents

Installation

Install using npm:

npm install neuroevolution

or Yarn yarn:

yarn add neuroevolution

Usage

1. Initialize a new population

The first step should be to initialize a new population.

A population accepts 3 main parameters:

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.

2. Evolve the population

You can start evolving the population using evolve with 2 parameters :

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.

Examples

Xor

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)
})

License

Neuroevolution is MIT licensed.