示例#1
0
    def policy_moves_cmd(self, args):
        moves = []
        probs = []
        if self.go_engine.random_simulation:
            color = self.board.current_player
            empty_pts = self.board.get_empty_points()
            legal_pts = [
                move for move in empty_pts if self.board.is_legal(move, color)
            ]
            probs = [
                str(round(1 / len(legal_pts), 3))
                for _ in range(len(legal_pts))
            ]

            for mv in legal_pts:
                coord = point_to_coord(mv, self.board.size)
                moves.append(format_point(coord).lower())
            mv_str = ' '.join(sorted(moves))
            prob_str = ' '.join(probs)
            self.respond(mv_str + " " + prob_str)
        else:

            m = PatternUtil.generate_pattern_moves(self.board,
                                                   self.pattern_table)
            mvs, vals = PatternUtil.calc_probabilities(m)
            for mv in mvs:
                coord = point_to_coord(mv, self.board.size)
                moves.append(format_point(coord).lower())

            sorted_mvs, sorted_vals = (list(i)
                                       for i in zip(*sorted(zip(moves, vals))))
            probs = [str(round(num, 3)) for num in sorted_vals]
            mvs_str = ' '.join(sorted_mvs)
            sorted_vals = ' '.join(probs)
            self.respond(mvs_str + " " + sorted_vals)
示例#2
0
    def policy_moves_cmd(self, args):
        # if the game is already end
        if (self.board.check_game_end_gomoku()[0]):
            self.respond()
            return

        # check if still have the empty points
        if len(GoBoardUtil.generate_legal_moves_gomoku(self.board)) == 0:
            self.respond()
            return

        # use copy board
        cboard = self.board.copy()

        # check policy
        if self.policy_type == "random":
            moves = GoBoardUtil.generate_legal_moves_gomoku(self.board)
            moves_string = []
            for move in moves:
                move_coord = point_to_coord(move, self.board.size)
                move_as_string = format_point(move_coord)
                moves_string.append(move_as_string)
            sorted_moves = ' '.join(sorted(moves_string))
            self.respond("Random {}".format(sorted_moves))
        else:
            policy, moves = PatternUtil.generate_policy_moves(cboard)

            moves_string = []
            for move in moves:
                move_coord = point_to_coord(move, self.board.size)
                move_as_string = format_point(move_coord)
                moves_string.append(move_as_string)

            sorted_moves = ' '.join(sorted(moves_string))
            self.respond(policy + " " + sorted_moves)
示例#3
0
 def simulate(self, board, move, to_play):
     """
     Run a simulate game for a given move.
     """
     cboard = board.copy()
     cboard.play_move_gomoku(move, to_play)
     opp = GoBoardUtil.opponent(to_play)
     return PatternUtil.play_game(cboard,
                                  opp,
                                  random_simulation=self.random_simulation)
示例#4
0
 def simulate(self, board, move, toplay, table):
     cboard = board.copy()
     cboard.play_move(move, toplay)
     opp = GoBoardUtil.opponent(toplay)
     return PatternUtil.playGame(cboard,
                                 opp,
                                 limit=self.limit,
                                 random_simulation=self.random_simulation,
                                 use_pattern=self.use_pattern,
                                 table=table)
 def policy_moves_cmd(self, args):
     """
     Return list of policy moves for the current_player of the board
     """
     policy_moves, type_of_move = PatternUtil.generate_all_policy_moves(
         self.board, self.go_engine.use_pattern,
         self.go_engine.check_selfatari)
     if len(policy_moves) == 0:
         self.respond("Pass")
     else:
         response = type_of_move + " " + sorted_point_string(
             policy_moves, self.board.size)
         self.respond(response)
示例#6
0
 def simulate(self, board, move, toplay):
     """
         Run a simulate game for a given move.
         """
     cboard = board.copy()
     cboard.play_move(move, toplay)
     opp = GoBoardUtil.opponent(toplay)
     return PatternUtil.playGame(cboard,
                                 opp,
                                 komi=self.komi,
                                 limit=self.limit,
                                 random_simulation=self.random_simulation,
                                 use_pattern=self.use_pattern)
示例#7
0
def simulate(board, move, toplay):
    """
    Run a simulate game for a given move.
    """
    cboard = board.copy()
    cboard.play_move(move, toplay)
    opp = GoBoardUtil.opponent(toplay)
    return PatternUtil.playGame(cboard,
                                opp,
                                komi=0,
                                limit=100,
                                random_simulation = True,      #implement a way to change this accordingly
                                use_pattern = False,           #implement a way to change this accordingly
                                check_selfatari = False)       #implement a way to change this accordingly
示例#8
0
    def policy_moves_cmd(self, args):
        if self.go_engine.random_simulation == True:
            empties = self.board.get_empty_points()
            color = self.board.current_player
            legal_moves = []
            for move in empties:
                if self.board.is_legal(move, color):
                    legal_moves.append(move)

            gtp_moves = []
            probabilities = []
            for move in legal_moves:
                coords = point_to_coord(move, self.board.size)
                gtp_moves.append(format_point(coords).lower())
                probabilities.append(str(round(1 / len(legal_moves), 3)))
            sorted_moves = ' '.join(sorted(gtp_moves))
            probability = ' '.join(probabilities)
            self.respond(sorted_moves + " " + probability)
        else:
            moves, values = PatternUtil.generate_pattern_moves(self.board)
            new_values = PatternUtil.normalize_val(values)
            dict = {}
            #sorted_moves = sorted(moves)
            new_sorted_values = []
            format_moves = []
            for move in moves:
                #new_sorted_values.append(str(round(dict[move], 3)))
                coords = point_to_coord(move, self.board.size)
                format_moves.append(format_point(coords).lower())
            for i in range(len(format_moves)):
                dict[format_moves[i]] = new_values[i]
            sorted_format_moves = sorted(format_moves)
            for move in sorted_format_moves:
                new_sorted_values.append(str(round(dict[move], 3)))
            sorted_moves_str = ' '.join(sorted_format_moves)
            probability = ' '.join(new_sorted_values)
            self.respond(sorted_moves_str + " " + probability)
