def main(agent_0, agent_1, first):
    # Initialisation
    cur_state = SquadroState()
    if first != -1:
        cur_state.cur_player = first
    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_1), 'MyAgent')()
    ]
    agents[0].set_id(0)
    agents[1].set_id(1)
    last_action = None

    while not cur_state.game_over():

        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(),
                                  last_action)

        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)
    print(cur_player)
def game(agent, validation, i, new_model_path):
    
    results = 0
    init = 1

    # Initialisation
    cur_state = SquadroState()
    cur_state.cur_player = 0 if i % 2 == 0 else 1
    agents = [getattr(__import__(agent), 'MyAgent')(), getattr(__import__(agent), 'MyAgent')()]
    agents[0].set_id(0)
    agents[1].set_id(1)
    
    #print_network(agents[0].deepnetwork)
    
    if validation: # Use different models during validation phase
        ind = 0 if i % 4 < 2 else 1 
        agents[ind].set_model_path(new_model_path)
        print('Main model:', ind, ', First player:', cur_state.cur_player)
        # Remove stochastic actions
        #agents[0].epsilonMCTS = 0
        #agents[1].epsilonMCTS = 0
        agents[0].epsilonMove = 0
        agents[1].epsilonMove = 0
        '''
        print('Current network model...............................................')
        print_network(agents[0].deepnetwork)
        print('New network model....................................................')
        print_network(agents[1].deepnetwork)
        '''
        
    last_action = None

    
    while not cur_state.game_over():
    
        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(), last_action)
    
        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)
        
        if init:
            results = agents[cur_player].results
            init = 0
        else:
            results = np.append(results, agents[cur_player].results, 0)
            
    #if validation:
    #    print(results)
    #    print(cur_player)
    return (results, cur_player)
Beispiel #3
0
def game(agent_0, agent_1, i):
    
    model_path = 'model/{}.pt'.format(agent_0)
    results = 0
    init = 1

    # Initialisation
    cur_state = SquadroState()
    cur_state.cur_player = 0 if i % 4 < 2 else 1
    
    if i % 2 == 0:
        agents = [getattr(__import__(agent_0), 'MyAgent')(), getattr(__import__(agent_1), 'MyAgent')()]
        DNN_player = 0
    else:
        agents = [getattr(__import__(agent_1), 'MyAgent')(), getattr(__import__(agent_0), 'MyAgent')()]
        DNN_player = 1
    
    print(DNN_player, cur_state.cur_player)
        
    agents[0].set_id(0)
    agents[1].set_id(1)
    agents[DNN_player].epsilonMove = 0
    #if i == 0:
        #print('Network 0 (main) -------------------------------------------------------')
        #print_network(agents[0].deepnetwork)
        #print('Network 1 (other) -------------------------------------------------------')
        #print_network(agents[1].deepnetwork)

    last_action = None
    
    while not cur_state.game_over():
    
        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(), last_action)
    
        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)

    #print('Last state:', agents[DNN_player].results)
    return (0, abs(cur_player - DNN_player))
