-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBMC.py
82 lines (62 loc) · 2.89 KB
/
BMC.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
from Aiger2Model import parse_aiger, aiger_to_cnf, cnf_to_string, write_cnf_file
input_folder = "aiger-safety-properties" # 存放 AIGER 文件的文件夹
output_file = "test_results.txt" # 用于保存测试结果的文件
K = 100 # K步展开
MaxK = 100 # 最大K步
def solve_sat(cnf_clauses):
from pysat.solvers import Minisat22
with Minisat22() as solver:
for clause in cnf_clauses:
solver.add_clause(clause)
return solver.solve()
def list_files_recursive(folder):
for root, dirs, files in os.walk(folder):
for file in files:
file_path = os.path.join(root, file)
yield file_path
def main():
# 遍历文件夹中的所有文件
with open(output_file, 'w') as f_out:
for file_name in list_files_recursive(input_folder):
if file_name.endswith(".aag"):
with open(file_name,'r') as f_in:
lines = f_in.readlines()
try:
num_inputs, num_latches, inputs, outputs, latches, and_gates = parse_aiger(lines)
except Exception as e:
print(e)
continue
if "UNSATISFIABLE" in lines[-1]:
satisfiable = False
elif "SATISFIABLE" in lines[-1]:
satisfiable = True
else:
satisfiable = None
if satisfiable is None:
continue
return_code = not satisfiable
k=0
while k<=MaxK:
k+=K
cnf_clauses = aiger_to_cnf(num_inputs, num_latches, inputs, outputs, latches, and_gates, K=k)
cnf_string = cnf_to_string(cnf_clauses)
cnf_file_path = file_name.replace(".aag", ".cnf").replace("aiger-safety-properties", "cnf-files")
write_cnf_file(cnf_string, cnf_file_path)
print(f"Solving {cnf_file_path}...")
try:
return_code = solve_sat(cnf_clauses)
if return_code == satisfiable:
break
except Exception as e:
print(f"Error occurred while solving {file_name}: {e}")
return_code = None
break
if satisfiable == return_code:
f_out.write(f"{file_name}: test OK\n")
elif satisfiable is not None and return_code is not None:
f_out.write(f"{file_name}: test Failed\n")
else:
f_out.write(f"{file_name}: UNKNOWN\n")
if __name__ == "__main__":
main()