Beispiel #1
0
 def test_dsharp_compile(sentence: And[Or[Var]]):
     compiled = dsharp.compile(sentence)
     compiled_smooth = dsharp.compile(sentence, smooth=True)
     assert compiled.decomposable()
     assert compiled_smooth.decomposable()
     assert compiled_smooth.smooth()
     if sentence.satisfiable():  # See nnf.dsharp.__doc__
         assert sentence.equivalent(compiled)
         assert sentence.equivalent(compiled_smooth)
Beispiel #2
0
 def test_dsharp_compile_uf20():
     sentence = uf20_cnf[0]
     compiled = dsharp.compile(sentence)
     compiled_smooth = dsharp.compile(sentence, smooth=True)
     assert sentence.equivalent(compiled)
     assert sentence.equivalent(compiled_smooth)
     assert compiled.decomposable()
     assert compiled_smooth.decomposable()
     assert compiled_smooth.smooth()
 def count_solutions(self, lits=[]):
     if lits:
         T = And(self.constraints + lits)
     else:
         T = And(self.constraints)
     return dsharp.compile(T.to_CNF(),
                           executable='bin/dsharp').model_count()
Beispiel #4
0
 def test_dsharp_compile_converting_names(sentence: And[Or[Var]]):
     sentence = And(
         Or(Var(str(var.name), var.true) for var in clause)
         for clause in sentence)
     compiled = dsharp.compile(sentence)
     assert all(isinstance(name, str) for name in compiled.vars())
     if sentence.satisfiable():
         assert sentence.equivalent(compiled)
Beispiel #5
0
    def count_solutions(self, lits=[]):
        if lits:
            T = And(self.constraints + lits)
        else:
            T = And(self.constraints)
        if not T.satisfiable():
            return 0

        return dsharp.compile(T.to_CNF(), executable='bin/dsharp', smooth=True).model_count()
Beispiel #6
0
def count_solutions(base_formula, lits=[]):
    """Counts the number of solutions to a given formula."""
    def _nnfify(lit):
        if type(lit).__name__ == "CustomNNF":
            assert lit.typ == 'not', "Literal must be a variable or negated variable."
            return ~(lit.args[0].args[0])
        else:
            return lit._var

    T = base_formula
    if lits:
        T = T & And([_nnfify(l) for l in lits])

    if not T.satisfiable():
        return 0

    return dsharp.compile(T.to_CNF(), smooth=True).model_count()