This repository holds a generic module(class) that can be used for Neuroevolution (Genetic Algorithms) in various environments. It's heavily based on my String Matcher implementation but instead of strings, we use Neural Networks.
The class is present here
- Numpy : For everything
- Pandas : Just in case I want to use it to display stuff later
- Keras : Neural Networks and whatnot
The class does not simulate the environment itself. init_generation
creates a population of individuals depending on input parameters and returns the population. We need to simulate every individual manually and record their rewards. Then pass the rewards array into the next_generation
.
This is done to keep the module reusable.
trainer = neuroevolution(
population_size = 55,
layers = [4, 8, 2],
mutation_rate = 0.20,
n_best_survivors = 5,
n_total_survivors = 10,
crossover_rate = 0.50
)
population_size
: size of the populationlayers
: the number of nodes in every layer of the neural network.
Expects an array of the form[observation_dims, layer_1, layer_2, ..., action_dims]
observation_dims
is the dimensions of the observation returned by the environment.action_dim
is the number of possible actions the individual can take in the environment.mutation_rate
: the mutation ratecrossover_rate
: the crossover rate (How likely it is to take parameters from the second parent). Generally 0.5.n_best_survivors
: number of best individuals in the population to be bred.n_total_survivors
: number of total individuals in the population to be bred.
population = trainer.init_generation()
init_generation
creates a keras dense neural network with softmax layers with units size provided by the layers tournament. It returns a ndarray of Keras models (which is the population of individuals).
population = trainer.next_generation(fitness)
next_generation
expects a fitness array, which should hold the respective rewards for every individual. fitness[i]
should hold the reward for current_population[i]
. next_generation
creates the next population by crossing individuals based on the mutation_rate
and the crossover_rate
. The child will take weights from the first parent or from the second parent.
I built a model for OpenAi gym's cartpole using this. Check it out here