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()
## data_name: (str) the dataset ## ## product_name: (str) the product ## ## total_budget: (int) the seed budget ## ## num_price: (int) the kinds of generated price ## ## num_ratio: (int) the kinds of generated ratio ## ## ctrlexpect: (str) the controller for which expected function is used ## data_name = "email" product_name = "prod_ratio_similar_n3p1" total_budget = 1 num_price, num_ratio = 3, 1 num_product = num_price * num_ratio ctrlexpect = "p" ini = Initialization(data_name) graph_dict = ini.constructGraphDict() seedcost_dict = ini.constructSeedCostDict() ### product_list: (list) [profit, cost, price] product_list = [] with open("product/" + product_name + ".txt") as f: for line in f: (p, c, r, pr) = line.split() product_list.append( [float(p), float(c), round(float(p) + float(c), 2)]) ### current_profit: (float4) the profit currently ### current_budget: (float4) the used budget currently ### seed_set: (list) the set of seeds for all products ### seed_set[k]: (set) the set of seeds for k-product
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()