-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
119 lines (90 loc) · 3.5 KB
/
Copy pathmain.py
File metadata and controls
119 lines (90 loc) · 3.5 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Description: File chính chứa hàm main, thực thi chương trình
import sys
from modules.inout import input_matrix, output_matrix, print_2matrix, output_CNFs
from modules.solvers import solve
from modules.utils import hash_model, update_matrix
# Danh sách thuật toán
_ALGORITHMS = {
"pysat": "pysat",
"dpll": "dpll",
"backtracking": "backtracking",
"bruteforce": "bruteforce",
}
# Danh sách test case
_TEST_CASES = {
"5x5": "testcases/5x5",
"9x9": "testcases/9x9",
"11x11": "testcases/11x11",
"15x15": "testcases/15x15",
"20x20": "testcases/20x20",
}
# In ra thông báo lỗi nếu tham số không hợp lệ: test_case, algorithm, measure_time
def read_args(argv):
algorithms = ", ".join(_ALGORITHMS.keys())
test_cases = ", ".join(_TEST_CASES.keys())
if len(argv) < 3:
print("\nUsage: python main.py <algorithm> <test_case>")
print(f"- algorithm: {algorithms}")
print(f"- test_case: {test_cases}")
return None, None
algorithm = argv[1]
test_case = argv[2]
if algorithm not in _ALGORITHMS:
print(f"Algorithm {algorithm} not found")
print(f"Available: {algorithms}")
return None, None, None
if test_case not in _TEST_CASES:
print(f"Test case {test_case} not found")
print(f"Available: {test_cases}")
return None, None, None
return algorithm, test_case
# ---------------------------------------------
# --------------- MAIN FUNCTION ---------------
# ---------------------------------------------
def run(argv, print_matrix = True):
algorithm, test_case = read_args(argv)
if test_case is None or algorithm is None: return
# Đọc file input và output
input_file = _TEST_CASES[test_case] + "/input.txt"
output_file = _TEST_CASES[test_case] + "/output.txt"
output_CNFs_file = _TEST_CASES[test_case] + "/CNFs.txt"
matrix = input_matrix(input_file);
original_matrix = [row.copy() for row in matrix]
# Giải bài toán
model, logging_info, CNFs, elapsed_time = solve(matrix, algorithm)
solution = update_matrix(matrix, model)
# Xuất kết quả
if CNFs is not None:
output_CNFs(CNFs, output_CNFs_file)
if solution is not None:
output_matrix(solution, output_file);
else:
output_matrix([[""]], output_file);
# In input và output ra console
print(f"\n{print_2matrix(original_matrix, solution)}") if print_matrix else None
# In thông tin ra console
print(f"Test {test_case.lower()}: {logging_info['CNFs']} CNFs - {logging_info['empties']} empty cells.")
if model is None: print("No solution found!")
else: print(f"Result hash: #{hash_model(model)} - {len([x for x in model if x > 0])} traps.")
print(f"Algorithm: {algorithm.upper()} - {elapsed_time:.4f} ms. Terminating...")
return test_case, algorithm, elapsed_time, logging_info, model
# ---------------------------------------------
if __name__ == "__main__":
try:
run(sys.argv)
except Exception as e:
print(f"Error: {e}")
# TESTING
# test_case = "5x5"
# test_case = "9x9"
# test_case = "11x11"
# test_case = "15x15"
# test_case = "20x20"
# logging = False
# logging = True
# run(["", "pysat", test_case], logging)
# run(["", "dpll", test_case], logging)
# run(["", "backtracking", test_case], logging)
# run(["", "bruteforce", test_case], logging)
# py -m cProfile -s cumtime main.py
# py -m cProfile -s ncalls main.py