-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConstraints.py
349 lines (271 loc) · 14.2 KB
/
Constraints.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
from gurobipy import *
from ilp.variable import Variable
from parsexml.relationtype import RelationType
from parsexml.relation import Relation
class Constraints:
def __init__(self, text_obj, test=False):
self.text_obj = text_obj
if not test:
self.directed_pairs = text_obj.directed_pairs
self.variables = []
self.model = Model("ILP")
self.model.setParam(GRB.Param.OutputFlag,0)
self.relations_optimized = None
# Run model for text_obj
if not test:
self._build_model()
def _build_model(self):
try:
objective_func = LinExpr()
# Create variables
for i, pair in enumerate(self.directed_pairs):
for rel_type in pair.confidence_scores:
x = self.model.addVar(vtype=GRB.BINARY, name="x_"+pair.source.id + pair.target.id + str(rel_type))
v = Variable(x, pair, rel_type)
self.variables.append(v)
objective_func += v.confidence_score * x
self.model.update()
# Set objective function
self.model.setObjective(objective_func, GRB.MAXIMIZE)
# Add constraint i) which says that among all pairs (variables) which represent _one_ relation, there can only be one pair which is set (1).
constraint_i = {}
for pair in self.directed_pairs:
for rel_type in pair.confidence_scores:
if constraint_i.has_key(pair):
constraint_i[pair] += self._get_variable_by_pair_and_rel_type(pair, rel_type)
else:
constraint_i[pair] = self._get_variable_by_pair_and_rel_type(pair, rel_type)
# Add constraint
self.model.addConstr(constraint_i[pair], GRB.EQUAL, 1, "i")
# Add constraint ii) which says that all triples of variables which make up a transitive graph should be activated
constraint_ii = {}
for triple in self._get_all_transitive_variable_triples():
i = triple[0].variable
j = triple[1].variable
k = triple[2].variable
self.model.addConstr(i+j-k, GRB.LESS_EQUAL, 1, "ii")
self.model.optimize()
except GurobiError:
print "Gurobi Error reported"
def _get_all_transitive_variable_triples(self):
triples = []
# A before B && B before C => A before C
rule = ["A", RelationType.BEFORE, "B", "B", RelationType.BEFORE, "C", "A", RelationType.BEFORE, "C"]
triples += self._get_triples_by_rule(rule)
# A before B && B includes C => A before C
rule = ["A", RelationType.BEFORE, "B", "B", RelationType.INCLUDES, "C", "A", RelationType.BEFORE, "C"]
triples += self._get_triples_by_rule(rule)
# A before B && B simultaneous C => A before C
rule = ["A", RelationType.BEFORE, "B", "B", RelationType.SIMULTANEOUS, "C", "A", RelationType.BEFORE, "C"]
triples += self._get_triples_by_rule(rule)
# A includes B && B includes C => A includes C
rule = ["A", RelationType.INCLUDES, "B", "B", RelationType.INCLUDES, "C", "A", RelationType.INCLUDES, "C"]
triples += self._get_triples_by_rule(rule)
# A includes B && B simultaneous C => A includes C
rule = ["A", RelationType.INCLUDES, "B", "B", RelationType.SIMULTANEOUS, "C", "A", RelationType.INCLUDES, "C"]
triples += self._get_triples_by_rule(rule)
# A simultaneous B && B before C => A before C
rule = ["A", RelationType.SIMULTANEOUS, "B", "B", RelationType.BEFORE, "C", "A", RelationType.BEFORE, "C"]
triples += self._get_triples_by_rule(rule)
# A simultaneous B && B includes C => A includes C
rule = ["A", RelationType.SIMULTANEOUS, "B", "B", RelationType.INCLUDES, "C", "A", RelationType.INCLUDES, "C"]
triples += self._get_triples_by_rule(rule)
# A simultaneous B && B simultaneous C => A simultaneous C
rule = ["A", RelationType.SIMULTANEOUS, "B", "B", RelationType.SIMULTANEOUS, "C", "A", RelationType.SIMULTANEOUS, "C"]
triples += self._get_triples_by_rule(rule)
# Deduced
# A simultaneous B && A before C => B before C
rule = ["A", RelationType.SIMULTANEOUS, "B", "A", RelationType.BEFORE, "C", "B", RelationType.BEFORE, "C"]
triples += self._get_triples_by_rule(rule)
# A simultaneous B && A includes C => B includes C
rule = ["A", RelationType.SIMULTANEOUS, "B", "A", RelationType.INCLUDES, "C", "B", RelationType.INCLUDES, "C"]
triples += self._get_triples_by_rule(rule)
# New (inverses)
# A after B && B after C => A after C
rule = ["A", RelationType.AFTER, "B", "B", RelationType.AFTER, "C", "A", RelationType.AFTER, "C"]
triples += self._get_triples_by_rule(rule)
# A after B && B includes C => A after C
rule = ["A", RelationType.AFTER, "B", "B", RelationType.INCLUDES, "C", "A", RelationType.AFTER, "C"]
triples += self._get_triples_by_rule(rule)
# A after B && B simultaneous C => A after C
rule = ["A", RelationType.AFTER, "B", "B", RelationType.SIMULTANEOUS, "C", "A", RelationType.AFTER, "C"]
triples += self._get_triples_by_rule(rule)
# A is_included B && B is_included C => A is_included C
rule = ["A", RelationType.IS_INCLUDED, "B", "B", RelationType.IS_INCLUDED, "C", "A", RelationType.IS_INCLUDED, "C"]
triples += self._get_triples_by_rule(rule)
# A is_included B && B simultaneous C => A is_included C
rule = ["A", RelationType.IS_INCLUDED, "B", "B", RelationType.SIMULTANEOUS, "C", "A", RelationType.IS_INCLUDED, "C"]
triples += self._get_triples_by_rule(rule)
# A is_included B && B after C => A after C
rule = ["A", RelationType.IS_INCLUDED, "B", "B", RelationType.AFTER, "C", "A", RelationType.AFTER, "C"]
triples += self._get_triples_by_rule(rule)
# A is_included B && B before C => A before C
rule = ["A", RelationType.IS_INCLUDED, "B", "B", RelationType.BEFORE, "C", "A", RelationType.BEFORE, "C"]
triples += self._get_triples_by_rule(rule)
return triples
def _get_triples_by_rule(self, rule):
# rule = [r1.source, r1.rel_type, r1.target, r2.source, r2.rel_type, r2.target, r3.source, r3.rel_type, r3.target]
r1_source = rule[0]
r1_rel_type = rule[1]
r1_target = rule[2]
r2_source = rule[3]
r2_rel_type = rule[4]
r2_target = rule[5]
# =>
r3_source = rule[6]
r3_rel_type = rule[7]
r3_target = rule[8]
if r1_source == r2_source:
return self._get_triples_source_equal(rule)
elif r1_source == r2_target:
return self._get_triples_r1_source_r2_target_equal(rule)
elif r1_target == r2_target:
return self._get_triples_target_equal(rule)
elif r1_target == r2_source:
return self._get_triples_r1_target_r2_source_equal(rule)
def _get_triples_r1_target_r2_source_equal(self, rule):
r1_source= rule[0]
r1_rel_type = rule[1]
r2_target = rule[5]
r2_rel_type = rule[4]
r3_source = rule[6]
r3_rel_type = rule[7]
r3_target = rule[8]
triples = []
# Search for variables where r1.target == r2.source
for v1 in self.variables:
for v2 in self.variables:
if v1.target == v2.source:
if v1.relation_type == r1_rel_type and v2.relation_type == r2_rel_type:
transitive = self._find_transitive(v1.source, v2.target, rule)
if transitive:
triples.append([v1, v2, transitive])
return triples
def _get_triples_r1_source_r2_target_equal(self, rule):
r1_source= rule[0]
r1_rel_type = rule[1]
r2_target = rule[5]
r2_rel_type = rule[4]
r3_source = rule[6]
r3_rel_type = rule[7]
r3_target = rule[8]
triples = []
# Search for variables where r1.target == r2.source
for v1 in self.variables:
for v2 in self.variables:
if v1.source == v2.target:
if v1.relation_type == r1_rel_type and v2.relation_type == r2_rel_type:
transitive = self._find_transitive(v1.target, v2.source, rule)
if transitive:
triples.append([v1, v2, transitive])
return triples
def _get_triples_target_equal(self, rule):
r1_source= rule[0]
r1_rel_type = rule[1]
r2_target = rule[5]
r2_rel_type = rule[4]
r3_source = rule[6]
r3_rel_type = rule[7]
r3_target = rule[8]
triples = []
# Search for variables where r1.target == r2.target
for v1 in self.variables:
for v2 in self.variables:
if v1.target == v2.target:
if v1.relation_type == r1_rel_type and v2.relation_type == r2_rel_type:
transitive = self._find_transitive(v1.source, v2.source, rule)
if transitive:
triples.append([v1, v2, transitive])
return triples
def _get_triples_source_equal(self, rule):
r1_target = rule[2]
r1_rel_type = rule[1]
r2_target = rule[5]
r2_rel_type = rule[4]
r3_source = rule[6]
r3_rel_type = rule[7]
r3_target = rule[8]
triples = []
# Search for variables with same source
for v1 in self.variables:
for v2 in self.variables:
if v1.source == v2.source:
if v1.relation_type == r1_rel_type and v2.relation_type == r2_rel_type:
transitive = self._find_transitive(v1.target, v2.target, rule)
if transitive:
triples.append([v1, v2, transitive])
return triples
def _find_transitive(self, source, target, rule):
r3_rel_type = rule[7]
for variable in self.variables:
if variable.source == source and variable.target == target and variable.relation_type == r3_rel_type:
return variable
else:
return None
def _get_variable_by_pair_and_rel_type(self, pair, rel_type):
for variable in self.variables:
if variable.pair == pair and variable.relation_type == rel_type:
return variable.variable
else:
return None
def get_best_set(self):
best_subset = []
for v in self.variables:
# Check if variable has value 1.0 which means it is set
if v.variable.getAttr("x") == 1.0:
relation = self.text_obj.find_relation_by_variable(v)
# Relation does not have to exist
if relation:
# Set predicted_class to generate output files
relation.predicted_class = relation.relation_type
best_subset.append(relation)
else:
# The relation exists but with a different relation type
rel_type = v.relation_type
# Find relation with source and target
relation = self.text_obj.find_relation_by_source_and_target(v.source, v.target)
new_relation = Relation(relation.lid, relation.parent, relation.source, relation.target, rel_type)
# Set predicted_class to generate output files
new_relation.predicted_class = rel_type
best_subset.append(new_relation)
self.relations_optimized = best_subset
return best_subset
def get_number_of_relations_changed(self):
relations = self.text_obj.relations
relations_optimized = self.relations_optimized
changed = 0
ee_changed = 0
et_changed = 0
improvement = 0
degradation = 0
change_with_no_improvemt_and_no_degradation = 0
for rel in relations:
for rel_optimized in relations_optimized:
if rel.source == rel_optimized.source and rel.target == rel_optimized.target:
if rel.predicted_class == rel.relation_type and rel.predicted_class != rel_optimized.relation_type:
# Global model introduced an new error
degradation += 1
print rel.parent.filename
print "Changed Relation %s --%s--> %s to %s --%s--> %s" % (rel.source.id, RelationType.get_string_by_id(rel.predicted_class), rel.target.id, rel_optimized.source.id, RelationType.get_string_by_id(rel_optimized.relation_type), rel_optimized.target.id)
changed += 1
if rel.is_event_event():
ee_changed += 1
if rel.is_event_timex():
et_changed += 1
break
if rel.predicted_class != rel.relation_type and rel.predicted_class != rel_optimized.relation_type:
# Count how many improvements and changes which do not matter the global model makes
if rel.relation_type == rel_optimized.relation_type:
improvement += 1
else:
# The global model changed a wrong label to another wrong label
change_with_no_improvemt_and_no_degradation += 1
print rel.parent.filename
print "Changed Relation %s --%s--> %s to %s --%s--> %s" % (rel.source.id, RelationType.get_string_by_id(rel.predicted_class), rel.target.id, rel_optimized.source.id, RelationType.get_string_by_id(rel_optimized.relation_type), rel_optimized.target.id)
changed += 1
if rel.is_event_event():
ee_changed += 1
if rel.is_event_timex():
et_changed += 1
break
return (changed, ee_changed, et_changed, improvement, degradation, change_with_no_improvemt_and_no_degradation)