I’m working on a school project where I need to implement the NEAT algorithm with OpenAI Gym for a game environment. I keep running into an issue with numpy arrays and the append method.
The main problem happens when I try to process the observation data from the game environment. Here’s the error I get:
AttributeError: 'numpy.ndarray' object has no attribute 'append'
Here’s my current implementation:
import retro
import numpy as np
import neat
import cv2
game_env = retro.make('SonicTheHedgehog-Genesis', 'GreenHillZone.Act1')
def run_evaluation(population, configuration):
for individual_id, individual in population:
observation = game_env.reset()
action = game_env.action_space.sample()
height, width, channels = game_env.observation_space.shape
new_height = int(height/8)
new_width = int(width/8)
network = neat.nn.RecurrentNetwork.create(individual, configuration)
max_score = 0
current_score = 0
step_count = 0
position_x = 0
best_position = 0
finished = False
while not finished:
game_env.render()
step_count += 1
observation = cv2.resize(observation, (new_height, new_width))
observation = cv2.cvtColor(observation, cv2.COLOR_BGR2GRAY)
observation = np.reshape(observation, (new_height, new_width))
pixel_data = np.ndarray.flatten(observation)
for row in observation:
for pixel in row:
pixel_data.append(pixel) # This line causes the error
network_output = network.activate(pixel_data)
observation, reward, finished, game_info = game_env.step(network_output)
pixel_data.clear()
config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
neat.DefaultSpeciesSet, neat.DefaultStagnation,
'config-feedforward')
population = neat.Population(config)
result = population.run(run_evaluation)
I’m trying to flatten the image data to feed it into the neural network but I’m clearly doing something wrong with the numpy array handling. Can someone help me understand what’s going wrong and how to fix it properly?
This is for my school project and I really want to understand the concept behind it.