Пример #1
0
class MCTSTrainer:
    def __init__(self, gnn, test_graphs, filename):
        self.mcts = MCTS(gnn)
        self.test_graphs = test_graphs
        self.test_result = []
        self.filename = filename

    # rollout until the end
    def train1(self, graph, TAU, batch_size=10, iter_p=2):
        self.mcts.train(graph, TAU, batch_size=batch_size, iter_p=iter_p)

    # rollout only until leaf
    def train2(self, graph, TAU, batch_size=10, iter_p=2):
        self.mcts.train(graph,
                        TAU,
                        batch_size=batch_size,
                        stop_at_leaf=True,
                        iter_p=2)

    def test(self):
        result = [self.mcts.search(graph) for graph in self.test_graphs]
        print(result)
        self.test_result.append(result)

    def save_test_result(self):
        os.makedirs("log", exist_ok=True)
        with open("log/{}.pickle".format(self.filename), mode="wb") as f:
            pickle.dump(self.test_result, f)

    def save_model(self):
        os.makedirs("model", exist_ok=True)
        torch.save(self.mcts.gnn.state_dict(),
                   "model/{}.pth".format(self.filename))
Пример #2
0
    timeout = int(args.timeout) - 5

    c1 = Client(args.ip, PORTS[player_arg], player_arg)
    c1.send_name(PLAYER_NAMES[player_arg])

    board = Board()
    game = Game(board)
    # Main game loop
    try:
        while not game.ended:
            state = None
            while state is None:
                state, turn = c1.receive_state()
                game.board.board = state
                if turn not in ["black", "white"]:
                    raise GameEndedException
                game.turn = TURN_MAPPING[turn]
                print(state, turn)
            if game.turn == OUR_PLAYER:
                mcts = MCTS(deepcopy(game),
                            OUR_PLAYER,
                            max_depth=max_depth,
                            C=C)
                start, end = mcts.search(timeout)
                print(start, end)
                c1.send_move(start, end)
    except GameEndedException:
        print("Game ended with state {}".format(turn))

    c1.close()