-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
127 lines (110 loc) · 4.12 KB
/
main.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
from LinUCB import LinUCB
from LTS import LTS
from Statistic import Statistic
from numpy.random import choice
import numpy as np
import Bagging
import Data
import plot
import time
class Sep_test(object):
def __init__(self,feature_size):
'''
self.total_runs: the total times of making prediction
self.total_reward: the total reward of prediction
B, miu, f are median parameters
'''
self.linucb = LinUCB(feature_size)
self.lts = LTS(feature_size)
self.stat = Statistic(feature_size)
self.his_linucb = []
self.his_lts = []
self.his_stat = []
self.his_hybrid = []
self.valid_linucb = 0
self.valid_lts = 0
self.valid_stat = 0
self.valid_hybrid = 0
self.vote = []
def LinUCB_predict_and_learn(self,context,articleID,reward,pool):
prediction = self.linucb.predict(context,pool)
self.vote.append(prediction)
#update records
if prediction==articleID:
self.his_linucb.append(reward)
self.valid_linucb += 1
#train one of the agents
self.linucb.learn(context,articleID,reward)
def lts_predict_and_learn(self,context,articleID,reward,pool):
prediction = self.lts.predict(context,pool)
self.vote.append(prediction)
#update records
if prediction==articleID:
self.his_lts.append(reward)
self.valid_lts += 1
#train one of the agents
self.lts.learn(context,articleID,reward)
def stat_predict_and_learn(self,context,articleID,reward,pool):
prediction = self.stat.predict(context,pool)
self.vote.append(prediction)
#update records
if prediction==articleID:
self.his_stat.append(reward)
self.valid_stat += 1
#train one of the agents
self.stat.learn(context,articleID,reward)
def predict_and_learn(self,context,articleID,reward,pool):
self.LinUCB_predict_and_learn(context,articleID,reward,pool)
self.lts_predict_and_learn(context,articleID,reward,pool)
self.stat_predict_and_learn(context,articleID,reward,pool)
def Hybrid_predict_and_learn(self,context,articleID,reward,pool):
self.predict_and_learn(context,articleID,reward,pool)
counts=np.bincount(self.vote)
prediction=choice(np.flatnonzero(counts == counts.max()))
#update records
if prediction==articleID:
self.his_hybrid.append(reward)
self.valid_hybrid+=1
self.vote=[]
print("==START==")
start_time = time.time()
data_dir = 'rewrite.txt'
batch_num = Data.process_large_data(data_dir)
data_gen=Data.get_batched_data(min(batch_num,3))
print("done processing data file")
seprate_test = Sep_test(Data.USER_VEC_SIZE)
print("Computation starts")
total_click=0
total_data=0#count data entries
for (display,click,user_vec,pool) in data_gen:
#do something with current data
total_data+=1
total_click+=click
#seprate_test.Hybrid_predict_and_learn(user_vec,display,click,pool)
seprate_test.LinUCB_predict_and_learn(user_vec,display,click,pool)
seprate_test.stat_predict_and_learn(user_vec,display,click,pool)
total_crt=total_click*1.0/total_data
print(total_crt)
record_linucb = seprate_test.his_linucb
#record_lts = seprate_test.his_lts
record_stat = seprate_test.his_stat
#record_hybrid=seprate_test.his_hybrid
print("Done computation")
#avg_hybrid=plot.cumulative_avg(record_hybrid)/total_crt
#np.savetxt("hybrid.csv",avg_hybrid,delimiter=',')
avg_linucb=plot.cumulative_avg(record_linucb)/total_crt
np.savetxt("linucb.csv",avg_linucb,delimiter=',')
#avg_lts=plot.cumulative_avg(record_lts)/total_crt
#np.savetxt('lts.csv',avg_lts,delimiter=',')
avg_stat=plot.cumulative_avg(record_stat)/total_crt
np.savetxt('stat.csv',avg_stat,delimiter=',')
plot.plot_avg(
(avg_linucb,avg_stat),
title="Average Reward",
filename='plot.png',
legend=['linucb','stat'],
xlabel="Sample Size",
ylabel="Average Reward")
end_time=time.time()
time_used=end_time-start_time
print("Total time used: {}".format(time_used))