🤖 A simple NEAT Algorithm implementation in JavaScript.
npm i @kylehue/neat
const Neat = require("@kylehue/neat");
let neat = new Neat(input, hidden, output, options);
{
// Total number of genomes
populationSize: 20,
// Determines how many genomes should be mutated in a generation
mutationRate: 0.25,
// Maximum number of generations a species can exist without making any improvements
maxStagnation: 30
}
let neat = new Neat(6, 1, 3, {
populationSize: 10
});
class Turtle {
constructor(genome) {
this.brain = genome;
}
eat() {
this.brain.fitness++; // Add fitness score
}
}
// Create turtles
let turtles = [];
for (var i = 0; i < neat.populationSize; i++) {
turtles.push(neat.population.genomes[i]);
}
// Feedforward inputs
for (var i = 0; i < turtles.length; i++) {
turtles[i].brain.feedforward([...]);
}
let neat = new Neat(6, 1, 3, {
populationSize: 10
});
function evolve() {
neat.population.evolve();
nextGen();
}
You can create pre-trained models by using toJSON()
let neat = new Neat(6, 1, 3, {
populationSize: 10
});
let trained = neat.toJSON();
download(trained);
And you can import pre-trained models by using fromJSON()
and import()
let neat = new Neat(6, 1, 3, {
populationSize: 10
});
let genomes = neat.fromJSON(jsonFile);
neat.import(genomes, neat.populationSize);