Exemplo n.º 1
0
 def select_move(self, boards, player, *args, **kwargs):
     n = boards.shape[1]
     game = GoSimulator(n)
     game.set_board_from_prev_boards(boards, player)
     legal = game.get_legal_moves().flatten()
     pi = np.append(legal, [1])
     pi = np.divide(pi, np.sum(pi))
     move = np.random.choice(len(pi), p=pi)
     return move, pi
Exemplo n.º 2
0
Arquivo: MCTS.py Projeto: yl573/BetaGo
 def __init__(self, model, size, n_input, cpuct):
     self.model = model
     self.n_input = n_input
     self.size = size
     self.cpuct = cpuct
     self.root = None
     game = GoSimulator(self.size)
Exemplo n.º 3
0
 def __init__(self,
              model,
              player,
              size,
              input_moves,
              start_boards=None,
              verbose=0):
     self.model = model
     self.game = GoSimulator(size)
     self.player = player
     self.n = size
     self.verbose = verbose
     self.input_moves = input_moves
Exemplo n.º 4
0
 def __init__(self, model, player, m=8, n=5, start_boards=None):
     self.model = model
     self.game = GoSimulator(n)
     self.m = m
     self.n = n
     self.root = self._create_root(start_boards, player)
Exemplo n.º 5
0
class MCTS:
    def __init__(self, model, player, m=8, n=5, start_boards=None):
        self.model = model
        self.game = GoSimulator(n)
        self.m = m
        self.n = n
        self.root = self._create_root(start_boards, player)

    # def print_tree(self, node=None):
    #     print('MCTS Node')
    #     if node is None:
    #         node = self.root
    #     print(node.boards)
    #     for child in node.children:
    #         self.print_tree(child)

    def _create_root(self, start_boards, player):
        if start_boards is not None:
            boards = start_boards
        else:
            boards = np.zeros((self.m, self.n, self.n))
        P, _ = self.model.eval(boards)
        return MCTNode(boards, P, player)

    def search_for_pi(self, iterations=10, temp=1):
        for i in range(iterations):
            self._search(self.root)
        pi = np.power(self.root.N, 1 / temp) / np.sum(
            np.power(self.root.N, 1 / temp))
        return pi

    def set_move(self, move):
        new_root = self.root.find_child(move)
        if new_root is None:
            raise ValueError("Invalid move")
        self.root = new_root

    def _search(self, node):
        self.game.set_board_from_prev_boards(node.boards, node.player)

        legal = self.game.get_legal_moves().flatten()
        legal = np.append(legal, 1)

        U = calc_U(node.P, node.N)
        score = (node.Q + U)
        if np.sum(score) == 0:
            # if no moves have been played before
            # pick a random position
            score = np.random.uniform(size=(self.n**2 + 1))

        score[np.where(legal == 0)] = -np.inf
        move = np.argmax(score)

        assert legal[move] == 1

        child = node.find_child(move)
        if child is not None:
            V = self._search(child)
        else:
            if move == self.n**2:  # this is the pass move
                board, next_player = self.game.pass_move()
            else:
                y, x = divmod(move, self.n)
                board, next_player = self.game.play(x, y)

            new_boards = update_boards(node.boards, board)
            P, V = self.model.eval(new_boards)
            child = MCTNode(new_boards, P, next_player)
            node.add_child(move, child)

        node.W[move] += V
        node.N[move] += 1
        node.Q[move] = node.W[move] / node.N[move]
        return V
Exemplo n.º 6
0
from GoGame.GoSimulator import GoSimulator
from GoGame import GoBackend
from Shared.Consts import BLACK, WHITE, num_to_char
import numpy as np

game = GoSimulator(5)

def board_to_string(board):
    board_flat = board.reshape(5**2)
    board_str = ''.join(list((map(lambda x: num_to_char[x], board_flat))))
    return board_str

boards = np.array([[
    [0,0,0,0,0],
    [0,0,1,0,0],
    [0,1,-1,1,0],
    [0,-1,0,-1,0],
    [0,0,-1,0,0]
],[
    [0,0,0,0,0],
    [0,0,1,0,0],
    [0,1,0,1,0],
    [0,-1,1,-1,0],
    [0,0,-1,0,0]
]])

