コード例 #1
0
    def generate_moves_with_feature_based_probs_Go5(board):
        from feature import Features_weight
        from feature import Feature
        assert len(Features_weight) != 0
        moves = []
        gamma_sum = 0.0
        empty_points = board.get_empty_points()
        color = board.current_player
        probs = np.zeros(board.maxpoint + 1)
        all_board_features = Feature.find_all_features(board)
        for move in empty_points:
            if board.check_legal(move, color) and not board.is_eye(move, color):
                moves.append(move)
                probs[move] = Feature.compute_move_gamma(Features_weight, all_board_features[move])
                gamma_sum += probs[move]
        #Append Pass to the list of moves
        moves.append("PASS")
        #Compute gamma for Pass move
        probs[len(probs) - 1] = Feature.compute_move_gamma(Features_weight, all_board_features["PASS"])
        #Add it to gamma sum
        gamma_sum += probs[len(probs) - 1]
        if len(moves) != 0:
            assert gamma_sum != 0.0
            for m in moves:
                if m == "PASS":
                    probs[len(probs) - 1] = probs[len(probs) - 1] / gamma_sum
                else:
                    probs[m] = probs[m] / gamma_sum

        return moves, probs
コード例 #2
0
 def expand(self, board, color):
     """
     Expands tree by creating new children.
     """
     moves = board.get_empty_points()
     probs = {}
     gamma_sum = 0.0
     all_board_features = Feature.find_all_features(board)
     for move in moves:
         if move not in self._children:
             if board.check_legal(move, color) and not board.is_eye(move, color):
                 self._children[move] = TreeNode(self)
                 self._children[move]._move = move
                 
                 #Tell it to use knowledge. default to not
                 self._children[move]._use_knowledge = self._use_knowledge
                 
                 #Compute the probability for each child move
                 if self._use_knowledge == "probabilistic":
                     probs[move] = Feature.compute_move_gamma(Features_weight, all_board_features[move])
                     gamma_sum += probs[move]    
     #passing is always allowed
     self._children[PASS] = TreeNode(self)
     self._children[PASS]._move = PASS
     
     if self._use_knowledge == "probabilistic":
         moves.append(PASS)
         probs[PASS] = Feature.compute_move_gamma(Features_weight, all_board_features["PASS"])
         gamma_sum += probs[PASS]
         
         #Normalize
         if len(moves) != 0:
             assert gamma_sum != 0.0
             for m in moves:
                 probs[m] = probs[m] / gamma_sum
                 
         best_move = max(probs.items(), key = lambda x: x[1])[0]
         best_move_prob = probs[best_move]
         
         b_win_sum, n_visit_sum = 0,0
         for move in moves:
             childSim = sim(move, probs[move], best_move_prob, 10)
             self._children[move]._black_wins = childSim.wins
             b_win_sum += childSim.wins
             self._children[move]._n_visits = childSim.sim
             n_visit_sum += childSim.sim
          
                     
     #self._black_wins += b_win_sum
     #self._n_visits += n_visit_sum
     
     self._expanded = True
