コード例 #1
0
    def algo_init(self):
        # add root node
        # globals() method returns the dictionary of the current global symbol table
        # used in this scenario to assign node to unique state key
        globals()[str(self.starting_board_state.fen()) +
                  str(0)] = MCTSEPT3Node(
                      state=str(self.starting_board_state.fen()),
                      eval=0,
                      key=str(self.starting_board_state.fen()) + str(0))

        # check komodo polyglot opening book if move exists
        # with chess.polyglot.open_reader("./OpeningBook/komodo.bin") as reader:
        #     opening_moves = []
        #     for entry in reader.find_all(self.starting_board_state):
        #         opening_moves.append(entry.move)
        #         break
        #         # print(entry.move, entry.weight, entry.learn)
        # if opening_moves:
        #     self.opening_expansion(
        #         globals()[str(self.starting_board_state.fen())+str(0)], opening_moves)
        # else:
        #     # generate legal moves for starting state
        #     # self.ordered_expansion(
        #     #     globals()[str(self.starting_board_state.fen())+str(0)], 8)
        #     self.run_expansion(
        #         globals()[str(self.starting_board_state.fen())+str(0)])

        # generate legal moves for starting state
        self.run_expansion(globals()[str(self.starting_board_state.fen()) +
                                     str(0)])
        # self.ordered_expansion(
        #     globals()[str(self.starting_board_state.fen())+str(0)], 8)

        # lock depth to follow mate score
        # mate_info = self.engine.analyse(
        #     self.starting_board_state, chess.engine.Limit(depth=3))
        # if self.original_player:
        #     # if mate_info["score"].white().__str__()[1] == '+':
        #     #     self.terminal_depth = 0
        #     #     self.lock_depth = True
        #     try:
        #         if mate_info["score"].white().__str__()[1] == '+':
        #             self.terminal_depth = 0
        #             self.lock_depth = True
        #         # lock depth when high advantage, extra moves wash out meaning
        #         # elif int(mate_info["score"].white().__str__()) > 2000 and int(mate_info["score"].white().__str__()) > 6382:
        #         #     self.terminal_depth = 0
        #         #     self.lock_depth = True
        #     except:
        #         print(mate_info["score"].white().__str__())
        # else:
        #     if mate_info["score"].black().__str__()[1] == '+':
        #         self.terminal_depth = 0
        #         self.lock_depth = True
        #     # elif int(mate_info["score"].black().__str__()) > 2000 and int(mate_info["score"].black().__str__()) > 6382:
        #     #     self.terminal_depth = 0
        #     #     self.lock_depth = True

        return globals()[str(self.starting_board_state.fen()) + str(0)]
コード例 #2
0
    def run_expansion(self, node):
        board_state = chess.Board(node.state)

        move_list = list(board_state.legal_moves)
        for move in move_list:  # add all legal move nodes to tree
            original_state = board_state.copy()
            # original_state.push_san(move)
            original_state.push(move)
            stat_eval = StaticEval.evaluate_board(original_state)
            if self.original_player:  # if player is white
                if node.depth % 2 == 0:  # expand children of even depth node in white eval
                    evaluation = 1 / (1 + (10 ** -(stat_eval / 400)))
                else:
                    evaluation = 1 / (1 + (10 ** -((-stat_eval) / 400)))
            else:  # if player is black
                if node.depth % 2 == 0:  # expand children of even depth node in black eval
                    evaluation = 1 / (1 + (10 ** -(-stat_eval / 400)))
                else:
                    evaluation = 1 / (1 + (10 ** -((stat_eval) / 400)))
            i = 0
            while(1):
                try:
                    # check if node object exists in global variables
                    globals()[str(original_state.fen())+str(i)]
                except:
                    # create node if does not exist
                    globals()[str(original_state.fen())+str(i)] = MCTSEPT3Node(state=str(original_state.fen()), eval=evaluation, key=str(
                        original_state.fen())+str(i), parent=globals()[node.key], weight=board_state.san(move))

                    # For case where node is expanded and new node is terminal node
                    if original_state.is_game_over():  # check if it is a terminal node
                        globals()[str(original_state.fen())+str(i)
                                  ].termnode = True  # set as terminal node
                        # set terminal result to false if draw or loss
                        globals()[str(original_state.fen()) +
                                  str(i)].termresult = 0.0
                        if original_state.is_checkmate():  # assign winner only if checkmate
                            if original_state.turn == self.original_player:  # original player lost
                                if node.depth % 2 == 0:  # expanded children is on original side
                                    globals()[str(original_state.fen()) +
                                              str(i)].termresult = 0.0
                                else:  # expanded children is on opponent side
                                    globals()[str(original_state.fen()) +
                                              str(i)].termresult = 1.0
                            else:  # original player won
                                if node.depth % 2 == 0:  # expanded children is on original side
                                    globals()[str(original_state.fen()) +
                                              str(i)].termresult = 1.0
                                else:  # expanded children is on opponent side
                                    globals()[str(original_state.fen()) +
                                              str(i)].termresult = 0.0

                    break
                else:
                    i += 1  # increment index if exists
