forked from qingyun-wu/IMBandits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_CLUB1.py
147 lines (126 loc) · 6.85 KB
/
run_CLUB1.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import time
import os
import pickle
import datetime
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from conf import *
from Tool.utilFunc import *
from BanditAlg.BanditAlgorithms import UCB1Algorithm, eGreedyAlgorithm
from BanditAlg.BanditAlgorithms_LinUCB import N_LinUCBAlgorithm, LinUCBAlgorithm
from BanditAlg.BanditAlgorithms_CLUB import CLUBAlgorithm
from BanditAlg.BanditAlgorithms_CAB import CABAlgorithm
from IC.IC import runIC, runICmodel, runICmodel_n
from IC.runIAC import weightedEp, runIAC, runIACmodel, randomEp, uniformEp
class simulateOnlineData:
def __init__(self, G, oracle, seed_size, iterations, batchSize, feature_dic, topic_list, dataset):
self.G = G
self.seed_size = seed_size
self.oracle = oracle
self.iterations = iterations
self.batchSize = batchSize
self.dataset = dataset
self.startTime = datetime.datetime.now()
self.BatchCumlateReward = {}
self.AlgReward = {}
self.result_oracle = []
self.feature_dic = feature_dic
self.topic_list = topic_list
def runAlgorithms(self, algorithms):
self.tim_ = []
for alg_name, alg in list(algorithms.items()):
self.AlgReward[alg_name] = []
self.BatchCumlateReward[alg_name] = []
self.resultRecord()
for iter_ in range(self.iterations):
TrueP = self.get_TrueP(iter_)
optS = self.oracle(self.G, self.seed_size, TrueP)
optimal_reward, live_nodes, live_edges = runICmodel_n(G, optS, TrueP)
self.result_oracle.append(optimal_reward)
print('oracle', optimal_reward)
for alg_name, alg in list(algorithms.items()):
S = alg.decide(self.topic_list[iter_])
reward, live_nodes, live_edges = runICmodel_n(G, S, TrueP)
if alg.feedback == 'node':
alg.updateParameters(S, live_nodes, self.topic_list[iter_])
elif alg.feedback == 'edge':
alg.updateParameters(S, live_nodes, live_edges, self.topic_list[iter_])
self.AlgReward[alg_name].append(reward)
self.resultRecord(iter_)
self.showResult()
def get_TrueP(self, iter_):
graph_p = nx.DiGraph()
for key in self.feature_dic:
prob = np.dot(self.feature_dic[key], self.topic_list[iter_])
graph_p.add_edge(key[0], key[1], weight=FeatureScaling*prob)
return graph_p
def resultRecord(self, iter_=None):
# if initialize
if iter_ is None:
timeRun = self.startTime.strftime('_%m_%d_%H_%M')
fileSig = '_seedsize'+str(self.seed_size) + '_iter'+str(self.iterations)+'_'+str(self.oracle.__name__)+'_'+self.dataset
self.filenameWriteReward = os.path.join(save_address, 'AccReward' + timeRun + fileSig + '.csv')
with open(self.filenameWriteReward, 'w') as f:
f.write('Time(Iteration)')
f.write(',' + ','.join( [str(alg_name) for alg_name in algorithms.keys()]))
f.write('\n')
else:
# if run in the experiment, save the results
if iter_ % self.batchSize == 0:
print("Iteration %d" % iter_, " Elapsed time", datetime.datetime.now() - self.startTime)
self.tim_.append(iter_)
for alg_name in algorithms.keys():
self.BatchCumlateReward[alg_name].append(sum(self.AlgReward[alg_name][-1:]))
with open(self.filenameWriteReward, 'a+') as f:
f.write(str(iter_))
f.write(',' + ','.join([str(self.BatchCumlateReward[alg_name][-1]) for alg_name in algorithms.keys()]))
f.write('\n')
def showResult(self):
print('average reward for oracle:', np.mean(self.result_oracle))
# plot average reward
f, axa = plt.subplots(1, sharex=True)
for alg_name in algorithms.keys():
axa.plot(self.tim_, self.BatchCumlateReward[alg_name],label = alg_name)
print('%s: %.2f' % (alg_name, np.mean(self.BatchCumlateReward[alg_name])))
axa.legend(loc='upper left',prop={'size':9})
axa.set_xlabel("Iteration")
axa.set_ylabel("Reward")
axa.set_title("Average Reward")
plt.savefig('./SimulationResults/AvgReward' + str(self.startTime.strftime('_%m_%d_%H_%M'))+'.pdf')
plt.show()
# plot accumulated reward
f, axa = plt.subplots(1, sharex=True)
for alg_name in algorithms.keys():
result = [sum(self.BatchCumlateReward[alg_name][:i]) for i in range(len(self.tim_))]
axa.plot(self.tim_, result, label = alg_name)
axa.legend(loc='upper left',prop={'size':9})
axa.set_xlabel("Iteration")
axa.set_ylabel("Reward")
axa.set_title("Accumulated Reward")
plt.savefig('./SimulationResults/AcuReward' + str(self.startTime.strftime('_%m_%d_%H_%M'))+'.pdf')
plt.show()
if __name__ == '__main__':
start = time.time()
G = pickle.load(open(graph_address, 'rb'), encoding='latin1')
feature_dic = pickle.load(open(feature_address, 'rb'), encoding='latin1')
topic_list = pickle.load(open(topic_address , "rb" ), encoding='latin1')
print('nodes:', len(G.nodes()))
print('edges:', len(G.edges()))
print('Done with Loading Feature')
print('Graph build time:', time.time() - start)
simExperiment = simulateOnlineData(G, oracle, seed_size, iterations, batchSize, feature_dic, topic_list, dataset)
algorithms = {}
# algorithms['LinUCB'] = N_LinUCBAlgorithm(G, seed_size, oracle, dimension, alpha, lambda_, FeatureScaling)
# algorithms['Uniform_LinUCB'] = LinUCBAlgorithm(G, seed_size, oracle, dimension, alpha, lambda_)
# algorithms['UCB1'] = UCB1Algorithm(G, seed_size, oracle)
# algorithms['egreedy_0'] = eGreedyAlgorithm(G, seed_size, oracle, 0)
# algorithms['egreedy_0.1'] = eGreedyAlgorithm(G, seed_size, oracle, 0.1)
# algorithms['egreedy_1'] = eGreedyAlgorithm(G, seed_size, oracle, 1.0)
#algorithms['UCB1'] = UCB1Algorithm(G, seed_size, oracle)
algorithms['CLUB_Erodos'] = CLUBAlgorithm(G, seed_size, oracle, dimension, alpha, alpha_2, lambda_, FeatureScaling)
# algorithms['CLUB_0.2'] = CLUBAlgorithm(G, seed_size, oracle, dimension, alpha, alpha_2, lambda_, Feature_Dic, FeatureScaling, feedback = 'edge', cluster_init="none")
#algorithms['CLUB_1'] = CLUBAlgorithm(G, seed_size, oracle, dimension, alpha, alpha_2, lambda_, FeatureScaling, feedback = 'edge', cluster_init="none")
# algorithms['CLUB_4'] = CLUBAlgorithm(G, seed_size, oracle, dimension, alpha, 4.0, lambda_, Feature_Dic, FeatureScaling, feedback = 'edge', cluster_init="none")
#algorithms['CAB'] = CABAlgorithm(G, seed_size, oracle, dimension, alpha, alpha_2, lambda_, FeatureScaling, gamma)
simExperiment.runAlgorithms(algorithms)