def runGenome(genome, genomeID, config): highScore = 0 highTile = 0 fitness = 0 for i in range(BOTS_PER_GENOME): bot = Bot() bot.brain = neat.nn.FeedForwardNetwork.create(genome, config) bot.useBrain() fitness += bot.fitness if bot.fitness > highScore: highScore = bot.fitness # Average bot fitnesses to get overall genome fitness fitness /= BOTS_PER_GENOME # Get high tile for row in bot.board.tiles: for cell in row: if cell > highTile: highTile = cell return fitness, genomeID, highScore, highTile
print(f'High Score: {highScore}') print(f'High Tile: {highTile}') if __name__ == '__main__': # Load configuration config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, 'config') # Create the population, which is the top-level object for a NEAT run population = neat.Population(config) # Add reporter so I can see stuff happen population.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() population.add_reporter(stats) # Train the population winner = population.run(runGeneration) # ======== Testing ============== bot = Bot() bot.brain = neat.nn.FeedForwardNetwork.create(winner, config) bot.useBrain(printGame=True) # ================================ # TODO: add visualization (https://neat-python.readthedocs.io/en/latest/xor_example.html)