Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Python/Sympy/sympy/logic/algorithms/     Datei vom 6.10.2026 mit Größe 1 kB image not shown  

SSL pycosat_wrapper.py   Sprache: Python

 

from __future__ import annotations
from sympy.assumptions.cnf import EncodedCNF


def pycosat_satisfiable(expr, all_models=False):
    import pycosat
    if not isinstance(expr, EncodedCNF):
        exprs = EncodedCNF()
        exprs.add_prop(expr)
        expr = exprs

    # Return UNSAT when False (encoded as 0) or empty clause is present in the CNF
    if {0} in expr.data or set() in expr.data:
        if all_models:
            return (f for f in [False])
        return False

    if not all_models:
        r = pycosat.solve(expr.data)
        result = (r != "UNSAT")
        if not result:
            return result
        res = {expr.symbols[abs(lit) - 1]: lit > 0 for lit in r}
        return {True: True} if res == {} else res
    else:
        r = pycosat.itersolve(expr.data)
        result = (r != "UNSAT")
        if not result:
            return result

        # Make solutions SymPy compatible by creating a generator
        def _gen(results):
            satisfiable = False
            try:
                while True:
                    sol = next(results)
                    res = {expr.symbols[abs(lit) - 1]: lit > 0 for lit in sol}
                    yield {True: True} if res == {} else res
                    satisfiable = True
            except StopIteration:
                if not satisfiable:
                    yield False

        return _gen(r)

Messung
CHZG
929510095

¤ Dauer der Verarbeitung: 0.18 Sekunden  ¤

*© Formatika GbR, Deutschland