-
Notifications
You must be signed in to change notification settings - Fork 1
/
Functions.py
150 lines (137 loc) · 4.21 KB
/
Functions.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
import math
import random
import numpy as np
from numpy.linalg import cholesky
import matplotlib.pyplot as plt
import pdb
import minVar
Max = 100000
N = 100000
def PullArm(a, x, Theta, number = 1):
reward = number*np.dot(Theta[a,:],x) + np.random.normal(0,np.sqrt(number))
return reward
def Chu(p, q):
if p == 0:
return 0
if q == 0:
return N
else:
return p/q
def Int(x):
if x<1e-10:
return 0
else:
return math.ceil(x)
def f(p):
if p == -1:
return 1
return 2**p
'''
def f(p):
if p == 0:
return 1
S = [1]
for i in range(1,p+1):
S.append(S[i-1] - K + 2 * np.sqrt(S[i-1] * 2**16))
return int(S[p]) - int(S[p-1])
'''
def UCBScore(y,t,n):
if t == 0:
return N
s = y/t + np.sqrt(2 * np.log(n * np.log(n) * np.log(n) + 1)/t)
return s
def FindTrueBestArm(i, c, Theta):
k = np.shape(c)[1]
B = []
for arm in range(k):
B.append(Theta[arm].dot(c[i,arm]))
return max( (v,i) for i,v in enumerate(B))[1]
def Estimation(c, i, p, n, messages,var_matrix,Y,T,A,R):
[m,k,d] = np.shape(c)
estimations = messages[0]
B = []
C = []
for arm in range(k):
if arm in A[i]:
if ((estimations[arm] == (np.zeros([d]) + Max)).all()):
B.append(UCBScore(Y[i,arm], T[i,arm],1))
C.append(T[i,arm])
else:
B.append(estimations[arm].dot(c[i,arm]))
C.append(var_matrix[arm].dot(c[i,arm]).dot(c[i,arm]))
else:
B.append(0)
C.append(1)
return B,C
def FindBestUCBArm(i, n, Y, T):
k = np.shape(Y)[1]
B = []
for arm in range(k):
B.append(UCBScore(Y[i,arm], T[i,arm], n))
return max( (v, i) for i, v in enumerate(B) )[1]
def PotentialSets(local_potential):
[m,k] = np.shape(local_potential)
A = []
R = []
for i in range(m):
A.append([])
for a in range(k):
if local_potential[i,a] == 1:
A[i].append(a)
if len(A[i]) == 0:
pdb.set_trace()
for a in range(k):
R.append([])
for i in range(m):
if a in A[i]:
R[a].append(i)
return A,R
def Broadcast(p, loc_info, messages, pi, A, R):
[m,k,d] = np.shape(loc_info)
Var_matrix = []
for arm in range(k):
X = np.zeros([d,d])
sum_theta = np.zeros(d)
for i in R[arm]:
if ((loc_info[i,arm,:] == (np.zeros([d])+Max)).all()):
X += np.zeros([d,d])
else:
X += Int(pi[i,arm]*f(p-1))*loc_info[i,arm].reshape([d,1]).dot(loc_info[i,arm].reshape([1,d]))/loc_info[i,arm].dot(loc_info[i,arm])
sum_theta += Int(pi[i,arm]*f(p-1))*loc_info[i,arm]
var_matrix = X
theta_hat = sum_theta.dot(np.linalg.pinv(var_matrix/f(p-1)))/f(p-1)
messages[0][arm] = theta_hat
Var_matrix.append(np.linalg.pinv(var_matrix))
return messages, Var_matrix
def FindGap(c, Theta):
[m,k,d] = np.shape(c)
Gap = np.zeros([m,k])
deltamin = 1
deltamax = 0
for i in range(m):
mu_star = Theta[FindTrueBestArm(i,c,Theta)].dot(c[i,FindTrueBestArm(i,c,Theta)])
for arm in range(k):
Gap[i,arm] = mu_star - Theta[arm].dot(c[i,arm])
if Gap[i,arm]!=0:
if Gap[i,arm] < deltamin:
deltamin = Gap[i,arm]
if Gap[i,arm] > deltamax:
deltamax = Gap[i,arm]
return Gap, deltamin, deltamax
def Broadcast_collaborate(c, p, loc_info, messages, pi, A, R):
[m,k,d] = np.shape(loc_info)
Var_matrix = []
for arm in range(k):
X = np.zeros([d,d])
sum_theta = np.zeros(d)
for i in R[arm]:
if ((loc_info[i,arm,:] == (np.zeros([d])+Max)).all()):
X += np.zeros([d,d])
else:
X += Int(pi[i,arm]*f(p-1))*c[i,arm].reshape([d,1]).dot(c[i,arm].reshape([1,d]))
sum_theta += Int(pi[i,arm]*f(p-1))*c[i,arm].dot(c[i,arm])*loc_info[i,arm]
var_matrix = X
theta_hat = sum_theta.dot(np.linalg.pinv(var_matrix/f(p-1)))/f(p-1)
messages[0][arm] = theta_hat
Var_matrix.append(np.linalg.pinv(var_matrix))
return messages, Var_matrix