def seed_select_per_community(DG, All_MIIA_DG, B_k, Vk_G, cost_per_user,
                              weight):
    l = 2
    S_k = []
    total_user_cost = 0.0
    while B_k - total_user_cost > 0:
        max_val = 0.0
        max_u = 0
        sigma_Sk = Expected_Influence(DG, S_k, All_MIIA_DG, l, weight)
        #print("**********************************************", sigma_Sk)
        Vk_G = [
            _vx for _vx in Vk_G if cost_per_user[_vx] <= B_k - total_user_cost
        ]
        for u in Vk_G:
            if u in S_k:
                continue
            new_S = S_k.copy()
            new_S.append(u)
            sigma_Sk_u = Expected_Influence(DG, new_S, All_MIIA_DG, l, weight)
            new_val = (sigma_Sk_u - sigma_Sk) / cost_per_user[u]
            #print("Old EI",sigma_Sk, "vertex", u, "new EI", sigma_Sk_u, "cost:", cost_per_user[u],\
            #     "new val:", new_val, "max_val:", max_val, "current Max u:", max_u )

            if new_val > max_val:
                max_val = new_val
                max_u = u

        if max_u > 0:
            if total_user_cost + cost_per_user[max_u] <= B_k:
                total_user_cost = total_user_cost + cost_per_user[max_u]
                S_k.append(max_u)
                Vk_G.remove(max_u)
                #print("vertex added:", max_u)
            else:
                #print("Not able to add maximum:", max_u)
                Vk_G.remove(max_u)
        ##### break : if no vertex can be added
        #check_flag = [ cost_per_user[_vx] + total_user_cost - B_k for _vx in Vk_G]
        #flag = all(item > 0 for item in check_flag)
        print(S_k)
        if len(Vk_G) > 0:
            if total_user_cost + min([cost_per_user[_vx]
                                      for _vx in Vk_G]) > B_k:
                #print("Not able to add any vertex")
                break
        else:
            break

    return S_k, (B_k - total_user_cost)
        p_vec = DG_prime_instance[edge[0]][
            edge[1]]['prob_weight_vec'][TK_prime]
        p_vec = np.ones(len(p_vec)) - p_vec
        DG_prime_instance[edge[0]][edge[1]]['tag_accum_prob_weight'] = (
            1 - np.prod(p_vec))
    for edge in DG_prime.edges():
        if DG_prime_instance[edge[0]][edge[1]]['tag_accum_prob_weight'] <= 0:
            DG_prime_instance.remove_edge(edge[0], edge[1])
    print("Number of edges: ", DG_prime_instance.number_of_edges())
    All_MIIA_DG = {}
    for t in DG_prime.nodes():
        #print(t)
        All_MIIA_DG[t] = Maximum_Influence_In_Arborescence(
            DG_prime_instance, t, theta=0.1, weight='tag_accum_prob_weight')

    EI_budget[b] = Expected_Influence(DG_prime_instance,  SK_prime , \
                   All_MIIA_DG, l=2, weight = 'tag_accum_prob_weight')
    end_ts = time.time()
    print("Expected Influence: ", EI_budget[b])
    result.append([EI_budget[b], len(SK_prime), end_ts - start_ts])
    #EI_setting_budget['count_prob']['comm'].append(EI_budget[b])

EI_setting_budget['count_prob'] = result
result = []

