z3 icon indicating copy to clipboard operation
z3 copied to clipboard

A question about boolean expression simplification.

Open musophobia opened this issue 6 years ago • 8 comments

Trying to simplify this boolean expression.

(not (and (or (not e) (not f) (not h))
          (or (not f) (not h) d)
          (or (not e) (not h) c)
          (or (not h) d c)
          (or (not e) (not f) (not g) a)
          (or (not f) d (not g) a)
          (or (not e) (not f) a i)
          (or (not f) d a i)
          (or (not e) c (not g) a)
          (or d c (not g) a)
          (or (not e) c a i)
          (or d c a i)
          (or (not e) (not f) a b)
          (or (not f) d a b)
          (or (not e) c a b)
          (or d c a b)))

This should be reduced to a boolean expression like this in cnf form.

(and 
(or (not a) h)
(or (not b) (not i) g h)
(or (not c) f)
(or (not d) e))

I've been trying to achieve this by using "ctx-solver-simplify" and "tseitin-cnf" tactic. If only "ctx-solver-simplify" is applied, no simplification is performed for this case. When both these tactics are applied by then("tseitin-cnf", "ctx-solver-simplify"), a result is returned which contains lots of auxiliary variables. Which is also not the reduced form expected.

Is there a way to simplify this expression to the expected output?

musophobia avatar Nov 26 '20 12:11 musophobia

There isn't a direct way, but you can program such clause extraction procedures around the solver.

Here is a simple method:

from z3 import *
fml = """(declare-const a Bool)
(declare-const b Bool)
(declare-const c Bool)
(declare-const d Bool)
(declare-const e Bool)
(declare-const f Bool)
(declare-const g Bool)
(declare-const h Bool)
(declare-const i Bool)
(assert 
(and (or (not e) (not f) (not h))
          (or (not f) (not h) d)
          (or (not e) (not h) c)
          (or (not h) d c)
          (or (not e) (not f) (not g) a)
          (or (not f) d (not g) a)
          (or (not e) (not f) a i)
          (or (not f) d a i)
          (or (not e) c (not g) a)
          (or d c (not g) a)
          (or (not e) c a i)
          (or d c a i)
          (or (not e) (not f) a b)
          (or (not f) d a b)
          (or (not e) c a b)
          (or d c a b)))"""

s1 = Solver()
s2 = Solver()
s1.from_string(fml)
print(s1)
s2.add(Not(And(s1.assertions())))
s2.set("core.minimize", True)

clauses = []
while sat == s1.check():
    mdl = s1.model()
    decls = mdl.decls()
    core = [d() for d in decls if is_true(mdl[d])] + [Not(d()) for d in decls if is_false(mdl[d])] 
    assert unsat == s2.check(core)
    clause = Or([mk_not(c) for c in s2.unsat_core()])
    clauses += [clause]
    s1.add(clause)


for c in clauses:
    print(c)

NikolajBjorner avatar Nov 26 '20 14:11 NikolajBjorner

Thanks for the great answer! It works like a charm. However, I am trying to make an z3 api out of this. The equivalent code for assert unsat == s2.check(core) below is failing. Am I doing anything wrong here in the loop?

        while (Z3_solver_check(c, s1) == Z3_L_TRUE) {
            Z3_model m = Z3_solver_get_model(c, s1);

            unsigned nFuncs = Z3_model_get_num_funcs(c, m);
            unsigned nConsts = Z3_model_get_num_ consts(c, m);


            for(unsigned i = 0; i < nFuncs; ++i) {
                Z3_func_decl fd = Z3_model_get_func_decl(c, m, i);
                Z3_ast const_itrp = Z3_model_get_const_interp(c, m, fd);

                if ((Z3_get_ast_kind(c, const_itrp) == Z3_APP_AST) && (Z3_OP_TRUE == Z3_get_decl_kind(c, fd))) {
                    Z3_solver_assert(c, s2, const_itrp);
                } else {
                    Z3_solver_assert(c, s2, Z3_mk_not(c, const_itrp));
                }
            }

            for(unsigned i = 0; i < nConsts; ++i) {
                Z3_func_decl cd = Z3_model_get_const_decl(c, m, i);
                Z3_ast const_itrp = Z3_model_get_const_interp(c, m, cd);
                
                if ((Z3_get_ast_kind(c, const_itrp) == Z3_APP_AST) && (Z3_OP_TRUE == Z3_get_decl_kind(c, cd))) {
                    Z3_solver_assert(c, s2, const_itrp);
                } else {
                    Z3_solver_assert(c, s2, Z3_mk_not(c, const_itrp));
                }
            }
            
            if(Z3_solver_check(c, s2) == Z3_L_FALSE) {
                Z3_ast_vector unsat_core = Z3_solver_get_unsat_core(c, s2);
                unsigned sz = Z3_ast_vector_size(c, unsat_core);
                Z3_ast unsat_core_asts[sz];
                for (unsigned i = 0; i < sz; ++i) {
                    unsat_core_asts[i] = Z3_mk_not(c, Z3_ast_vector_get(c, unsat_core, i));
                }
                Z3_ast clause = Z3_mk_or(c, sz, unsat_core_asts);
                Z3_solver_assert(c, s1, clause);
                // add clause to clauses
            } else {
                // assertion failed
            }
        }