コード例 #3
0
    def ordered_expansion(self, node, count):
        board_state = chess.Board(node.state)

        # order legal moves with stockfish eval
        ordered_list = self.move_ordering(board_state, node.depth)
        for move in ordered_list[:count]:  # add top 5 legal move nodes to tree
            # for move in ordered_list:
            original_state = board_state.copy()
            # original_state.push_san(move)
            original_state.push(move[0])
            stat_eval = StaticEval.evaluate_board(original_state)
            if self.original_player:
                evaluation = 1 / (1 + (10**-(stat_eval / 400)))
            else:
                evaluation = 1 / (1 + (10**-((-stat_eval) / 400)))
            i = 0
            # print(type(move[0]))
            while (1):
                try:
                    # check if node object exists in global variables
                    globals()[str(original_state.fen()) + str(i)]
                except:
                    # create node if does not exist
                    globals()[str(original_state.fen()) +
                              str(i)] = MCTSEPT3Node(
                                  state=str(original_state.fen()),
                                  eval=evaluation,
                                  key=str(original_state.fen()) + str(i),
                                  parent=globals()[node.key],
                                  weight=board_state.san(move[0]))

                    # For case where node is expanded and new node is terminal node
                    if original_state.is_game_over(
                    ):  # check if it is a terminal node
                        globals()[
                            str(original_state.fen()) +
                            str(i)].termnode = True  # set as terminal node
                        # set terminal result to false if draw or loss
                        globals()[str(original_state.fen()) +
                                  str(i)].termresult = 0.0
                        if original_state.is_checkmate(
                        ):  # assign winner only if checkmate
                            if original_state.turn == self.original_player:
                                globals()[str(original_state.fen()) +
                                          str(i)].termresult = 0.0
                            else:
                                globals()[str(original_state.fen()) +
                                          str(i)].termresult = 1.0

                    break
                else:
                    i += 1  # increment index if exists
コード例 #4
0
    def opening_expansion(self, node, opening_moves):
        board_state = chess.Board(node.state)
        for move in opening_moves:  # add all legal move nodes to tree
            original_state = board_state.copy()
            # original_state.push_san(move)
            original_state.push(move)
            stat_eval = StaticEval.evaluate_board(original_state)
            # stat_eval = self.engine.analyse(board_state, chess.engine.Limit(depth=3))
            if self.original_player:
                evaluation = 1 / (1 + (10**-(stat_eval / 400)))
            else:
                evaluation = 1 / (1 + (10**-((-stat_eval) / 400)))
            i = 0
            while (1):
                try:
                    # check if node object exists in global variables
                    globals()[str(original_state.fen()) + str(i)]
                except:
                    # create node if does not exist
                    globals()[str(original_state.fen()) +
                              str(i)] = MCTSEPT3Node(
                                  state=str(original_state.fen()),
                                  eval=evaluation,
                                  key=str(original_state.fen()) + str(i),
                                  parent=globals()[node.key],
                                  weight=board_state.san(move))

                    # For case where node is expanded and new node is terminal node
                    if original_state.is_game_over(
                    ):  # check if it is a terminal node
                        globals()[
                            str(original_state.fen()) +
                            str(i)].termnode = True  # set as terminal node
                        # set terminal result to false if draw or loss
                        globals()[str(original_state.fen()) +
                                  str(i)].termresult = 0.0
                        if original_state.is_checkmate(
                        ):  # assign winner only if checkmate
                            if original_state.turn == self.original_player:
                                globals()[str(original_state.fen()) +
                                          str(i)].termresult = 0.0
                            else:
                                globals()[str(original_state.fen()) +
                                          str(i)].termresult = 1.0

                    break
                else:
                    i += 1  # increment index if exists