-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdolphinn.py
More file actions
53 lines (51 loc) · 1.67 KB
/
dolphinn.py
File metadata and controls
53 lines (51 loc) · 1.67 KB
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
import random, itertools
import numpy as np
class Dolphinn:
def __init__(self, P, D, K):
self.P=P
self.D=D
self.K=K
self.h=np.random.multivariate_normal(np.zeros(K), np.eye(K), size=self.D)
X=np.sign(P.dot(self.h))
b=np.array([2**j for j in range(self.K)])
Y=X.dot(b)
self.cube=dict([])
for i in range(len(Y)):
if self.cube.get(int(Y[i]))==None:
self.cube[int(Y[i])]=[]
self.cube[int(Y[i])].append(i)
def queries(self, Q, M, num_of_probes):
n=0
flag=False
combs=np.ones((num_of_probes,self.K))
for r in range(self.K+1):
for c in itertools.combinations(range(self.K),r):
for i in c:
combs[n,i]=-1
n=n+1
if n>=num_of_probes:
flag=True
break
if flag:
break
#Queries
#assign keys to queries
A=np.sign(Q.dot(self.h))
b=np.array([2**j for j in range(self.K)])
solQ=[]
for j in range(len(A)):
cands=[]
N=np.multiply(combs,A[j])
N=N.dot(b)
for k in N:
if self.cube.get(int(k))!= None:
cands.extend(self.cube[int(k)])
if len(cands)>M:
args=np.argpartition([np.linalg.norm(np.subtract(self.P[i],Q[j])) for i in cands],M)
sols=[]
for i in range(M):
sols.append(cands[args[i]])
solQ.append(sols)
else:
solQ.append([-1])
return solQ