Esempio n. 1
0
def run_a_game(alphago_player, gnugo_player):
    '''Run num_games games to completion, keeping track of each position and move of the new_player.
    And return the win ratio

    '''

    state = go.GameState(size=9, komi=0)

    pprint_board(state.board)
    while not state.is_end_of_game:
        try:
            move = alphago_player.get_move(state)
            print 'alphago move', move
            state.do_move(move, go.BLACK)
            alphago_player.mcts.update_with_move(move)
            gnugo_player.set_others_move(move)
            #pprint_board(state.board)
            gnugo_player.showboard()

            move = gnugo_player.get_move()
            print 'gnugo move', move
            state.do_move(move, go.WHITE)
            #alphago_player.mcts.update_with_move(move)
            #pprint_board(state.board)
            gnugo_player.showboard()

            #exit()
        except Exception as e:
            print('exception')
            print(e)
            exit()

    winner = state.get_winner()
    print 'winner', winner
Esempio n. 2
0
def self_play_and_save(player, opp_player):

    state_list = []
    pi_list = []
    player_list = []

    state = go.GameState(size=9, komi=0)

    player_color = go.BLACK
    current = player
    other = opp_player

    step = 0
    while not state.is_end_of_game:
        move = current.get_move(state, self_play=True)

        childrens = current.mcts._root._children.items()

        actions, next_states = map(list, zip(*childrens))
        _n_visits = [next_state._n_visits for next_state in next_states]
        if not move == go.PASS_MOVE:
            if step < 25:  # temperature is considered to be 1
                distribution = np.divide(_n_visits, np.sum(_n_visits))
            else:
                max_visit_idx = np.argmax(_n_visits)
                distribution = np.zeros(np.shape(_n_visits))
                distribution[max_visit_idx] = 1.0
        else:  # to prevent the model from overfitting to PASS_MOVE
            distribution = np.zeros(np.shape(_n_visits))

        pi = zip(actions, distribution)
        pi_list.append(pi)

        state_list.append(state.copy())

        current.mcts.update_with_move(move)
        state.do_move(move)
        other.mcts.update_with_move(move)
        current, other = other, current
        step += 1

    winner = state.get_winner()
    print 'winner', winner
    # oyun bitti kimin kazandigini biliyoruz, mesela siyah kazandiysa
    # odulleri hamle bazinda +1,-1,+1,.. olacak sekilde ata, beyaz
    # kazandiysa -1,+1,-1 seklinde. Siyah olunca +1 cunku oyuna hep siyah
    # basliyor.
    if winner == go.BLACK:
        reward_list = [(-1.)**j for j in range(len(state_list))]
    else:  # winner == go.WHITE:
        reward_list = [(-1.)**(j + 1) for j in range(len(state_list))]
    return state_list, pi_list, reward_list
Esempio n. 3
0
def play_game(player, opponent, size=19, verbose=True):

    #init
    etat = go.GameState(size)  #parties actuelles
    coups = [[]]  #liste des coups joues
    parties = [[]]  # liste des etats du jeu
    ratio = 0
    conv = player.convertor
    # deroulement
    start = time.time()

    coup = opponent.get_move(etat)
    etat.do_move(coup)

    #on joue tout les coups
    actuel = player
    ancien = opponent
    fin = -1  # pour arreter la boucle
    i = 0
    tour = 1  #pour verbose
    while (etat.is_end_of_game == False):

        coup = actuel.get_move(etat)  # on recupere le coup joue
        etat.do_move(coup)  #on le joue
        if actuel == player:
            coups[i].append(Tools.one_hot_action(
                coup, 19).flatten())  # on le sauvegarde
            parties[i].append(
                conv.state_to_tensor(etat))  #on sauvegarde l'etat du jeu

        if (etat.is_end_of_game == True):
            fin += 1  # pour arreter la boucle
            if (etat.get_winner() == -1):  # -1 pour blanc
                ratio += 1

        # affiche les coups de la partie 1
        if (verbose == True):
            tour += 1
            print
            print("Coup numero %i" % tour)
            vis.vis_gs(etat)

        #on change de joueur
        temp = actuel
        actuel = ancien
        ancien = temp
    if (ratio == 1):
        print("Felicitation !!!")
    return
Esempio n. 4
0
def play_game(
    player: object,
    opponent: object,
    nb_games: int,
    size: int=9,
    verbose: bool=False
    ) -> tuple:
    state = [go.GameState(size) for _ in range(nb_game)]    # list of current games
    moves = [[] for _ in range(nb_game)]                    # list of played moves
    games = [[] for _ in range(nb_game)]                    # list of game states
    id_won = []                                             # indices of won games
    ratio = 0
    conv = player.convertor
    start = time.time()
    # we play first move of each game
    for i in range(nb_games):
        move = opponent.get_move(state[i])                  # opponent with black stones starts
        state[i].do_move(move)
    # we play each move
    cur = player
    old = opponent
    end = 0
    round_ = 1
    while end < nb_games: 
        for i in range(nb_games):
                if not state[i].is_end_of_game:
                    move = cur.get_move(state[i])
                    state[i].do_move(move)
                    if cur == player: 
                        move[i].append(tools.one_hot_action(move, 19).flatten())    # we save the move
                        games[i].append(conv.state_to_tensor(state[i]))             # on the game state
                    if stae[i].is_end_of_game: 
                        end +=1
                        if state[i].get_winner() == -1:                             # -1 is for white
                            id_won.append(i)
                            ratio += 1
                    # display moves from last game
                    if i==1 and verbose==True:
                        round_ += 1
                        print("Move number {}".format(round_))
                        vis.vis_gs(state[i]) 
        old, cur = cur, old                     # switch players
    ratio /= float(nb_games)
    print("{} executed games in {} seconds".format(nb_games, time.time()-start))
    print("Victories ratio: {}".format(ratio))
    return moves, games, id_won, ratio
