示例#1
0
文件: test_utils.py 项目: lygztq/MuGo
 def test_flatten(self):
     self.assertEqual(utils.flatten_coords((0, 0)), 0)
     self.assertEqual(utils.flatten_coords((0, 3)), 3)
     self.assertEqual(utils.flatten_coords((3, 0)), 27)
     self.assertEqual(utils.unflatten_coords(27), (3, 0))
     self.assertEqual(utils.unflatten_coords(10), (1, 1))
     self.assertEqual(utils.unflatten_coords(80), (8, 8))
     self.assertEqual(utils.flatten_coords(utils.unflatten_coords(10)), 10)
     self.assertEqual(utils.unflatten_coords(utils.flatten_coords((5, 4))), (5, 4))
示例#2
0
 def test_flatten(self):
     self.assertEqual(utils.flatten_coords((0, 0)), 0)
     self.assertEqual(utils.flatten_coords((0, 3)), 3)
     self.assertEqual(utils.flatten_coords((3, 0)), 27)
     self.assertEqual(utils.unflatten_coords(27), (3, 0))
     self.assertEqual(utils.unflatten_coords(10), (1, 1))
     self.assertEqual(utils.unflatten_coords(80), (8, 8))
     self.assertEqual(utils.flatten_coords(utils.unflatten_coords(10)), 10)
     self.assertEqual(utils.unflatten_coords(utils.flatten_coords((5, 4))), (5, 4))
示例#3
0
def make_onehot(coords):
    num_positions = len(coords)
    output = np.zeros([num_positions, go.N**2], dtype=np.uint8)
    for i, coord in enumerate(coords):
        if coord:
            output[i, utils.flatten_coords(coord)] = 1
        else:
            continue
    return output
示例#4
0
def make_onehot(coords):  #onehot则是顾名思义,一个长度为n的数组,蜂窝煤矩阵,只有一个元素是1,其他元素是0
    print("生成坐标棋谱图")
    num_positions = len(coords)                 #有多少步?

    output = np.zeros([num_positions, go.N ** 2], dtype=np.uint8) #返回给定形状和类型的矩阵,用0填充。uint8是专门用于存储各种图像的 现在是生成多少步,第步有361个点
    #print(output)
    for i, coord in enumerate(coords):                #遍历矩阵
        output[i, utils.flatten_coords(coord)] = 1    #放置坐标 将每一步落子转换成一位数组  flatten即降维 Flatten层用来将输入“压平”,即把多维的输入一维化,常用在从卷积层到全连接层的过渡
                                                      #将第i手,坐标置1
        #display(i,output[i].reshape(go.N,go.N))           #升维成棋盘显示
    return output
示例#5
0
 def suggest_move(self, board, caps):
     if caps:
         for m in caps:
             self.ills[m] = 0
     if board.n > 30:
         self.t = 0.05
     if board.n > 0:
         self.root = self.get_node(board)
     if not self.root:
         move_probs, win_rate = self.policy_network.run(board)
         self.root = MCTSNode.root_node(board, move_probs, self.ills)
     else:
         win_rate = self.root.Q
         print('使用上次的node')
     start = time.time()
     ts = self.select_child(start)
     if random.randint(0, 1000) > 997:
         print('第%d手,搜索了%d次' % (board.n, ts))
     #self.printmsg("Searched for %s seconds" % (time.time() - start))
     #sorted_moves = sorted(self.root.children.keys(), key=lambda move, rt=self.root: rt.children[move].N, reverse=True)
     self.would_pass = 0
     winnode = None
     pi = np.zeros(go.N * go.N + 1, dtype=np.float32)
     if self.can_pass and self.root.children:
         if board.n > 100 and board.recent[-1].move == go.PASS:
             self.would_pass += 2
         if (win_rate + 1) / 2 < config.vresign or (win_rate + 1) / 2 > (
                 1 - config.vresign):
             self.would_pass += 2
         winnode = max(self.root.children.values(), key=lambda x: x.Q)
         if (winnode.Q * board.to_play + 1) / 2 < config.vresign or (
                 winnode.Q * board.to_play + 1) / 2 > (1 - config.vresign):
             self.would_pass += 2
         self.would_pass += board.n // 100
     if not self.root.children or (self.can_pass and self.would_pass > 5):
         pi[go.N * go.N] = 1
         return go.PASS, pi, winnode.Q if winnode else 0
     alln = reduce(lambda x, y: x + y.N, self.root.children.values(), 0)
     if alln == 0:
         alln = 1
     for m, node in self.root.children.items():
         ind = utils.flatten_coords(m)
         pi[ind] = node.N**(1 / self.t) / alln**(1 / self.t)
     maxnode = max(self.root.children.values(),
                   key=lambda x: x.N**(1 / self.t) / alln**(1 / self.t))
     #if self.root.Q<self.vresign and select_node.Q<self.vresign:
     #    return go.PASS, 0, 0
     return maxnode.move, pi, maxnode.Q  #黑棋胜率
示例#6
0
def make_onehot(coords):
    num_positions = len(coords)
    output = np.zeros([num_positions, go.N ** 2], dtype=np.uint8)
    for i, coord in enumerate(coords):
        output[i, utils.flatten_coords(coord)] = 1
    return output
示例#7
0
def make_onehot(coord):
    output = np.zeros([N**2], dtype=np.uint8)
    output[utils.flatten_coords(coord)] = 1
    return output