当前位置: 首页>>代码示例>>Python>>正文


Python AI.evolve方法代码示例

本文整理汇总了Python中ai.AI.evolve方法的典型用法代码示例。如果您正苦于以下问题:Python AI.evolve方法的具体用法?Python AI.evolve怎么用?Python AI.evolve使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ai.AI的用法示例。


在下文中一共展示了AI.evolve方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: main

# 需要导入模块: from ai import AI [as 别名]
# 或者: from ai.AI import evolve [as 别名]
def main():
    """The application's entry point.
    """
    print("ANNFlappy V0.0.3. Window dimensions: " + str(WIN_WIDTH) + ', ' +
          str(WIN_HEIGHT))
    brain = AI(20)
    brain.best_fit = 0

    index = 0
    if os.path.exists("best_fit.genome"):
        print("Filling with best candidate from another run.")
        for genome in brain.genomes:
            genome.load("best_fit.genome")
            brain.anns[index].set_internal_data(genome.genome)
            index += 1
            if (len(brain.genomes) / 2) < index:
                break

        brain.best_fit = brain.genomes[0].fitness
        print("Name of the candidate: " + brain.genomes[0].name)
    else:
        print("Starting from scratch.")
    print("Populated " + str(index) + " genomes from file.")

    pygame.init()

    display_surface = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    pygame.display.set_caption('ANN Flappy Bird')

    clock = pygame.time.Clock()
    score_font = pygame.font.SysFont(None, 32, bold=True)  # default font
    images = load_images()

    the_end = False
    player_nr = 0
    rounds = 0
    print('SHALL WE PLAY A GAME?')
    while not the_end:
        done = paused = False
        if len(brain.anns) <= player_nr:
            brain.evolve()
            player_nr = 0

        player = brain.anns[player_nr]
        print('Player ' + str(player_nr))
        if rounds == 0:
            brain.genomes[player_nr].fitness = 0
        next_ai_clock = 0

        # the bird stays in the same x position, so bird.x is a constant
        # center bird on screen
        bird = Bird(50, int(WIN_HEIGHT / 2 - Bird.HEIGHT / 2), 2,
                    (images['bird-wingup'], images['bird-wingdown']))
        pipes = deque()

        # this counter is only incremented if the game isn't paused
        frame_clock = 0
        score = 0

        while not done:
            clock.tick(FPS)

            # Handle this 'manually'.  If we used pygame.time.set_timer(),
            # pipe addition would be messed up when paused.
            if not (paused or frame_clock % msec_to_frames(PipePair.ADD_INTERVAL)):
                pp = PipePair(images['pipe-end'], images['pipe-body'])
                pipes.append(pp)

            for e in pygame.event.get():
                if e.type == QUIT or (e.type == KEYUP and e.key == K_ESCAPE):
                    done = the_end = True
                    break
                elif e.type == KEYUP and e.key in (K_PAUSE, K_p):
                    paused = not paused
                # elif e.type == MOUSEBUTTONUP or (e.type == KEYUP and
                #        e.key in (K_UP, K_RETURN, K_SPACE)):
                #    bird.msec_to_climb = Bird.CLIMB_DURATION

            if paused:
                continue  # don't draw anything

            # Wait a little before next action so as to not allow the AI to go
            # mad with key presses.
            if next_ai_clock < time.clock():
                ai_data = list()
                ai_data.append(bird.y)
                ai_data.append(pipes[0].x - bird.x)  # Distance from pipe
                ai_data.append(pipes[0].top_height_px +  # End of top pipe
                               90 -  # Most of the free space
                               bird.y)  # The bird

                if player.action(ai_data):
                    bird.msec_to_climb = Bird.CLIMB_DURATION
                    # Try to stop the network from going nuts with the clicks
                    brain.genomes[player_nr].fitness -= 0.5
                    next_ai_clock = time.clock() + 0.25

            # check for collisions
            pipe_collision = any(p.collides_with(bird) for p in pipes)
            if pipe_collision or (0 >= bird.y or bird.y >= WIN_HEIGHT -
#.........这里部分代码省略.........
开发者ID:deadbok,项目名称:ann-flappy-bird-pygame,代码行数:103,代码来源:annflappy.py


注:本文中的ai.AI.evolve方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。