如何修好整洁经常性.py文件,整洁的python库openAI-gym

2024-05-18 08:20:10 发布

您现在位置:Python中文网/ 问答频道 /正文

尝试让python整洁算法与openAI一起工作。 我正在youtube上使用python3:https://www.youtube.com/watch?v=8dY3nQRcsac&list=PLTWFMbPFsvz3CeozHfeuJIXWAJMkPtAdS&index=8&t=410s 在openAI env中尝试整洁地与sonic一起工作。这辆车好像有问题重现.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'

第89行人口.py文件


    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)


如果你们能帮忙就太好了。 我想充分理解,因为这是一个学校的项目。你知道吗

谢谢你的帮助 :))


Tags: inpyimportselfenvconfigevalcv2
1条回答
网友
1楼 · 发布于 2024-05-18 08:20:10

while循环中有一些错误。使您的评估基因组功能如下所示:

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)

那个展平与for x和for y循环做相同的事情,因此您只需要两种解决方案中的一种,而展平更易于阅读。另外,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

祝你好运!你知道吗

相关问题 更多 >