Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ListIndex超出了NEAT算法的范围_Python_Pygame_Artificial Intelligence_Genetic Algorithm - Fatal编程技术网

Python ListIndex超出了NEAT算法的范围

Python ListIndex超出了NEAT算法的范围,python,pygame,artificial-intelligence,genetic-algorithm,Python,Pygame,Artificial Intelligence,Genetic Algorithm,我正在尝试创建一个玩游戏的AI。游戏要求人工智能躲避来袭的小行星。我使用的是NEAT库,但由于某种原因,我一直收到一个列表索引错误,我似乎不知道为什么。旁注:我是新的整洁和遗传算法 我希望我的游戏一次只运行一个基因组 def main(genomes, config): ge = [] # List to hold all genome objects nets = [] # List to hold the NN associated with each

我正在尝试创建一个玩游戏的AI。游戏要求人工智能躲避来袭的小行星。我使用的是NEAT库,但由于某种原因,我一直收到一个列表索引错误,我似乎不知道为什么。旁注:我是新的整洁和遗传算法

我希望我的游戏一次只运行一个基因组

def main(genomes, config):
    ge = []        # List to hold all genome objects
    nets = []      # List to hold the NN associated with each genome
    players = []   # List to hold the player objects (Contains x y coordinates and other attributes)

   # To the three lists append each of the following objects: NNs, genomes and players 
     - such that they each match with their index position.
     Example: players[1] has its genome stored at genome[1] and it's neural network at nets[1]

    for __, g in genomes:
        net = neat.nn.FeedForwardNetwork.create(g, config)
        nets.append(net)
        players.append(player())
        g.fitness = 0
        ge.append(g)


    # Asteroid objects that are meant to be avoided
      asteroid(img_num, max_y, speed, width, height, threshold_score)
    asteroids = [0,
             asteroid(1, playable_height-39, 10.5, 35, 39, 0),
             asteroid(2, playable_height-25, 15, 25, 25, 10),
             asteroid(3, playable_height-30, 12.5, 30, 30, 15),
             asteroid(4, playable_height-35, 10, 35, 35, 20)]




    for x in range(len(players)):  # Suspected source of error
        game_over = False
        while not game_over:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()


            # Calculates the output of the current player playing using it's neural network

            output = nets[x].activate(((players[x].y / 169),
                GetDistance(1, players[x], asteroids),
                GetDistance(2, players[x], asteroids),
                GetDistance(3, players[x], asteroids),
                GetDistance(4, players[x], asteroids)))


            if output[0] > 0:
                players[x].moveDOWN()
            elif output[0] < 0:
                players[x].moveUP()



            if players[x].y > playable_height - players[x].height:
                players[x].y = playable_height - players[x].height
            elif players[x].y < 0:
                players[x].y = 0
            window.fill((0, 0, 0))
            window.blit(bg_img, (0, 0))
            players[x].draw()


           # Checks to see if asteroid has been dodged and then increases fitness by 5 for that genome. If collision occurs remove the genome, NN and player from their lists.

            for n in range(1, 5):
                if asteroids[n].x < 0:
                    players[x].score += 1
                    ge[x].fitness += 5
                asteroids[n].move(players[x])
                if asteroids[n].collision(players[x]):
                    asteroids[n].reset(asteroids)
                    ge[x].fitness -= 1
                    del players[x]
                    del nets[x]
                    del ge[x]
                    game_over = True


            Text_display("Score: " + str(players[x].score * 100), white, 0, 200)
            Text_display("Asteroid 1: " + str(GetDistance(1, players[x], asteroids)), white, 0, 220)
            Text_display("Asteroid 2: " + str(GetDistance(2, players[x], asteroids)), white, 0, 240)
            Text_display("Asteroid 3: " + str(GetDistance(3, players[x], asteroids)), white, 0, 260)
            Text_display("Asteroid 4: " + str(GetDistance(4, players[x], asteroids)), white, 0, 280)
            Text_display("Space Ship Position: " + str(players[x].y / 169), white, 0, 300)





            pygame.display.update()





