Neuroevolution is a powerful approach to machine learning and artificial intelligence that uses evolutionary algorithms to evolve neural networks.
This project contain a neuro-evolution library that allows you to easily use NeuroEvolution in your game.
In this project, we have applied GeneticEvolution to multiple games such as self-driving cars, smart caps and flappy bird.
This project is a follow-up to my project about Artificial Neural Networks from scratch, where I show how to create an ANN from scratch without libraries. In that project the learning process is done using backpropagation(gradient descent), this project use a different approach, we will use Evolutionary Algorithm.
Evolutionary algorithm uses mechanisms inspired by biological evolution, such as reproduction, mutation, recombination, and selection. Candidate solutions to the optimization problem play the role of individuals in a population, and the fitness function determines the quality of the solutions. Evolution of the population then takes place after the repeated application of the above operators.
The project is developed from scratch and with no external libraries.
The library is in the neuroevolution
folder. The library is composed of 3 main classes:
NeuralNetwork
: This class represents the required Artificial Neural network.GeneticEvolution
: This class represents the Genetic Evolution Algorithm.PopulationHandler
: This class represents the genetic population.PopulationItem
: This class represents the item in population.
## Inititalize the genetic evolution
let population = [new PopulationItem(), new PopulationItem()]
let populationHandler = new PopulationHandler(population)
let geneticEvolution = new GeneticEvolution(populationHandler);
## Evolve the population after each generation
geneticEvolution.evolve(); // evolve the population based on the fitness function
PopulationItem
and PopulationHandler
are classes that you need to implement in your game.
Population Item examples:
-
PopulationHandler examples:
You can use the population genes to perform the game action. See the Car.steer, SmartCaps.makeMove and FlappyBird.think methods.
geneticEvolution.getPopulationHandler().getPopulation().forEach((item) => {
let output = item.useGenes(input);
log(output);
});
To learn more about the game examples check the following links: