IEM信息曝光最大化评估算法(蒙特卡洛算法)

import argparse
import random
import numpy as np

# def find_all_adjust(aj, i):
#     ls = []
#     for index, j in enumerate(aj[i]):
#         if j > 0:
#             ls.append(index)
#     return ls

# def find_all_adjust(aj, i):
#     ls = np.array([])
#     for index, j in enumerate(aj[i]):
#         if j > 0:
#             ls = np.append(ls, index)
#     return ls

def find_all_adjust(aj, i):
    ls = []
    for index, j in enumerate(aj[i]):
        if j > 0 :
            ls.append(index)
    return np.array(ls)



#从e中找到i节点的所有邻居节点
def r(s1, aj, i1):
    #初始化曝光到的点集
    all_active_list = all_exposed_list = current_active_stack = np.union1d(i1, s1).astype(int)

    while(len(current_active_stack) != 0): #直到没有激活的点产生,判断结束
        #找到当前所有激活节点的所有邻居节点
        temp = current_active_stack
        current_active_stack = np.array([], dtype=int)
        #i为当前层激活的点集
        for i in temp:
            #当前i节点的所有邻居且没有被激活的
            i_adjust_all = np.setdiff1d(find_all_adjust(aj, i), all_active_list)
            #尝试激活这些曝光到的节点进行便利
            for j in i_adjust_all:
                #更新总曝光节点
                all_exposed_list = np.append(all_exposed_list, j)
                #蒙特卡洛随机抽样激活
                if(aj[i][j] >= random.random()): # 激活成功
                    current_active_stack = np.append(current_active_stack, j)
                    all_active_list = np.append(all_active_list, j)

    return all_exposed_list

def fai(v, r1, r2):
    return len(np.setdiff1d(v, np.setxor1d(r1, r2)))

def run(n = r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\dataset1',
        i = r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\seed',
        b = r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\seed_balanced',
        o = r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\ob',
        k = 2,
        ):
    #读取社会网络,e为边集

    with open(n, 'r') as f:
        data = f.readlines()
        for line in data:
            # print(line)
            line = list(line.replace('\r', '').replace('\n', '').replace('\t', '').split(' '))
            if(len(line) == 2):
                m = int(line[0])
                v = np.array(range(m))
                aj1 = np.zeros([m, m], dtype = float)
                aj2 = np.zeros([m, m], dtype = float)

                # print(line)
            if(len(line) == 4):
                ii = int(line[0])
                j = int(line[1])
                aj1[ii][j] = float(line[2])
                aj2[ii][j] = float(line[3])
        f.close()
    # print(aj1.shape, aj2.shape)
    # print(e_num, v_num, len(e), v)

    # 读取i1,i2集
    i_list = []
    with open(i, 'r') as f:
        data = f.readlines()
        for line in data:
            # print(line)
            line = list(line.replace('\r', '').replace('\n', '').replace('\t', '').split(' '))
            if(len(line) == 2):
                i1_num = int(line[0])
                i2_num = int(line[1])

            if(len(line) == 1):

                i_list.append(int(line[0]))
        f.close()
    i1 = np.array(i_list[: i1_num])
    i2 = np.array(i_list[i1_num:])
    # print(i1.shape,i2)
    #
    # # 读取s1,s2集
    s = []
    with open(b, 'r') as f:
        data = f.readlines()
        for line in data:
            # print(line)
            line = list(line.replace('\r', '').replace('\n', '').replace('\t', '').split(' '))
            if (len(line) == 2):
                line[0] = int(line[0])
                line[1] = int(line[1])
                s1_num = line[0]
                s2_num = line[1]
                # print(line)
            if (len(line) == 1):
                line[0] = int(line[0])
                # print(type(line[2]))
                s.append(line[0])
        f.close()
    s1 = np.array(s[: s1_num])
    s2 = np.array(s[s1_num: ])
    # print(s1.shape, s2)

    find_all_adjust(aj1, 3)

    # #进行m次蒙特卡洛随机筛选
    total = 0
    m = 10
    for i in range(m):
        r1 = r(s1,aj1,i1)
        r2 = r(s2,aj2,i2)
        result = fai(v, r1, r2)
        total += result
        # print("result is", result)
        # print("result is", len(result))
    total /= m
    with open(o,'w')as f:
        f.write(str(total))
        f.close()
    return




def parse_opt():
    parser = argparse.ArgumentParser()

    parser.add_argument('-n', type=str, default=r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\dataset1', help='The absolute path of the social network file')
    parser.add_argument('-i', type=str, default=r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\seed', help='The absolute path of the two campaigns initial seed set')
    parser.add_argument('-b', type=str, default=r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\seed_balanced', help='The absolute path of the two campaigns blanced seed set')
    parser.add_argument('-o', type=str, default=r'C:\Users\ASUS\Desktop\project1\Testcase (Student)\Evaluator\map1\output.txt', help='The absolute path of the output file of the objective value of the balanced seed set')
    parser.add_argument('-k', nargs='+', type=int, help='The positive integer budget')

    opt = parser.parse_args()
    return opt

def main(opt):
    run(**vars(opt))



if __name__ == '__main__':
    opt = parse_opt()
    main(opt)

你可能感兴趣的:(算法)