Exemple #1
0
    def constructGcsP(self):
        """Construct the subgraph Gcs(P)=(U \cup V, Ees)
        According to paper "optimum matchings in weighted bipartite graphs"
        GcsP is obtained by removing the edges uv of G such that w(uv) -\pi(u)-p(v) != 0

        :returns: subgraph Gcs(P), the auxiliary subgraph, bipartite

        """
        BiG_re = copy.deepcopy(self.bipartite)
        weightMatrix = self.__getWM()
        Mu, Mv, val, lu, lv = kuhnMunkres.maxWeightMatching(weightMatrix)
        #print "lu:", lu
        #print "lv", lv

        print "begin [[constructGcsP_getResult"
        time1 = datetime.datetime.now()
        [[
            self.__constructGcsP_getResult(j, i, BiG_re, weightMatrix, lu, lv)
            for i in range(len(self.DGNodes))
        ] for j in range(len(self.DGNodes))]
        time2 = datetime.datetime.now()
        print "time elapsed: {}".format(time2 - time1)
        print "finish [[constructGcsP_getResult"

        return BiG_re
Exemple #2
0
def minProduct(v1, v2):
    matrix = []
    size = len(v1)
    for i in range(size):
        row = []
        for j in range(size):
            row.append(-v1[i] * v2[j])
        matrix.append(list(row))

    a, b, result = maxWeightMatching(matrix)
    return -result
Exemple #3
0
def minProduct(v1, v2):
    matrix = []
    size = len(v1)
    for i in range(size):
        row = []
        for j in range(size):
            row.append(-v1[i] * v2[j])
        matrix.append(list(row))

    a, b, result = maxWeightMatching(matrix)
    return -result
Exemple #4
0
    def findMatch():
        distance = []  # use negative distance
        for i in range(S):
            row = []
            for j in range(S):
                d = cells[pos[i] - 1].distance(target[j])
                v = -1 * weight[i] * d
                row.append(v)
            distance.append(row)

        v = maxWeightMatching(distance)[2]
        return -1.0 * v
Exemple #5
0
    def findMatch():
        distance = []  # use negative distance
        for i in range(S):
            row = []
            for j in range(S):
                d = cells[pos[i] - 1].distance(target[j])
                v = -1 * weight[i] * d
                row.append(v)
            distance.append(row)

        v = maxWeightMatching(distance)[2]
        return -1.0 * v
Exemple #6
0
    def constructGcsP(self):
        """Construct the subgraph Gcs(P)=(U \cup V, Ees)
        According to paper "optimum matchings in weighted bipartite graphs"
        GcsP is obtained by removing the edges uv of G such that w(uv) -\pi(u)-p(v) != 0

        :returns: subgraph Gcs(P), the auxiliary subgraph, bipartite

        """
        BiG_re = copy.deepcopy(self.bipartite)
        weightMatrix = self.__getWM()
        Mu, Mv, val, lu, lv = kuhnMunkres.maxWeightMatching(weightMatrix)
        #print "lu:", lu
        #print "lv", lv

        print "begin [[constructGcsP_getResult"
        time1 = datetime.datetime.now()
        [[self.__constructGcsP_getResult(j, i, BiG_re, weightMatrix, lu, lv) for i in range(len(self.DGNodes))] for j in range(len(self.DGNodes))]
        time2 = datetime.datetime.now()
        print "time elapsed: {}".format(time2 - time1)
        print "finish [[constructGcsP_getResult"

        return BiG_re
Exemple #7
0
        sys.stdout.flush()
        placements += 1
        score += err

    if algo == "test":
        index = range(len(games))
        random.shuffle(index)
        for i, game in enumerate(games):
            place(game, 0, index[i])
    elif algo == "kuhn-munkres":
        import kuhnMunkres
        costs = [game.scores for game in games]
        profits = max(max(game.scores) for game in games)
        profits = [[profits - score for score in game.scores]
                   for game in games]
        best = kuhnMunkres.maxWeightMatching(profits)[0]
        for game, xy in best.items():
            game = games[game]
            place(game, game.scores[xy], xy)
    elif algo in ("greedy", "timed"):
        print "sorting MSE scores..."
        matches = []
        for game in games:
            for i, score in enumerate(game.scores):
                matches.append((score, i, game))
            if is_cuckoo:
                game.by_place = range(cols * rows)
                game.by_err = sorted(game.by_place,
                                     key=lambda i: game.scores[i])
                for i, j in enumerate(game.by_err):
                    game.by_place[j] = i
     most_peaked = s1_lpeaks
     most_peaked_id = s1
 n = less_peaked.size
 p = most_peaked.size
 if n != 0:
     ldistances = np.zeros((n,p))
     for i, vertex_id in enumerate(less_peaked):
         ldistances[i,:] = \
                     mean_lmesh_graph.dijkstra(vertex_id)[most_peaked]
     # scale distances
     ldistances = ((1. + np.sqrt(2.))/2.) * ldistances
     ldistances = phi(ldistances, bound)
     #ldistances = np.hstack((ldistances, gamma*np.ones((n,n))))
     if (p-n) > 0:
         ldistances = np.vstack((ldistances, gamma*np.ones((p-n,p))))
     (Mu,Mv,val) = maxWeightMatching(-ldistances)
     matching_res = ldistances[Mu.keys(), Mu.values()]
     val = np.abs((val)/min(n,p))
     print val
     mask = np.where(matching_res < gamma)[0]
     mask = mask[mask < less_peaked.size]
     all_llinked[less_peaked_id].append(less_peaked[mask])
     mask = np.where(matching_res >= gamma)[0]
     mask = mask[mask < less_peaked.size]
     all_lalone[less_peaked_id].append(less_peaked[mask])
     mask = ldistances[Mv.values(), Mv.keys()]
     
     matching_res = ldistances[Mv.values(), Mv.keys()]
     mask = np.where(matching_res < gamma)[0]
     mask = mask[mask < most_peaked.size]
     all_llinked[most_peaked_id].append(most_peaked[mask])