Applying saved NEAT-Python Genome to test environment after training

淺唱寂寞╮ 提交于 2020-12-13 03:18:19

问题


I have used some NEAT algorithms to code my own AI for some easy games like flappy bird. Everything works fine and I know what is going on. The problem is I do not know what to do with the result. The AI learns something and I want to save that progress. TechwithTim YouTuber said something about using pickle, which worked for me when I saved it. I can even load it from the file, but that is where I end. I don't know what to do next to start just one bird to play the game with the knowledge of those birds playing the game before him.

Saving in one code

winner = p.run(game,50)
with open("winner.pkl", "wb") as f:
    pickle.dump(winner, f)
    f.close()

Opening in another code:

with open("winner.pkl", "wb") as f:
    genome = pickle.load(f)

When using

print(type(genome))

output is

<class "neat.genome.DefaultGenome">

回答1:


I assume that the code you supplied is not your own and that you were following some sort of tutorial. The quality of the code is very low, documentation in form of comments is literally non-existent and variable naming is not english. If you coded that, than that's completely fine for a beginner. Actually even impressive. Though especially for a beginner's tutorial do I highly recommend to search for better explained and documented tutorials.

With that being said, here is the code that you need to add to your project in order to replay a saved genome:

def replay_genome(config_path, genome_path="winner.pkl"):
    # Load requried NEAT config
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)

    # Unpickle saved winner
    with open(genome_path, "rb") as f:
        genome = pickle.load(f)

    # Convert loaded genome into required data structure
    genomes = [(1, genome)]

    # Call game with only the loaded genome
    game(genomes, config)

Obviously as the code quality was quite low was I unable to understand it to such a degree to provide a clean replay code. Therefore the code is simply reusing the existing game code to train the population, though the population consists only of the loaded genome in this case.

Shameless plug: If you want to learn more about Neuroevolution, see here: https://towardsdatascience.com/9068f532f7f7



来源:https://stackoverflow.com/questions/61365668/applying-saved-neat-python-genome-to-test-environment-after-training

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!