0

Trying to get the python neat algorithm to work with openAI gym retro. I am using python3 with the youtube:https://www.youtube.com/watch?v=8dY3nQRcsac&list=PLTWFMbPFsvz3CeozHfeuJIXWAJMkPtAdS&index=8&t=410s trying to get neat to work with sonic in the openAI env. it seems there is a problem with the recrrent.py file.

Find the code here: https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/tut2.py

This Is the Error message


    File "tut3.py", line 53, in <module>
        winner = p.run(eval_genomes)
      File "/home/gym/OPAI/lib/python3.6/site-packages/neat/population.py", line 89, in run
        fitness_function(list(iteritems(self.population)), self.config)
      File "tut3.py", line 41, in eval_genomes
        imgarray.append(y)
    AttributeError: 'numpy.ndarray' object has no attribute 'append'

Line 89 in the population.py file


    self.reporters.start_generation(self.generation)

                # Evaluate all genomes using the user-provided function.
                fitness_function(list(iteritems(self.population)), self.config)



The tut3 code that I got from @lucas Just plan neat net work.


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

env = retro.make('SonicTheHedgehog-Genesis', 'GreenHillZone.Act1')

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.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)
            for x in ob:
                for y in x:
                    imgarray.append(y)

                nnOutput = net.activate(imgarray)
                print(nnOutput)

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

config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                     neat.DefaultSpeciesSet, neat.DefaultStagnation,
                     'config-feedforward')
p = neat.Population(config)
winner = p.run(eval_genomes)


Would be great if you guys could help. I want to fully understand as it is a school project.

Thanks for your help :))

alexa bot
  • 13
  • 3

1 Answers1

0

Your while loop has some errors in it. Make your eval_genomes function look like this below :

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.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)
            ob, rew,done, info = env.step(nnOutput)

The ndarray.flatten does the same thing as the for x and for y loop so you only need one of the two solutions and flatten is easier to read. Additionally python is a language where indentation really matters. Always make sure your tabs/spaces are lined up correctly!

Hope that works. If it doesn't, just go on and use this:

https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/tut2.py

or this:

https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/neat-paralle-sonic.py

Good luck!