Beispiel #4
0
def main(agent_0, agent_1, time_out, first):
    # Initialisation
    pygame.init()
    n_tiles = 7
    n_pawns = 5
    board = Board(n_tiles, n_pawns)
    cur_state = SquadroState()
    if first != -1:
        cur_state.cur_player = first
    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_1), 'MyAgent')()
    ]
    agents[0].set_id(0)
    agents[1].set_id(1)
    times_left = [time_out, time_out]
    last_action = None

    while not cur_state.game_over():
        # Draw board
        board.screen.fill(0)
        board.draw_board(cur_state)
        board.show_turn(cur_state)

        # Update screen
        pygame.display.flip()

        # Make move
        cur_player = cur_state.get_cur_player()
        timer_stop = [False]
        timer = TimerDisplay(board, cur_player, times_left.copy(), timer_stop)
        timer.start()
        try:
            action, exe_time = get_action_timed(agents[cur_player],
                                                cur_state.copy(), last_action,
                                                times_left[cur_player])
            timer_stop[0] = True
            times_left[cur_player] -= exe_time

            if cur_state.is_action_valid(action):
                cur_state.apply_action(action)
                last_action = action
            else:
                cur_state.set_invalid_action(cur_player)

        except TimeoutError:
            timer_stop[0] = True
            cur_state.set_timed_out(cur_player)

        timer.join()

        # Events
        for event in pygame.event.get():

            # Quit when pressing the X button
            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)

    # Game finished: display the winner
    while True:
        # Draw board
        board.screen.fill(0)
        board.draw_board(cur_state)

        # Print the winner
        font = pygame.font.Font("freesansbold.ttf", 48)

        if cur_state.get_winner() == 0:
            text = font.render(" Yellow wins! ", True, (255, 164, 0),
                               (34, 34, 34))
        else:
            text = font.render(" Red wins! ", True, (150, 0, 0), (34, 34, 34))

        textRect = text.get_rect()
        textRect.center = (n_tiles * 100 // 2, n_tiles * 100 // 2)
        board.screen.blit(text, textRect)

        # Print if time-out or invalid action
        if cur_state.timeout_player != None:
            font2 = pygame.font.Font("freesansbold.ttf", 18)
            text2 = font2.render("The opponent timed out", True,
                                 (255, 255, 255), (34, 34, 34))
            textRect2 = text2.get_rect()
            textRect2.center = (n_tiles * 100 // 2, n_tiles * 100 // 2 + 34)
            board.screen.blit(text2, textRect2)
        elif cur_state.invalid_player != None:
            font2 = pygame.font.Font("freesansbold.ttf", 18)
            text2 = font2.render("The opponent made an invalid move", True,
                                 (255, 255, 255), (34, 34, 34))
            textRect2 = text2.get_rect()
            textRect2.center = (n_tiles * 100 // 2, n_tiles * 100 // 2 + 34)
            board.screen.blit(text2, textRect2)

        # Update screen
        pygame.display.flip()

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                exit(0)

        sleep(1)
Beispiel #5
0
def game(agent_0, agent_1, i):

    model_path = 'model/{}.pt'.format(agent_0)
    other_model_path = 'model/{}.pt'.format(agent_1)

    results = 0
    init = 1

    # Initialisation
    cur_state = SquadroState()

    agents = [
        getattr(__import__(agent_0), 'MyAgent')(),
        getattr(__import__(agent_0), 'MyAgent')()
    ]

    DNN_main_player = 0 if i % 2 == 0 else 1
    cur_state.cur_player = 0 if (2 * i) % 2 == 0 else 1

    print(DNN_main_player, cur_state.cur_player)

    agents[0].set_id(0)
    agents[1].set_id(1)
    agents[1 - DNN_main_player].set_model_path(other_model_path)
    '''
    agents[0].epsilonMCTS = 0
    agents[1].epsilonMCTS = 0
    '''
    agents[0].epsilonMove = 0
    agents[1].epsilonMove = 0
    if i == 0:
        print(
            'Network 0 (main) -------------------------------------------------------'
        )
        print_network(agents[DNN_main_player].deepnetwork)
        print(
            'Network 1 (other) -------------------------------------------------------'
        )
        print_network(agents[1 - DNN_main_player].deepnetwork)

    last_action = None

    while not cur_state.game_over():

        # Make move
        cur_player = cur_state.get_cur_player()
        action = get_action_timed(agents[cur_player], cur_state.copy(),
                                  last_action)

        if cur_state.is_action_valid(action):
            cur_state.apply_action(action)
            last_action = action
        else:
            cur_state.set_invalid_action(cur_player)

        if init:
            results = agents[cur_player].results
            init = 0
        else:
            results = np.append(results, agents[cur_player].results, 0)

    return (results, abs(DNN_main_player - cur_player))