-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulationNonLinear.py
224 lines (182 loc) · 7.18 KB
/
SimulationNonLinear.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import copy
import numpy as np
from random import sample, shuffle
import datetime
import os.path
import matplotlib.pyplot as plt
import argparse
# local address to save simulated users, simulated articles, and results
from conf import sim_files_folder, save_address
from util_functions import featureUniform, gaussianFeature
from Articles import ArticleManager
from Users import UserManager
from lib.GeneralizedLinearBandit import GeneralizedLinearBandit
class simulateOnlineData(object):
def __init__(self, context_dimension, testing_iterations, plot, articles,
users, noise=lambda: 0, signature='', NoiseScale=0.0, poolArticleSize=None):
self.simulation_signature = signature
self.context_dimension = context_dimension
self.testing_iterations = testing_iterations
self.batchSize = 100
self.plot = plot
self.noise = noise
self.NoiseScale = NoiseScale
self.articles = articles
self.users = users
if poolArticleSize is None:
self.poolArticleSize = len(self.articles)
else:
self.poolArticleSize = poolArticleSize
def getTheta(self):
Theta = np.zeros(shape = (self.context_dimension, len(self.users)))
for i in range(len(self.users)):
Theta.T[i] = self.users[i].theta
return Theta
def batchRecord(self, iter_):
print("Iteration %d"%iter_, " Elapsed time", datetime.datetime.now() - self.startTime)
def getReward(self, user, pickedArticle):
# modified reward function
return np.square(np.dot(user.theta, pickedArticle.featureVector))
def GetOptimalReward(self, user, articlePool):
maxReward = float('-inf')
maxx = None
for x in articlePool:
reward = self.getReward(user, x)
if reward > maxReward:
maxReward = reward
maxx = x
return maxReward, maxx
def getL2Diff(self, x, y):
return np.linalg.norm(x-y) # L2 norm
def regulateArticlePool(self):
# Randomly generate articles
self.articlePool = sample(self.articles, self.poolArticleSize)
def runAlgorithms(self, algorithms):
self.startTime = datetime.datetime.now()
timeRun = self.startTime.strftime('_%m_%d_%H_%M')
filenameWriteRegret = os.path.join(save_address, 'AccRegret' + timeRun + '.csv')
filenameWritePara = os.path.join(save_address, 'ParameterEstimation' + timeRun + '.csv')
tim_ = []
BatchCumlateRegret = {}
AlgRegret = {}
ThetaDiffList = {}
ThetaDiff = {}
# Initialization
userSize = len(self.users)
for alg_name, alg in algorithms.items():
AlgRegret[alg_name] = []
BatchCumlateRegret[alg_name] = []
if alg.CanEstimateUserPreference:
ThetaDiffList[alg_name] = []
with open(filenameWriteRegret, 'w') as f:
f.write('Time(Iteration)')
f.write(',' + ','.join([str(alg_name) for alg_name in algorithms.keys()]))
f.write('\n')
with open(filenameWritePara, 'w') as f:
f.write('Time(Iteration)')
f.write(','+ ','.join([str(alg_name)+'Theta' for alg_name in ThetaDiffList.keys()]))
f.write('\n')
for iter_ in range(self.testing_iterations):
# prepare to record theta estimation error
for alg_name, alg in algorithms.items():
if alg.CanEstimateUserPreference:
ThetaDiff[alg_name] = 0
for u in self.users:
self.regulateArticlePool()
noise = self.noise()
#get optimal reward for user x at time t
OptimalReward, OptimalArticle = self.GetOptimalReward(u, self.articlePool)
OptimalReward += noise
for alg_name, alg in algorithms.items():
pickedArticle = alg.decide(self.articlePool, u.id)
reward = self.getReward(u, pickedArticle) + noise
alg.updateParameters(pickedArticle, reward, u.id)
regret = OptimalReward - reward # pseudo regret, since noise is canceled out
AlgRegret[alg_name].append(regret)
#update parameter estimation record
if alg.CanEstimateUserPreference:
ThetaDiff[alg_name] += self.getL2Diff(u.theta, alg.getTheta(u.id))
for alg_name, alg in algorithms.items():
if alg.CanEstimateUserPreference:
ThetaDiffList[alg_name] += [ThetaDiff[alg_name]/userSize]
if iter_%self.batchSize == 0:
self.batchRecord(iter_)
tim_.append(iter_)
for alg_name in algorithms.keys():
BatchCumlateRegret[alg_name].append(sum(AlgRegret[alg_name])/userSize)
with open(filenameWriteRegret, 'a+') as f:
f.write(str(iter_))
f.write(',' + ','.join([str(BatchCumlateRegret[alg_name][-1]) for alg_name in algorithms.keys()]))
f.write('\n')
with open(filenameWritePara, 'a+') as f:
f.write(str(iter_))
f.write(','+ ','.join([str(ThetaDiffList[alg_name][-1]) for alg_name in ThetaDiffList.keys()]))
f.write('\n')
if (self.plot==True): # only plot
# plot the results
f, axa = plt.subplots(1)
for alg_name in algorithms.keys():
axa.plot(tim_, BatchCumlateRegret[alg_name],label = alg_name)
print('%s: %.2f' % (alg_name, BatchCumlateRegret[alg_name][-1]))
axa.legend(loc='upper left',prop={'size':9})
axa.set_xlabel("Iteration")
axa.set_ylabel("Regret")
axa.set_title("Accumulated Regret")
plt.show()
# plot the estimation error of theta
f, axa = plt.subplots(1)
time = range(self.testing_iterations)
for alg_name, alg in algorithms.items():
if alg.CanEstimateUserPreference:
axa.plot(time, ThetaDiffList[alg_name], label = alg_name + '_Theta')
axa.legend(loc='upper right',prop={'size':6})
axa.set_xlabel("Iteration")
axa.set_ylabel("L2 Diff")
axa.set_yscale('log')
axa.set_title("Parameter estimation error")
plt.show()
finalRegret = {}
for alg_name in algorithms.keys():
finalRegret[alg_name] = BatchCumlateRegret[alg_name][:-1]
return finalRegret
if __name__ == '__main__':
parser = argparse.ArgumentParser(description = '')
parser.add_argument('--contextdim', type=int, help='Set dimension of context features.')
parser.add_argument('--actionset', type=str, help='Set dimension of context features.')
args = parser.parse_args()
## Environment Settings ##
if args.contextdim:
context_dimension = args.contextdim
else:
context_dimension = 25
if args.actionset:
actionset = args.actionset
else:
actionset = "basis_vector" # "basis_vector" or "random"
testing_iterations = 200000
NoiseScale = 0.1 # standard deviation of Gaussian noise
n_articles = 25
n_users = 10
poolArticleSize = None
if actionset == "basis_vector":
n_articles = context_dimension # there can be at most context_dimension number of basis vectors
## Set Up Simulation ##
UM = UserManager(context_dimension, n_users, thetaFunc=gaussianFeature, argv={'l2_limit': 1})
users = UM.simulateThetafromUsers()
AM = ArticleManager(context_dimension, n_articles=n_articles, argv={'l2_limit':1})
articles = AM.simulateArticlePool(actionset)
simExperiment = simulateOnlineData( context_dimension=context_dimension,
testing_iterations=testing_iterations,
plot=True,
articles=articles,
users = users,
noise=lambda: np.random.normal(scale=NoiseScale),
signature=AM.signature,
NoiseScale=NoiseScale,
poolArticleSize=poolArticleSize)
## Initiate Bandit Algorithms ##
algorithms = {}
algorithms['GeneralizedLinearBandit'] = GeneralizedLinearBandit(dimension=context_dimension, lambda_=0.1, alpha=1.5)
## Run Simulation ##
print("Starting for ", simExperiment.simulation_signature)
simExperiment.runAlgorithms(algorithms)