-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXY_ORACLE.py
182 lines (129 loc) · 5.5 KB
/
XY_ORACLE.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
## Modified from: https://github.com/fiezt/Transductive-Linear-Bandit-Code/blob/master/XY_ORACLE.py
import numpy as np
import itertools
import logging
import time
import warnings
warnings.filterwarnings('error')
logger = logging.getLogger()
logging.basicConfig(level=logging.DEBUG)
def randargmin(b,**kw):
idxs = np.where(np.abs(b-b.min())<1e-20)[0]
return np.random.choice(idxs)
class XY_ORACLE(object):
def __init__(self, X, theta_star, delta, Z=None):
self.X = X
if Z is None:
self.Z = X
else:
self.Z = Z
self.K = len(X)
self.K_Z = len(self.Z)
self.d = X.shape[1]
self.theta_star = theta_star
self.opt_arm = np.argmax(self.Z@theta_star)
rewards = [email protected]_star
self.gaps = np.max(rewards) - rewards
self.gaps = np.delete(self.gaps, self.opt_arm, 0)
self.delta = delta
def algorithm(self, seed, binary=False, sigma=1):
self.seed = seed
self.sigma = sigma
np.random.seed(self.seed)
self.arm_counts = np.zeros(self.K)
self.build_Y()
self.N = 0
self.A = np.zeros((self.d, self.d))
self.b = np.zeros((self.d, 1))
stop = False
self.u = 1.1
self.phase_index = 1
design, rho = self.optimal_allocation()
while True:
self.delta_t = self.delta/(2*self.phase_index**2*self.K_Z)
num_samples = int(np.ceil(self.u**self.phase_index))
logging.info('num samples %s' % str(num_samples))
allocation = np.random.choice(self.K, num_samples, p=design).tolist()
allocation = np.array([allocation.count(i) for i in range(self.K)])
logging.info('allocation %s' % str(allocation))
pulls = np.vstack([np.tile(self.X[i], (num, 1)) for i, num in enumerate(allocation) if num > 0])
if not binary:
rewards = [email protected]_star + np.random.randn(num_samples, 1) * self.sigma
else:
rewards = np.random.binomial(1, [email protected]_star, (num_samples, 1))
self.A += pulls.T@pulls
self.A_inv = np.linalg.pinv(self.A)
self.b += pulls.T@rewards
self.theta_hat = [email protected]
best_idx = self.check_stop()
if best_idx is None:
pass
else:
stop = True
self.phase_index += 1
self.arm_counts += allocation
self.N += num_samples
if self.N % 100000 == 0:
logging.info('\n\n')
logging.debug('arm counts %s' % str(self.arm_counts))
logging.info('total sample count %s' % str(self.N))
logging.info('\n\n')
if stop:
break
self.phase_index += 1
del self.b
del self.A
del self.A_inv
del self.Yhat
self.success = (self.opt_arm == best_idx)
logging.critical('Succeeded? %s' % str(self.success))
logging.critical('Sample complexity %s' % str(self.N))
def build_Y(self):
self.Yhat = self.Z[self.opt_arm, :] - self.Z
self.Yhat = np.delete(self.Yhat, self.opt_arm, 0)
self.Yhat = self.Yhat/self.gaps
def optimal_allocation(self):
design = np.ones(self.K)
design /= design.sum()
max_iter = 5000
for count in range(1, max_iter):
A_inv = np.linalg.pinv([email protected](design)@self.X)
U,D,V = np.linalg.svd(A_inv)
Ainvhalf = [email protected](np.sqrt(D))@V.T
newY = (self.Yhat@Ainvhalf)**2
rho = [email protected]((newY.shape[1], 1))
idx = np.argmax(rho)
y = self.Yhat[idx, :, None]
g = ((self.X@A_inv@y)*(self.X@A_inv@y)).flatten()
g_idx = np.argmax(g)
gamma = 2/(count+2)
design_update = -gamma*design
design_update[g_idx] += gamma
relative = np.linalg.norm(design_update)/(np.linalg.norm(design))
design += design_update
if relative < 0.01:
break
idx_fix = np.where(design < 1e-5)[0]
drop_total = design[idx_fix].sum()
design[idx_fix] = 0
design[np.argmax(design)] += drop_total
return design, np.max(rho)
def check_stop(self):
stop = True
arm = self.Z[self.opt_arm, :, None]
for arm_idx_prime in range(self.K_Z):
if self.opt_arm == arm_idx_prime:
continue
arm_prime = self.Z[arm_idx_prime, :, None]
y = arm - arm_prime
try:
if np.sqrt(2*(self.sigma**2)*[email protected]_inv@y*np.log(1/self.delta_t)) >= [email protected]_hat:
stop = False
break
except:
stop = False
break
if stop:
return self.opt_arm
else:
None