微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python NEAT表现怪异

如何解决Python NEAT表现怪异

嗨,我在python中使用整洁的模块制作了一只飞扬的小鸟AI,但AI的表现很怪异。 一半的鸟只飞起来,另一半什么也不做,直接掉下来。

这是净模块调用的主要功能

def main(genomes,config):
nets = []
birds = []
ge = []

for _,genome in genomes:
    genome.fitness = 0
    net = neat.nn.FeedForwardNetwork.create(genome,config)
    nets.append(net)
    birds.append(Bird(50,WIDTH/2))
    ge.append(genome)

floor = Floor() 
pipes = [Pipe(WIDTH)]
score = 0

clock = pygame.time.Clock()
run = True
while run and len(birds):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            sys.exit()
            break
        
        if event.type == Bird.BIRDFLAP:
            for bird in birds:
                bird.animate()

    pipeIndex = 0
    if len(birds):
        if len(pipes) > 1 and birds[0].x > pipes[0].x + pipeWidth:
            pipeIndex = 1 
    
    for i,bird in enumerate(birds):
        genomes[i][1].fitness += 0.03
        bird.move()
        topPipedist = abs(bird.y - pipes[pipeIndex].topPipeY)
        bottomPipedist = abs(bird.y - pipes[pipeIndex].bottomPipeY)
        output = nets[i].activate((bird.y,topPipedist,bottomPipedist))

        if output[0] > 0.5:
            bird.jump()
    floor.move()


    rem = []
    addPipe = False
    for pipe in pipes:
        pipe.move()
        for bird in birds:
            if bird.collide(pipe):
                birdindex = birds.index(bird)
                genomes[birdindex][1].fitness -= 1
                nets.pop(birdindex)
                ge.pop(birdindex)
                birds.pop(birdindex)

        if pipe.x + pipeWidth < 0:
            rem.append(pipe)
            
        if not pipe.passed and pipe.x < bird.x:
            pipe.passed = True
            addPipe = True
    
    if addPipe:
        score += 1
        for i in range(len(ge)):
            genomes[i][1].fitness += 5
        pipes.append(Pipe(WIDTH))
    
    for r in rem:
        pipes.remove(r)
    

    for bird in birds:
        if bird.boundary():
            birdindex = birds.index(bird)
            nets.pop(birdindex)
            ge.pop(birdindex)
            birds.pop(birdindex)
        
    screen.blit(bgSurface,(0,0))         
    

    for pipe in pipes:
        pipe.draw(screen)
    floor.draw(screen)
    for bird in birds:
       bird.draw(screen)
    scoreText = FONT.render(f'score: {score}',1,(255,255,255))
    screen.blit(scoreText,0))
    
    pygame.display.flip()
    clock.tick(100)

我们非常感谢您的帮助。 这是github上代码链接

https://github.com/arduino-monkey/flappy-bird-NEAT

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。