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) 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)] = MCTSEPT2Node(state=str(original_state.fen()), 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
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)] = MCTSEPT2Node( state=str(self.starting_board_state.fen()), 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) # 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)]
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)] = MCTSEPT2Node( state=str(self.starting_board_state.fen()), 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) # 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.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)]) mate_info = self.engine.analyse(self.starting_board_state, chess.engine.Limit(time=0.01)) 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 except: print(mate_info["score"].white().__str__()) else: if mate_info["score"].black().__str__()[1] == '+': self.terminal_depth = 0 self.lock_depth = True # print(mate_info["score"].white().__str__()) # print(self.terminal_depth) return globals()[str(self.starting_board_state.fen()) + str(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]) 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)] = MCTSEPT2Node( state=str(original_state.fen()), 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