Ejemplo n.º 1
0
def eval_genome(genomes, config):
    nets = []
    snakes = []
    ge = []
    food = fd.Food()
    
    for genome_id, genome in genomes:
        genome.fitness = 0
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        nets.append(net)
        snakes.append(snk.Snake('NEAT'))
        ge.append(genome)

    pygame.init()
    fpsClock = pygame.time.Clock()  
    for i in range(len(snakes)):
        while snakes[i].state == 'alive' and snakes[i].nb_moves < 200:
            

            ge[i].fitness = snakes[i].score
            relative_directions = const.RELATIVE_POSITIONS_DICTIONARY[str(snakes[i].direction)]
            #list relative to the current direction ['FORWARD', 'LEFT', 'RIGHT']

            inputs = generate_inputs(snakes[i], food.position, relative_directions)
            output = nets[i].activate(inputs) # output in the form [FORWARD, LEFT, RIGHT]

            decision_index = output.index(max(output))
            move = relative_directions[decision_index]

            graphics.initiateGraphics()
                
            snakes[i].move()

            food.isEaten(snakes[i], food)

            snakes[i].chooseDirection(move)

            graphics.updateGraphics(snakes[i], food, fpsClock, const.FPS)
Ejemplo n.º 2
0
            elif event.type == KEYDOWN:

                if event.key == K_n:
                    fps -= 5

                if event.key == K_m:
                    fps += 5

        #initiate graphics
        graphics.initiateGraphics()

        #The function choose_move() from the naiveAlgorithm script returns the calculated next move
        snake.chooseDirection(NA.choose_move(snake, food.position))

        #print(BT.find_path(snake, food.position, last_moves))
        #print(BT.is_food_around(snake, food.position))

        #the snake takes the next the next position
        snake.move()

        #for each iteration  we check if the snake and the food collided
        food.isEaten(snake, food)

        #the graphics are updated and the frame rate is controlled
        graphics.updateGraphics(snake, food, fpsClock, fps)

    #log results into xml file
    datalogger.logAverageScore('Naive1.2-g')
    pygame.quit()
    sys.exit()