训练后将保存的 NEAT-Python Genome 应用到测试环境

Gru*_*uny 3 python artificial-intelligence neat

我使用了一些 NEAT 算法为一些简单的游戏(例如 flappybird)编写了自己的 AI 代码。一切正常,我知道发生了什么。问题是我不知道如何处理结果。人工智能学到了一些东西,我想保存这个进度。TechwithTim YouTuber 说了一些关于使用 pickle 的事情,当我保存它时它对我有用。我什至可以从文件中加载它,但这就是我的结束。我不知道接下来要做什么,在知道在他之前玩游戏的那些鸟的情况下,只让一只鸟开始玩游戏。

保存在一个代码中

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

用另一个代码打开:

with open("winner.pkl", "wb") as f:
    genome = pickle.load(f)
Run Code Online (Sandbox Code Playgroud)

使用时

print(type(genome))
Run Code Online (Sandbox Code Playgroud)

输出是

<class "neat.genome.DefaultGenome">
Run Code Online (Sandbox Code Playgroud)

Pau*_*low 6

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

话虽如此,以下是您需要添加到项目中以重放保存的基因组的代码:

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)
Run Code Online (Sandbox Code Playgroud)

显然,由于代码质量相当低,我无法理解它到提供干净的重放代码的程度。因此,代码只是重用现有的游戏代码来训练群体,尽管在这种情况下群体仅由加载的基因组组成。

无耻插件:如果你想了解更多关于Neurevolution的知识,请看这里: https: //towardsdatascience.com/9068f532f7f7