Esempio n. 1
0
    def _read_thor_file(self, file_name):
        file_header_size = 16
        record_header_size = 8
        shots = 60
        record_size = 68

        games = []
        with open(file_name, "rb") as f:
            c = f.read()
            board_size = _byte_to_int(c[12])
            if board_size == 8 or board_size == 0:
                for i in xrange(file_header_size, len(c), record_size):
                    moves = []
                    b = Board()
                    player = Board.BLACK
                    black_score = _byte_to_int(c[i + 6])
                    for j in xrange(record_header_size, record_size):
                        play = _byte_to_int(c[i + j])
                        if play > 0:
                            column = (play % 10) - 1
                            row = (play // 10) - 1
                            if not b.is_feasible(row, column, player):
                                player = Board.opponent(player)
                            moves.append((player, row, column))
                            b.flip(row, column, player)
                            player = Board.opponent(player)

                    score = b.score(Board.BLACK)
                    if b.score(Board.BLACK) > b.score(Board.WHITE):
                        score += b.score(Board.BLANK)
                    if score == black_score:
                        games.append((moves, black_score * 2 - 64))
                    else:
                        self.inconsistencies += 1
        return games
Esempio n. 2
0
def self_play(n, model):
    b = Board()
    for t in xrange(1, n+1):
        b.init_board()
        p = Board.BLACK

        while not b.is_terminal_state():
            options = b.feasible_pos(p)
            vals = []

            if len(options) > 0:
                for i,j in options:
                    with b.flip2(i, j, p):
                        if b.is_terminal_state():
                            vals.append(b.score(Board.BLACK) - b.score(Board.WHITE))
                        else:
                            vals.append(model(b))
                (a0, a1), v = epsilon_greedy(0.07, options, vals, p == Board.BLACK)
                model.update(b, v)
                b.flip(a0, a1, p)

            p = Board.opponent(p)

        if t % 100 == 0:
            logging.info("Number of games played: {}".format(t))
            logging.info(b.cache_status())

        if t % 1000 == 0:
            model.save("./model/model.cpt")

    model.save("./model/model.cpt")
Esempio n. 3
0
def validate(db):
    import random
    for moves, result in db.games:
        if random.random() < 0.9:
            continue
        b = Board()
        for p, r, c in moves:
            assert b.is_feasible(r, c, p)
            b.flip(r, c, p)
        black_score = b.score(Board.BLACK)
        white_score = b.score(Board.WHITE)
        score = black_score
        if b.score(Board.BLACK) > b.score(Board.WHITE):
            score = b.score(Board.BLANK) + black_score
        assert result in (black_score - white_score, 2 * score - 64)