训练后将保存的NEAT-Python基因组应用于测试环境 [英] Applying saved NEAT-Python Genome to test environment after training

查看:66
本文介绍了训练后将保存的NEAT-Python基因组应用于测试环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用了一些NEAT算法来为一些易玩的游戏(如飞扬的小鸟)编写自己的AI.一切正常,我知道发生了什么.问题是我不知道如何处理结果.人工智能学到了一些东西,我想保存这一进步.TechwithTim YouTuber说了一些有关使用泡菜的方法,当我保存泡菜时,它对我有用.我什至可以从文件中加载它,但这就是我要结束的地方.我不知道下一步该怎么做,只知道一只鸟在他前面玩游戏就开始了一只鸟玩游戏.

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.

保存一个代码

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

打开另一个代码:

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

使用

print(type(genome))

输出是

<class "neat.genome.DefaultGenome">

推荐答案

我假设您提供的代码不是您自己的,并且您正在遵循某种教程.代码的质量很低,注释形式的文档实际上不存在,变量命名不是英语.如果您编写了代码,那对初学者来说完全没问题.实际上甚至令人印象深刻.尽管特别是对于初学者的教程,但我还是强烈建议您搜索更好的解释和文档化的教程.

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.

无耻插件:如果您想了解有关Neuroevolution的更多信息,请参见此处: https://towardsdatascience.com/9068f532f7f7

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

这篇关于训练后将保存的NEAT-Python基因组应用于测试环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