Ejemplo n.º 1
0
def process_position(tokens):
    board = LeelaBoard()

    offset = 0

    if tokens[1] == 'startpos':
        offset = 2
    elif tokens[1] == 'fen':
        fen = " ".join(tokens[2:8])
        board = LeelaBoard(fen=fen)
        offset = 8

    if offset >= len(tokens):
        return board

    if tokens[offset] == 'moves':
        for i in range(offset + 1, len(tokens)):
            board.push_uci(tokens[i])

    return board
Ejemplo n.º 2
0
class ClientTask(threading.Thread):
    """ClientTask"""
    def __init__(self, id):
        self.id = id
        threading.Thread.__init__(self)
        self.board = LeelaBoard()
        self.board.push_uci('d2d4')

    def run(self):
        context = zmq.Context()
        socket = context.socket(zmq.DEALER)
        identity = u'client-%d' % self.id
        socket.identity = identity.encode('ascii')
        socket.connect('ipc:///tmp/lcztools/network_0')
        socket.send(bytes([1]))  # Hi message
        socket.recv()
        message = self.board.serialize_features()
        for _ in range(20000):
            # print("Client {} sending message".format(identity))
            # message = self.board.serialize_features()
            for _ in range(32):
                socket.send(message)
                # print("Send:", self.id)

            for _ in range(32):
                response = memoryview(socket.recv())
                # print("Response:", self.id)
                # print ("Client {} received message of length {}".format(identity, len(response)))
                if len(response) == 7436:  # single precision
                    value = np.frombuffer(response[:4], dtype=np.float32)
                    policy = np.frombuffer(response[4:], dtype=np.float32)
                elif len(response) == 3718:  # half precision
                    value = np.frombuffer(response[:2], dtype=np.float16)
                    policy = np.frombuffer(response[2:], dtype=np.float16)
                # time.sleep(0.5)
        socket.close()
        context.term()
Ejemplo n.º 3
0
            if players[turn]['root']:
                print('starting with', players[turn]['root'].number_visits,
                      'visits')
        start = time.time()
        if players[turn]['engine'] != default_engine:
            search.engines[default_engine](board, args.nodes, net=nn)
        best, node = search.engines[players[turn]['engine']](
            board, args.nodes, net=nn, root=players[turn]['root'])
        print(board.pc_board.fullmove_number, players[turn]['engine'],
              "best: ", best)
        elapsed = time.time() - start
        if args.verbosity:
            print("Time: {:.3f} nps".format(args.nodes / elapsed))
        players[turn]['root'] = node

    board.push_uci(best)
    if players[1 - turn]['root'] and best in players[1 -
                                                     turn]['root'].children:
        players[1 - turn]['root'] = players[1 - turn]['root'].children[best]
    else:
        if args.verbosity:
            print('tree reset for player', 1 - turn,
                  players[1 - turn]['engine'])
        players[1 - turn]['resets'] += 1
        players[1 - turn]['root'] = None

    if board.pc_board.is_game_over() or board.is_draw():
        print("Game over... result is {}".format(
            board.pc_board.result(claim_draw=True)))
        print(board)
        print(chess.pgn.Game.from_board(board.pc_board))
Ejemplo n.º 4
0
    nn = search.NeuralNet(net=net, lru_size=min(5000, nodes))


load_leela_network()

SELFPLAY = True

board = LeelaBoard()
while True:
    if not SELFPLAY:
        print(board.unicode())
        print("Enter move: ", end='')
        sys.stdout.flush()
        line = sys.stdin.readline()
        line = line.rstrip()
        board.push_uci(line)
    print(board.unicode())
    print("thinking...")
    start = time.time()
    best, node = search.UCT_search(board, nodes, net=nn, C=3.4)
    elapsed = time.time() - start
    print("best: ", best)
    print("Time: {:.3f} nps".format(nodes / elapsed))
    print(nn.evaluate.cache_info())
    board.push_uci(best)
    if board.pc_board.is_game_over() or board.is_draw():
        result = board.pc_board.result(claim_draw=True)
        print("Game over... result is {}".format(result))
        print(board.unicode())
        print()
        pgn_game = chess.pgn.Game.from_board(board.pc_board)