CodeReclaimers / neat-python

Python implementation of the NEAT neuroevolution algorithm
BSD 3-Clause "New" or "Revised" License
1.4k stars 488 forks source link

neat.Population.run() does not return the best genome #271

Open jongdetim opened 1 year ago

jongdetim commented 1 year ago

Describe the bug After training, the fittest genome is often not the one that was returned. Instead it seems to return the strongest genome from the final generation. I noticed that the top fitness during training can be higher than the genome returned by neat.Population.run().

To Reproduce Steps to reproduce the behavior:

  1. Configure any model with a neat.StdOutReporter reporter
  2. Train the model, such as with: pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), evaluate_genome) winner = population.run(pe.evaluate, 20)
  3. Observe the reported species MAX fitness genomes during training, and notice that the highest fitness if often not the one that population.run() returned.

Expected behavior The genome returned by Population.run() should be the fittest genome it has seen at any point during training.

Stdout example

In the following output snippet, we can see that the max fitness of both species 2 and 3 is 22.0, however, the training function returns a genome with fitness: 17.0:

... ** Running generation 18 **

Population's average fitness: 2.98333 stdev: 4.80101 Best fitness: 22.00000 - size: (8, 19) - species 2 - id 956 Average adjusted fitness: 0.131 Mean genetic distance 2.401, standard deviation 0.954
Population of 59 members in 3 species: ID age size fitness adj fit stag ==== === ==== ======= ======= ==== 1 18 14 5.0 0.091 5 2 18 23 22.0 0.146 0 3 14 22 22.0 0.156 1 Total extinctions: 0 Generation time: 0.744 sec (0.499 average)

** Running generation 19 **

Population's average fitness: 2.79661 stdev: 3.76325 Best fitness: 17.00000 - size: (8, 19) - species 2 - id 956 Average adjusted fitness: 0.163 Mean genetic distance 2.369, standard deviation 1.078
Population of 61 members in 3 species: ID age size fitness adj fit stag ==== === ==== ======= ======= ==== 1 19 17 7.0 0.155 6 2 19 24 17.0 0.189 1 3 15 20 15.0 0.144 2 Total extinctions: 0 Generation time: 0.757 sec (0.531 average) winner fitness: 17 Key: 956

Desktop (please complete the following information):

jongdetim commented 1 year ago

I can also confirm the same happens when using a single process, instead of multiprocessing. Strangely, looking through population.py, everything seems normal, as it returns self.best_genome.

My current workaround is instead calling population.reporters.reporters[0].best_genome() to get the best all-time genome.

CodeReclaimers commented 1 year ago

Thank you for reporting this, especially the detailed reproduction! This code is long overdue for an overhaul after all the changes I've merged into it in the last couple years. I will try to find the time to get these issues addressed in a minor release soon, and leave the deep dive for later this year.