def test_simplify_preserves_meaning(sentence: nnf.NNF, merge_nodes): simple = sentence.simplify(merge_nodes) assert sentence.equivalent(simple) for model in sentence.models(): assert simple.satisfied_by(model) for model in simple.models(): assert sentence.condition(model).simplify(merge_nodes) == nnf.true
def test_forget(sentence: nnf.NNF): # Assumption to reduce the time in testing assume(sentence.size() <= 15) # Test that forgetting a backbone variable doesn't change the theory T = sentence & Var('added_var') assert sentence.equivalent(T.forget({'added_var'})) # Test the tseitin projection assert sentence.equivalent(sentence.to_CNF().forget_aux()) # Test that models of a projected theory are consistent with the original names = list(sentence.vars())[:2] T = sentence.forget(names) assert not any([v in T.vars() for v in names]) for m in T.models(): assert sentence.condition(m).satisfiable()
def test_smoothing(sentence: nnf.NNF): if not sentence.smooth(): event("Sentence not smooth yet") smoothed = sentence.make_smooth() assert type(sentence) is type(smoothed) assert smoothed.smooth() assert sentence.equivalent(smoothed) assert smoothed.make_smooth() == smoothed else: event("Sentence already smooth") assert sentence.make_smooth() == sentence
def test_equivalent(sentence: nnf.NNF): assert sentence.equivalent(sentence) assert sentence.equivalent(sentence | nnf.false) assert sentence.equivalent(sentence & (nnf.Var('A') | ~nnf.Var('A'))) if sentence.satisfiable(): assert not sentence.equivalent(sentence & nnf.false) assert not sentence.equivalent(sentence & nnf.Var('A')) else: assert sentence.equivalent(sentence & nnf.false) assert sentence.equivalent(sentence & nnf.Var('A'))
def test_random_equivalent(a: nnf.NNF, b: nnf.NNF): if a.vars() != b.vars(): if a.equivalent(b): event("Equivalent, different vars") assert b.equivalent(a) for model in a.models(): assert b.condition(model).valid() for model in b.models(): assert a.condition(model).valid() else: event("Not equivalent, different vars") assert (any(not b.condition(model).valid() for model in a.models()) or any(not a.condition(model).valid() for model in b.models())) else: if a.equivalent(b): event("Equivalent, same vars") assert b.equivalent(a) assert model_set(a.models()) == model_set(b.models()) else: event("Not equivalent, same vars") assert model_set(a.models()) != model_set(b.models())