Reputation: 390
I'm new to z3 so this may be really easy.
I have some variables and clauses:
d = {
"p0": Bool("p0"),
"p1": Bool("p1"),
"p2": Bool("p2"),
"p3": Bool("p3")
}
d['p4'] = And([d["p0"], Or([d["p1"],d["p2"]])])
d['p5'] = d['p4']
d['p6'] = And([d["p3"], d['p5']])
d['p7'] = And([d['p2'],d['p3']])
I can obtain a satisfying model
s = Solver()
s.add(d['p6'])
s.check()
sol = s.model()
sol ---> [p3 = True, p1 = True, p0 = True, p2 = False]
What's the best and most efficient way to implement a function f(sol,d)
that returns an eval_dict
such that
eval_dict = f(sol,d)
eval_dict ---> {
'p0': True,
'p1': True,
'p2': False,
'p3': True,
'p4': True,
'p5': True,
'p6': True,
'p7': False
}
?
Upvotes: 1
Views: 78
Reputation: 30418
The following function should do:
def modelDict(sol, d):
return {k: sol.evaluate(v, model_completion=True) for k, v in d.items()}
When used with your program, it prints:
>>> print(modelDict(sol, d))
{'p0': True, 'p1': True, 'p2': False, 'p3': True, 'p4': True, 'p5': True, 'p6': True, 'p7': False}
Upvotes: 2