コード例 #3
0
    def prior_knowledge_cmd(self, args):
        moves = []
        gamma_sum = 0.0
        empty_points = self.board.get_empty_points()
        color = self.board.current_player
        #probs = np.zeros(self.board.maxpoint)
        probs = {}
        all_board_features = Feature.find_all_features(self.board)

        for move in empty_points:
            if self.board.check_legal(
                    move, color) and not self.board.is_eye(move, color):
                moves.append(move)
                probs[move] = Feature.compute_move_gamma(
                    Features_weight, all_board_features[move])
                gamma_sum += probs[move]

        #passing is always allowed, add it
        moves.append("PASS")
        probs["PASS"] = Feature.compute_move_gamma(Features_weight,
                                                   all_board_features["PASS"])
        gamma_sum += probs["PASS"]

        if len(moves) != 0:
            assert gamma_sum != 0.0
            for m in moves:
                probs[m] = probs[m] / gamma_sum

        best_move = max(probs.items(), key=lambda x: x[1])[0]
        best_move_prob = probs[best_move]

        result = []
        for i in moves:
            if i == "PASS":
                result.append(sim(i, probs[i], best_move_prob, 10))
            else:
                result.append(
                    sim(GoBoardUtil.sorted_point_string([i], self.board.NS),
                        probs[i], best_move_prob, 10))

        result.sort(key=sim.move)
        result.sort(reverse=True, key=sim.winrate)

        response = ""
        for i in result:
            if i.move == "PASS":
                response += "Pass " + str(i.wins) + " " + str(i.sim) + " "
            else:
                response += i.move + " " + str(i.wins) + " " + str(i.sim) + " "

        self.respond(response[:-1])
コード例 #4
0
ファイル: mcts.py プロジェクト: LinkaiQi/CMPUT496_Winter2018
 def calc_prior_knowledge(self, board):
     from feature import Features_weight
     from feature import Feature
     # initialization
     all_board_features = Feature.find_all_features(board)
     probs = {}
     p_max = 0
     gamma_sum = 0.0
     # calculate probabilistic
     for move in self._children:
         probs[move] = Feature.compute_move_gamma(Features_weight, \
             all_board_features[move])
         gamma_sum += probs[move]
     for m in self._children:
         probs[m] = probs[m] / gamma_sum
         # find the largest probability 'p_max'
         if probs[m] > p_max:
             p_max = probs[m]
     # calculate/assign the number of sims and wins to each of the expanded children
     for m in self._children:
         sim = 10 * probs[m] / p_max
         winrate = 0.5 + probs[m] / p_max * 0.5
         win = winrate * sim
         self._children[m]._n_visits = int(round(sim))
         self._children[m]._black_wins = int(round(win))
     # For the root only, initialize its _black_wins and _n_visits with the
     # sum of its children's _black_wins and _n_visits.
     # this will avoid problems with computing log np in the UCT formula if np=0.
     if self.is_root():
         sum_visits = 0; sum_wins = 0
         for m in self._children:
             sum_visits += self._children[m]._n_visits
             sum_wins += self._children[m]._black_wins
         self._n_visits = sum_visits
         self._black_wins = sum_wins
コード例 #5
0
ファイル: gtp_connection.py プロジェクト: RAYFC/MCTS-GO
def get_the_prob(move, board):
    from feature import Features_weight
    from feature import Feature
    assert len(Features_weight) != 0
    all_board_features = Feature.find_all_features(board)
    return Feature.compute_move_gamma(Features_weight,
                                      all_board_features[move])
コード例 #6
0
 def prob_moves(board):
     from feature import Feature
     from feature import Features_weight
     num_features= Feature.find_all_features(board)
             #print(num_features)
             #print(Features_weight)
     dict1={}
     problist=[]
     for item in num_features:
         #print(item)
         if item =="PASS":
             #gamma=Feature.compute_move_gamma(Features_weight, num_features[item])
             #dict1[item]=gamma
             continue
         else:
             move = GoBoardUtil.format_point(GoBoardUtil.point_to_coord(item,board.NS))
                 #print(move)
             gamma=Feature.compute_move_gamma(Features_weight, num_features[item])
             dict1[move]=gamma
             problist.append(gamma)
                 #print(gamma)
     norm = [float(i)/sum(problist) for i in problist]
     norm.sort(reverse=True)
     dict2={}
     for item in dict1:
         dict2[item]=float(dict1[item])/sum(dict1.values())
     sorted(dict2.values(),reverse=True)
     #print(dict2)
     return dict2
