-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.py
50 lines (37 loc) · 2.15 KB
/
benchmark.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
from main import run
from modules.utils import hash_model
from tabulate import tabulate
# Hàm ghi kết quả ra file dưới dạng bảng tabulate
def write_result(result, file="benchmark.txt"):
with open(file, "w") as f:
f.write(result)
# Đo thời gian chạy của thuật toán
if __name__ == "__main__":
BRUTEFORCE_LIMIT = 4e9
# Create a table to store the results
result = []
test, algorithm, elapsed_time, logging_info, model = None, None, None, None, None
test_index = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"]
test_count = 0
for test_case in ["5x5", "9x9", "11x11", "15x15", "20x20"]:
print("--------------------")
print(f"\n{test_index[test_count]}. RUNNING TEST-CASE {test_case.upper()}")
test_count += 1
alg_count = 0
for algorithm in ["pysat", "dpll", "backtracking", "bruteforce"]:
print(f"\n{alg_count + 1}. Running algorithm {algorithm}")
alg_count += 1
if algorithm == "bruteforce" and hash_model(model) is not None and hash_model(model) >= BRUTEFORCE_LIMIT:
print("Bruteforce algorithm is too slow for this model. Skipping...\n")
result += [[test_case, logging_info['CNFs'], logging_info['empties'], len([x for x in model if x > 0]), algorithm, "N/A", hash_model(model)]]
else:
print(f"Running {algorithm} on test-case {test_case}...")
test, algorithm, elapsed_time, logging_info, model = run(["", algorithm, test_case], False)
result += [[test_case, logging_info['CNFs'], logging_info['empties'], len([x for x in model if x > 0]), algorithm, f"{elapsed_time:.4f} ms", hash_model(model)]]
table = tabulate(result, headers=['Test case', 'CNFs', 'Empty cells', 'Traps', 'Algorithm', 'Time', 'Model hash (binary)'], tablefmt='orgtbl')
write_result(table, "benchmark.txt")
# Draw a horizontal line to separate test cases
result += [["-" for _ in range(7)]]
print("--------------------")
print("\nBenchmarking completed. Results are saved in benchmark.txt")
print(table)