mercredi 24 avril 2019

Why is not the new game equal to the previous one if I have reinitialized the seed and they have the same seed?

I am training a neural network to play a game. The game is randomly generated, but I want the agent to train and play always in the same environment. Therefore I always initialize the seed before starting a new game. However, the only result I see is that when I run the program, the games are all different, but when I run it for a second time, the games are equal to the first time. It seems like I correctly set the seed, but I am not able to reinitialize it.

I could not upload the code properly to stackoverflow, so here is the link: https://gist.github.com/AOrtizDZ/725c1efc191d3aeb854dddbc1cbacbd3

Here is the part of the code that initiates the game each time:

def game(genome, config):

random.seed(12)

global reward, mustcreate, dontcreate, must_creation_counter, score
global forbidden_creation_counter

net = neat.nn.FeedForwardNetwork.create(genome, config)

pygame.init()
pygame.mixer.init()
# screen = pygame.display.set_mode((WIDTH, HEIGHT))
# pygame.display.set_caption("Dinosaur")
clock = pygame.time.Clock()
# font = pygame.font.Font(None, 35)

players = list()
for i in range(num_players):
    players.append(Player(keys[i], colors[i], semicolors[i], i * 45))

enemies = list()

# Game loop
running = True
fps_counter = 0
reward = 0
action = np.zeros([3])
action[0] = 1

while running:

    reward += 1
    # keep loop running at the right speed

    #   clock.tick(FPS)

    # Process input (events)

    for event in pygame.event.get():
        # check for closing window

        if event.type == pygame.QUIT:
            running = False

    if (random.random() < prob_per_second / FPS and not dontcreate) or mustcreate:

        enemies.append(Enemy(level))

        dontcreate = True

        mustcreate = False

        must_creation_counter = 0

    else:
        must_creation_counter += 1

    if dontcreate == True:

        forbidden_creation_counter += 1

        if forbidden_creation_counter == 30:
            dontcreate = False

            forbidden_creation_counter = 0

    if must_creation_counter == 120:
        mustcreate = True

    #   screen.fill(WHITE)
    # Update

    for player in players:
        player.update(action)

    #       screen.blit(player.image, player.rect)
    # Draw / render

    for enemy in enemies:

        enemy.update()

        #       screen.blit(enemy.image, enemy.rect)

        if enemy.rect.right < 0:
            enemies.remove(enemy)

            score += 1

        for player in players:

            if pygame.sprite.collide_rect(player, enemy):
                player.choque()

    for player in players:

        if player.inmortal == True:

            player.counter += 1

            if player.counter == 120:
                player.counter = 0

                player.inmortal = False

        if player.vidas == 0:
            running = False

            return reward

    #       screen.blit(font.render(str(player.vidas), True, player.color), [60 + 60 * players.index(player), 40])

    # enemies.sort(key=operator.attrgetter('rect.left'))
    enemies_coord = list()

    for enemy in enemies:
        enemies_coord.append(enemy.rect.left)

        enemies_coord.append(enemy.rect.width)

        enemies_coord.append(enemy.rect.height)

        enemies_coord.append(enemy.speedx)

    player_coord = [player.rect.left,
                    player.rect.top,
                    player.speedy if player.speedy != 0.5 else 0]

    state = player_coord + enemies_coord

    while len(state) < 15:
        state.append(0)

    while len(state) > 15:
        state.pop()

    output = net.activate(state)
    max_Q = np.argmax(output)
    action = np.zeros([3])

    action[max_Q] = 1

    if max_Q == 1: reward -= 2

    #   screen.blit(font.render(str(reward), True, BLACK), [WIDTH - 60, 40])

    #   pygame.draw.line(screen, BLACK, (0, HEIGHT - 10), (WIDTH, HEIGHT - 10), 3)
    # *after* drawing everything, flip the display

    #   pygame.display.flip()

    fps_counter += 1




Aucun commentaire:

Enregistrer un commentaire