试图使python整洁算法与openAI Gym retro配合使用。 我在youtube上使用python3:https://www.youtube.com/watch?v=8dY3nQRcsac&list=PLTWFMbPFsvz3CeozHfeuJIXWAJMkPtAdS&index=8&t=410s 试图在openAI的环境中与音速一起工作。似乎recrrent.py文件有问题。
在此处找到代码:https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/tut2.py
这是错误消息
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'
人口.py文件中的第89行
self.reporters.start_generation(self.generation)
# Evaluate all genomes using the user-provided function.
fitness_function(list(iteritems(self.population)), self.config)
我从@lucas获得的tut3代码 只需计划整洁的网络即可。
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)
如果你们能帮助的话会很棒。 我想完全理解这是一个学校项目。
感谢您的帮助 :))
答案 0 :(得分:0)
您的while循环中有一些错误。使您的eval_genomes函数如下所示:
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)
ndarray.flatten与for x和for y循环具有相同的作用,因此您只需要两种解决方案之一,并且flatten易于阅读。另外,python是缩进真正重要的语言。始终确保您的标签页/空格正确对齐!
希望有效。如果不是,请继续并使用它:
https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/tut2.py
或者这个:
https://gitlab.com/lucasrthompson/Sonic-Bot-In-OpenAI-and-NEAT/blob/master/neat-paralle-sonic.py
祝你好运!