Skip to content

romainsimon/neuroevolution

Repository files navigation

NeuroEvolution of Augmenting Topologies (NEAT)

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

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:

  • populationSize Total size of the genomes population
  • nbInput Number of input nodes
  • nbOutput 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.

2. Evolve the population

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

  • iterations How many iterations to evolve
  • fitnessFunction 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.

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.

About

🐣 NeuroEvolution of Augmenting Topologies (NEAT) implementation with Tensorflow.js (still WIP)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published