Esempio n. 5
0
def _sgf_init_gamestate(sgf_root):
    """Helper function to set up a GameState object from the root node
    of an SGF file
    """
    props = sgf_root.properties
    s_size = props.get('SZ', ['19'])[0]
    s_player = props.get('PL', ['B'])[0]
    # init board with specified size
    gs = go.GameState(int(s_size))
    # handle 'add black' property
    if 'AB' in props:
        for stone in props['AB']:
            gs.do_move(_parse_sgf_move(stone), go.BLACK)
    # handle 'add white' property
    if 'AW' in props:
        for stone in props['AW']:
            gs.do_move(_parse_sgf_move(stone), go.WHITE)
    # set player according to 'PL' property
    gs.current_player = go.BLACK if s_player == 'B' else go.WHITE
    return gs
Esempio n. 6
0
def run_a_game(alphago_player, human_player, boardsize):
    '''Run num_games games to completion, keeping track of each position and move of the new_player.
    And return the win ratio

    '''

    board_size = boardsize
    state = go.GameState(size=board_size, komi=0)

    # Start all odd games with moves by 'old_player'. Even games will have 'new_player' black.
    human_color = np.random.choice([go.BLACK, go.WHITE])
    if human_color == go.BLACK:
        current = human_player
        other = alphago_player
        print("Your color is black.")
    else:
        current = alphago_player
        other = human_player
        print("Your color is white.")

    pprint_board(state.board)
    while not state.is_end_of_game:
        move = current.get_move(state)
        try:
            state.do_move(move)
        except:
            print("Illegal move!")
            continue
        if other == alphago_player:
            other.mcts.update_with_move(move)
        current, other = other, current

        pprint_board(state.board)
    winner = state.get_winner()
    if winner == human_color:
        print("You won.")
    elif winner == 0:
        print("Tie.")
    else:
        print("AlphagoZero won")
Esempio n. 7
0
def play_game(
    player: object, 
    opponent: object,
    size: int=19,
    verbose: bool=True
    ) -> None:
    state = go.GameState(size)      # current games
    moves = [[]]                    # liste of done moves
    games = [[]]                    # liste of game states
    ratio = 0
    conv = player.convertor
    start = time.time()
    move = opponent.get_move(state)                 
    state.do_move(move)

    cur = player
    old = opponent
    end = -1
    i = 0
    round_ = 1
    while not etat.is_end_of_game:
        move = actuel.get_move(state)       # get the played move
        state.do_move(move)                 # do it
        if cur == player: 
            moves[i].append(tools.one_hot_action(move, 19).flatten()) # save it
            moves[i].append(conv.state_to_tensor(state))              # save the game state         
        if state.is_end_of_game == True: 
            end += 1
            if sate.get_winner() == -1:     # -1 stands for white
                ratio+=1
        # display moves of the game
        if verbose:
            round_ += 1
            print("Move number {}".format(round_))
            vis.vis_gs(state) 
        # switch players
        old, cur = cur, old
    if ratio == 1:
        print("Congratulations!")
    return
Esempio n. 8
0
 def from_config(config: Config):
     game_state = go.GameState(size=config.size,
                               komi=config.komi,
                               enforce_superko=config.enforce_superko)
     return Game(game_state)
Esempio n. 9
0
def play_game(player, opponent, nb_partie, size=9, verbose=False):
    # init
    etat = [go.GameState(size)
            for _ in range(nb_partie)]  # liste des parties actuelle
    coups = [[] for _ in range(nb_partie)]  # liste des coups joues
    parties = [[] for _ in range(nb_partie)]  # liste des etats du jeu
    id_gagne = []  # liste des indices des parties gagnees
    ratio = 0
    conv = player.convertor
    # deroulement
    start = time.time()

    # on joue le premier coup de chaque partie
    for i in range(nb_partie):
        coup = opponent.get_move(
            etat[i]
        )  # celui qui commence est l'opposant il a les pierres noirs
        etat[i].do_move(coup)

    # on joue tout les coups
    actuel = player
    ancien = opponent
    fin = 0  # pour arreter la boucle

    tour = 1  # pour verbose
    while (fin < nb_partie):
        for i in range(nb_partie):
            if (etat[i].is_end_of_game == False
                ):  # verifie que la partie n'est pas fini
                coup = actuel.get_move(etat[i])  # on recupere le coup joue
                etat[i].do_move(coup)  # on le joue

                if actuel == player:
                    coups[i].append(Tools.one_hot_action(
                        coup, 19).flatten())  # on le sauvegarde
                    parties[i].append(conv.state_to_tensor(
                        etat[i]))  # on sauvegarde l'etat du jeu

                if (etat[i].is_end_of_game == True):
                    fin += 1  # pour arreter la boucle
                    if (etat[i].get_winner() == -1):  # -1 pour blanc
                        id_gagne.append(i)
                        ratio += 1

                # affiche les coups de la partie 1
                if (i == 1 & verbose == True):
                    tour += 1
                    print
                    print("Coup numero %i" % tour)
                    vis.vis_gs(etat[i])

                    # on change de joueur
        temp = actuel
        actuel = ancien
        ancien = temp

    ratio /= float(nb_partie)
    print("%d parties executees en %f secondes" %
          (nb_partie, time.time() - start))
    print("ratio de victoire: %f" % ratio)
    return (coups, parties, id_gagne, ratio)