-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombo_searcher.py
283 lines (229 loc) · 9.4 KB
/
combo_searcher.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
from itertools import product
import random
class Expression:
def __init__(self, op, *children):
self.op = op
self.children = list(children)
self.canonize()
def __hash__(self):
return hash(str(self))
def __eq__(self, other):
return str(self) == str(other)
@property
def associative_set(self):
if not hasattr(self, '_associative_set'):
retval = set()
for child in self.children:
if child.op == self.op:
retval.update(child.associative_set)
else:
retval.add(child)
self._associative_set = retval
return self._associative_set
@property
def child_set(self):
if not hasattr(self, '_child_set'):
self._child_set = set(self.children)
return self._child_set
def __gt__(self, other):
return str(self) > str(other)
def getstr(self):
if self.op.unary:
return f'{self.op.symbol}({self.children[0]})'
else:
return self.op.symbol.join(f'({child})' for child in self.children)
def __str__(self):
if not hasattr(self, '_str'):
self._str = self.getstr()
return self._str
def canonize(self):
startstr = self.getstr()
while True:
for c in self.children:
c.canonize()
nl = []
if self.op == notop and self.children[0].op == notop:
self.op = self.children[0].children[0].op
if type(self.children[0].children[0]) == Expression:
self.children = self.children[0].children[0].children
else:
self.name = self.children[0].children[0].name
self.__class__ = Leaf
break
elif self.op == notop and self.children[0].op == orop:
self.op = andop
a = self.children[0].children[0]
b = self.children[0].children[1]
self.children = [Expression(notop, a), Expression(notop, b)]
elif self.op == notop and self.children[0].op == andop:
self.op = orop
a = self.children[0].children[0]
b = self.children[0].children[1]
self.children = [Expression(notop, a), Expression(notop, b)]
elif self.op == xorop and self.children[0].op == notop and self.children[1].op == notop:
self.children = [self.children[0].children[0], self.children[1].children[0]]
if self.op.associative:
for c in self.children:
if c.op == self.op:
nl += c.children
else:
nl.append(c)
self.children = nl
if self.op.commutative:
self.children.sort()
if self.getstr() == startstr:
break
else:
startstr = self.getstr()
self._str = startstr
@property
def leaves(self):
if not hasattr(self, '_leaves'):
self._leaves = set().union(*[child.leaves for child in self.children])
return self._leaves
class Op:
def __init__(self, symbol = "", associative=False, commutative=False, unary=False):
self.symbol = symbol
self.associative = associative
self.commutative = commutative
self.unary = unary
def __str__(self):
return self.symbol
andop = Op('&', associative = True, commutative=True)
orop = Op('|', associative = True, commutative=True)
xorop = Op('^', commutative = True)
notop = Op('~', unary=True)
leafop = Op('')
class Leaf(Expression):
op = leafop
def __init__(self, name):
self.name = name
def __eq__(self, other):
return str(self) == str(other)
def getstr(self):
return self.name
def __str__(self):
return self.name
def __hash__(self):
return hash(str(self))
@property
def leaves(self):
return {self.name}
def canonize(self):
pass
def test_scoring_function(e):
return random.random()
#used_binops is the binary operators being combined. used_unops is the unary_operators being combined
#used_leaves is the name of all the sources for the ensembles
#minimum_improvement is how much improvement needs to occur each step for a subensemble to be added to the search list
#no_overlap, if True, forbids the same source being used twice in an expression
def get_best_ensembles(score_method=test_scoring_function,
names=['metamap', 'ctakes', 'clamp', 'biomedicus', 'quick_umls'],
used_binops=[andop, orop, xorop],
used_unops = [notop],
minimum_increase=0.1,
no_overlap=True):
tiers = {}
minimum_improvement = minimum_increase
used_leaves = names
def scoref(e):
return score_method(str(e))
tiers[1] = []
leaf_no = len(used_leaves)
for leaf in used_leaves:
leaf = Leaf(leaf)
score = scoref(leaf)
leaf.score = score
tiers[1].append(leaf)
for outsize in range(1, leaf_no+1):
tiers[outsize] = tiers.get(outsize, [])
for insize in range(1, outsize):
lsize = insize
rsize = outsize - insize
for l, r in product(tiers[lsize], tiers[rsize]):
if no_overlap:
if not l.leaves.isdisjoint(r.leaves):
continue
for b in used_binops:
e = Expression(b, l, r)
if e in tiers[outsize]:
continue
lscore = l.score
rscore = r.score
escore = scoref(e)
e.score = escore
compscore = max(lscore, rscore)
if escore > compscore + minimum_improvement:
print(e, e.score)
tiers[outsize].append(e)
appendend = []
for negator in tiers[outsize]:
for unop in used_unops:
if negator.op == notop:
break
e = Expression(unop, negator)
escore = scoref(e)
e.score = escore
if e.score > leaf.score + minimum_improvement:
print(e, e.score)
appendend.append(e)
tiers[outsize] += appendend
retval = []
for v in tiers.values():
for vv in v:
retval.append((str(vv), vv.score))
retval.sort(key=lambda x: x[1], reverse=True)
return retval
##associative
aa = Expression(orop, Expression(orop, Leaf('a'), Leaf('b')), Leaf('c'))
bb = Expression(orop, Expression(orop, Leaf('b'), Leaf('c')), Leaf('a'))
print('commutative should be true', aa == bb)
print(str(aa), str(bb))
aa = Expression(xorop, Expression(xorop, Leaf('a'), Leaf('b')), Leaf('c'))
bb = Expression(xorop, Expression(xorop, Leaf('b'), Leaf('c')), Leaf('a'))
print('associative should not be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(xorop, Expression(xorop, Leaf('a'), Leaf('b')), Leaf('c'))
bb = Expression(xorop, Expression(xorop, Leaf('b'), Leaf('a')), Leaf('c'))
print('commutative should be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(notop, Expression(andop, Leaf('a'), Leaf('b')))
bb = Expression(orop, Expression(notop, Leaf('a')), Expression(notop, Leaf('b')))
print('demorgans 1 should be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(notop, Expression(orop, Leaf('a'), Leaf('b')))
bb = Expression(andop, Expression(notop, Leaf('a')), Expression(notop, Leaf('b')))
print('demorgans 2 should be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(notop, Expression(orop, Leaf('a'), Leaf('b')))
bb = Expression(andop, Expression(notop, Leaf('a')), Expression(notop, Leaf('b')))
print('demorgans 3 should be true', bb==aa)
print(str(aa), str(bb))
aa = Expression(notop, Expression(andop, Leaf('a'), Leaf('b')))
bb = Expression(orop, Expression(notop, Leaf('a')), Expression(notop, Leaf('b')))
print('demorgans 4 should be true', bb==aa)
print(str(aa), str(bb))
aa = Expression(notop, Expression(andop, Leaf('a'), Leaf('b')))
bb = Expression(orop, Expression(notop, Leaf('a')), Expression(andop, Leaf('b')))
print('demorgans difference 1 should not be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(notop, Expression(andop, Leaf('a'), Leaf('b')))
bb = Expression(andop, Expression(notop, Leaf('a')), Expression(notop, Leaf('b')))
print('demorgans difference 2 should not be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(notop, Expression(orop, Leaf('a'), Leaf('b')))
bb = Expression(orop, Expression(notop, Leaf('a')), Expression(notop, Leaf('b')))
print('demorgans difference 3 should not be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(notop, Expression(orop, Leaf('a'), Leaf('b')))
bb = Expression(orop, Expression(notop, Leaf('a')), Expression(andop, Leaf('b')))
print('demorgans difference 4 should not be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(notop, Expression(andop, Leaf('a'), Leaf('b')))
bb = Expression(orop, Expression(notop, Leaf('a')), Expression(notop, Leaf('c')))
print('demorgans difference 5 should not be true', aa==bb)
print(str(aa), str(bb))
aa = Expression(xorop, Leaf('a'), Leaf('b'))
bb = Expression(xorop, Expression(notop, Leaf('a')), Expression(notop, Leaf('b')))
print('demorgans xor should be true', aa==bb)
print(str(aa), str(bb))