Example #1
0
def replay_sgf(sgf_contents):
    '''
    Wrapper for sgf files, exposing contents as position_w_context instances
    with open(filename) as f:
        for position_w_context in replay_sgf(f.read()):
            print(position_w_context.position)
    '''
    collection = sgf.parse(sgf_contents)
    game = collection.children[0]
    props = game.root.properties
    assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!"

    komi = 0
    if props.get('KM') != None:
        komi = float(sgf_prop(props.get('KM')))
    metadata = GameMetadata(
        result=sgf_prop(props.get('RE')),
        handicap=int(sgf_prop(props.get('HA', [0]))),
        board_size=int(sgf_prop(props.get('SZ'))))
    go.set_board_size(metadata.board_size)

    pos = Position(komi=komi)
    current_node = game.root
    while pos is not None and current_node is not None:
        pos = handle_node(pos, current_node)
        maybe_correct_next(pos, current_node.next)
        next_move = get_next_move(current_node)
        yield PositionWithContext(pos, next_move, metadata)
        current_node = current_node.next
 def test_onehot(self):
     go.set_board_size(9)
     coords = [(1, 2), (3, 4)]
     expected = np.zeros([2, 81], dtype=np.uint8)
     expected[0, 11] = 1
     expected[1, 31] = 1
     onehot = load_data_sets.make_onehot(coords)
     self.assertEqualNPArray(onehot, expected)
Example #3
0
 def test_onehot(self):
     go.set_board_size(9)
     coords = [(1, 2), (3, 4)]
     expected = np.zeros([2, 81], dtype=np.uint8)
     expected[0, 11] = 1
     expected[1, 31] = 1
     onehot = load_data_sets.make_onehot(coords)
     self.assertEqualNPArray(onehot, expected)
Example #4
0
def thread_play(queue, net=None, num=100, qp=None):
    config.read_cfg('config-b10f2')
    go.set_board_size(config.board_size)
    data = dataset.DataSet()
    name = mp.current_process().name
    for i in range(num):
        train_start = time.time()
        queue.put("{}--{:.0f}:开始下第{}盘棋……".format(name,
                                                 time.time() - train_start,
                                                 i + 1))
        train_start = time.time()
        board = play(None, data, queue, net, qp, "sgf")
        queue.put("{}--{:.0f}:第{}盘棋下完。{}".format(name,
                                                 time.time() - train_start,
                                                 i + 1,
                                                 go.result_str(board.result)))
    if data.data_size > 256:
        queue.put(name + "--保存训练数据……")
        data.save()
        queue.put(name + "--保存完毕!")
Example #5
0
def replay_position(position):
    '''
    Wrapper for a go.Position which replays its history.
    Assumes an empty start position! (i.e. no handicap, and history must be exhaustive.)

    for position_w_context in replay_position(position):
        print(position_w_context.position)
    '''
    assert position.n == len(position.recent), "Position history is incomplete"
    metadata = GameMetadata(
        result=position.result(),
        handicap=0,
        board_size=position.board.shape[0]
    )
    go.set_board_size(metadata.board_size)

    pos = Position(komi=position.komi)
    for player_move in position.recent:
        color, next_move = player_move
        yield PositionWithContext(pos, next_move, metadata)
        pos = pos.play_move(next_move, color=color)
    # return the original position, with unknown next move
    yield PositionWithContext(pos, None, metadata)
 def set_size(self, n):
     self.size = n
     go.set_board_size(n)
     self.clear()
Example #7
0
 def setUp(self):
     go.set_board_size(9)
Example #8
0
import numpy as np
import re
import time
import unittest

import go
import utils

go.set_board_size(9)


def load_board(string):
    reverse_map = {
        'X': go.BLACK,
        'O': go.WHITE,
        '.': go.EMPTY,
        '#': go.FILL,
        '*': go.KO,
        '?': go.UNKNOWN
    }

    string = re.sub(r'[^XO\.#]+', '', string)
    assert len(string) == go.N**2, "Board to load didn't have right dimensions"
    board = np.zeros([go.N, go.N], dtype=np.int8)
    for i, char in enumerate(string):
        np.ravel(board)[i] = reverse_map[char]
    return board


class TestUtils(unittest.TestCase):
    def test_parsing(self):
Example #9
0
 def setUp(self):
     go.set_board_size(9)
Example #10
0
import numpy as np
import random
import re
import time
import unittest

import go
import utils

go.set_board_size(9)

def load_board(string):
    reverse_map = {
        'X': go.BLACK,
        'O': go.WHITE,
        '.': go.EMPTY,
        '#': go.FILL,
        '*': go.KO,
        '?': go.UNKNOWN
    }

    string = re.sub(r'[^XO\.#]+', '', string)
    assert len(string) == go.N ** 2, "Board to load didn't have right dimensions"
    board = np.zeros([go.N, go.N], dtype=np.int8)
    for i, char in enumerate(string):
        np.ravel(board)[i] = reverse_map[char]
    return board

class TestUtils(unittest.TestCase):
    def test_parsing(self):
        self.assertEqual(utils.parse_sgf_coords('aa'), (0, 0))
Example #11
0
 def set_size(self, n):
     self.size = n
     go.set_board_size(n)
     self.clear()