示例#1
0
class Engine():
    def __init__(self, time=5, policy=None):
        self.player = MCTSPlayer(time, policy)

    def set_komi(self, komi):
        go.KOMI = komi

    def set_size(self, size):
        go.init(size)

    def clear(self):
        self.player.clear()

    def debug(self, info=''):
        print(self.player.debug_info + info)

    def move(self, color, vertex=None):
        legal = True

        if vertex is None:
            vertex = self.player.move()
            legal = vertex is not None
        else:
            legal = go.move(vertex)

        if legal:
            take = go.get_take(go.POSITION)
            return go.toJI(go.POSITION.vertex), {go.toJI(v) for v in take}
        else:
            return None, {}

    def get_score(self):
        return go.POSITION.result()

    def save(self):
        return go.POSITION.toJSON()

    def load(self, str):
        go.POSITION.fromJSON(str)
示例#2
0
def main(count, path):
    sys.setrecursionlimit(500000)
    go.init(9)
    playerBlack = MCTSPlayer(90, resnet.Policy(40,
                                               '../data/r1/resnet_pars.pkl'))
    playerWhite = MCTSPlayer(30, randmove.Policy(40))

    records = [
        f for f in listdir(path) if f[-4:] == 'json' and f[:6] == 'record'
    ]
    fcount = len(records)

    vertex = None
    caps = None

    black_win = 0
    white_win = 0
    draw = 0

    c = 1
    while c <= count:
        records = '[\n'
        i = 0

        print('ready: %d in %d, POSPOOL: %d' %
              (c, count, len(go.POSITION_POOL)))

        while go.POSITION.pass_count() < 2:
            i += 1

            legal = True
            if i % 2 == 1:
                legal = playerBlack.move()
            else:
                legal = playerWhite.move()

            if not legal:
                print('Illegal move!')
                break

            go.POSITION.debug()

            record = go.POSITION.toJSON()
            records += '  '
            records += record

            if go.POSITION.pass_count() < 2:
                records += ',\n'
            else:
                records += '\n]'

        score = go.POSITION.score() - go.KOMI
        playerBlack.clear()
        playerWhite.clear()
        go.clear()

        fcount += 1
        filename = path + 'record_%d.json' % fcount

        f = open(filename, 'w')
        f.write(records)
        f.close()

        if score > 0:
            black_win += 1
        elif score < 0:
            white_win += 1
        else:
            draw += 1

        c += 1

    print('back win: %d, white win: %d, draw: %d' %
          (black_win, white_win, draw))