-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmab.py
executable file
·297 lines (258 loc) · 7.86 KB
/
cmab.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
#!/usr/local/bin/python3
# Author: Pranab Ghosh
#
# Licensed under the Apache License, Version 2.0 (the "License"); you
# may not use this file except in compliance with the License. You may
# obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
import os
import sys
import random
import math
import numpy as np
from matumizi.util import *
from matumizi.mlutil import *
from matumizi.sampler import *
"""
Contextual multi arm bandit
"""
class LinUpperConfBound(object):
"""
linear upper conf bound multi arm bandit (lin ucb1)
"""
def __init__(self, actions, nfeat, horizon, totPlays=0, reg=None, pthresh=None, a=None, b=None, alpha=None, logFilePath=None, logLevName=None):
"""
initializer
Parameters
actions : action names
nfeat : feature size
reg ; regularizing param (lambda)
horizon : num of plays
pthresh: probability threshold (delta)
a : regression matrix
b : regression vector
alpha : constant
logFilePath : log file path set None for no logging
logLevName : log level e.g. info, debug
"""
self.actions = actions
self.naction = len(actions)
self.totPlays = totPlays
self.nfeat = nfeat
self.reg = reg
self.horizon = horizon
self.pthresh = pthresh
self.a = np.identity(nfeat) * reg if a is None else a
self.b = np.zeros(nfeat) if b is None else b
self.horizon = horizon
self.alpha = 1 + sqrt(0.5 * math.log(2 * horizon * self.naction / pthresh)) if alpha is None else alpha
self.sactions = dict()
self.logger = None
if logFilePath is not None:
self.logger = createLogger(__name__, logFilePath, logLevName)
self.logger.info("******** stating new session of " + "LinUpperConfBound")
def getAction(self, features):
"""
next play return selected action
Parameters
features : features for all actions with 1 row per action
"""
inva = np.linalg.inv(self.a)
theta = np.matmul(inva, self.b)
sact = None
smax = None
saf = None
for i in range(np.shape(features)[0]):
af = features[i]
af = np.array(af)
aft = np.transpose(af)
t = np.matmul(aft, inva)
t = np.matmul(t, af)
s = np.dot(aft, theta) + self.alpha * sqrt(t)
if smax is None or s > smax:
smax = s
sact = self.actions[i]
saf = af
self.sactions[sact] = saf
if self.logger is not None:
self.logger.info("play count {} action {} reward upper bound {:.3f}".format(self.totPlays + 1, sact, smax))
self.totPlays += 1
return sact
def setReward(self, aname, reward):
"""
reward feedback for action
Parameters
aname : action name
reward : reward value
"""
af = self.sactions[aname]
aft = np.transpose(af)
self.a = np.add(self.a, np.matmul(af, aft))
self.b = np.add(self.b, af * reward)
if self.logger is not None:
self.logger.info("action {} feature {} actual reward {:.3f}".format(aname, floatArrayToString(af, delem=None), reward))
self.sactions.pop(aname)
def save(self, fpath):
"""
saves object
Parameters
fpath : file path
"""
mod = dict()
mod["actions"] = self.actions
mod["nfeat"] = self.nfeat
mod["horizon"] = self.horizon
mod["a"] = self.a
mod["b"] = self.b
mod["alpha"] = self.alpha
mod["totPlays"] = self.totPlays
saveObject(mod, fpath)
if self.logger is not None:
self.logger.info("cherckpointed model")
@staticmethod
def create(fpath, logFilePath=None, logLevName=None):
"""
restores object
Parameters
fpath : file path
logFilePath : log file path set None for no logging
logLevName : log level e.g. info, debug
"""
mod = restoreObject(fpath)
actions = mod["actions"]
nfeat = mod["nfeat"]
horizon = mod["horizon"]
a = mod["a"]
b = mod["b"]
alpha = mod["alpha"]
totPlays = mod["totPlays"]
linUcb = LinUpperConfBound(actions, nfeat, horizon, totPlays=totPlays, a=a, b=b, alpha=alpha, logFilePath=logFilePath, logLevName=logLevName)
if linUcb.logger is not None:
linUcb.logger.info("restored model from cherckpoint")
return linUcb
class LinThompsonSampling(object):
"""
linear thompson sampling multi arm bandit (lin ts)
"""
def __init__(self, actions, nfeat, subgaus, eps, pthresh, totPlays=None, b=None, mean=None, f=None, logFilePath=None, logLevName=None):
"""
initializer
Parameters
actions : action names
nfeat : feature size
subgaus ; sub gaussian (R)
eps : parameter (epsilon)
pthresh: probability threshold (delta)
totPlays : total plays so far
b : b param
mean : mean vector linear params
f : f param
logFilePath : log file path set None for no logging
logLevName : log level e.g. info, debug
"""
self.actions = actions
self.naction = len(actions)
self.totPlays = 0 if totPlays is None else totPlays
self.nfeat = nfeat
self.subgaus = subgaus
self.eps = eps
self.pthresh = pthresh
self.b = np.identity(nfeat) if b is None else b
self.mean = np.zeros(nfeat) if mean is None else mean
self.f = np.zeros(nfeat) if f is None else f
self.sactions = dict()
self.logger = None
if logFilePath is not None:
self.logger = createLogger(__name__, logFilePath, logLevName)
self.logger.info("******** stating new session of LinThompsonSampling")
def getAction(self, features):
"""
next play return selected action
Parameters
features : features for all actions with 1 row per action
"""
self.totPlays += 1
v = self.subgaus * sqrt(24 * self.nfeat * math.log(self.totPlays / self.pthresh) / self.eps)
v2 = v * v
invb = np.linalg.inv(self.b)
sd = invb * v2
mu = np.random.multivariate_normal(self.mean, sd)
sact = None
smax = None
saf = None
for i in range(np.shape(features)[0]):
af = features[i]
af = np.array(af)
aft = np.transpose(af)
s = np.dot(aft, mu)
if smax is None or s > smax:
smax = s
sact = self.actions[i]
saf = af
self.sactions[sact] = saf
if self.logger is not None:
self.logger.info("play count {} action {} expected reward {:.3f}".format(self.totPlays, sact, smax))
return sact
def setReward(self, aname, reward):
"""
reward feedback for action
Parameters
aname : action name
reward : reward value
"""
af = self.sactions[aname]
taf = np.transpose(af)
self.b = np.add(self.b, np.matmul(taf, af))
self.f = np.add(self.f, af * reward)
invb = np.linalg.inv(self.b)
self.mean = np.matmul(invb, f)
if self.logger is not None:
self.logger.info("action {} feature {} reward {:.3f}".format(aname, floatArrayToString(af, delem=None), reward))
self.sactions.pop(aname)
def save(self, fpath):
"""
saves object
Parameters
fpath : file path
"""
mod = dict()
mod["actions"] = self.actions
mod["nfeat"] = self.nfeat
mode["subgaus"] = self.subgaus
mod["eps"] = self.eps
mod["pthresh"] = self.pthresh
mod["b"] = self.b
mode["mean"] = self.mean
mod["f"] = self.f
mod["totPlays"] = self.totPlays
saveObject(mod, fpath)
@staticmethod
def create(fpath, logFilePath=None, logLevName=None):
"""
restores object
Parameters
fpath : file path
logFilePath : log file path set None for no logging
logLevName : log level e.g. info, debug
"""
mod = restoreObject(fpath)
actions = mod["actions"]
nfeat = mod["nfeat"]
subgaus = mode["subgaus"]
eps = mod["eps"]
pthresh = mod["pthresh"]
b = mod["b"]
mean = mode["mean"]
f = mod["f"]
totPlays = mod["totPlays"]
linThSamp = LinThompsonSampling(actions, nfeat, subgaus, eps, pthresh,totPlays=totPlays, b=b, mean=mean, f=f, logFilePath=logFilePath, logLevName=logLevName)
if linThSamp.logger is not None:
linThSamp.logger.info("restored model from cherckpoint")
return linThSamp