-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRAGE_opt.py
194 lines (145 loc) · 7.12 KB
/
RAGE_opt.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
## Modified from: https://github.com/fiezt/Transductive-Linear-Bandit-Code/blob/master/RAGE.py
import numpy as np
import itertools
import logging
import time
class RAGE_opt(object):
def __init__(self, X, theta_star, factor, 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)
self.delta = delta
self.factor = factor
self.theta_hat_list = []
def algorithm(self, seed, var=True, binary=False, sigma=1, stop_arm_count=1, rel_thresh=0.01):
self.var=var
self.seed = seed
self.sigma = sigma
self.stop_arm_count = stop_arm_count
self.rel_thresh=rel_thresh
np.random.seed(self.seed)
self.active_arms = list(range(len(self.Z)))
self.arm_counts = np.zeros(self.K)
self.N = 0
self.phase_index = 1
self.theta_hat = None
while len(self.active_arms) > self.stop_arm_count:
self.delta_t = self.delta/(self.phase_index**2)
self.build_Y()
design, rho = self.optimal_allocation()
support = np.sum((design > 0).astype(int))
n_min = 2*self.factor*support
eps = 1/self.factor
# num_samples = max(np.ceil(8*(2**(self.phase_index-1))**2*rho*(1+eps)*np.log(2*self.K_Z**2/self.delta_t)), n_min).astype(int)
num_samples = max(np.ceil(8*(2**(self.phase_index+1))**2*rho*(1+eps)*np.log(2*self.K_Z**2/self.delta_t) * (self.sigma ** 2)), n_min).astype(int)
allocation = self.rounding(design, num_samples)
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(allocation.sum(), 1) * self.sigma
else:
rewards = np.random.binomial(1, [email protected]_star, (allocation.sum(), 1))
self.A_inv = np.linalg.pinv(pulls.T@pulls)
self.theta_hat = self.A_inv @ pulls.T @ rewards
# print(f"theta_hat shape: {self.theta_hat.shape}")
self.reference_update()
self.drop_arms()
self.phase_index += 1
self.arm_counts += allocation
self.N += num_samples
self.theta_hat_list.append((self.N, self.theta_hat, len(self.active_arms)))
logging.info('\n\n')
logging.info('finished phase %s' % str(self.phase_index-1))
logging.info('design %s' % str(design))
logging.debug('allocation %s' % str(allocation))
logging.debug('arm counts %s' % str(self.arm_counts))
logging.info('round sample count %s' % str(num_samples))
logging.info('total sample count %s' % str(self.N))
logging.info('active arms %s' % str(self.active_arms))
logging.info('rho %s' % str(rho))
logging.info('\n\n')
del self.Yhat
del self.X
del self.Z
self.success = (self.opt_arm in self.active_arms)
logging.critical('Succeeded? %s' % str(self.success))
logging.critical('Sample complexity %s' % str(self.N))
def build_Y(self):
if self.theta_hat is None:
# Randomly select a reference point if theta_hat is not initialized
reference_idx = np.random.choice(self.active_arms)
# print(f"Random best arm chosed, {reference_idx}\n")
else:
active_Z = self.Z[self.active_arms]
reference_idx = np.argmax(active_Z @ self.theta_hat)
reference = self.Z[reference_idx]
Y = self.Z[self.active_arms] - reference
self.Yhat = Y
def reference_update(self):
if self.theta_hat is not None:
active_Z = self.Z[self.active_arms]
# print(f"active_Z.shape: {active_Z.shape}\n")
# print(f"theta_hat.shape: {self.theta_hat.shape}")
# print(f"opt_index: {np.argmax(active_Z @ self.theta_hat)}\n")
self.opt_arm = self.active_arms[np.argmax(active_Z @ self.theta_hat)]
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 count % 100 == 0:
logging.debug('design status %s, %s, %s, %s' % (self.seed, count, relative, np.max(rho)))
if relative < self.rel_thresh:
break
idx_fix = np.where(design < 1e-5)[0]
design[idx_fix] = 0
return design, np.max(rho)
def rounding(self, design, num_samples):
num_support = (design > 0).sum()
support_idx = np.where(design>0)[0]
support = design[support_idx]
n_round = np.ceil((num_samples - .5*num_support)*support)
while n_round.sum()-num_samples != 0:
if n_round.sum() < num_samples:
idx = np.argmin(n_round/support)
n_round[idx] += 1
else:
idx = np.argmax((n_round-1)/support)
n_round[idx] -= 1
allocation = np.zeros(len(design))
allocation[support_idx] = n_round
return allocation.astype(int)
def drop_arms(self):
if self.theta_hat is None:
return
presumed_best_arm = self.Z[self.opt_arm, :, None]
active_arms_matrix = self.Z[self.active_arms, :]
y = presumed_best_arm.T - active_arms_matrix
projections = y @ self.theta_hat.flatten()
if not self.var:
thresholds = 2 ** (-self.phase_index - 2)
else:
quadratic_forms = np.einsum('ij,jk,ik->i', y, self.A_inv, y)
thresholds = np.sqrt(2 * (self.sigma**2) * np.log(2 * self.K**2 / self.delta_t) * quadratic_forms)
removes = np.array(self.active_arms)[projections >= thresholds]
self.active_arms = [arm for arm in self.active_arms if arm not in removes]