def evaluate(self, bi, wallet_distribution_type, sample_seed_set, ss_time): eva_start_time = time.time() ini = Initialization(self.dataset_name, self.product_name, wallet_distribution_type) seed_cost_dict = ini.constructSeedCostDict(self.seed_cost_option) graph_dict = ini.constructGraphDict(self.cascade_model) product_list = ini.constructProductList()[0] num_product = len(product_list) wallet_dict = ini.constructWalletDict() total_cost = sum(seed_cost_dict[k][i] for i in seed_cost_dict[0] for k in range(num_product)) total_budget = round(total_cost / 2 ** bi, 4) eva = Evaluation(graph_dict, product_list, wallet_dict) print('@ evaluation @ ' + self.new_dataset_name + '_' + self.cascade_model + '_' + self.seed_cost_option + '\t' + self.model_name + '\t' + wallet_distribution_type + '_' + self.new_product_name + '_bi' + str(bi)) sample_pnn_k = [0.0 for _ in range(num_product)] seed_diffusion_dict_k = {(k, s): 0 for k in range(num_product) for s in sample_seed_set[k]} for _ in range(self.eva_monte_carlo): pnn_k_list, seed_diffusion_dict = eva.getSeedSetProfit(sample_seed_set) sample_pnn_k = [(pnn_k + sample_pnn_k) for pnn_k, sample_pnn_k in zip(pnn_k_list, sample_pnn_k)] for seed_diffusion_flag in seed_diffusion_dict: seed_diffusion_dict_k[seed_diffusion_flag] += seed_diffusion_dict[seed_diffusion_flag] sample_pnn_k = [round(sample_pnn_k / self.eva_monte_carlo, 4) for sample_pnn_k in sample_pnn_k] sample_pro_k = [round(sample_pnn_k[k] * product_list[k][0], 4) for k in range(num_product)] sample_sn_k = [len(sample_sn_k) for sample_sn_k in sample_seed_set] sample_bud_k = [round(sum(seed_cost_dict[k][i] for i in sample_seed_set[k]), 4) for k in range(num_product)] sample_bud = round(sum(sample_bud_k), 4) sample_pro = round(sum(sample_pro_k), 4) seed_diffusion_list = [(seed_diffusion_flag, round(seed_diffusion_dict_k[seed_diffusion_flag] / self.eva_monte_carlo, 4)) for seed_diffusion_flag in seed_diffusion_dict_k] seed_diffusion_list = [(round(sd_item[1] * product_list[sd_item[0][0]][0], 4), sd_item[0], sd_item[1]) for sd_item in seed_diffusion_list] seed_diffusion_list = sorted(seed_diffusion_list, reverse=True) result = [sample_pro, sample_bud, sample_sn_k, sample_pnn_k, sample_pro_k, sample_bud_k, sample_seed_set] print('eva_time = ' + str(round(time.time() - eva_start_time, 2)) + 'sec') print(result) print('------------------------------------------') path0 = 'result/' + self.new_dataset_name + '_' + self.cascade_model + '_' + self.seed_cost_option if not os.path.isdir(path0): os.mkdir(path0) path = path0 + '/' + wallet_distribution_type + '_' + self.new_product_name + '_bi' + str(bi) if not os.path.isdir(path): os.mkdir(path) result_name = path + '/' + self.model_name + '.txt' fw = open(result_name, 'w') fw.write(self.new_dataset_name + '_' + self.cascade_model + '_' + self.seed_cost_option + '\t' + self.model_name + '\t' + wallet_distribution_type + '_' + self.new_product_name + '_bi' + str(bi) + '\n' + 'budget_limit = ' + str(total_budget) + '\n' + 'time = ' + str(ss_time) + '\n\n' + 'profit = ' + str(sample_pro) + '\n' + 'budget = ' + str(sample_bud) + '\n') fw.write('\nprofit_ratio = ') for kk in range(num_product): fw.write(str(sample_pro_k[kk]) + '\t') fw.write('\nbudget_ratio = ') for kk in range(num_product): fw.write(str(sample_bud_k[kk]) + '\t') fw.write('\nseed_number = ') for kk in range(num_product): fw.write(str(sample_sn_k[kk]) + '\t') fw.write('\ncustomer_number = ') for kk in range(num_product): fw.write(str(sample_pnn_k[kk]) + '\t') fw.write('\n\n') fw.write(str(sample_seed_set)) for sd_item in seed_diffusion_list: fw.write('\n' + str(sd_item[1]) + '\t' + str(sd_item[0]) + '\t' + str(sd_item[2])) fw.close()
dataset_name = 'email' * (data_setting == 1) + 'dnc_email' * (data_setting == 2) + \ 'email_Eu_core' * (data_setting == 3) + 'NetHEPT' * (data_setting == 4) new_dataset_name = 'email' * (data_setting == 1) + 'dnc' * (data_setting == 2) + \ 'Eu' * (data_setting == 3) + 'Net' * (data_setting == 4) seed_cost_option = 'dp' * (sc_option == 1) + 'd' * (sc_option == 2) + 'p' * ( sc_option == 3) cascade_model = 'ic' * (cm == 1) + 'wc' * (cm == 2) product_name = 'item_lphc' * (prod_setting == 1) + 'item_hplc' * (prod_setting == 2) new_product_name = 'lphc' * (prod_setting == 1) + 'hplc' * (prod_setting == 2) wallet_distribution_type = 'm50e25' * (wd == 1) + 'm99e96' * ( wd == 2) + 'm66e34' * (wd == 3) ini = Initialization(dataset_name, product_name, wallet_distribution_type) seed_cost_dict = ini.constructSeedCostDict(seed_cost_option) wallet_dict = ini.constructWalletDict() num_node = len(wallet_dict) graph_dict = ini.constructGraphDict(cascade_model) product_list, product_weight_list = ini.constructProductList() ssmioa_model = SeedSelectionMIOA(graph_dict, seed_cost_dict, product_list, product_weight_list) seed_mioa_dict = [{} for _ in range(num_product)] mioa_dict = ssmioa_model.generateMIOA() if epw_flag: mioa_dict = ssmioa_model.updateMIOAEPW(mioa_dict) celf_heap = [(round((sum(mioa_dict[k][i][j][0] for j in mioa_dict[k][i]) * product_list[k][0]) * (1.0 if epw_flag else product_weight_list[k]), 4), k, i, 0) for k in range(num_product) for i in mioa_dict[k]]
def evaluate(self, bi, wallet_key, sample_seed_set, ss_time): ss_time = 0.001 if ss_time == 0.0 else ss_time eva_start_time = time.time() ini = Initialization(self.data_key, self.prod_key, self.cas_key, wallet_key) seed_cost_dict = ini.constructSeedCostDict() graph_dict = ini.constructGraphDict() product_list = ini.constructProductList()[0] num_product = len(product_list) wallet_dict = ini.constructWalletDict() total_cost = sum(seed_cost_dict[i] for i in seed_cost_dict) total_budget = round(total_cost / 2 ** bi, 4) eva = Evaluation(graph_dict, product_list, wallet_dict) print('@ evaluation @ ' + self.data_name + '_' + self.cas_name + '\t' + self.model_name + '\t' + wallet_distribution_type_dict[wallet_key] + '_' + self.prod_name + '_bi' + str(bi)) for times in range(self.times): model_name = self.model_name + '_' + str(times) if self.times == 10 else self.model_name sample_pnn_k, seed_diffusion_dict_k = eva.getSeedSetProfit(sample_seed_set) sample_pro_k = [round(sample_pnn_k[k] * product_list[k][0], 4) for k in range(num_product)] sample_sn_k = [len(sample_sn_k) for sample_sn_k in sample_seed_set] sample_bud_k = [round(sum(seed_cost_dict[i] for i in sample_seed_set[k]), 4) for k in range(num_product)] sample_bud = round(sum(sample_bud_k), 4) sample_pro = round(sum(sample_pro_k), 4) seed_diffusion_list = [(seed_diffusion_flag, seed_diffusion_dict_k[seed_diffusion_flag]) for seed_diffusion_flag in seed_diffusion_dict_k] seed_diffusion_list = [(round(sd_item[1] * product_list[sd_item[0][0]][0], 4), sd_item[0], sd_item[1]) for sd_item in seed_diffusion_list] seed_diffusion_list = sorted(seed_diffusion_list, reverse=True) result = [sample_pro, sample_bud, sample_sn_k, sample_pnn_k, sample_pro_k, sample_bud_k, sample_seed_set] print('eva_time = ' + str(round(time.time() - eva_start_time, 2)) + 'sec') print(result) print('------------------------------------------') path0 = 'result/' + self.data_name + '_' + self.cas_name if not os.path.isdir(path0): os.mkdir(path0) path = path0 + '/' + wallet_distribution_type_dict[wallet_key] + '_' + self.prod_name + '_bi' + str(bi) if not os.path.isdir(path): os.mkdir(path) result_name = path + '/' + model_name + '.txt' fw = open(result_name, 'w') fw.write(self.data_name + '_' + self.cas_name + '\t' + model_name + '\t' + wallet_distribution_type_dict[wallet_key] + '_' + self.prod_name + '_bi' + str(bi) + '\n' + 'budget_limit = ' + str(total_budget) + '\n' + 'time = ' + str(ss_time) + '\n\n' + 'profit = ' + str(sample_pro) + '\n' + 'budget = ' + str(sample_bud) + '\n') fw.write('\nprofit_ratio = ') for kk in range(num_product): fw.write(str(sample_pro_k[kk]) + '\t') fw.write('\nbudget_ratio = ') for kk in range(num_product): fw.write(str(sample_bud_k[kk]) + '\t') fw.write('\nseed_number = ') for kk in range(num_product): fw.write(str(sample_sn_k[kk]) + '\t') fw.write('\ncustomer_number = ') for kk in range(num_product): fw.write(str(sample_pnn_k[kk]) + '\t') fw.write('\n\n') fw.write(str(sample_seed_set)) for sd_item in seed_diffusion_list: fw.write('\n' + str(sd_item[1]) + '\t' + str(sd_item[0]) + '\t' + str(sd_item[2])) fw.close()
wallet_distribution_type = 'm50e25' * ( wd == 1) + 'm99e96' * (wd == 2) + 'm66e34' * (wd == 3) r_flag = True epw_flag = True for dag_class in [1, 2]: print(new_dataset_name + '\t' + cascade_model + '\t' + new_product_name + '\t' + wallet_distribution_type + '\t' + str(dag_class)) heap_order_path = 'heap_order/' + new_dataset_name + '_' + cascade_model + '_' + new_product_name + '_' + \ wallet_distribution_type + '_' + str(dag_class) + '.txt' ini = Initialization(dataset_name, product_name, wallet_distribution_type) seed_cost_dict = ini.constructSeedCostDict() wallet_dict = ini.constructWalletDict() num_node = len(wallet_dict) graph_dict = ini.constructGraphDict(cascade_model) product_list, product_weight_list = ini.constructProductList( ) if not os.path.isfile(heap_order_path): now_time = time.time() ssmioa_model = SeedSelectionMIOA( graph_dict, product_list, product_weight_list, dag_class, r_flag, epw_flag) mioa_dict = ssmioa_model.generateMIOA() celf_heap = ssmioa_model.generateCelfHeap( mioa_dict)
def evaluate(self, bi, wallet_distribution_type, ppp, seed_set_sequence, ss_time_sequence): eva_start_time = time.time() ini = Initialization(self.dataset_name, self.product_name) iniW = IniWallet(self.dataset_name, self.product_name, wallet_distribution_type) seed_cost_dict = ini.constructSeedCostDict() graph_dict = ini.constructGraphDict(self.cascade_model) product_list = ini.constructProductList() num_product = len(product_list) wallet_dict = iniW.constructWalletDict() total_cost = sum(seed_cost_dict[k][i] for i in seed_cost_dict[0] for k in range(num_product)) total_budget = round(total_cost / 2**bi, 4) ppp_strategy = 'random' * (ppp == 1) + 'expensive' * ( ppp == 2) + 'cheap' * (ppp == 3) result = [] eva = Evaluation(graph_dict, product_list, ppp_strategy, self.wpiwp) personal_prob_dict = eva.setPersonalPurchasingProbDict(wallet_dict) for sample_count, sample_seed_set in enumerate(seed_set_sequence): print('@ ' + self.model_name + ' evaluation @ dataset_name = ' + self.dataset_name + '_' + self.cascade_model + ', product_name = ' + self.product_name + ', wd = ' + wallet_distribution_type + ', ppp = ' + ppp_strategy + ', sample_count = ' + str(sample_count)) sample_pro_k_acc, sample_pnn_k_acc = [ 0.0 for _ in range(num_product) ], [0 for _ in range(num_product)] for _ in range(self.eva_monte_carlo): pro, pro_k_list, pnn_k_list = eva.getSeedSetProfit( sample_seed_set, copy.deepcopy(wallet_dict), copy.deepcopy(personal_prob_dict)) sample_pro_k_acc = [(pro_k + sample_pro_k) for pro_k, sample_pro_k in zip( pro_k_list, sample_pro_k_acc)] sample_pnn_k_acc = [(pnn_k + sample_pnn_k) for pnn_k, sample_pnn_k in zip( pnn_k_list, sample_pnn_k_acc)] sample_pro_k_acc = [ round(sample_pro_k / self.eva_monte_carlo, 4) for sample_pro_k in sample_pro_k_acc ] sample_pnn_k_acc = [ round(sample_pnn_k / self.eva_monte_carlo, 4) for sample_pnn_k in sample_pnn_k_acc ] sample_bud_k_acc = [ round( sum(seed_cost_dict[sample_seed_set.index(sample_bud_k)][i] for i in sample_bud_k), 4) for sample_bud_k in sample_seed_set ] sample_sn_k_acc = [ len(sample_sn_k) for sample_sn_k in sample_seed_set ] sample_pro_acc = round(sum(sample_pro_k_acc), 4) sample_bud_acc = round(sum(sample_bud_k_acc), 4) result.append([ sample_pro_acc, sample_bud_acc, sample_sn_k_acc, sample_pnn_k_acc, sample_pro_k_acc, sample_bud_k_acc, sample_seed_set ]) print('eva_time = ' + str(round(time.time() - eva_start_time, 2)) + 'sec') print(result[sample_count]) print('------------------------------------------') avg_pro = round(sum(r[0] for r in result) / len(seed_set_sequence), 4) avg_bud = round(sum(r[1] for r in result) / len(seed_set_sequence), 4) avg_sn_k = [ round(sum(r[2][kk] for r in result) / len(seed_set_sequence), 4) for kk in range(num_product) ] avg_pnn_k = [ round(sum(r[3][kk] for r in result) / len(seed_set_sequence), 4) for kk in range(num_product) ] avg_pro_k = [ round(sum(r[4][kk] for r in result) / len(seed_set_sequence), 4) for kk in range(num_product) ] avg_bud_k = [ round(sum(r[5][kk] for r in result) / len(seed_set_sequence), 4) for kk in range(num_product) ] path = 'sample_result/' + \ self.model_name + '_' + wallet_distribution_type + '_ppp' + str(ppp) + '_wpiwp' * self.wpiwp if not os.path.isdir(path): os.mkdir(path) fw = open( path + '/' + self.dataset_name + '_' + self.cascade_model + '_' + self.product_name + '_bi' + str(bi) + '.txt', 'w') fw.write(self.model_name + ', ' + self.dataset_name + '_' + self.cascade_model + ', ' + self.product_name + '\n' + 'ppp = ' + str(ppp) + ', wd = ' + wallet_distribution_type + ', wpiwp = ' + str(self.wpiwp) + '\n' + 'total_budget = ' + str(total_budget) + ', sample_number = ' + str(len(seed_set_sequence)) + '\n' + 'avg_profit = ' + str(avg_pro) + ', avg_budget = ' + str(avg_bud) + '\n' + 'total_time = ' + str(round(sum(ss_time_sequence), 4)) + ', avg_time = ' + str(round(sum(ss_time_sequence) / len(ss_time_sequence), 4)) + '\n') fw.write('\nprofit_ratio =') for kk in range(num_product): fw.write(' ' + str(avg_pro_k[kk])) fw.write('\nbudget_ratio =') for kk in range(num_product): fw.write(' ' + str(avg_bud_k[kk])) fw.write('\nseed_number =') for kk in range(num_product): fw.write(' ' + str(avg_sn_k[kk])) fw.write('\ncustomer_number =') for kk in range(num_product): fw.write(' ' + str(avg_pnn_k[kk])) fw.write('\n') for t, r in enumerate(result): fw.write('\n' + str(t) + '\t' + str(round(r[0], 4)) + '\t' + str(round(r[1], 4)) + '\t' + str(r[2]) + '\t' + str(r[3]) + '\t' + str(r[4]) + '\t' + str(r[5]) + '\t' + str(r[6])) fw.close()
def evaluate(self, bi, wallet_distribution_type, sample_seed_set, ss_time): eva_start_time = time.time() ini = Initialization(self.dataset_name, self.product_name, wallet_distribution_type) seed_cost_dict = ini.constructSeedCostDict(self.seed_cost_option) graph_dict = ini.constructGraphDict(self.cascade_model) product_list = ini.constructProductList()[0] num_product = len(product_list) wallet_dict = ini.constructWalletDict() total_cost = sum(seed_cost_dict[k][i] for i in seed_cost_dict[0] for k in range(num_product)) total_budget = round(total_cost / 2 ** bi, 4) eva = Evaluation(graph_dict, product_list, wallet_dict) print('@ ' + self.model_name + ' evaluation @ dataset_name = ' + self.dataset_name + '_' + self.cascade_model + ', product_name = ' + self.product_name + ', seed_cost_option = ' + self.seed_cost_option + ', wd = ' + wallet_distribution_type) sample_pro_k, sample_pnn_k = [0.0 for _ in range(num_product)], [0 for _ in range(num_product)] for _ in range(self.eva_monte_carlo): pro_k_list, pnn_k_list = eva.getSeedSetProfit(sample_seed_set) sample_pro_k = [(pro_k + sample_pro_k) for pro_k, sample_pro_k in zip(pro_k_list, sample_pro_k)] sample_pnn_k = [(pnn_k + sample_pnn_k) for pnn_k, sample_pnn_k in zip(pnn_k_list, sample_pnn_k)] sample_pro_k = [round(sample_pro_k / self.eva_monte_carlo, 4) for sample_pro_k in sample_pro_k] sample_pnn_k = [round(sample_pnn_k / self.eva_monte_carlo, 4) for sample_pnn_k in sample_pnn_k] sample_bud_k = [round(sum(seed_cost_dict[sample_seed_set.index(sample_bud_k)][i] for i in sample_bud_k), 4) for sample_bud_k in sample_seed_set] sample_sn_k = [len(sample_sn_k) for sample_sn_k in sample_seed_set] sample_pro = round(sum(sample_pro_k), 4) sample_bud = round(sum(sample_bud_k), 4) result = [sample_pro, sample_bud, sample_sn_k, sample_pnn_k, sample_pro_k, sample_bud_k, sample_seed_set] print('eva_time = ' + str(round(time.time() - eva_start_time, 2)) + 'sec') print(result) print('------------------------------------------') path = 'result/' + self.model_name + '_' + wallet_distribution_type if not os.path.isdir(path): os.mkdir(path) fw = open(path + '/' + self.dataset_name + '_' + self.cascade_model + '_' + self.product_name + '_' + self.seed_cost_option + '_ds' * self.diff_seed_option + '_bi' + str(bi) + '.txt', 'w') fw.write(self.model_name + ', ' + wallet_distribution_type + ', ' + self.dataset_name + '_' + self.cascade_model + ', ' + self.product_name + ', seed_cost_option = ' + self.seed_cost_option + '\n\n' + 'total_budget = ' + str(total_budget) + '\n' + 'profit = ' + str(sample_pro) + '\n' + 'budget = ' + str(sample_bud) + '\n' + 'time = ' + str(ss_time) + '\n') fw.write('\nprofit_ratio =') for kk in range(num_product): fw.write(' ' + str(sample_pro_k[kk])) fw.write('\nbudget_ratio =') for kk in range(num_product): fw.write(' ' + str(sample_bud_k[kk])) fw.write('\nseed_number =') for kk in range(num_product): fw.write(' ' + str(sample_sn_k[kk])) fw.write('\ncustomer_number =') for kk in range(num_product): fw.write(' ' + str(sample_pnn_k[kk])) fw.write('\n\n') for r in result: # -- pro, bud, sn_k, pnn_k, pro_k, bud_k, seed_set -- fw.write(str(r) + '\t') fw.close()