forked from FFrankyy/FINDER
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testReal.py
87 lines (76 loc) · 3.82 KB
/
testReal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys,os
sys.path.append(os.path.dirname(__file__) + os.sep + '../')
from FINDER import FINDER
import numpy as np
from tqdm import tqdm
import time
import networkx as nx
import pandas as pd
import pickle as cp
import graph
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_ND/models/'
model_file_ckpt = MODEL_FILE_CKPT
model_file = model_file_path + model_file_ckpt
## save_dir
save_dir = '../results/FINDER_ND/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 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():
model_file_ckpt = 'nrange_30_50_iter_78000.ckpt'
GetSolution(0.01, model_file_ckpt)
EvaluateSolution(0.01, model_file_ckpt, 0)
if __name__=="__main__":
main()