CodeReclaimers / neat-python

Python implementation of the NEAT neuroevolution algorithm
BSD 3-Clause "New" or "Revised" License
1.41k stars 490 forks source link

Unable to Import Config file with openai #193

Open Nparte777 opened 4 years ago

Nparte777 commented 4 years ago

Have been getting this issue while importing config-feedforwad from examples/xor I am new to this so open to corrections Code Written with Help/Reference from : https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/-/tree/master [Tutorial]

Code Enclosed :

import retro 
import numpy as np
import cv2
import neat
import pickle

env = retro.make ('SuperMarioWorld-Snes', 'Start.state')

imgarray = []

def eval_genomes(genomes, config):
    for genome_id, genome in genomes:
        ob = env.reset()
        ac = env.action_space.sample()

        inx, iny ,inc = env.observation_space.shape
        inx = int(inx/8)
        iny = int(iny/8)
        net = neat.nn.recurrent.RecurrentNetwork.create(genome, config)

        current_max_fitness = 0
        fitness_current = 0
        frame = 0
        counter = 0
        xpos = 0
        xpos_max = 0

        done = False

        while not done:

            env.render()
            frame+=1

            ob = cv2.resize(ob, (inx, iny))
            ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY)
            ob = np.reshape(ob, (inx,iny))

            imgarray = np.ndarray.flatten(ob)

            nnOutput =  net.activate(imgarray)

            print(nnOutput)

config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation,'config-feedforward')

p = neat.Population(config)

p.add_reporter(neat.StdOutReporter(True))
stats = neat.StatisticsReporter()
p.add_reporter(stats)
p.add_reporter(neat.Checkpointer(10))

winner = p.run(eval_genomes)

with open('winner.pkl', 'wb') as output:
    pickle.dump(winner, output, 1)
Traceback (most recent call last):
  File "part2.py", line 46, in <module>
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation,'config-feedforward')
  File "/home/warp/.local/lib/python3.8/site-packages/neat/config.py", line 189, in __init__
    self.genome_config = genome_type.parse_config(genome_dict)
  File "/home/warp/.local/lib/python3.8/site-packages/neat/genome.py", line 158, in parse_config
    return DefaultGenomeConfig(param_dict)
  File "/home/warp/.local/lib/python3.8/site-packages/neat/genome.py", line 65, in __init__
    c, p = self.initial_connection.split()
ValueError: too many values to unpack (expected 2)
lucasraythompson commented 2 years ago

2 years late, but maybe you're still looking for help? I wrote the tutorial code you're using (using what I learned from this very repo)! For one, make sure you have the config-feedforward file in the same directory as your python script. Second, ensure that the 'inputs' line in your config-feedforward file is set to the value for SNES games which I believe is 896.

Then, once you fix that, you're going to need to make sure you update your ob variable in your white not done loop because otherwise the BGR2GRAY CV2 function will on your second frame. Just add this at the bottom of your loop "ob, rew, done, info = env.step(nnOutput)" like this:

        while not done:

            env.render()
            frame+=1

            ob = cv2.resize(ob, (inx, iny))
            ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY)
            ob = np.reshape(ob, (inx,iny))

            imgarray = np.ndarray.flatten(ob)

            nnOutput =  net.activate(imgarray)

            print(nnOutput)
            ob, rew, done, info = env.step(nnOutput)

Now it'll run without an issue, but you probably want to add the rest of the lines from my tutorial if you want the neural network to learn. By the way, I took all this code from the Neat examples for xor! So it's a bit funny that it's come back full circle. You might be better served following their tutorials first before complicating it with gym-retro.

If you have further questions, please contact me via youtube comments, or maybe even here! But as you may have noticed, I don't check very often.

Good luck!