Neuroevolutional Optimization in Godot

A neuroevolution experiment in Godot, where a neural network and a genetic algorithm teach a virtual bat to avoid obstacles.

Neuroevolutional Optimization in Godot
3 mins

Problem descriptionh2

In nature, organisms evolve through natural selection: traits that provide an advantage are passed on to subsequent generations. In a simulated environment, the challenge is to build an artificial model capable of learning and adapting in a similar way.

This project aims to:

  • develop an artificial neural network that controls the movement of a virtual bat;
  • implement a genetic algorithm that optimizes the network’s parameters;
  • build a system that favors the selection and reproduction of the best-performing individuals.

Proposed solutionh2

The solution consists of three main components.

Neural networkh3

The bat’s behavior is controlled by an artificial neural network. It processes information received from the game environment and produces a decision that determines the bat’s movement.

Genetic algorithmh3

The network’s parameters are optimized through a genetic algorithm that simulates selection, crossover, and mutation. At the end of each generation, the individuals with the best results, according to the fitness function, have a greater chance of passing their “genes” on to the next generation.

Simulation in Godoth3

Godot provides the graphical environment in which the experiment takes place. The bat must traverse the level without hitting the walls, floor, ceiling, or other obstacles.

The application of evolutionary algorithms to optimize neural networks is called neuroevolution. NEAT (NeuroEvolution of Augmenting Topologies) is a specific neuroevolution method known for evolving both a network’s weights and its topology.

Controlling the bath2

The bat has only one action: it can flap its wings at a given interval. When it does, it accelerates upward and to the right, gaining a little height. It then starts descending under simulated gravity. If it hits an obstacle, the individual fails and its simulation ends.

Neural network architecture with six input neurons, two hidden layers of six neurons each, and two outputs for jumping or continuing without a jump

The neural network architecture used to control the bat.

The neural network receives data that the bat “sees” through its sensors:

The bat's sensors and the coordinates of the top, middle, and bottom reference points on the next obstacle

The bat’s position and the reference points used to calculate the network’s inputs.

  • m.y - bat.y;
  • t.y - bat.y;
  • t.x - bat.x;
  • b.x - bat.x;
  • b.y - bat.y;
  • the time elapsed since the last wing flap.

These values describe the positions of environmental reference points relative to the bat, giving the network the information it needs to decide when to perform the next action.

Genetic algorithmh2

The neural network’s weights act as genes. They are generated randomly at the beginning, then improved over successive generations through three operations:

  • selection, using elitism;
  • crossover, by combining the genes of two parents;
  • mutation, by randomly altering some genes.

The evolutionary process works as follows:

  1. A generation of 100 individuals with random genes is created.
  2. After every individual fails, they are ranked by fitness—the distance they traveled through the level.
  3. The five best individuals pass unchanged into the next generation.
  4. The next five are produced by randomly crossing individuals from the top five.
  5. The rest of the population is produced by crossing other pairs of individuals or through mutation.

The process repeats until the network produces satisfactory results. In the experiments, approximately 20–30 generations were enough for individuals to clear more than 50 obstacles.

Source codeh2

The complete project source code is available on GitHub.