コード例 #7
0
    def get_move_prob(self):
        moves = GoBoardUtil.generate_random_moves(self.board) # legal and not eye-filling
        features = Feature.find_all_features(self.board)
        gammas_sum = 0
        move_gammas = dict()
        if len(Features_weight) != 0:
            for move in moves:
                move_gammas[move] = Feature.compute_move_gamma(Features_weight, features[move])
                gammas_sum += move_gammas[move]

            # normalize to get probability
            if gammas_sum:
                for move in move_gammas.keys():
                    move_gammas[move] /= gammas_sum

        if move_gammas and gammas_sum:
            move_prob_tuples = [(self.board.point_to_string(k), v) for (k, v) in move_gammas.items()]

            # sort list by probability and alphabetic order
            # based on
            # http://stackoverflow.com/questions/5212870/sorting-a-python-list-by-two-criteria
            # answered by jaap on Stack Overflow http://stackoverflow.com/users/1186954/jaap
            move_prob_tuples = sorted(move_prob_tuples, key=lambda k:(-k[1], k[0][0], k[0][1]))
        else:
            move_prob_tuples = list()
            move_prob_tuples.append(('pass', 1))

        return move_prob_tuples
コード例 #8
0
ファイル: board_util.py プロジェクト: Shuaiqun-Pan/Game-of-Go
    def generate_all_policy_moves(board, pattern, check_selfatari):
        """
            generate a list of policy moves on board for board.current_player.
            Use in UI only. For playing, use generate_move_with_filter
            which is more efficient
        """
        from feature import Feature
        from feature import Features_weight

        gammas_sum = 0.0
        prob = {}
        moves = board.get_empty_points()
        all_board_features = Feature.find_all_features(board)
        for move in moves:
            if board.check_legal(move,
                                 board.current_player) and not board.is_eye(
                                     move, board.current_player):
                if len(Features_weight) != 0:
                    # when we have features weight, use that to compute knowledge (gamma) of each move
                    assert move in all_board_features
                    prob[move] = Feature.compute_move_gamma(
                        Features_weight, all_board_features[move])
                    gammas_sum += prob[move]

        for k, v in prob.items():
            prob[k] = v / gammas_sum

        ret = sorted(prob.items(), key=lambda x: (x[1], x[0]), reverse=True)
        return ret
コード例 #9
0
    def expand_without_pass(self, board, color):
        """Expands tree by creating new children.
        """
        gammas_sum = 0.0
        moves = board.get_empty_points()
        all_board_features = Feature.find_all_features(board)
        for move in moves:
            if move not in self._children:
                if board.check_legal(move,
                                     color) and not board.is_eye(move, color):
                    self._children[move] = TreeNode(self)
                    self._children[move]._move = move
                    if len(Features_weight) != 0:
                        # when we have features weight, use that to compute knowledge (gamma) of each move
                        assert move in all_board_features
                        self._children[
                            move]._prob_simple_feature = Feature.compute_move_gamma(
                                Features_weight, all_board_features[move])
                        gammas_sum += self._children[move]._prob_simple_feature

        # Normalize to get probability
        if len(Features_weight) != 0 and gammas_sum != 0.0:
            for move in moves:
                if move in self._children:
                    if board.check_legal(
                            move, color) and not board.is_eye(move, color):
                        self._children[
                            move]._prob_simple_feature = self._children[
                                move]._prob_simple_feature / gammas_sum

        self._expanded = True
コード例 #10
0
    def generate_moves_with_feature_based_probs(board):
        from feature import Features_weight
        from feature import Feature

        assert len(Features_weight) != 0
        moves = []
        gamma_sum = 0.0
        # need to include PASS move
        totalPoints = board.maxpoint + 1
        empty_points = board.get_empty_points()
        color = board.current_player
        probs = np.zeros(totalPoints)
        all_board_features = Feature.find_all_features(board)

        for move in empty_points:
            if board.check_legal(move,
                                 color) and not board.is_eye(move, color):
                moves.append(move)
                probs[move] = Feature.compute_move_gamma(
                    Features_weight, all_board_features[move])
                gamma_sum += probs[move]

        # need to apply the pass move now
        moves.append("PASS")
        probs[-1] = Feature.compute_move_gamma(Features_weight,
                                               all_board_features["PASS"])
        gamma_sum += probs[-1]

        if len(moves) != 0:
            assert gamma_sum != 0.0
            for m in moves:
                # calculate pass move probability now
                if m == 'PASS':
                    probs[-1] = probs[-1] / gamma_sum
                else:
                    probs[m] = probs[m] / gamma_sum
        return moves, probs