game.set_board_from_prev_boards(boards, WHITE)
print(game.board)
print(game.board.ko)
assert game.board.ko == 17
Exemplo n.º 7
0
 def __init__(self, model, player, n=5, start_boards=None):
     self.model = model
     self.game = GoSimulator(n)
     self.player = player
     self.n = n
Exemplo n.º 8
0
from Selfplay import Selfplay_2models
from Model import Model
from GoGame.GoSimulator import GoSimulator
from Shared.Consts import BLACK, WHITE
import time

import numpy as np

N = 5
n_input = 4

game = GoSimulator(N)

# Starting player
player = BLACK

model1 = Model(saved_path='go_model_3.h5', size=N,
               input_moves=n_input)  #if there is a saved model
#model1 = Model(size=N, input_moves=n_input)
model2 = Model(size=N, input_moves=n_input)
#model2 = Model(saved_path='go_model_9.h5', size=N, input_moves=n_input)
game_selfplay = Selfplay_2models(model1,
                                 model2,
                                 player,
                                 size=N,
                                 input_moves=n_input,
                                 verbose=1)

black_score_allgames = []
boards_across_allgames = []
Exemplo n.º 9
0
 def __init__(self, model, player, size, input_moves, start_boards=None):
     self.model = model
     self.game = GoSimulator(size)
     self.m = size
     self.n = size
     self.root = self._create_root(start_boards, player)
Exemplo n.º 10
0
Arquivo: MCTS.py Projeto: yl573/BetaGo
        self.Q = np.zeros(n**2 + 1)
        self.P = P
        self.player = player
        self.children = {}
        self.legal = get_legal(boards, player)

    def find_child(self, move):
        if str(move) in self.children:
            return self.children[str(move)]
        return None

    def add_child(self, move, child):
        self.children[str(move)] = child


game = GoSimulator(5)


def simulate_move(boards, player, move):
    game.set_board_from_prev_boards(boards, player)
    n = boards.shape[1]
    if move == n**2:  # this is the pass move
        return game.pass_move()
    else:
        y, x = divmod(move, n)
        return game.play(x, y)


def process_end_state(boards, player):
    game.set_board_from_prev_boards(boards, player)
    black_lead = game.black_score_lead()
Exemplo n.º 11
0
    if player == WHITE:
        return BLACK
    elif player == BLACK:
        return WHITE


class Model:
    def eval(self, board):
        P = np.ones(26) / 26
        V = 0.1
        return P, V


model = Model()

game = GoSimulator(5)

boards = np.array([[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
                   [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                    [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]])

player = BLACK

game.set_board_from_prev_boards(boards, player)
# game.print_board()
# or if there is no KO, you can use
# game.set_board(boards[-1], BLACK)

print('+--------- START ---------+:\n')
Exemplo n.º 12
0
from GoGame.GoSimulator import GoSimulator
from Shared.Consts import BLACK, WHITE
import numpy as np

game = GoSimulator(5)

boards = np.array([[
    [0,0,0,0,0],
    [0,0,1,0,0],
    [0,1,-1,1,0],
    [0,-1,0,-1,0],
    [0,0,-1,0,0]
],[
    [0,0,0,0,0],
    [0,0,1,0,0],
    [0,1,0,1,0],
    [0,-1,1,-1,0],
    [0,0,-1,0,0]
]])

game.set_board_from_prev_boards(boards, WHITE)
# or if there is no KO, you can use
# game.set_board(boards[-1], BLACK)

print(game.board)

# this function is currently faulty, it doesn't deal with ko
# but for the go player it makes no difference
legal = game.get_legal_moves()
print(legal, '\n')
Exemplo n.º 13
0
 def __init__(self, agent1, agent2, player=BLACK, size=5, n_input=4):
     self.agents = [agent1, agent2]
     self.game = GoSimulator(size)
     self.player = player
     self.n = size
     self.n_input = n_input