-
Notifications
You must be signed in to change notification settings - Fork 6
/
mab.py
311 lines (262 loc) · 11.3 KB
/
mab.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
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
'''
Implementation of various Multi-Armed Bandit Algorithms and Strategies.
'''
import operator
import math
import random
######################################################################
## 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 = {}
weigth_sum = 0
if len(self.average_reward)!=0:
for arm,reward in self.average_reward.items():
arm_prob[arm] = math.exp(reward/self.tau)
weigth_sum += arm_prob[arm]
for arm in arm_prob:
arm_prob[arm] /= weigth_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
####################################################################
## 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