コード例 #11
0
    def probability(self, board):
        from feature import Features_weight
        from feature import Feature
        # print(Features_weight)
        assert len(Features_weight) != 0

        #legal moves
        moves = []

        gamma_sum = 0.0

        legal_moves_broad = Feature.legal_moves_on_board(board)
        legal_moves = []

        for elem in legal_moves_broad:
            if not board.is_eye(elem, board.current_player):
                legal_moves.append(elem)

        legal_moves.append(0)

        probs = np.zeros(board.maxpoint)

        feature_legal_move = Feature.find_all_features(board)

        for move in legal_moves:
            if move == 0:
                probs[move] = Feature.compute_move_gamma(Features_weight, feature_legal_move['PASS'])
            else:
                probs[move] = Feature.compute_move_gamma(Features_weight, feature_legal_move[move])
            gamma_sum += probs[move]

        if len(legal_moves) != 0.0:
            assert gamma_sum != 0.0
            for m in legal_moves:
                probs[m] = probs[m]/gamma_sum

        return legal_moves, probs
コード例 #12
0
    def prior_knowledge_cmd(self, args):
        # move = self.go_engine.get_move(self.board, color)
        # initialization
        all_board_features = Feature.find_all_features(self.board)
        probs = {}
        p_max = 0
        gamma_sum = 0.0
        moves = [PASS]
        knowledge = []

        # find all legal moves
        color = self.board.current_player
        for m in self.board.get_empty_points():
            if self.board.check_legal(
                    m, color) and not self.board.is_eye(m, color):
                moves.append(m)

        # calculate probabilistic
        for m in moves:
            probs[m] = Feature.compute_move_gamma(Features_weight, \
                all_board_features[m])
            gamma_sum += probs[m]
        for m in moves:
            probs[m] = probs[m] / gamma_sum
            # find the largest probability 'p_max'
            if probs[m] > p_max:
                p_max = probs[m]

        # calculate the numner of sims and wins
        for m in moves:
            sim = 10 * probs[m] / p_max
            winrate = 0.5 + probs[m] / p_max * 0.5
            win = winrate * sim
            if m == PASS:
                m = None
            knowledge.append(
                (self.board.point_to_string(m), winrate, sim, win))

        # sort by the (true, not rounded) winrate in descending order
        # break ties in alphanumeric order of the move
        knowledge.sort(key=lambda move: (-move[1], move[0]))
        output = []
        for move in knowledge:
            m, winrate, sim, win = move
            output.append(m + ' ' + str(int(round(win))) + ' ' +
                          str(int(round(sim))))
        self.respond(' '.join(output))
コード例 #13
0
def generate_moves_with_feature_based_probs(board, color):
    from feature import Features_weight
    from feature import Feature
    assert len(Features_weight) != 0
    moves = []
    gamma_sum = 0.0
    empty_points = board.get_empty_points()
    probs = np.zeros(board.maxpoint)
    all_board_features = Feature.find_all_features(board)
    for move in empty_points:
        if board.check_legal(move, color) and not board.is_eye(move, color):
            moves.append(move)
            probs[move] = Feature.compute_move_gamma(Features_weight,
                                                     all_board_features[move])
            gamma_sum += probs[move]
    if len(moves) != 0:
        assert gamma_sum != 0.0
        for m in moves:
            probs[m] = probs[m] / gamma_sum
    return moves, probs