'Open AI Gym with Neat Algorithm not working on 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()
Trying to run CartPole v1 with Neat Algorithm on jupyter notebook but the output is stuck on
**** Running generation 0 ****
With increase in amount of GPU usage instead of the CPU.
But when trying to run it on CMD everything is working fine(GPU usage being normal). Is there any way I can do it on Jupyter Notebook.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|