示例#9
0
 def _evaluate_rollout(self, board, toplay):
     """
     Use the rollout policy to play until the end of the game, returning +1 if the current
     player wins, -1 if the opponent wins, and 0 if it is a tie.
     """
     winner = PatternUtil.playGame(board,
                                   toplay,
                                   komi=self.komi,
                                   limit=self.limit,
                                   random_simulation=self.simulation_policy,
                                   use_pattern=self.use_pattern,
                                   check_selfatari=self.check_selfatari)
     if winner == BLACK:
         return 1
     else:
         return 0
示例#10
0
    def policy_moves_cmd(self, args):
        if self.go_engine.random_simulation:
            #get legal moves
            first_moves = GoBoardUtil.generate_legal_moves(
                self.board, self.board.current_player)
            moves = []
            #convert points to formated
            for move in first_moves:
                #convert to coords
                temp = point_to_coord(move, self.board.size)
                #format version
                temp = format_point(temp)
                moves.append(temp)
            moves.sort()

            #reverse it
            new_moves = []
            column_letters = "ABCDEFGHJKLMNOPQRSTUVWXYZ"
            #put format to point
            for move in moves:
                temp = str(column_letters.find(move[0]) + 1)
                temp = coord_to_point(int(move[1]), int(temp), self.board.size)
                new_moves.append((temp))

            movelist = new_moves

            p = []
            for x in range(0, len(movelist)):
                p.append(round(float(1) / float(len(movelist)), 3))
        else:
            movelist, p = PatternUtil.pattern(self.board)

        result = ''
        for move in movelist:
            temp = point_to_coord(move, self.board.size)
            temp = format_point(temp)
            result += temp.lower() + " "
        for i in p:
            result += str(i) + " "

        try:
            if args[0] == "1":
                return movelist, p

        except:
            self.respond(result)
示例#11
0
    def get_move(self, board, color):
        """
        Run one-player MC simulations to get a move to play
        """
        color = board.current_player
        cboard = board.copy()
        if self.random_simulation:
            moves = GoBoardUtil.generate_legal_moves_gomoku(board)
        else:
            _, moves = PatternUtil.generate_policy_moves(board)

        move_wins = []
        for move in moves:
            wins = self.simulate_move(cboard, move, color)
            move_wins.append(wins)

        return select_best_move(board, moves, move_wins)
示例#12
0
 def find_pattern_feature(features, board, point):
     p = PatternUtil.neighborhood_33(board, point)
     if p in pat3set:
         Feature.set_feature(features, point,
                             patIndex[p] + NUM_SIMPLE_FEATURE, 1)
示例#13
0
 def get_move(self, board, toplay):
     return PatternUtil.generate_move_with_filter(board, self.use_pattern,
                                                  self.check_selfatari)
示例#14
0
    def policy_moves_cmd(self, args):

        signal.signal(signal.SIGALRM, self.handler)  #added for timer
        signal.alarm(int(self.timel) - 10)  #added for timer
        print("timer start")
        try:
            movelist = GoBoardUtil.generate_legal_moves(
                self.board, self.board.current_player)
            if self.go_engine.random_simulation:
                """#get legal moves
                first_moves = GoBoardUtil.generate_legal_moves(self.board, self.board.current_player)
                moves =[]
                #convert points to formated
                for move in first_moves:
                    #convert to coords
                    temp = point_to_coord(move, self.board.size)
                    #format version
                    temp = format_point(temp)
                    moves.append(temp)
                moves.sort()

                #reverse it
                new_moves =[]
                column_letters = "ABCDEFGHJKLMNOPQRSTUVWXYZ"
                #put format to point
                for move in moves:
                    temp = str(column_letters.find(move[0])+1)
                    temp = coord_to_point(int(move[1]),int(temp), self.board.size)
                    new_moves.append((temp))

                movelist = new_moves"""

                p = []
                for x in range(0, len(movelist)):
                    p.append(round(float(1) / float(len(movelist)), 3))
            else:
                p = []
                for move in movelist:
                    p.append(PatternUtil.pattern(self.board, move))
                for y in range(0, len(p)):
                    p.append(round(float(p.pop()) / float(len(movelist)), 3))
            try:
                if args[0] == "1":
                    signal.alarm(0)
                    return movelist, p

            except:
                result = ''
                for move in movelist:
                    temp = point_to_coord(move, self.board.size)
                    temp = format_point(temp)
                    result += temp.lower() + " "
                for i in p:
                    result += str(i) + " "
                signal.alarm(0)
                self.respond(result)

        except TimeoutException:  #added for timer
            #give best we have
            if len(p) < len(movelist):
                for x in range(0, (len(movelist) - len(p))):
                    p.append(1.0)
            for y in range(0, len(p)):
                p.append(round(float(p.pop()) / float(len(movelist)), 3))
            signal.alarm(0)
            return movelist, p