This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
maxeu.py
504 lines (421 loc) · 20.4 KB
/
maxeu.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
"""
ProbLog expected utility computation task using aProbLog mechanics.
Copyright 2019 KU Leuven, DTAI Research Group
"""
import sys
import time
from collections import namedtuple
import graphviz
from graphviz import Digraph
from ulearner import PrinterDefault
from problog.sdd_formula_explicit import x_constrained_named
from problog.program import PrologFile, PrologString
from problog.engine import DefaultEngine
from problog import get_evaluatable
from problog.evaluator import Semiring
from problog.logic import Term, Constant, Not
from problog.util import init_logger
from problog.sdd_formula import SDD
from problog.sdd_formula import x_constrained
from problog.formula import BaseFormula, LogicFormula
printer = PrinterDefault()
pn_weight = namedtuple('pos_neg_weight', 'p_weight, n_weight')
def main(argv):
args = argparser().parse_args(argv)
inputfile = args.inputfile
init_logger(args.verbose)
pl = PrologFile(inputfile)
return map_task(pl, evaluatable_name='sddx')
def get_best_decision(db):
engine = DefaultEngine(label_all=True, keep_order=True)
utilities = dict(engine.query(db, Term('utility', None, None)))
true_term = Term('true')
pl_queries = [true_term]
queries = set(pl_queries).union(set(utilities.keys()))
# Ground as utilities
lf = engine.ground_all(db, queries=queries) # type: LogicFormula
# Decisions
decisions = []
decision_term = Term("?")
for _, n, type in lf:
if type == 'atom' and n.probability == decision_term: #TODO remove type == 'atom' ??
decisions.append(n.name)
# SDDX
kc_class = get_evaluatable(name='sddx')
var_constraint = x_constrained_named(X_named=decisions)
starttime_compilation = time.time()
kc = kc_class.create_from(lf, var_constraint=var_constraint)
endtime_compilation = time.time()
compile_time = endtime_compilation - starttime_compilation
printer.print("Compilation took %s seconds." % compile_time)
decision_dict = {kc.get_node_by_name(decision): decision for decision in decisions}
decision_keys = {*decision_dict.keys()}
semiring = SemiringMAXEU(decision_keys)
# Fix weights
starttime_weights = time.time()
weights = _get_fixed_weights(kc, semiring, utilities, decisions, decision_keys)
endtime_weights = time.time()
weight_time = endtime_weights - starttime_weights
printer.print("Fixing weights took %s seconds" % weight_time)
#print("\nFixed weights: %s" % weights)
# query True
starttime_evaluation = time.time()
results = {x: kc.evaluate(index=kc.get_node_by_name(x), semiring=semiring, weights=weights) for x in pl_queries}
endtime_evaluation = time.time()
evaluation_time = endtime_evaluation - starttime_evaluation
printer.print("Circuit evaluation took %s seconds." % evaluation_time)
# Map decision keys to terms
temp_dict = dict()
for key, term in decision_dict.items():
if key == 0:
temp_dict[key] = term
elif not isinstance(term, Not):
temp_dict[key] = term
temp_dict[-key] = Not('not', term)
elif isinstance(term, Not):
temp_dict[key] = term
temp_dict[-key] = term.args[0]
decision_dict = temp_dict
p, eu, d_set = results[true_term]
d_dict = {decision_dict[abs(d_key)]: 1 if d_key >= 0 else 0 for d_key in d_set}
runtime = evaluation_time + compile_time + weight_time
size = kc.get_manager().get_manager().size()
printer.print("Best decisions %s" % d_dict)
printer.print("Expected utility %s" % eu)
printer.print("Compile time %s" % compile_time)
printer.print("Runtime %s" % runtime)
return d_dict, eu, size, compile_time, runtime
#for result_key in results:
# p, eu, d_set = results[result_key]
# d_set_new = {("" if d_key >= 0 else "-") + str(decision_dict[abs(d_key)]) for d_key in d_set}
# results[result_key] = (p, eu, d_set_new)
#return results[true_term][2], results[true_term][1]
def map_task(pl: PrologString, evaluatable_name=None):
"""
Compute the expected utility using the queries and model described by pl.
:param pl: The PrologString to compute the expected utility of. Syntax: utility(attribute,utility). and ?::decision.
:param evaluatable_name: The preferred evaluatable. e.g. 'ddnnf'. Beware: the SDD and BDD classes are using an
encoding that can yield wrong results.
:return: dict {name : (prob, expected_utility)}. The expected utility of a query is defined as the sum of the
expected utilities of each world satisfying the query. The expected utility of a world is defined as the
probability of the world multiplied with the sum of the utilities in that world.
"""
engine = DefaultEngine(label_all=True, keep_order=True)
db = engine.prepare(pl)
return map_task_db(db, evaluatable_name)
def map_task_db(db, evaluatable_name=None):
"""
Compute the expected utility using the queries and model described by pl.
:param db: The database to compute the expected utility from.
:param evaluatable_name: The preferred evaluatable. e.g. 'ddnnf'. Beware: the SDD and BDD classes are using an
encoding that can yield wrong results.
:return: dict {name : (prob, expected_utility)}. The expected utility of a query is defined as the sum of the
expected utilities of each world satisfying the query. The expected utility of a world is defined as the
probability of the world multiplied with the sum of the utilities in that world.
"""
engine = DefaultEngine(label_all=True, keep_order=True)
utilities = dict(engine.query(db, Term('utility', None, None)))
pl_queries = [q[0] for q in engine.query(db, Term('query', None))]
queries = set(pl_queries).union(set(utilities.keys()))
# Ground as utilities
lf = engine.ground_all(db, queries=queries) # type: LogicFormula
# Decisions
decisions = []
decision_term = Term("?")
for _, n, type in lf:
if type == 'atom' and n.probability == decision_term: #TODO remove type == 'atom' ??
decisions.append(n.name)
#print.printer("decisions %s" % decisions)
#print.printer("lf")
#print.printer(lf)
# SDD
kc_class = get_evaluatable(name=evaluatable_name)
var_constraint = x_constrained_named(X_named=decisions)
starttime_compilation = time.time()
kc = kc_class.create_from(lf, var_constraint=var_constraint)
endtime_compilation = time.time()
printer.print("Compilation took %s seconds." % (endtime_compilation - starttime_compilation))
decision_dict = {kc.get_node_by_name(decision): decision for decision in decisions}
decision_keys = {*decision_dict.keys()}
semiring = SemiringMAXEU(decision_keys)
# Fix weights
starttime_weights = time.time()
weights = _get_fixed_weights(kc, semiring, utilities, decisions, decision_keys)
endtime_weights = time.time()
printer.print("Fixing weights took %s seconds" % (endtime_weights - starttime_weights))
printer.print("\nFixed weights: %s" % weights)
# query True
starttime_evaluation = time.time()
results = {x: kc.evaluate(index=kc.get_node_by_name(x), semiring=semiring, weights=weights) for x in pl_queries}
endtime_evaluation = time.time()
printer.print("Circuit evaluation took %s seconds." % (endtime_evaluation - starttime_evaluation))
# Map decision keys to decision names.
for result_key in results:
p, eu, d_set = results[result_key]
d_set_new = {("" if d_key >= 0 else "-") + str(decision_dict[abs(d_key)]) for d_key in d_set}
results[result_key] = (p, eu, d_set_new)
printer.print("Results: %s\n" % results)
view = False
if view:
#dot = kc.to_dot(use_internal=True)
kc.get_manager().get_manager().garbage_collect() #.minimize()
dot = kc.sdd_to_dot(node=None, litnamemap=True)
b = graphviz.Source(dot)
b.view()
#print(kc.get_manager().get_manager())
return results
def _get_fixed_weights(kc: BaseFormula, semiring: Semiring, utilities, decision_names, decision_keys):
"""
Get the weights present in kc, adjusted with the weights provided in utilities.
A weight for the 0 node is used to keep track of the costs for the utility variables which are always true.
The negative weight of 0 is the relevant weight as the -0 weight will override the 0 weight.
:param kc: The BaseFormula of which to retrieve existing weights.
:param semiring: The semiring that will be used for evaluation.
:param utilities: dict of type {name : external_weight} (external weights are those given to the semiring to get
the value of.) The utility variables to adjust the weight of.
:return: dict of type {name : external_weight} The weights of kc, adjusted with the weights of utilities.
When both a positive and negative weight are specified, the problog.formula.pn_weight namedtuple is used.
"""
current_weights = dict(kc.get_weights())
current_weights[0] = pn_weight(Term('s', 1.0, Constant(0), set()), Term('s', 0.0, Constant(0), set()))
printer.print("start decisions: %s" % decision_keys)
printer.print('start utilities: %s' % utilities)
for decision_key in decision_keys:
# TODO: if key is None?
abs_key = abs(decision_key)
current_weights[abs_key] = pn_weight(Term('s', 1.0, Constant(0), {abs_key}), Term('s', 1.0, Constant(0), {-abs_key}))
# Fill in current_weights: key -> weight
for util in utilities:
cost_cst = utilities[util]
cost = utilities[util].compute_value()
key = kc.get_node_by_name(util)
if key is None:
continue # TODO Possible?
else:
current_weight = current_weights.get(abs(key))
#if current_weight is None: # Indicator variable
# current_weight = pn_weight(Term('s', 1.0, Constant(0)), Term('s', 1.0, Constant(0)))
l_set = {key} if abs(key) in decision_keys else set()
l_set_opposite = {-key} if len(l_set) else set()
if key == 0: # True nodes, p_w = n_w because -0 will override 0.
cur_pos_weight, neg_weight = current_weight
new_cost = Constant(cur_pos_weight.args[1].compute_value() + cost)
new_l_set = cur_pos_weight.args[2] | l_set
new_pos_weight = Term('s', 1.0, new_cost, new_l_set)
current_weights[key] = pn_weight(new_pos_weight, neg_weight)
elif key > 0:
if current_weight is True:
pos_weight = Term('s', 1.0, cost_cst, l_set)
neg_weight = Term('s', 1.0, Constant(0), l_set_opposite)
current_weights[key] = pn_weight(pos_weight, neg_weight)
elif isinstance(current_weight, Constant):
pos_weight = Term('s', current_weight.compute_value(), cost_cst, l_set)
neg_weight = Term('s', 1 - current_weight.compute_value(), Constant(0), l_set_opposite)
current_weights[key] = pn_weight(pos_weight, neg_weight)
elif isinstance(current_weight, Term):
prob = current_weight.args[0].compute_value()
new_cost = Constant(current_weight.args[1].compute_value() + cost)
pos_weight = Term('s', prob, new_cost, l_set)
neg_weight = Term('s', 1 - prob, Constant(0), l_set_opposite)
current_weights[key] = pn_weight(pos_weight, neg_weight)
elif isinstance(current_weight, pn_weight):
pos_weight, neg_weight = current_weight
pos_new_cost = Constant(pos_weight.args[1].compute_value() + cost)
new_pos_weight = Term('s', pos_weight.args[0], pos_new_cost, pos_weight.args[2] | l_set)
new_neg_weight = Term('s', neg_weight.args[0], neg_weight.args[1], neg_weight.args[2] | l_set_opposite)
current_weights[key] = pn_weight(new_pos_weight, new_neg_weight)
else:
if current_weight is True:
pos_weight = Term('s', 1.0, Constant(0), l_set_opposite)
neg_weight = Term('s', 1.0, cost_cst, l_set)
current_weights[abs(key)] = pn_weight(pos_weight, neg_weight)
elif isinstance(current_weight, Constant):
prob = current_weight.compute_value()
neg_weight = Term('s', 1 - prob, cost_cst, l_set)
pos_weight = Term('s', prob, Constant(0), l_set_opposite)
current_weights[abs(key)] = pn_weight(pos_weight, neg_weight)
elif isinstance(current_weight, Term):
prob = current_weight.args[0].compute_value()
new_cost = Constant(current_weight.args[1].compute_value() + cost)
neg_weight = Term('s', 1 - prob, new_cost, l_set) # TODO not sure if this is correct
pos_weight = Term('s', prob, Constant(0), l_set_opposite)
current_weights[abs(key)] = pn_weight(pos_weight, neg_weight)
elif isinstance(current_weight, pn_weight):
pos_weight, neg_weight = current_weight
neg_new_cost = Constant(neg_weight.args[1].compute_value() + cost)
new_neg_weight = Term('s', neg_weight.args[0], neg_new_cost, neg_weight.args[2] | l_set)
new_pos_weight = Term('s', pos_weight.args[0], pos_weight.args[1], pos_weight.args[2] | l_set_opposite)
current_weights[abs(key)] = pn_weight(new_pos_weight, new_neg_weight)
printer.print("final current_weights %s" % current_weights)
# Convert current_weights to util_name -> weight
result = dict()
for name in set(decision_names).union(utilities):
key = kc.get_node_by_name(name)
if key is not None:
if key >= 0:
result[name] = current_weights[key]
else:
value = current_weights[abs(key)]
result[name] = pn_weight(value.n_weight, value.p_weight)
return result
class SemiringMAXEU(Semiring):
"""
A pseudo semiring to maximise the expected utility. It is only a semiring within an X-constrained setting.
Each element of this semiring is a triple (prob, eu, decision_set) where the last element is used to keep track
of the decisions made so far in this node. Evidently, a times will result in union. A plus results in choosing
between different decision sets and thus a max-like operation.
"""
# element = (prob, eu, decision_set)
def __init__(self, decisions):
"""
:param decisions: A set of all possible positive decision keys
:type decisions: set[int]
"""
Semiring.__init__(self)
all_decisions = {-x for x in decisions}
all_decisions |= decisions
self.val_zero = (0.0, 0.0, all_decisions)
self.zero_decision_length = len(all_decisions)
def one(self):
return 1.0, 0.0, set()
def zero(self):
return self.val_zero
def is_one(self, value):
p, eu, d = value
return 1.0 - 1e-12 < p < 1.0 + 1e-12 and 1.0 - 1e-12 < eu < 1.0 + 1e-12 and len(d) == 0
def is_zero(self, value):
p, eu, d = value
return -1e-12 < p < 1e-12 and -1e-12 < eu < 1e-12
def plus(self, a, b):
p_a, eu_a, d_a = a
p_b, eu_b, d_b = b
if len(d_a) or len(d_b): # 'or' because of ad_complement which has empty decision set.
if len(d_b) == self.zero_decision_length: # avoid false
result = a
elif len(d_a) == self.zero_decision_length: # avoid false
result = b
elif p_a == 0:
result = b
elif p_b == 0:
result = a
elif eu_a / p_a >= eu_b / p_b: # check on eu
result = a
else:
result = b
#print("MAX a %s b %s = %s" % (a,b,result))
return result
else:
result = p_a + p_b, eu_a + eu_b, set()
#print("plus a %s b %s = %s" % (a, b, result))
return result
def times(self, a, b):
p_a, eu_a, d_a = a
p_b, eu_b, d_b = b
eu_n = p_a * eu_b + p_b * eu_a
result = p_a * p_b, eu_n, d_a.union(d_b)
#print("times a %s b %s = %s" % (a, b, result))
return result
def normalize(self, a, z):
p_a, eu_a, d_a = a
p_z, eu_z, d_z = z
# each world I has p(I) and eu(I) = p(I) * u(I).
# Normalizing the probability of the world (p(I)/p(Z) results p(I) / p(Z) * u(I) = eu(I) / p(Z)
# Since total result = Sum_I [p(I) * u(I)]
#print("normaling %s with %s" % (a,z))
return p_a / p_z, eu_a / p_z, d_a
def negate(self, a):
return 1 - a[0], 0, a[2]
def value(self, a):
if type(a) is Constant:
return float(a), 0, set()
elif type(a) is Term and a.functor == '?':
return 1, 0, set()
elif type(a) is Term:
return float(a.args[0]), (float(a.args[0]) * float(a.args[1])), a.args[2]
else:
raise ValueError("Could not interpret %s during conversion from external to internal representation." % a)
def pos_value(self, a, key=None):
if isinstance(a, pn_weight):
return self.value(a.p_weight)
else:
return self.value(a)
def neg_value(self, a, key=None):
if isinstance(a, pn_weight):
return self.value(a.n_weight)
else:
return self.negate(self.value(a))
def is_dsp(self):
return True
def is_nsp(self):
return True
def in_domain(self, a):
return 0.0 - 1e-9 <= a[0] <= 1.0 + 1e-9
def to_evidence(self, pos_weight, neg_weight, sign):
# Note: When eu = 0 because of p = 0 and now we set p = 1 then the eu can not be reconstructed and stays 0.
p_p, p_eu, p_d = pos_weight
n_p, n_eu, n_d = neg_weight
if sign > 0:
return pos_weight, (0.0, 0.0, n_d)
else:
return (0.0, 0.0, p_d), neg_weight
# if sign > 0:
# if p_p == 0: # normally does not happen because of inconsistentEvidenceError thrown before.
# return (1.0, 0.0, p_d), (0.0, 0.0, n_d)
# else:
# return (1.0, p_eu / p_p, p_d), (0.0, 0.0, n_d) # positive weight rescaled to p=1
# else:
# if n_p == 0: # normally does not happen because of inconsistentEvidenceError thrown before.
# return (0.0, 0.0, p_d), (1.0, 0.0, n_d)
# else:
# return (0.0, 0.0, p_d), (1.0, n_eu / n_p, n_d) # negative weight rescaled to p=1
def ad_negate(self, pos_weight, neg_weight):
p_p, p_eu, p_d = pos_weight
n_p, n_eu, n_d = neg_weight
neg_d = {-x for x in p_d}
if n_p == 0:
return 1.0, n_eu, neg_d # TODO can this happen?
else:
return 1.0, n_eu / n_p, neg_d
def ad_complement(self, ws, key=None):
p, eu, d = ws[0]
if len(d):
return (0.0, 0.0, set())
else: # normal p procedure + decision checks
s = self.zero()
for w in ws:
if len(w[2]):
return None # Trigger InvalidValueError. Must not mix decisions and probabilities #TODO Redo compliment check. Have a is_valid_ad method which returns a tuple of True/False and a message. Deprecate in_domain?
s = self.plus(s, w)
return self.negate(s)
def kc_to_dot(kc):
g = Digraph('G', filename='hello.gv')
for i, n, t in kc:
if t == 'conj':
g.node(str(i), label='*')
for j in n.children:
if(j >= 0):
g.edge(str(i), str(j), dir='back')
else:
g.edge(str(i), str(abs(j)), label=str(j), dir='back')
elif t == 'disj':
g.node(str(i), label='+')
for j in n.children:
if(j >= 0):
g.edge(str(i), str(j), dir='back')
else:
g.edge(str(i), str(abs(j)), label=str(j), dir='back')
elif t == 'atom':
g.node(str(i), label=str(n.identifier) + ", " + str(n.name))
else:
print("i:" + str(i) + " " + str(n))
g.view(cleanup=True)
def argparser():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('inputfile')
parser.add_argument('-o', '--output', type=str, default=None,
help='Write output to given file (default: write to stdout)')
parser.add_argument('-v', '--verbose', action='count', help='Increase verbosity')
return parser
if __name__ == '__main__':
main(sys.argv[1:])