musophobia avatar Mar 05 '21 18:03 musophobia

Don't use the plain C API, use the C++ API. For the C API you get into reference counting mistakes. For example

unsat_core_asts[i] = Z3_mk_not(c, Z3_ast_vector_get(c, unsat_core, i));

fails to ensure a reference count on the ast stored in the array entries.

Also, the memory assigned to unsat_core is also invalidated as soon as you create another reference counted object.

Z3_ast_vector unsat_core = Z3_solver_get_unsat_core(c, s2);

You need to use the inc_ref/dec_ref calls

But again, don't use the C API, unless you have read very carefully how it is used.

NikolajBjorner avatar Mar 05 '21 18:03 NikolajBjorner

The reason behind trying to do this is that we think that the Z3 Java API for this code is being slow because of back-and-forth native calls. We want to achieve better performance by invoking a single method from java, for which there will be an equivalent C method(the snippet provided above). I may be mistaken, but I understood that the generated Java native codes connect with C; we cannot access C++ API for this purpose. Is it right?

musophobia avatar Mar 05 '21 20:03 musophobia

Native pointers might be inaccessible due to being marked private in the API, so a mixed-mode setup would have to consider whether the Java API needs to change. Now, looking at your example, the reference counting issue suggests that some more familarity with the assumptions of the API is required before you can successfully embark on a design.

NikolajBjorner avatar Mar 05 '21 23:03 NikolajBjorner

I am trying to go through the existing uses of the reference counts in C API and test codes to understand the uses. I tried with making the inc_ref calls in the above code. However, with and without these inc_ref calls, Z3_get_decl_kind(c, cd) is always returning a fixed value, 45101. Note that I have replaced the is_true(mdl[d]) in the python snippet by (Z3_get_ast_kind(c, const_itrp) == Z3_APP_AST) && (Z3_OP_TRUE == Z3_get_decl_kind(c, cd)).

core = [d() for d in decls if is_true(mdl[d])] + [Not(d()) for d in decls if is_false(mdl[d])]

Aside from the ref_count issues you mentioned, I am wondering if the equivalent C code I have written is correctly equivalent at the first place.

musophobia avatar Mar 06 '21 07:03 musophobia

Thanks for the insightful suggestions! Ended up porting the java codes to cpp instead of c.

Found the suitable replacement of the following python snippet in cpp.

core = [d() for d in decls if is_true(mdl[d])] + [Not(d()) for d in decls if is_false(mdl[d])]
  expr_vector core(c);
  for (unsigned i = 0; i < mdl.size(); i++)
  {
    func_decl d = mdl[i];
    if (mdl.eval(d()).is_true())
    {
      core.push_back(d());
    }
    else
    {
      core.push_back(!d());
    }
  }

musophobia avatar Mar 12 '21 11:03 musophobia

Thanks for this answer! I'm running into the same problem, and this solution works like a magic. I'm new to Z3 (and SAT/SMT), and I'm curious why this solution works. Could anyone explain this to me? Thanks!

VoodooChild99 avatar Jul 12 '22 03:07 VoodooChild99