-
Notifications
You must be signed in to change notification settings - Fork 1
/
cnfformula.py
61 lines (50 loc) · 1.81 KB
/
cnfformula.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""This class is a *very basic* way of building and manipulating CNF formulas and helps to output them in the DIMACS-format.
This file is part of the concealSATgen project.
Copyright (c) 2021 Jan-Hendrik Lorenz and Florian Wörz
Institute of Theoretical Computer Science
Ulm University, Germany
"""
class CNF(object):
def __init__(self, n):
# Initial the CNF is empty
self.clauses = []
self.n = n
self.header = "Default header"
#
# Implementation of class methods
#
def __len__(self):
"""Number of clauses in the formula
"""
return len(self.clauses)
#
# Methods for building the CNF
#
def add_clause(self, clause):
"""Adds a clause to the CNF object.
"""
if not hasattr(clause, '__iter__'):
raise TypeError("Invalid clause. A clauses must be iterable.")
if not all((type(lit) is int) and (type(truth) is bool) for (truth, lit) in clause):
raise TypeError("All literals have to be integers.")
if any((lit > self.n) or (lit == 0) for (_, lit) in clause):
raise ValueError(f"Invalid clause. The clause may only contain positive/negative integers up to {self.n}/{-self.n}.")
if len( set( [lit for (_, lit) in clause] ) ) != len(clause):
raise ValueError("This class does not accept tautological clauses or clauses with reappearing literals.")
clause_list = [lit if truth else -lit for (truth, lit) in clause]
self.clauses.append(clause_list)
def dimacs(self):
"""Converts the CNF file to the DIMACS format.
"""
output = ""
# Write header
for header_line in self.header.split("\n"):
output += "c " + header_line + "\n"
# Write formula specification
output += f"p cnf {self.n} {len(self.clauses)}\n"
# Write clauses
for cls in self.clauses:
clause_string = ' '.join([str(lit) for lit in cls])
clause_string += " 0\n"
output += clause_string
return output