예제 #1
0
 def calc_amp2(self, img, phase=0):
     # 対称形 x2 を考慮して確率計算
     amp_prob = self.sess.run(self.prob_op[phase],
                              feed_dict={self.x_placeholder: img})
     prob = np.zeros(go.IMAGE_SIZE, dtype=np.float32)
     for x in range(go.IMAGE_LENGTH):
         for y in range(go.IMAGE_LENGTH):
             pb = 0
             pb += amp_prob[0][go.XYtoZ(x, y)]
             pb += amp_prob[1][go.XYtoZ(go.IMAGE_LENGTH - 1 - x,
                                        go.IMAGE_LENGTH - 1 - y)]
             prob[go.XYtoZ(x, y)] = pb / 2
     return prob
예제 #2
0
 def __init__(self, l):
     super().__init__(l)
     self.blockturn = 0
     self.blockcell = np.zeros((go.IMAGE_SIZE), dtype=np.int32)
     self.blocks = np.zeros((2, BlockType.NUM), dtype=np.int32)
     self.z_star = [
         go.XYtoZ(self.padding + 3, self.padding + 3),
         go.XYtoZ(self.padding + 3, self.padding + self.length - 4),
         go.XYtoZ(self.padding + self.length - 4, self.padding + 3),
         go.XYtoZ(self.padding + self.length - 4,
                  self.padding + self.length - 4)
     ]
     self.clear()
예제 #3
0
 def to_string(self):
     s = ""
     for x in range(self.length):
         for y in range(self.length):
             s += str(self.blockcell[go.XYtoZ(x + self.padding,
                                              self.padding + y)]) + " "
         s += "\n"
     return s + super().to_string() + "Block Turn : " + str(
         self.blockturn) + "\n"
예제 #4
0
def test(img_file, mdl_file):

    # 精度テスト
    policy = PolicyClient(mdl_file)

    # 画像ログの読み込み
    middle_image_logs = go.load_middle_image_logs(img_file)

    phase_index_vector = [[] for _ in range(N_PHASES)]
    for i, il in enumerate(middle_image_logs):
        phase_index_vector[0].append(i)
    phase_data_num = [len(v) for v in phase_index_vector]
    for ph in range(N_PHASES):
        np.random.shuffle(phase_index_vector[ph])

    image_vector = [
        np.empty((phase_data_num[ph], go.IMAGE_LENGTH, go.IMAGE_LENGTH,
                  go.IMAGE_PLAINS),
                 dtype=np.float32) for ph in range(N_PHASES)
    ]
    move_vector = [
        np.zeros((phase_data_num[ph], go.IMAGE_SIZE), dtype=np.float32)
        for ph in range(N_PHASES)
    ]

    for ph in range(N_PHASES):
        for i in range(phase_data_num[ph]):
            il = middle_image_logs[phase_index_vector[ph][i]]
            for x in range(go.IMAGE_LENGTH):
                for y in range(go.IMAGE_LENGTH):
                    val = il['img'][x][y]
                    for p in range(go.IMAGE_PLAINS):
                        b = (val >> p) & 1
                        image_vector[ph][i][x][y][p] = b
            move_vector[ph][i][il['mv']] = 1

    for ph in range(N_PHASES):
        for i, img in enumerate(image_vector[ph]):
            prob = policy.calc(img)
            print(i)
            for x in range(go.IMAGE_LENGTH):
                print([
                    int(prob[go.XYtoZ(x, y)] * 100)
                    for y in range(go.IMAGE_LENGTH)
                ])