# Set up function for the NEAT library

def run(config_path):
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path)
    p = neat.Population(config)

    p.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)

    winner = p.run(main, 100)

if __name__ == "__main__":
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, "NEATconfig.txt")
    run(config_path)

def main(基因组,配置):
ge=[]保存所有基因组对象的列表
nets=[]#保存与每个基因组相关的NN的列表
players=[]#用于保存player对象的列表(包含x-y坐标和其他属性)
#在三个列表中分别添加以下对象:NNs、基因组和玩家
-这样,它们每个都与它们的索引位置相匹配。
示例:玩家[1]的基因组存储在基因组[1],神经网络存储在网络[1]
对于基因组中的_g,g:
net=neat.nn.FeedForwardNetwork.create(g,config)
net.append(net)
players.append(player())
g、 适合度=0
通用电气公司(g)
#本应避免的小行星物体
小行星(img_num、max_y、速度、宽度、高度、阈值分数)
小行星=[0,
小行星(1,可玩_高度-39,10.5,35,39,0),
小行星(2,可玩的_高度-25,15,25,25,10),
小行星(3,可玩的_高度-30,12.5,30,30,15),
小行星(4,可玩_高度-35,10,35,35,20)]
对于范围内的x(len(玩家)):#疑似错误源
游戏结束=错误
虽然游戏尚未结束:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
#使用当前播放器的神经网络计算其输出
输出=网络[x]。激活((玩家[x].y/169),
GetDistance(1,玩家[x],小行星),
GetDistance(2,玩家[x],小行星),
GetDistance(3,玩家[x],小行星),
GetDistance(4,玩家[x],小行星)))
如果输出[0]>0:
玩家[x].moveDOWN()
elif输出[0]<0:
玩家[x].moveUP()
如果玩家[x].y>可玩的高度-玩家[x].高度:
玩家[x].y=可玩高度-玩家[x].height
elif玩家[x].y<0:
玩家[x].y=0
窗口填充((0,0,0))
窗口(bg_img,(0,0))
玩家[x].抽签()
#检查小行星是否被闪避,然后增加该基因组的适应度5。如果发生冲突,则从其列表中删除基因组、NN和播放器。
对于范围(1,5)内的n:
如果小行星[n].x<0:
玩家[x]。得分+=1
ge[x].适合度+=5
小行星[n]。移动(玩家[x])
如果小行星[n]。碰撞(玩家[x]):
小行星[n]。重置(小行星)
ge[x].适合度-=1
del玩家[x]
del nets[x]
德尔格[x]
游戏结束=正确
文本显示(“分数:+str(玩家[x]。分数*100),白色,0,200)
文本显示(“小行星1:+str(GetDistance(1,玩家[x],小行星)),白色,0220)
文本显示(“小行星2:+str(GetDistance(2,玩家[x],小行星)),白色,0240)
文本显示(“小行星3:+str(GetDistance(3,玩家[x],小行星)),白色,0260)
文本显示(“小行星4:+str(GetDistance(4,玩家[x],小行星)),白色,0280)
文本显示(“太空船位置:”+str(玩家[x].y/169),白色,0,300)
pygame.display.update()
#设置整洁库的函数
def运行(配置路径):
config=neat.config.config(neat.DefaultGenome、neat.DefaultReproduction、neat.DefaultSpeciesSet、neat.defaultstaging、config_path)
p=neat.Population(配置)
p、 add_reporter(neat.StdOutReporter(True))
stats=neat.StatisticsReporter()
p、 添加报告器(统计数据)
获胜者=p.run(主,100)
如果名称=“\uuuuu main\uuuuuuuu”:
local\u dir=os.path.dirname(\u文件\u)
config\u path=os.path.join(local\u dir,“NEATconfig.txt”)
运行(配置路径)

请提供完整的错误消息和发生错误的行。@rabbi76它发生在output=nets[x]行中。激活后,错误列表索引超出范围