-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.py
162 lines (99 loc) · 3.38 KB
/
merge.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
from matrix import mat , show , subscripts , parse
from information import comparator , Oracle
from math import log
from dynamic import tableau
def merge ( partial , P , oracle , e , m , n , verbose = 0 ) :
i = m
j = n
while i > 0 and j > 0 :
if verbose >= 2 :
callback( "partial information" , i , j )
callback( show( P ) , end = "" )
callback( "tableau" , i , j )
callback( show( e ) , end = "" )
# A_m > B_n
if partial( i - 1 , j - 1 ) > 0 :
if verbose >= 2 : callback( "I know that A_m > B_n, so I output A_m and decrease m" )
yield "a[%d]" % i
i -= 1
# A_m < B_n
elif partial( i - 1 , j - 1 ) < 0 :
if verbose >= 2 : callback( "I know that A_m < B_n, so I output B_n and decrease n" )
yield "b[%d]" % j
j -= 1
# e(P(A_m < B_n)) / e(P) > 2/3
# =>
# e(P(B_n < A_m)) / e(P) < 1/3
elif 3 * e[i-1][j] < e[i][j] :
if verbose >= 2 : callback( "A_m > B_n with probability < 1/3" )
r = j - 1
# By Linial's theorem we are guaranteed to break out this loop.
while not e[i][j] <= 3 * e[i][r-1] <= 2 * e[i][j] : r -= 1
if oracle( i - 1 , r - 1 ) < 0 :
while j >= r :
yield "b[%d]" % j
j -= 1
else :
P[i-1][r-1] = 1 # update partial information
e[i][r] = e[i-1][r] # update lin. ext. count
r += 1
while r <= j :
e[i][r] = e[i-1][r] + e[i][r-1] # update lin. ext. count
r += 1
# e(P(A_m < B_n)) / e(P) < 1/3
elif 3 * e[i][j-1] < e[i][j] :
if verbose >= 2 : callback( "A_m < B_n with probability < 1/3" )
r = i - 1
# By Linial's theorem we are guaranteed to break out this loop.
while not e[i][j] <= 3 * e[r-1][j] <= 2 * e[i][j] : r -= 1
if oracle( r - 1 , j - 1 ) > 0 :
while i >= r :
yield "a[%d]" % i
i -= 1
else :
P[r-1][j-1] = -1 # update partial information
e[r][j] = e[r][j-1] # update lin. ext. count
r += 1
while r <= i :
e[r][j] = e[r][j-1] + e[r-1][j] # update lin. ext. count
r += 1
else :
# we can simply compare A_m with B_n
P[i-1][j-1] = oracle( i - 1 , j - 1 )
while i > 0 :
yield "a[%d]" % i
i -= 1
while j > 0 :
yield "b[%d]" % j
j -= 1
def main ( partial , total , verbose ) :
P , m , n = parse( partial )
if verbose >= 2 :
print( "partial information" )
print( show( P ) , end = "" )
T , m , n = parse( total )
oracle = Oracle( T )
if verbose >= 2 :
print( "total information" )
print( show( T ) , end = "" )
partial = comparator( P )
e = tableau( partial , m , n )
eP = e[m][n]
ITLB = log( eP , 2 )
UB = ITLB / log((1+5**(1/2))/2,2)
print( *list( reversed( list( merge( partial , P , oracle , e , m , n , verbose ) ) ) ) , sep = " < " )
if verbose :
print( "e(P) :" , eP )
print( "ITLB :" , ITLB )
print( "UB = 1.44.. ITLB :" , UB )
print( "N = total queries :" , len( oracle ) )
print( "N / ITLB :" , len( oracle ) / ITLB )
print( "N / UB :" , len( oracle ) / UB )
if __name__ == "__main__" :
import sys , fileinput , argparse
parser = argparse.ArgumentParser(description='Merge A and B.')
parser.add_argument('poset', metavar='P' , help='the partial information to use')
parser.add_argument('oracle', metavar='T' , help='the oracle')
parser.add_argument('-v' , '--verbose', default = 0 , action='count', help='make output verbose')
args = parser.parse_args( )
main( fileinput.input( [ args.poset ] ) , fileinput.input( [ args.oracle ] ) , args.verbose )