コード例 #1
0
    def prior_knowledge_cmd(self,args):
        move, probs = self.probability(self.board)
        sim_probs = self.sim(probs, move, self.board)
        win_rate = self.winrates(probs, move, self.board)

        # print("winrates " + str(win_rate))
        wins = np.zeros(self.board.maxpoint)


        for num1 in range(len(move)):
            for num2 in range(0, len(move)-num1-1):
                if move[num2] != move[num2+1]:
                    if win_rate[move[num2]] < win_rate[move[num2+1]]:
                        move[num2], move[num2+1] = move[num2+1], move[num2]
                    elif win_rate[move[num2]]==win_rate[move[num2+1]]:
                        move1 = GoBoardUtilGo4.format_point(self.board._point_to_coord(move[num2]))
                        move2 = GoBoardUtilGo4.format_point(self.board._point_to_coord(move[num2+1]))

                        if(move1[0]>move2[0]):
                            move[num2] , move[num2+1] = move[num2+1] , move[num2]
        for elem in move:
            # print(GoBoardUtilGo4.format_point(self.board._point_to_coord(elem)), sim_probs[elem], win_rate[elem])
            wins[elem] = int(round(sim_probs[elem] * win_rate[elem]))
            # after calculating wins, we need to round simulation
            sim_probs[elem] = int(round(sim_probs[elem]))
            # wins[elem] = sim_probs[elem] * win_rate[elem]

        values = []

        for elem in move:
            # print(elem)
            elem2 = elem
            if elem == 0:
                elem2 = 'Pass'
                # print((elem2), sim_probs[elem], wins[elem])
                values.append(elem2)
                values.append(int(wins[elem]))
                values.append(int(sim_probs[elem]))

            else:
                # print(GoBoardUtilGo4.format_point(self.board._point_to_coord(elem2)), sim_probs[elem], wins[elem])
                values.append(GoBoardUtilGo4.format_point(self.board._point_to_coord(elem)))
                values.append(int(wins[elem]))
                values.append(int(sim_probs[elem]))

        str1 = ' '.join(str(e) for e in values)
        self.respond(''.join(str1))
コード例 #2
0
ファイル: Go4.py プロジェクト: kvongaza/c496a4
def writeMoves(board, moves, count, numSimulations):
    gtp_moves = []
    for i in range(len(moves)):
        if moves[i] != None:
            x, y = board._point_to_coord(moves[i])
            gtp_moves.append((GoBoardUtilGo4.format_point(
                (x, y)), float(count[i]) / float(numSimulations)))
        else:
            gtp_moves.append(('Pass', float(count[i]) / float(numSimulations)))
    sys.stderr.write("win rates: {}\n".format(
        sorted(gtp_moves, key=byPercentage, reverse=True)))
    sys.stderr.flush()
コード例 #3
0
    def genmove_cmd(self, args):
        """
        generate a move for the specified color

        Arguments
        ---------
        args[0] : {'b','w'}
            the color to generate a move for
            it gets converted to  Black --> 1 White --> 2
            color : {0,1}
            board_color : {'b','w'}
        """
        try:
            board_color = args[0].lower()
            color = GoBoardUtilGo4.color_to_int(board_color)
            self.debug_msg("Board:\n{}\nko: {}\n".format(str(self.board.get_twoD_board()),
                                                          self.board.ko_constraint))
            move = self.go_engine.get_move(self.board, color)
            if move is None:
                self.respond("pass")
                return

            if not self.board.check_legal(move, color):
                move = self.board._point_to_coord(move)
                board_move = GoBoardUtilGo4.format_point(move)
                self.respond("Illegal move: {}".format(board_move))
                raise RuntimeError("Illegal move given by engine")

            # move is legal; play it
            self.board.move(move,color)

            self.debug_msg("Move: {}\nBoard: \n{}\n".format(move, str(self.board.get_twoD_board())))
            move = self.board._point_to_coord(move)
            board_move = GoBoardUtilGo4.format_point(move)
            self.respond(board_move)
        except Exception as e:
            self.respond('Error: {}'.format(str(e)))
            raise