CodeReclaimers / neat-python

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

division by zero when pop size is 1 #214

Closed JacobWoolbright closed 2 years ago

JacobWoolbright commented 3 years ago
File "C:\Users\hunty\AppData\Local\RLBotGUIX\venv\lib\site-packages\neat\math_util.py", line 9, in mean
    return sum(map(float, values)) / len(values)
ZeroDivisionError: division by zero

When the population size of my model is set to 1, I receive a division by 0 error, when set to a number > 1, I don't get the error. I found a stack overflow page relating to this, but there was no solution.

Here is some of my code: config file (the portion that matters):

[NEAT]
fitness_criterion     = max
fitness_threshold     = 9999
pop_size              = 2
reset_on_extinction   = False

eval/fitness function:

def main(gnomes, config):
    global net, ge
    net = neat.nn.FeedForwardNetwork(gnomes[0], config)
    ge = gnomes[0]
    ge.fitness = 0

If you have a solution or need more code, please say so!

ixenion commented 3 years ago

yeah, same problem

krzyLadda commented 2 years ago

This is due to the lines: gdmean = mean(distances.distances.values()) and gdstdev = stdev(distances.distances.values()) in the species.py file. But it is not big bug ;) The program wants to count the mean distance and mean deviation from a sequence of zero-length numbers. This is logical when there is only one genome, because to whom this genetic distance would be counted ;). There are two solutions: comment out this part in species.py. gdmean = mean(distances.distances.values()) gdstdev = stdev(distances.distances.values()) self.reporters.info( 'Mean genetic distance {0:.3f}, standard deviation {1:.3f}'.format(gdmean, gdstdev))

OR add an appropriate exception to mean and stdev in math_util.py. Somethink like: 'if not values: return 0'