Evaluating assigned variables and clauses in Z3?

Question:

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
}

?

Asked By: RonenKi

||

Answers:

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}
Answered By: alias
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.