-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMD.py
66 lines (62 loc) · 1.9 KB
/
MD.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
import KsetEnum
from KsetEnum import *
from time import time
from functools import cmp_to_key
import basestuff
from basestuff import *
import numpy as np
M = None
ids = None
def cmp(x, y):
if x > y:
return 1
if x == y:
return 0
if x < y:
return -1
def maxk_rand(theSet,iter):
n = basestuff.n; d = basestuff.d
id = np.arange(basestuff.n).reshape(basestuff.n,1)
max = 0; i=0
for i in range(iter): #or iter<=stopiter:
w = np.absolute(np.random.randn(d))
s = sorted([[j,basestuff.score(j, w)] for j in range(n)], key = cmp_to_key(lambda x, y: cmp(x[1], y[1])), reverse=True)
k = n
for j in range(n):
if s[j][0] in theSet:
k = j
break
if k>max: max = k
return max
def MDRRR(randkset=True, RandStopTh = 100, ksets = None, turnonlog = False):
global M,ids;
n = basestuff.n;
ids = np.array(range(n)).reshape(n,1)
stats = ""
if ksets == None:
t = time()
ksets = Kset_random()
#ksets = Kset_random(RandStopTh) if randkset else Kset_Enum()
stats += str(time()-t)
# M = np.append(np.zeros(n,len(ksets)),ids,axis=1)
if turnonlog:
st = str(basestuff.d)+','+str(basestuff.k)+','+str(len(ksets))+','+stats
basestuff.mylog(st,'results/MDRRRlog.csv')
M = np.zeros(n*len(ksets)).reshape(n,len(ksets))
for j in range(len(ksets)):
for i in ksets[j]:
M[i,j] = 1
Indx = ~np.all(M == 0, axis=1)
M = M[Indx]; ids = ids[Indx]
return _HSN(),stats
def _HSN():
global M,ids;
n = basestuff.n;
selected = []
while( len(M)>0):
maxR = M.sum(axis=0).argmax()
selected.append(ids[maxR][0])
M = M[:,M[maxR]==0]
Indx = ~np.all(M == 0, axis=1)
M = M[Indx]; ids = ids[Indx]
return selected