def getmark(board: Chessboard, isFirst: bool): "评估函数" Value, Belong = np.array(board.getRaw(), ).transpose((2, 0, 1)) Belong ^= isFirst Ours = Belong << Value Theirs = (1 - Belong) << Value return np.sum((Ours * Weight[isFirst] - Theirs * Weight[not isFirst]) * (1 + 0.02 * Value))
''' Board = Chessboard(tuple(randrange(720720) for i in range(500))) print(Board) for Gamemode in range(4): t_start = time() result = simulation(0,0,Gamemode,True,Board,is_root=True) print(f't{Gamemode+1} = {time()-t_start}') print(f'result = {result}') if Gamemode >= 2: Board.move(1^(Gamemode%2),result) else: Board.add(1^(Gamemode%2),result) print(Board)''' Board = Chessboard(tuple(randrange(720720) for i in range(500))) from Source import Chessman ARRAY = [ [0,3,2,4,2,-4,-2,-11], [4,5,3,-2,-3,-1,-6,-3], [2,4,1,-7,-6,8,-1,-2], [1,3,5,-2,-3,-5,-4,0] ] for i in range(4): for j in range(8): value = ARRAY[i][j] if value!=0: Board.board[(i,j)] = Chessman(value>0,(i,j),abs(value)) print(Board) print(simulation(0,193,0,True,Board,is_root=True))
def getmark(board, isFirst): '评估函数' #part1:棋子直接估值部分 #获得两方棋子数值的从大到小的列表 ours = board.getScore(isFirst) theirs = board.getScore(not isFirst) zeros = 32 - len(ours) - len(theirs) result = zeros * (1 << 70) for chess in ours: result += 1 << ((14 + chess) * 5) for chess in theirs: result += 1 << ((14 - chess) * 5) return result if __name__ == "__main__": from Source import Chessboard c = Chessboard(tuple(randrange(720720) for i in range(500))) c.add(True, (0, 1), 1) c.add(True, (0, 3), 2) c.add(True, (0, 2)) c.add(False, (0, 4)) print(c) print(getmark(c, 1)) from time import time t1 = time() for _ in range(100000): getmark(c, 1) t2 = time() print(t2 - t1)
def MCTS(root: Node, Gamemode: int, Round: int, explore_time: int = 100, rollertime=20): '探索主函数' for _ in range(explore_time): #探索多次 node = Selection(root) #选节点 #到达底层,直接返回 if node.Round == 499: dn = 1 Q = getmark(node.board, 1 - Gamemode % 2) else: #进行模拟对局 Q = 0 dn = rollertime for _ in range(rollertime): Q += Rollout(Round, node.board.copy(), (node.Gamemode + 1) % 4, 1 - root.Gamemode % 2) Backpropagation(node, dn, Q) return root.Get_operation() if __name__ == "__main__": from Source import Chessboard from random import randrange Board = Chessboard(tuple(randrange(720720) for i in range(50))) node = Node(0, 0, Board) print(MCTS(node, 0, 0, 1))
if self.alpha >= self.beta: break if is_root and not No_available: return result #无合法移动的特殊情况 if No_available: child = Node( self.Round + (1^self.isFirst), #局数后手+1 (self.Gamemode+1)%4, #模式+1后对4取余 not self.is_max, #敌我反转 self.board, #新的棋盘 self.alpha, #alpha保留 self.beta #beta保留 ) child.simulation(depth+(1^self.isFirst)) self.alpha = child.alpha self.beta = child.beta if __name__ == "__main__": from random import randrange Board = Chessboard(tuple(randrange(720720) for i in range(500))) Board.add(True,Board.getNext(True,0)) Board.add(False,Board.getNext(False,0)) Board.move(True,1) print(Board) node = Node(0,3,True,Board) print(node.simulation(0,True))
if No_way: self.root = self.root.Nextchild(0) return None else: self.root, result = MCTS(self.root, mode * 2 + 1 - self.isFirst, currentRound) return result if __name__ == "__main__": from Source import Chessboard from random import randrange Array = tuple(randrange(720720) for i in range(500)) player1 = Player(True, Array) player2 = Player(False, Array) board = Chessboard(Array) for r in range(10): operation = player1.output(r, board, 'position') print(operation) board.add(True, operation) operation = player2.output(r, board, 'position') print(operation) board.add(True, operation) operation = player1.output(r, board, 'direction') print(operation) board.move(True, operation) operation = player2.output(r, board, 'direction')