def EvaluateSolution(STEPRATIO, MODEL_FILE_CKPT, STRTEGYID): ####################################################################################################################### ##................................................Evaluate Solution..................................................... dqn = FINDER() data_test_path = '../data/real/' # data_test_name = ['Crime', 'HI-II-14', 'Digg', 'Enron', 'Gnutella31', 'Epinions', 'Facebook', 'Youtube', 'Flickr'] data_test_name = ['Crime', 'HI-II-14'] save_dir = '../results/FINDER_ND/real/StepRatio_%.4f/' % STEPRATIO ## begin computing... df = pd.DataFrame(np.arange(2 * len(data_test_name)).reshape( (2, len(data_test_name))), index=['solution', 'time'], columns=data_test_name) for i in range(len(data_test_name)): print('\nEvaluating dataset %s' % data_test_name[i]) data_test = data_test_path + data_test_name[i] + '.txt' solution = save_dir + data_test_name[i] + '.txt' t1 = time.time() # strategyID: 0:no insert; 1:count; 2:rank; 3:multiply ################################## modify to choose which strategy to evaluate strategyID = STRTEGYID score, MaxCCList = dqn.EvaluateSol(data_test, solution, strategyID, reInsertStep=0.001) t2 = time.time() df.iloc[0, i] = score df.iloc[1, i] = t2 - t1 result_file = save_dir + '/MaxCCList_Strategy_' + data_test_name[ i] + '.txt' with open(result_file, 'w') as f_out: for j in range(len(MaxCCList)): f_out.write('%.8f\n' % MaxCCList[j]) print('Data: %s, score:%.6f' % (data_test_name[i], score)) df.to_csv(save_dir + '/solution_score.csv', encoding='utf-8', index=False)
def main(): dqn = FINDER() graph_types = ['barabasi_albert'] cost_types = ['uniform'] heur_types = ['HDA', 'HBA', 'HCA', 'HPRA'] file_path = '../../results/FINDER_CN/synthetic' if not os.path.exists(file_path): os.makedirs(file_path) for graph_type in graph_types: for cost_type in cost_types: for heur in heur_types: data_test_path = '../../data/synthetic/{}/{}/'.format( graph_type, cost_type) data_test_name = [ '30-50', '50-100', '100-200', '200-300', '300-400', '400-500' ] with open( '%s/%s_%s_%s_score.txt' % (file_path, graph_type, heur, cost_type), 'w') as fout: for i in tqdm(range(len(data_test_name))): data_test = data_test_path + data_test_name[i] score_mean, score_std, time_mean, time_std = dqn.EvaluateHeuristics( data_test, heur) fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) fout.write('%.2f±%.2f\n' % (time_mean, time_std)) fout.flush() print(100 * '#') print('data_test_{} has been tested!'.format( data_test_name[i]))
def GetSolution(STEPRATIO, MODEL_FILE_CKPT): ###################################################################################################################### ##................................................Get Solution (model)..................................................... dqn = FINDER() data_test_path = '../data/real/' # data_test_name = ['Crime','HI-II-14','Digg','Enron','Gnutella31','Epinions','Facebook','Youtube','Flickr'] data_test_name = ['Crime', 'HI-II-14'] model_file_path = './FINDER_CN/models/' model_file_ckpt = MODEL_FILE_CKPT model_file = model_file_path + model_file_ckpt ## save_dir save_dir = '../results/FINDER_CN/real' if not os.path.exists(save_dir): os.mkdir(save_dir) ## begin computing... print('The best model is :%s' % (model_file)) dqn.LoadModel(model_file) df = pd.DataFrame(np.arange(1 * len(data_test_name)).reshape( (1, len(data_test_name))), index=['time'], columns=data_test_name) #################################### modify to choose which stepRatio to get the solution stepRatio = STEPRATIO for j in range(len(data_test_name)): print('\nTesting dataset %s' % data_test_name[j]) data_test = data_test_path + data_test_name[j] + '.txt' solution, time = dqn.EvaluateRealData(model_file, data_test, save_dir, stepRatio) df.iloc[0, j] = time print('Data:%s, time:%.2f' % (data_test_name[j], time)) save_dir_local = save_dir + '/StepRatio_%.4f' % stepRatio if not os.path.exists(save_dir_local): os.mkdir(save_dir_local) df.to_csv(save_dir_local + '/sol_time.csv', encoding='utf-8', index=False)
def main(): dqn = FINDER() data_test_path = 'data/synthetic/' # data_list = ['degree_cost','uniform_cost','random_cost'] data_list = ['degree_cost'] data_test_name = ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] # data_test_name = [ '200-300'] model_file = 'models/nrange_30_50_iter_78000.ckpt' file_path = 'results/FINDER_ND/synthetic' if not os.path.exists('results/FINDER_ND/'): os.mkdir('results/FINDER_ND/') if not os.path.exists('results/FINDER_ND/synthetic'): os.mkdir('results/FINDER_ND/synthetic') # fd = open( "f.txt", 'xt' ) for data in data_list: with open('%s/result_1_%s.csv'%(file_path,data), 'w') as fout: for i in range(len(data_test_name)): data_test = data_test_path + data+'/' +data_test_name[i] le, _, ge , ans= dqn.getGraphAndSol(data_test, model_file) print("%s better case %.2f worse case %.2f"%(data_test_name[i],1.0*ge/100,1.0*le/100)) # fd.write("%s better case %.2f worse case %.2f\n"%(data_test_name[i],1.0*ge/100,1.0*le/100)) for line in ans: fout.write("%d,%d,\n"%(line[0],line[1])) fout.flush()
def GetSolution(STEPRATIO, MODEL_FILE): ###################################################################################################################### ##................................................Get Solution (model)..................................................... dqn = FINDER() ## data_test data_test_path = '../../data/real/' # data_test_name = ['Crime', 'HI-II-14', 'Digg', 'Enron', 'Gnutella31', 'Epinions', 'Facebook', 'Youtube', 'Flickr'] data_test_name = ['Crime', 'HI-II-14', 'Facebook'] data_test_costType = ['degree', 'random'] model_file_path = './models/Model_barabasi_albert/' model_file_ckpt = MODEL_FILE model_file = model_file_path + model_file_ckpt ## save_dir save_dir = '../../results/FINDER_ND_cost/real/' if not os.path.exists(save_dir): os.mkdir(save_dir) save_dir_degree = save_dir + '/Data_degree' save_dir_random = save_dir + '/Data_random' if not os.path.exists(save_dir_degree): os.mkdir(save_dir_degree) if not os.path.exists(save_dir_random): os.mkdir(save_dir_random) ## begin computing... print('The best model is :%s' % (model_file)) dqn.LoadModel(model_file) for costType in data_test_costType: df = pd.DataFrame(np.arange(2 * len(data_test_name)).reshape( (-1, len(data_test_name))), index=['time', 'score'], columns=data_test_name) #################################### modify to choose which stepRatio to get the solution stepRatio = STEPRATIO for j in range(len(data_test_name)): print('Testing dataset %s' % data_test_name[j]) data_test = data_test_path + data_test_name[ j] + '_' + costType + '.gml' if costType == 'degree': solution, time, robustness = dqn.EvaluateRealData( model_file, data_test, save_dir_degree, stepRatio) elif costType == 'random': solution, time, robustness = dqn.EvaluateRealData( model_file, data_test, save_dir_random, stepRatio) df.iloc[0, j] = time df.iloc[1, j] = robustness print('Data:%s, cost:%s, time:%.4f, score:%.4f' % (data_test_name[j], costType, time, robustness)) if costType == 'degree': save_dir_local = save_dir_degree + '/StepRatio_%.4f' % stepRatio elif costType == 'random': save_dir_local = save_dir_random + '/StepRatio_%.4f' % stepRatio if not os.path.exists(save_dir_local): os.mkdir(save_dir_local) df.to_csv(save_dir_local + '/solution_%s_time.csv' % costType, encoding='utf-8', index=False) print('model has been tested!')
def main(): dqn = FINDER() graph_types = ['barabasi_albert', 'erdos_renyi', 'small-world'] cost_types = ['degree', 'random'] file_path = '../../results/FINDER_ND_cost/synthetic' if not os.path.exists(file_path): os.makedirs(file_path) for graph_type in graph_types: for cost_type in cost_types: data_test_path = '../../data/synthetic/{}/{}/'.format( graph_type, cost_type) data_test_name = [ '30-50', '50-100', '100-200', '200-300', '300-400', '400-500' ] model_file = f'./models/Model_{graph_type}/nrange_30_50_iter_37500_{graph_type}_{cost_type}.ckpt' with open( '%s/%s_%s_score.txt' % (file_path, graph_type, cost_type), 'w') as fout: for i in tqdm(range(len(data_test_name))): data_test = data_test_path + data_test_name[i] score_mean, score_std, time_mean, time_std = dqn.Evaluate( data_test, model_file) fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) fout.flush() print(100 * '#') print('data_test_{} has been tested!'.format( data_test_name[i]))
def main(): dqn = FINDER() data_test_path = "data/final/" graph_type = ["BA4","BA6","ER1","ER2","TR"] # graph_type = ["TR"] data_test_name = ["30-50", "50-100", "100-200", "200-300", "300-400", "400-500"] data_test = [30,50,100,200,300,400] model_file = "models/nrange_30_50_iter_78000.ckpt" file_path = "results/" # for data in graph_type: # if not os.path.exists(file_path+data): # os.mkdir(file_path+data) for data in graph_type: with open("%s/%s_c.csv"%(file_path,data), "w") as fout_c: with open("%s/%s_t.csv"%(file_path,data), "w") as fout_t: for i in range(len(data_test_name)): data_test_f = data_test_path + data+"/" +data_test_name[i] low, upper, avc , avt= dqn.getGraphAndSol(data_test_f, model_file) fout_c.write("%d,"%(data_test[i])) for j in range(len(low)): fout_c.write("%f,%f,%f,"%(avc[j],low[j]-avc[j],upper[j]-avc[j])) fout_c.write("\n") fout_c.flush() fout_t.write("%d,"%(data_test[i])) for j in range(len(avt)): fout_t.write("%f,"%(avt[j])) fout_t.write("\n") fout_t.flush()
def EvaluateSolution(STEPRATIO, STRTEGYID): ####################################################################################################################### ##................................................Evaluate dqn = FINDER() ## data_test data_test_path = '../data/real/Cost/' # data_test_name = ['Crime', 'HI-II-14', 'Digg', 'Enron', 'Gnutella31', 'Epinions', 'Facebook', 'Youtube', 'Flickr'] data_test_name = ['Gnutella31'] data_test_costType = ['degree', 'random'] ## save_dir save_dir_degree = '../results/FINDER_CN_cost/real/Data_degree/StepRatio_%.4f/' % STEPRATIO save_dir_random = '../results/FINDER_CN_cost/real/Data_random/StepRatio_%.4f/' % STEPRATIO ## begin computing... for costType in data_test_costType: df = pd.DataFrame(np.arange(2 * len(data_test_name)).reshape((2, len(data_test_name))), index=['solution', 'time'], columns=data_test_name) for i in range(len(data_test_name)): print('Evaluating dataset %s' % data_test_name[i]) data_test = data_test_path + data_test_name[i] + '_' + costType + '.gml' if costType == 'degree': solution = save_dir_degree + data_test_name[i] + '_degree.txt' elif costType == 'random': solution = save_dir_random + data_test_name[i] + '_random.txt' t1 = time.time() # strategyID: 0:no insert; 1:count; 2:rank; 3:multiply ################################## modify to choose which strategy to evaluate strategyID = STRTEGYID score, MaxCCList, solution = dqn.EvaluateSol(data_test, solution, strategyID, reInsertStep=20) t2 = time.time() df.iloc[0, i] = score df.iloc[1, i] = t2 - t1 if costType == 'degree': result_file = save_dir_degree + '/MaxCCList__Strategy_' + data_test_name[i] + '.txt' elif costType == 'random': result_file = save_dir_random + '/MaxCCList_Strategy_' + data_test_name[i] + '.txt' with open(result_file, 'w') as f_out: for j in range(len(MaxCCList)): f_out.write('%.8f\n' % MaxCCList[j]) print('Data:%s, score:%.6f!' % (data_test_name[i], score)) if costType == 'degree': df.to_csv(save_dir_degree + '/solution_%s_score.csv' % (costType), encoding='utf-8', index=False) elif costType == 'random': df.to_csv(save_dir_random + '/solution_%s_score.csv' % (costType), encoding='utf-8', index=False)
def GetSolution(STEPRATIO, MODEL_FILE_CKPT, method='HDA'): ###################################################################################################################### ##................................................Get Solution (model)..................................................... dqn = FINDER() data_test_path = '../../data/real/' # data_test_name = ['Crime','HI-II-14','Digg','Enron','Gnutella31','Epinions','Facebook','Youtube','Flickr'] data_test_name = ['Facebook', 'HI-II-14', 'Crime'] # , 'US_airports_unweighted'] # model_file_path = './models/Model_barabasi_albert/' # model_file_ckpt = MODEL_FILE_CKPT # model_file = model_file_path + model_file_ckpt ## save_dir save_dir = '../../results/FINDER_CN/real' if not os.path.exists(save_dir): os.mkdir(save_dir) ## begin computing... # print ('The best model is :%s'%(model_file)) # dqn.LoadModel(model_file) df = pd.DataFrame(np.arange(2 * len(data_test_name)).reshape( (-1, len(data_test_name))), index=['time', 'score'], columns=data_test_name) #################################### modify to choose which stepRatio to get the solution stepRatio = STEPRATIO for j in range(len(data_test_name)): print('\nTesting dataset %s' % data_test_name[j]) data_test = data_test_path + data_test_name[j] + '.txt' solution, time, robustness = dqn.HeurRealData(data_test, save_dir, stepRatio, method='HDA') df.iloc[0, j] = time df.iloc[1, j] = robustness print('Data:%s, time:%.2f, score:%.2f' % (data_test_name[j], time, robustness)) save_dir_local = save_dir + '/StepRatio_%.4f' % stepRatio if not os.path.exists(save_dir_local): os.mkdir(save_dir_local) df.to_csv(save_dir_local + '/sol_time_' + method + '.csv', encoding='utf-8', index=False)
def main(): dqn = FINDER() data_test_path = '../data/synthetic/uniform_cost/' # data_test_name = ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] data_test_name = ['30-50', '50-100'] model_file = './FINDER_ND/models/nrange_30_50_iter_78000.ckpt' file_path = '../results/FINDER_ND/synthetic' if not os.path.exists('../results/FINDER_ND'): os.mkdir('../results/FINDER_ND') if not os.path.exists('../results/FINDER_ND/synthetic'): os.mkdir('../results/FINDER_ND/synthetic') with open('%s/result.txt' % file_path, 'w') as fout: for i in tqdm(range(len(data_test_name))): data_test = data_test_path + data_test_name[i] score_mean, score_std, time_mean, time_std = dqn.Evaluate( data_test, model_file) fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) fout.flush() print('\ndata_test_%s has been tested!' % data_test_name[i])
def main(): dqn = FINDER() graph_types = ['barabasi_albert'] cost_types = ['uniform'] heur_types = ['RL', 'HDA', 'HBA', 'HCA', 'HPRA'] file_path = '../../plots/FINDER_CN/synthetic' if not os.path.exists(file_path): os.makedirs(file_path) data_test_name = '50-100' # ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] for i in range(100): for graph_type in graph_types: for cost_type in cost_types: data_test_path = '../../data/synthetic/{}/{}/'.format( graph_type, cost_type) model_file = f'./models/Model_{graph_type}/nrange_30_50_iter_399000_{graph_type}.ckpt' data_test = data_test_path + data_test_name g_path = '%s/' % data_test + f'g_{i}' # could be changed for other g_i g = nx.read_gml(g_path) for heur in heur_types: dqn.Evaluate1(g, i, file_path, model_file, heur)
def main(): dqn = FINDER() cost_types = ['degree_cost', 'random_cost'] for cost in cost_types: data_test_path = '../data/synthetic/%s/' % cost # data_test_name = ['30-50', '50-100', '100-200', '200-300', '300-400', '400-500'] data_test_name = ['30-50', '50-100'] model_file = './FINDER_CN_cost/models/nrange_30_50_iter_122100.ckpt' file_path = '../results/FINDER_CN_cost/synthetic' if not os.path.exists('../results/FINDER_CN_cost'): os.mkdir('../results/FINDER_CN_cost') if not os.path.exists('../results/FINDER_CN_cost/synthetic'): os.mkdir('../results/FINDER_CN_cost/synthetic') with open('%s/%s_score.txt' % (file_path, cost), 'w') as fout: for i in tqdm(range(len(data_test_name))): data_test = data_test_path + data_test_name[i] score_mean, score_std, time_mean, time_std = dqn.Evaluate( data_test, model_file) fout.write('%.2f±%.2f,' % (score_mean * 100, score_std * 100)) fout.flush() print('data_test_%s has been tested!' % data_test_name[i])
def main(): dqn = FINDER() dqn.Train()