Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tseitin Encoding #6

Merged
merged 19 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nnf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
Model = t.Dict[Name, bool]

__all__ = ('NNF', 'Internal', 'And', 'Or', 'Var', 'Builder', 'all_models',
'decision', 'true', 'false', 'dsharp', 'dimacs', 'amc', 'operators')
'decision', 'true', 'false', 'dsharp', 'dimacs', 'amc', 'tseitin', 'operators')


def all_models(names: 't.Iterable[Name]') -> t.Iterator[Model]:
Expand Down
47 changes: 47 additions & 0 deletions nnf/tseitin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""Transformations using the well-known Tseitin encoding <https://en.wikipedia.org/wiki/Tseytin_transformation>

The Tseitin transformation converts any arbitrary circuit to one in CNF in
polynomial time/space. It does so at the cost of introducing new variables
(one for each logical connective in the formula).
"""

from nnf import NNF, Var, And, Or, Name, true, false
haz marked this conversation as resolved.
Show resolved Hide resolved

def to_cnf(theory: NNF) -> NNF:
haz marked this conversation as resolved.
Show resolved Hide resolved

node_to_var = {}
haz marked this conversation as resolved.
Show resolved Hide resolved
clauses = []

def process_node(node: NNF) -> NNF:
haz marked this conversation as resolved.
Show resolved Hide resolved

if node in node_to_var:
haz marked this conversation as resolved.
Show resolved Hide resolved
return node
haz marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(node, Var):
node_to_var[node] = node
return node

aux = Var("aux_%d" % len(node_to_var))
haz marked this conversation as resolved.
Show resolved Hide resolved
node_to_var[node] = aux
children = [process_node(c) for c in node.children]
haz marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(node, And):
clauses.append(Or([~c for c in children] + [aux]))
haz marked this conversation as resolved.
Show resolved Hide resolved
for c in children:
clauses.append(Or([~aux, c]))

elif isinstance(node, Or):
clauses.append(Or(list(children)+[~aux]))
for c in children:
clauses.append(Or([~c, aux]))

else:
raise TypeError(node)

return aux

root = process_node(theory)
clauses.append(root)
haz marked this conversation as resolved.
Show resolved Hide resolved

return And(clauses)