def __init__(self, parent, move, turn, game: Core = None): self.parent = parent self.expanded = False self.move = move self.turn = turn if game: self.game = game game.AddJudge() else: bitMove = 1 << int(move) self.parent.game.AddSite(bitMove) self.parent.game.NextBoard() self.game = Core(turn, self.parent.game.black, self.parent.game.white) self.game.AddJudge() self.legalMoves = BitArray(self.game.judge) self.childNodes = {} self.isGameRoot = False self.isSearchRoot = False self.isTerminal = False self.pi = np.zeros([64], dtype=float) self.edgeN = np.zeros([64], dtype=float) self.edgeW = np.zeros([64], dtype=float) self.edgeP = np.zeros([64], dtype=float)
def __init__(self): self.start = time.time() super().__init__(0, 0x0000000810000000, 0x0000001008000000) self.model = Model() #self.model.load('model/Gen' + str(0)) self.score = 0 self.table = {} self.currentNode = mcts.Node(mcts.FakeNode(), 0, 0, Core(self.turn, self.black, self.white)) self.currentNode.isGameRoot = True self.currentNode.isSearchRoot = True self.mctsBatch = mcts.MCTSBatch(self.model,NUM_MCTS)
def InputEnemy(self): nb, nw = self.Count() nmTurn = 64 - nb - nw if nmTurn < 16: site, socre, self.table = moveAI(self.black,self.white,self.turn, nmTurn, self.table, self.score) self.score = socre self.AddSite(site) else: # site, score, self.table = moveAINN(self.black,self.white,self.turn, 3, self.table, self.score,self.model) # self.score = score # self.AddSite(site) self.currentNode = mcts.Node(mcts.FakeNode(), 0, self.turn, Core(self.turn, self.black, self.white)) self.currentNode.isGameRoot = True self.currentNode.isSearchRoot = True pi = self.mctsBatch.alpha([self.currentNode], 1)[0] print(pi) self.currentNode = self.makeMove(self.currentNode,int(np.argmax(pi)))
def InputPlayer(self): nb, nw = self.Count() nmTurn = 64 - nb - nw if nmTurn < RAND: self.currentNode = mcts.Node( mcts.FakeNode(), 0, self.turn, Core(self.turn, self.black, self.white)) self.currentNode.isGameRoot = True self.currentNode.isSearchRoot = True pi = self.mctsBatch.alpha([self.currentNode], 1)[0] move = int(np.argmax(pi)) self.addHistory(pi, self.turn) self.currentNode = self.makeMove(self.currentNode, move) else: while True: move = random.randrange(0, 64) site = 1 << move if self.judge & site: # self.AddSite(site) self.currentNode = self.makeMove(self.currentNode, move) break
def InputEnemy(self): nb, nw = self.Count() nmTurn = 64 - nb - nw if nmTurn < 16: site, maxScore, sumScore, self.table = moveAITrain( self.black, self.white, self.turn, nmTurn, self.table, nw - nb) if maxScore > 0: maxScore = 1 elif maxScore < 0: maxScore = -1 site.sort(reverse=True) pi = np.zeros(64) for move in site: pi[bitFind(move[1])] = move[0] / (sumScore if sumScore else 1) self.addHistory(pi, self.turn) temp = copy.deepcopy(self.history) self.addValue(self.history, maxScore) for i in range(len(site) // 4 + 1): history2 = copy.deepcopy(temp) self.AddSite(site[i][1]) self.NextBoard() site2, maxScore2, sumScore2, self.table = moveAITrain( self.black, self.white, self.turn ^ 1, nmTurn - 1, self.table, nw - nb) self.NextBoard() maxScore2 = -maxScore2 if maxScore2 > 0: maxScore2 = 1 elif maxScore2 < 0: maxScore2 = -1 if site2: site2.sort(reverse=True) pi2 = np.zeros([64]) for move2 in site2: pi2[bitFind(move2[1])] = move2[0] / (sumScore2 if sumScore2 else 1) features = [] features.append(self.black) features.append(self.white) features.append(self.judge) history2.append([features, pi2, self.turn ^ 1]) self.addValue(history2, maxScore2) self.history.extend(history2) self.AddSite(0) elif nmTurn < RAND: self.currentNode = mcts.Node( mcts.FakeNode(), 0, self.turn, Core(self.turn, self.black, self.white)) self.currentNode.isGameRoot = True self.currentNode.isSearchRoot = True pi = self.mctsBatch.alpha([self.currentNode], 1)[0] move = int(np.argmax(pi)) self.addHistory(pi, self.turn) self.currentNode = self.makeMove(self.currentNode, move) else: while True: move = random.randrange(0, 64) site = 1 << move if self.judge & site: # self.AddSite(site) self.currentNode = self.makeMove(self.currentNode, move) break
class Node: def __init__(self, parent, move, turn, game: Core = None): self.parent = parent self.expanded = False self.move = move self.turn = turn if game: self.game = game game.AddJudge() else: bitMove = 1 << int(move) self.parent.game.AddSite(bitMove) self.parent.game.NextBoard() self.game = Core(turn, self.parent.game.black, self.parent.game.white) self.game.AddJudge() self.legalMoves = BitArray(self.game.judge) self.childNodes = {} self.isGameRoot = False self.isSearchRoot = False self.isTerminal = False self.pi = np.zeros([64], dtype=float) self.edgeN = np.zeros([64], dtype=float) self.edgeW = np.zeros([64], dtype=float) self.edgeP = np.zeros([64], dtype=float) @property def edgeQ(self): return self.edgeW / (self.edgeN + (self.edgeN == 0)) @property def edgeU(self): return C_PUCT * self.edgeP * math.sqrt(max( 1, self.selfN)) / (1 + self.edgeN) @property def edgeUNoise(self): noise = NormalizeMask(np.random.dirichlet([NOISE_ALPHA] * 64), self.legalMoves) pWithNoise = self.edgeP * (1 - NOISE_WEIGHT) + noise * NOISE_WEIGHT return C_PUCT * pWithNoise * math.sqrt(max( 1, self.selfN)) / (1 + self.edgeN) @property def edgeQU(self): if self.isSearchRoot: return self.edgeQ * self.turn + self.edgeUNoise + self.legalMoves * 1000 else: return self.edgeQ * self.turn + self.edgeU + self.legalMoves * 1000 @property def selfN(self): return self.parent.edgeN[self.move] @selfN.setter def selfN(self, n): self.parent.edgeN[self.move] = n @property def selfW(self): return self.parent.edgeW[self.move] @selfW.setter def selfW(self, w): self.parent.edgeW[self.move] = w def toFeatures(self): features = np.zeros([3, 64], dtype=float) turn = self.turn if turn: features[0] = BitArray(self.game.white) features[1] = BitArray(self.game.black) else: features[0] = BitArray(self.game.black) features[1] = BitArray(self.game.white) features[2] = self.legalMoves return features