예제 #5
0
    def play(self, my_color):
        print_error('Engine.play()')

        print_error("remained time = %s" % self.time_limit)
        tstart = time.time()

        sc2 = self.board.count_chinese_score2()
        print_error("current chinese score x 2 = %d" % sc2)
        if self.board.lastZ == go.PASS:
            if self.board.turn >= 200:
                return blockgo.BlockType.NONE, -1, go.PASS
            if my_color == go.BLACK and sc2 > 0:
                self.time_limit -= time.time() - tstart
                return blockgo.BlockType.NONE, -1, go.PASS
            elif my_color == go.WHITE and sc2 < 0:
                self.time_limit -= time.time() - tstart
                return blockgo.BlockType.NONE, -1, go.PASS

        img = go.to_image(self.board, my_color)

        amp_img = go.amplify_image8(img)
        prob = self.policy.calc_amp8(amp_img)

        print_error(go.colorString[my_color])
        for ix in range(self.board.length):
            print_error(
                str([
                    int(prob[go.XYtoZ(ix + self.board.padding,
                                      iy + self.board.padding)] * 100)
                    for iy in range(self.board.length)
                ]))

        vlist = []
        for bt in range(blockgo.BlockType.NUM):
            for dir in range(4):
                for z in range(1, go.IMAGE_SIZE):
                    validity = self.board.check_block(my_color, bt, dir, z)
                    if validity == go.VALID:  # 合法である
                        # このブロックのスコアを計算
                        psum, pprod = 0, 1
                        for dz in blockgo.BLOCK_COMPONENTS_ZD[bt][dir]:
                            p = prob[z + dz]
                            psum += p
                            pprod *= p
                        pmean, ppmean = psum / blockgo.BLOCK_NUM[bt], pprod**(
                            1 / blockgo.BLOCK_NUM[bt])
                        score = (0.3 * pmean +
                                 0.7 * ppmean) * blockgo.BLOCK_STONES[bt]
                        # 初期の手に対するヒューリスティクス
                        ix = go.ZtoX(z) - self.board.padding
                        iy = go.ZtoY(z) - self.board.padding
                        ixmin, ixmax, iymin, iymax = blockgo.bt_dir_range(
                            bt, dir, ix, iy)
                        if self.board.blockturn < 6:
                            if ixmax < 3 or self.board.length - 4 < ixmin or iymax < 3 or self.board.length - 4 < iymin:
                                score = 0
                        elif self.board.blockturn < 8:
                            if ixmax < 2 or self.board.length - 3 < ixmin or iymax < 2 or self.board.length - 3 < iymin:
                                score = 0
                        # 地を確定させる手
                        """tbd = copy.deepcopy(self.board)
                        tbd.move_block(my_color, bt, dir, z + dz)
                        tsc2 = tbd.count_chinese_score2()
                        if my_color == go.BLACK:
                            score += max(tsc2 - sc2, 0)
                        else:
                            score += max(sc2 - tsc2, 0)"""
                        vlist.append((bt, dir, z, score))
        if len(vlist) == 0:
            print_error("no valid move.")
            self.time_limit -= time.time() - tstart
            return blockgo.BlockType.NONE, -1, go.PASS

        np.random.shuffle(vlist)  # 同一局面を避けるために先にランダムに並べ替え
        sorted_vlist = sorted(vlist, key=lambda x: -x[3])
        self.time_limit -= time.time() - tstart
        return sorted_vlist[0][0], sorted_vlist[0][1], sorted_vlist[0][2]
예제 #6
0
def gtp_main(my_id, my_version, config_directory, model_path):

    engine = Engine(1800)
    ingame = False
    end = False

    engine.init_all([model_path])

    while not end:
        rmsg_str = recv_msg()
        if len(rmsg_str) == 0:
            print_error("disconnected...")
            end = True
        rmsg_list = str(rmsg_str).split('\n')
        for command in rmsg_list:
            if len(command) == 0:
                continue
            print_error("Server >> %s" % command)
            com_list = command.split(' ')
            com = com_list[0].lower()
            # メッセージが複雑なのでこちらで分岐する
            if com == "boardsize":
                l = int(com_list[1])
                engine.set_length(l)
                ingame = False
                send_msg("= \n\n")
            elif com == "clear_board":
                # ログアウト判定
                #if ingame and os.path.isfile(config_directory + "stop_signal"):
                #    end = True
                engine.init_game([])
                send_msg("= \n\n")
            elif com == "name":
                send_msg("= " + my_id + "\n\n")
            elif com == "version":
                send_msg("= " + my_version + "\n\n")
            elif com == "genmove":
                bt, dir, move_z = blockgo.BlockType.NONE, -1, go.RESIGN
                if com_list[1].lower() == "b":
                    bt, dir, move_z = engine.play(go.BLACK)
                    engine.recv_play(go.BLACK, bt, dir, move_z)
                elif com_list[1].lower() == "w":
                    bt, dir, move_z = engine.play(go.WHITE)
                    engine.recv_play(go.WHITE, bt, dir, move_z)
                send_msg("= " + to_GTP_move_string(engine.length, move_z) +
                         "\n\n")
            elif com == "play":
                move_z = go.RESIGN
                if com_list[2].lower().find("resign") >= 0:
                    end = True
                else:
                    if com_list[2].lower().find("pass") >= 0:
                        bt = blockgo.BlockType.NONE
                        dir = -1
                        move_z = go.PASS
                    else:
                        bt = blockgo.char_to_bt(com_list[2])
                        dir = int(com_list[3])
                        padding = go.PADDING + (go.MAX_LENGTH -
                                                engine.length) // 2
                        x = go.char_to_ix(com_list[4][0].upper()) + padding
                        y = int(com_list[4][1:]) - 1 + padding
                        move_z = go.XYtoZ(x, y)

                    if com_list[1].lower() == "b":
                        engine.recv_play(go.BLACK, bt, dir, move_z)
                    elif com_list[1].lower() == "w":
                        engine.recv_play(go.WHITE, bt, dir, move_z)
                send_msg("= \n\n")
            elif com == "final_status_list":
                send_msg("= \n\n")
            elif com == "list_commands":
                lst = [
                    "boardsize", "clear_board", "name", "version", "genmove",
                    "play", "list_commands"
                ]
                send_msg("= " + '\n'.join(lst) + "\n\n")
            else:
                send_msg("= \n\n")
    return