使用 Neat 算法打开 AI Gym 不适用于 Jupyter

问题描述

import multiprocessing
import os
import pickle
import numpy as np
import neat
import gym

runs_per_net = 2

# Use the NN network phenotype and the discrete actuator force function.
def eval_genome(genome,config):
    net = neat.nn.FeedForwardNetwork.create(genome,config)

    fitnesses = []

    for runs in range(runs_per_net):
        env = gym.make("CartPole-v1")
        observation = env.reset()
        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        done = False
        while not done:
            
            action = np.argmax(net.activate(observation))
            observation,reward,done,info = env.step(action)    
            fitness += reward

        fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return np.mean(fitnesses)

def eval_genomes(genomes,config):
    for genome_id,genome in genomes:
        genome.fitness = eval_genome(genome,config)


def run():
    # Load the config file,which is assumed to live in
    # the same directory as this script.
    config = neat.Config(neat.DefaultGenome,neat.DefaultReproduction,neat.DefaultSpeciesSet,neat.DefaultStagnation,"config")

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))

    pe = neat.ParallelEvaluator(multiprocessing.cpu_count(),eval_genome)
    winner = pop.run(pe.evaluate)

    # Save the winner.
    with open('winner','wb') as f:
        pickle.dump(winner,f)

    print(winner)

if __name__ == '__main__':
    run()

尝试在 jupyter notebook 上使用 Neat Algorithm 运行 CartPole v1,但输出卡住

**** Running generation 0 **** 

随着 GPU 使用量的增加,而不是 cpu

但是当尝试在 CMD 上运行它时,一切正常(GPU 使用正常)。有什么办法可以在 Jupyter Notebook 上做到这一点。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)