【发布时间】:2019-09-07 19:14:45
【问题描述】:
试图让 python 简洁的算法与 openAI gym retro一起使用。 我在 youtube 上使用 python3:https://www.youtube.com/watch?v=8dY3nQRcsac&list=PLTWFMbPFsvz3CeozHfeuJIXWAJMkPtAdS&index=8&t=410s 试图在 openAI 环境中与 sonic 一起工作。 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'
population.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)
如果你们能帮忙就太好了。 我想完全理解,因为这是一个学校项目。
感谢您的帮助 :))
【问题讨论】:
-
@lucas 你能看看吗:)
标签: python machine-learning openai-gym neat