-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsat.py
More file actions
50 lines (45 loc) · 1.72 KB
/
Copy pathsat.py
File metadata and controls
50 lines (45 loc) · 1.72 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
class Sat(object):
def __init__(self, file):
"""Read DIMACS CNF format"""
self.clauses = []
for line in file:
if line.startswith('c'):
continue
elif line.startswith('p'):
meta_data = line.split()
self.variables_count = int(meta_data[2])
self.clauses_count = int(meta_data[3])
elif line.startswith('w'):
self.weights = []
self.total_weight = 0
for var in line.split()[1:]:
self.total_weight += int(var)
self.weights.append(int(var))
else:
clause = []
for var in line.split()[:-1]:
clause.append(int(var))
# because of invalid row
if len(clause) == 3:
self.clauses.append(clause)
def get_ratios(self, configuration):
satisfied_count = 0
for clause in self.clauses:
# print(Sat.is_satisfied(clause, configuration))
if Sat.is_satisfied(clause, configuration):
satisfied_count += 1
c = float(satisfied_count) / self.clauses_count
w = float(self.get_weight(configuration)) / self.total_weight
return c, w
@staticmethod
def is_satisfied(clause, configuration):
for x in clause:
if (x > 0 and configuration[abs(x)-1]) or (x < 0 and not configuration[abs(x)-1]):
return True
return False
def get_weight(self, configuration):
weight_usage = 0
for idx, val in enumerate(configuration):
if val:
weight_usage += self.weights[idx]
return weight_usage