def addChildren(self, moves, model): gameState = af.fenToTensor(self.state) inputState = np.expand_dims(gameState, axis=0) policy, value = model.predict(inputState) # Extract move with the highest value: find legit moves, max index and lookup in the dict moves = [str(x) for x in list(game.board.legal_moves)] kala = {move: policy[0][enumDict[move].value] for move in moves} move = max(kala, key=kala.get) objects = [ MctsNode(move, value) for move, value in zip(moves, self.calc_policy(moves)) ] self.children = self.children + objects
def move(self, game, enumDict): # Create legitimate moves and pick one if self.model is None: moves = list(game.legal_moves) move = random.choice(moves) self.moves.append(move) value = np.nan return move, value else: gameState = game.fen() gameState = af.fenToTensor(gameState) inputState = np.expand_dims(gameState, axis=0) policy, value = self.model.predict(inputState) # Move with the highest value: find legit moves, max index and lookup in the dict moves = [str(x) for x in list(game.legal_moves)] kala = {move: policy[0][enumDict[move].value] for move in moves} move = max(kala, key=kala.get) self.moves.append(move) return move, value
a7 = Node(7, 71, a4) def visitor(node): if node.parent: kala = node.parent print(kala.value) kala.value = kala.value + node.value return(visitor(kala)) else: return 1 visitor(a7) # for i in range(20): import anxilliaryfunctions as af import numpy as np fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1' a = af.fenToTensor(fen) sum_pieces = np.sum(a, axis=(1, 2)) # {'P':1,'N':2,'B':3,'R':4,'Q':5,'K':6,'p':7,'n':8,'b':9,'r':10,'q':11,'k':12} piece_values = [1, 3, 3.5, 5, 9, 10, -1, -3, -3.5, -5, -9, -10] total_value = sum_pieces * piece_values bala = sum(total_value)
def nextMove(): pass # game = chess.Board() # state = game.board_fen() # node = MctsNode(state, 0) i = 0 while i < 10: i += 1 node.update() gameState = game.board.fen() gameState = af.fenToTensor(gameState) inputState = np.expand_dims(gameState, axis=0) policy, value = model.predict(inputState) # moves = [str(x) for x in list(game.board.legal_moves)] if not node.children: possible_moves = [str(x) for x in list(game.board.legal_moves)] node.addChildren(possible_moves, model) # node = MctsNode(state, 0) # best = node.pickBest() # makemove with chess ba = node.children
files = glob.glob( 'C:\\Users\\Watson\\Projects\\remus\\input\\final_input\\*.pkl') # Convert and merge all file in the numpy arrays stateList = [] moveList = [] resultList = [] for file in files: data = pd.read_pickle(file) # Extract and save states tqdm.pandas(mininterval=10) data['TensorState'] = data['Fen'].progress_apply( lambda x: af.fenToTensor(x)) inputStates = data['TensorState'].values inputStates = np.array([x for x in inputStates]) # Extract and save result and best move for a game target1 = data['Result'].values.astype(np.int8) target2 = af.convertMoves(data[['Move']]) target2 = np.array([x for x in target2]) # Save results stateList = stateList.append(inputStates) moveList = moveList.append(target1) resultList = resultList.append(target2) stateArray = np.array(stateList)