-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmab.py
More file actions
383 lines (323 loc) · 14.2 KB
/
mab.py
File metadata and controls
383 lines (323 loc) · 14.2 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
'''
Implementation of various Multi-Armed Bandit Algorithms and Strategies.
'''
import operator
import math
import random
import numpy as np
######################################################################
## Simple Multi-Armed Bandit
######################################################################
class MAB:
'''
Simple Multi-armed Bandit implementation.
'''
def __init__(self):
'''Constructor.'''
self.total_rewards = {}
self.total_count = {}
self.average_reward = {}
def description(self) -> str:
'''Return a string which describes the algorithm.'''
return "Simple MAB"
def update_reward(self, arm, reward):
'''Use this method to update the algorithm which `arm` has been
selected and what `reward` has been observed from the environment.'''
if arm not in self.total_rewards: # new arm?
self.total_rewards[arm] = 0
self.total_count[arm] = 0
self.total_count[arm] += 1
self.total_rewards[arm] += reward
self.average_reward[arm] = self.total_rewards[arm]/self.total_count[arm]
def get_reward(self, arm):
'''Get the reward for a particular `arm`.'''
if arm not in self.average_reward: return 0
return self.average_reward[arm]
def get_arm_count(self, arm):
'''Return how many times have this `arm` been selected.'''
if arm not in self.total_count: return 0
return self.total_count[arm]
def get_best_arm(self):
'''Return a tuple (arm,reward) representing the best arm and
the corresponding average reward. If this arm has not been
seen by the algorithm, it simply returns (None,None).'''
if len(self.average_reward)==0:
return (None,None)
return max(self.average_reward.items(), key=operator.itemgetter(1))
######################################################################
## Upper Confidence Bound (UCB)
######################################################################
class UCB1(MAB):
'''
Upper Confidence Bound (UCB) implementation.
'''
def __init__(self, beta=1.0):
'''Constructor.'''
super().__init__()
self.beta = beta
self.overall_total_count = 0
self.ucb = 0
def description(self):
'''Return a string which describes the algorithm.'''
return "UCB MAB"
def update_reward(self, arm, reward):
'''Use this method to update the algorithm which `arm` has been
selected and what `reward` has been observed from the environment.'''
if arm not in self.total_rewards:
self.total_rewards[arm] = 0
self.total_count[arm] = 0
self.total_count[arm] += 1
self.overall_total_count += 1
self.ucb = math.sqrt(2*self.beta*math.log(self.total_count[arm])/self.total_count[arm])
ucb_reward = reward + self.ucb
self.total_rewards[arm] += ucb_reward
self.average_reward[arm] = self.total_rewards[arm]/self.total_count[arm]
def get_last_ucb(self):
return self.ucb
######################################################################
## Thompson Sampling Technique
######################################################################
class TS:
'''
Multi-armed Bandit with Thompson Sampling technique.
'''
def __init__(self):
'''Constructor.'''
self.total_count = {}
self.alpha = {}
self.beta = {}
self.last_drawn = {}
def description(self) -> str:
'''Return a string which describes the algorithm.'''
return "Multi-armed Bandit with Thompson Sampling technique"
def update_reward(self, arm, reward):
'''Use this method to update the algorithm which `arm` has been
selected and what `reward` (must be either 0 or 1) has been observed
from the environment.'''
if arm not in self.total_count: # new arm?
self.alpha[arm] = 1
self.beta[arm] = 1
self.total_count[arm] = 0
self.last_drawn[arm] = 0
self.total_count[arm] += 1
self.alpha[arm] += reward
self.beta[arm] += 1-reward
def get_reward(self, arm):
'''Get the reward for a particular `arm`.
This is $\frac{\alpha-1}{(\alpha-1)+(\beta-1)}$.'''
if arm not in self.total_count: return 0
return (self.alpha[arm]-1) / (self.alpha[arm]-1+self.beta[arm]-1)
def get_arm_count(self, arm):
'''Return how many times have this `arm` been selected.'''
if arm not in self.total_count: return 0
return self.total_count[arm]
def get_best_arm(self):
'''Return a tuple (arm,reward) representing the best arm and
the corresponding average reward. If this arm has not been
seen by the algorithm, it simply returns (None,None).'''
best_arm = { "arm":None, "value":0.0 }
for arm in self.total_count:
self.last_drawn[arm] = random.betavariate(self.alpha[arm],self.beta[arm])
if self.last_drawn[arm]>=best_arm["value"]:
best_arm["arm"] = arm
best_arm["value"] = self.last_drawn[arm]
if best_arm["arm"] is None:
return (None,None)
return (best_arm["arm"],best_arm["value"])
def get_last_drawn_value(self, arm):
if arm not in self.last_drawn: return 0
return self.last_drawn[arm]
######################################################################
## Boltzmann Exploration (Softmax)
######################################################################
class SoftMax(MAB):
'''
Boltzmann Exploration (Softmax).
'''
def __init__(self, tau=1.0):
'''Constructor.'''
super().__init__()
self.tau = tau
def description(self) -> str:
'''Return a string which describes the algorithm.'''
return "Boltzmann Exploration (Softmax)"
def get_best_arm(self):
'''Return a tuple (arm,reward) representing the best arm and
the corresponding average reward. If this arm has not been
seen by the algorithm, it simply returns (None,None).'''
if len(self.average_reward)==0:
return (None,None) # nothing in Q-table yet, do exploration
arm_list = [arm for arm in self.average_reward]
arm_weight = [math.exp(reward/self.tau) for reward in self.average_reward.values()]
# note that we don't need to divide the denominator because
# `random.choices()` will scale `arm_weight` automatically
choice = random.choices(arm_list,arm_weight)[0]
return (choice,self.average_reward[choice])
def get_prob_list(self):
'''Get the probability dictionary for all arms. Each quantity describes
the probability that an arm will be picked.'''
arm_prob = {}
weight_sum = 0
if len(self.average_reward)!=0:
for arm,reward in self.average_reward.items():
arm_prob[arm] = math.exp(reward/self.tau)
weight_sum += arm_prob[arm]
for arm in arm_prob:
arm_prob[arm] /= weight_sum
return(arm_prob)
######################################################################
## Simple Discrete Contextual MAB
## Using Multi-UCB1
######################################################################
class CMAB:
'''
Simple Discrete Contextual Multi-armed Bandit implementation
using Multi-UCB1.
'''
def __init__(self):
'''Constructor.'''
self.mab = {}
def description(self):
'''Return a string which describes the algorithm.'''
return "Contextual MAB using Multi-UCB1"
def update_reward(self, arm, reward, context=None):
'''Use this method to update the algorithm which `arm` has been
selected under which `context, and what `reward` has been observed
from the environment.'''
if context not in self.mab:
self.mab[context] = UCB1() # we use UCB1 model for each context
self.mab[context].update_reward(arm, reward)
def get_reward(self, arm, context=None):
'''Get the reward for a particular `arm` under this `context`.'''
if context not in self.mab: # new context?
return 0
return self.mab[context].get_reward(arm)
def get_best_arm(self, context=None):
'''Return a tuple (arm,reward) representing the best arm and
the corresponding average reward. If this context has not been
seen by the algorithm, it simply returns (None,None).'''
if context not in self.mab: return (None,None)
return self.mab[context].get_best_arm()
######################################################################
## Simple Discrete Contextual MAB
## Using Summarized Contexts
######################################################################
class CMAB2(MAB):
'''
Simple Discrete Contextual Multi-armed Bandit implementation
using Summarized Contexts. This class extends simple MAB,
extending other models are also possible, e.g. UCB1.
'''
def __init__(self):
'''Constructor.'''
super().__init__()
def description(self):
'''Return a string which describes the algorithm.'''
return "Contextual MAB using Summarized Contexts"
def context(self, feature, action=None):
'''Return the context summarizing feature and action.'''
return (feature,action)
def update_reward(self, context, reward):
'''Use this method to update the algorithm which `context` has been
observed and what `reward` has been obtained from the environment.'''
super().update_reward(context,reward)
def get_best_arm(self, context):
'''Return a tuple (action,reward) representing the best arm and
the corresponding average reward. If this context has not been
seen by the algorithm, it simply returns (None,None).'''
best_action = (None,None) # (action,reward)
for cnx in self.average_reward:
if cnx[0]==context[0]: # context=(feature,action)
if best_action[0] is None or best_action[1]<self.average_reward[cnx]:
best_action = (cnx[1],self.average_reward[cnx])
return best_action
######################################################################
## LinUCB with online Ridge regression solver
######################################################################
class OnlineRidgeRegression:
def __init__(self, num_features, lambda_ridge, alpha=1.0):
self.lambda_ridge = lambda_ridge
self.dim = num_features+1 # dimension of the problem
self.A = np.eye(self.dim) # initialize X^T X matrix, or A
self.b = np.zeros(self.dim) # initialize X^T y vector, or b
self.coeffs = None # coefficients
self.alpha = alpha # exploration-exploitation tradeoff
def update(self, xi, yi):
xi = np.insert(xi,0,1).reshape(-1,1) # add intercept & reshape to column vector
self.A += xi @ xi.T
self.b += yi * xi.flatten()
def predict(self, xi):
xi = np.insert(xi,0,1).reshape(-1,1) # add intercept & reshape to column vector
self.coeffs = np.linalg.inv(self.A) @ self.b
pred = self.coeffs.T @ xi.flatten() + \
self.alpha * np.sqrt(xi.T @ np.linalg.inv(self.A) @ xi.flatten())[0]
return pred
def get_coeffs(self):
return self.coeffs
class LinUCB:
'''
Linear Upper Confidence Bound (LinUCB) algorithm implementing the disjoint model.
'''
def __init__(self, num_features, alpha=0.5):
'''Constructor.'''
self.num_features = num_features
self.alpha = alpha
self.lambda_ridge = 1.0
self.ridge_regression = {} # to store Ridge regression of each arm
self.all_known_arms = []
def description(self):
'''Return a string which describes the algorithm.'''
return "LinUCB"
def update_reward(self, arm, reward, context):
'''Use this method to update the algorithm which `arm` has been
selected under which `context, and what `reward` has been observed
from the environment.'''
if arm not in self.all_known_arms: # new arm?
self.all_known_arms.append(arm)
self.ridge_regression[arm] = OnlineRidgeRegression(self.num_features,self.lambda_ridge)
self.ridge_regression[arm].update(context, reward)
def get_reward(self, arm, context):
'''Get the reward for a particular `arm` under this `context`.'''
if arm not in self.all_known_arms: # new arm?
return None
return self.ridge_regression[arm].predict(context)
def get_best_arm(self, context):
'''Return a tuple (arm,reward) representing the best arm and
the corresponding average reward.'''
if len(self.all_known_arms)==0:
return (None,None)
arm_reward_list = []
for arm in self.all_known_arms:
reward = self.ridge_regression[arm].predict(context)
arm_reward_list.append((arm,reward))
return max(arm_reward_list, key=lambda x: x[1])
####################################################################
## MAB Strategy
####################################################################
class BaseStrategy:
def description(self):
return f"100% exploration"
def is_exploration(self,round):
return True # default is 100% exploration
class EpsilonGreedy(BaseStrategy):
def __init__(self,epsilon):
self.epsilon = epsilon
def description(self):
return f"Epsilon Greedy, epsilon = {self.epsilon}"
def is_exploration(self,round):
return random.random()<self.epsilon
class EpsilonDecreasing(BaseStrategy):
def __init__(self,exponent):
self.exponent = exponent
def description(self):
return f"Epsilon-Decreasing, exponent = {self.exponent}"
def is_exploration(self,round):
epsilon = round**(self.exponent)
return random.random()<epsilon
class ExplorationFirst(BaseStrategy):
def __init__(self,switch_round):
self.switch_round = int(switch_round)
def description(self):
return f"Explore-First for {self.switch_round} rounds"
def is_exploration(self,round):
return round<self.switch_round