print(
    "*****************************************************************************************************"
)
print(
    "***********       BaseLine TBIM-with community -- trivalency prob setting      **********************"
)
print(
Beispiel #3
0
def seed_tag_set_selection( DG_prime, TK_prime, Bk_u, Bk_t, sorted_1000tags, Initial_Tag, considered_community_nodes,\
                           cost_per_user, cost_per_mtag, weight = prob_weight_vec ):
    total_tag_cost = 0.0
    total_user_cost = 0.0
    S_k = []
    T_k = Initial_Tag.copy()
    Vk_G = considered_community_nodes.copy()

    no_user_to_Add = False
    no_tag_to_Add = False

    while Bk_u - total_user_cost > 0 and Bk_t - total_tag_cost > 0:

        All_MIIA_DG, DG_prime_instance = get_All_MIIA_DG(
            DG_prime, weight,
            T_k + TK_prime)  ### change 07-05-2019 added + TK_prime
        sigma_STk = Expected_Influence(DG_prime_instance,
                                       S_k,
                                       All_MIIA_DG,
                                       l=2,
                                       weight='tag_accum_prob_weight')

        max_val = 0.0
        max_u = 0
        max_t = 0

        sorted_1000tags = [
            tg for tg in sorted_1000tags
            if total_tag_cost + cost_per_mtag[tg] < Bk_t and tg not in T_k +
            TK_prime
        ]
        Vk_G = [
            ux for ux in Vk_G if total_user_cost + cost_per_user[ux] < Bk_u
        ]

        for tag in sorted_1000tags:
            if tag in T_k:
                continue

            new_T_k = T_k.copy()
            new_T_k.append(tag)
            try:
                del All_MIIA_DG, DG_prime_instance
                All_MIIA_DG, DG_prime_instance = get_All_MIIA_DG(
                    DG_prime, weight,
                    new_T_k + TK_prime)  ### change 07-05-2019 added + TK_prime
            except:
                All_MIIA_DG, DG_prime_instance = get_All_MIIA_DG(
                    DG_prime, weight,
                    new_T_k + TK_prime)  ### change 07-05-2019 added + TK_prime

            for user in Vk_G:
                if user in S_k:
                    continue
                new_S = S_k.copy()
                new_S.append(user)
                sigma_Sk_u = Expected_Influence(DG_prime_instance, new_S , All_MIIA_DG, \
                                                l=2, weight='tag_accum_prob_weight')
                new_val = (sigma_Sk_u - sigma_STk) / (cost_per_user[user] +
                                                      cost_per_mtag[tag])
                #print("Old EI",sigma_Sk, "vertex", u, "new EI", sigma_Sk_u, "cost:", cost_per_user[u],\
                #     "new val:", new_val, "max_val:", max_val, "current Max u:", max_u )

                if new_val > max_val:
                    max_val = new_val
                    max_u = user
                    max_t = tag

        ###### update seed and tag set  ##############
        if max_u > 0 and max_t > 0:
            if total_user_cost + cost_per_user[max_u] <= Bk_u:
                total_user_cost = total_user_cost + cost_per_user[max_u]
                S_k.append(max_u)
                Vk_G.remove(max_u)
                #print("vertex added:", max_u)
            else:
                #print("Not able to add maximum:", max_u)
                Vk_G.remove(max_u)

            if total_tag_cost + cost_per_mtag[max_t] <= Bk_t:
                total_tag_cost = total_tag_cost + cost_per_mtag[max_t]
                T_k.append(max_t)
                sorted_1000tags.remove(max_t)
                #print("vertex added:", max_u)
            else:
                #print("Not able to add maximum:", max_u)
                sorted_1000tags.remove(max_t)

        ################  check for no user to add   ###################
        if len(Vk_G) > 0:
            if total_user_cost + min([cost_per_user[_vx]
                                      for _vx in Vk_G]) > Bk_u:
                #print("Not able to add any vertex")
                no_user_to_Add = True
        else:
            no_user_to_Add = True

        ############### Check for no tag to add ##################
        if len(sorted_1000tags) > 0:
            if total_tag_cost + min(
                [cost_per_mtag[_vx] for _vx in sorted_1000tags]) > Bk_t:
                #print("Not able to add any vertex")
                no_tag_to_Add = True
        else:
            no_tag_to_Add = True

        ####### utilize left out tag budget  ####
        if (not no_tag_to_Add) and (no_user_to_Add):
            for tag in sorted_1000tags:
                if cost_per_mtag[
                        tag] + total_tag_cost < Bk_t and tag not in T_k:
                    total_tag_cost = total_tag_cost + cost_per_mtag[tag]
                    T_k.append(tag)
            no_tag_to_Add = True

        ###### utilize left out user budget  #####
        if (not no_user_to_Add) and (no_tag_to_Add):
            node_outdeg_list = sorted(list(DG_prime.out_degree(Vk_G)),
                                      key=lambda x: x[1],
                                      reverse=True)
            Vk_G = [
                node_outdeg_list[node][0]
                for node in range(len(node_outdeg_list))
            ]

            for user in Vk_G:
                if cost_per_user[
                        user] + total_user_cost < Bk_u and user not in S_k:
                    total_user_cost = total_user_cost + cost_per_user[user]
                    S_k.append(user)
            no_user_to_Add = True

        if no_user_to_Add and no_tag_to_Add:
            break

    rtb_S = Bk_u - total_user_cost
    rtb_T = Bk_t - total_tag_cost

    return S_k, T_k, rtb_S, rtb_T
Beispiel #4
0
def seed_select_per_community_prun_2(DG, All_MIIA_DG, B_k, Vk_G, cost_per_user,
                                     weight):
    l = 2
    S_k = []
    total_user_cost = 0.0
    Vk_G_prun = Vk_G.copy()

    while B_k - total_user_cost > 0:
        ### pruning
        prunned_vertex = [[_vx, cost_per_user[_vx], DG.out_degree[_vx]]
                          for _vx in Vk_G_prun if DG.out_degree[_vx] > 0]
        prunned_vertex = prunned_vertex[:200]
        Vk_G_prun = [item[0] for item in prunned_vertex]
        for idx in range(len(Vk_G_prun)):
            for _nvx in list(DG.predecessors(Vk_G_prun[idx])):
                if _nvx in S_k:
                    prunned_vertex[idx][2] = prunned_vertex[idx][2] - 1

        prunned_vertex = sorted(prunned_vertex,
                                key=lambda x: float(x[2]) / float(x[1]))
        Vk_G_prun = [item[0] for item in prunned_vertex]

        max_val = 0.0
        max_u = 0
        sigma_Sk = Expected_Influence(DG, S_k, All_MIIA_DG, l, weight)
        #print("**********************************************",sigma_Sk)
        #### remove user whose cost does not support within the budget
        Vk_G_prun = [
            _vx for _vx in Vk_G_prun
            if cost_per_user[_vx] <= B_k - total_user_cost
        ]

        for u in Vk_G_prun:
            if u in S_k:
                continue
            new_S = S_k.copy()
            new_S.append(u)
            sigma_Sk_u = Expected_Influence(DG, new_S, All_MIIA_DG, l, weight)
            new_val = (sigma_Sk_u - sigma_Sk) / cost_per_user[u]
            #print("Old EI",sigma_Sk, "vertex", u, "new EI", sigma_Sk_u, "cost:", cost_per_user[u],\
            #     "new val:", new_val, "max_val:", max_val, "current Max u:", max_u )

            if new_val > max_val:
                max_val = new_val
                max_u = u

        if max_u > 0:
            if total_user_cost + cost_per_user[max_u] <= B_k:
                total_user_cost = total_user_cost + cost_per_user[max_u]
                S_k.append(max_u)
                Vk_G_prun.remove(max_u)
                #print("vertex added:", max_u)

            else:
                #print("Not able to add maximum:", max_u)
                Vk_G_prun.remove(max_u)
        ##### break : if no vertex can be added
        #check_flag = [ cost_per_user[_vx] + total_user_cost - B_k for _vx in Vk_G]
        #flag = all(item > 0 for item in check_flag)
        print(S_k)
        if len(Vk_G_prun) > 0:
            if total_user_cost + min([cost_per_user[_vx]
                                      for _vx in Vk_G_prun]) > B_k:
                #print("Not able to add any vertex")
                break
        else:
            break

    return S_k, (B_k - total_user_cost
                 )  ############################################# change
Beispiel #5
0
        DG_prime_instance[edge[0]][edge[1]]['tag_accum_prob_weight'] = (
            1 - np.prod(p_vec))

    for edge in DG_prime.edges():
        if DG_prime_instance[edge[0]][edge[1]]['tag_accum_prob_weight'] <= 0:
            DG_prime_instance.remove_edge(edge[0], edge[1])

    print("Number of edges:", DG_prime_instance.number_of_edges())

    All_MIIA_DG = {}
    for t in DG_prime.nodes():
        #print(t)
        All_MIIA_DG[t] = Maximum_Influence_In_Arborescence(DG_prime_instance, t, \
                                                           theta= 0.1, weight = 'tag_accum_prob_weight')

    EI_budget[i] = Expected_Influence(DG_prime_instance, Seed_Set[i], All_MIIA_DG,\
                   l=2, weight = 'tag_accum_prob_weight')

    end_ts = time.time()
    print("Expected Influence:", EI_budget[i])
    result.append([EI_budget[i], len(Seed_Set[i]), end_ts - start_ts])
    #EI_setting_budget['count_prob']['RN+RT'].append(EI_budget[i])

EI_setting_budget['count_prob'] = result
result = []

print(
    "*****************************************************************************************************"
)
print(
    "***********      Influence with (2) trivalency prob setting                **********************"
)