首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListIndex超出范围的NEAT算法

ListIndex超出范围的NEAT算法
EN

Stack Overflow用户
提问于 2019-11-19 13:38:00
回答 1查看 90关注 0票数 0

我试着创造一个AI来玩游戏。游戏要求人工智能避开即将到来的小行星。我正在使用这个整洁的库,但是由于某种原因,我总是得到一个列表索引错误,而且我似乎也找不出原因。Sidenote:我对整洁的遗传算法很陌生。

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

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-01-09 18:13:15

我认为问题在于,您在while循环之前编写了一个用于player的循环,这样就会发生索引错误。我建议你试试这个

代码语言:javascript
复制
game_over = False
while not game_over:
    for x,_ in enumerate(players):
        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)))
            # and just put all your code after this I think it gonna be work :)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58935375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档