-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib204.py
More file actions
51 lines (37 loc) · 1.28 KB
/
lib204.py
File metadata and controls
51 lines (37 loc) · 1.28 KB
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
from nnf import And, dsharp, NNF, config
class Encoding(object):
def __init__(self):
self.constraints = []
def vars(self):
ret = set()
for c in self.constraints:
ret |= c.vars()
return ret
def size(self):
ret = 0
for c in self.constraints:
ret += c.size()
return ret
def valid(self):
return And(self.constraints).valid()
def negate(self):
return And(self.constraints).negate()
def add_constraint(self, c):
assert isinstance(c, NNF), "Constraints need to be of type NNF"
self.constraints.append(c)
@config(sat_backend="kissat")
def is_satisfiable(self):
return And(self.constraints).satisfiable()
@config(sat_backend="kissat")
def solve(self):
return And(self.constraints).solve()
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()
def likelihood(self, lit):
return self.count_solutions([lit]) / self.count_solutions()