-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
64 lines (49 loc) · 1.9 KB
/
main.py
File metadata and controls
64 lines (49 loc) · 1.9 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
"""main function running the whole puzzle solver."""
from utility import read_board
from read_cnf import convert
from sat import sat, decode, visualize
from solver import solve
from time import clock
FILENAME = 'puzzles/input991.txt'
# choose random_heuristic or POSIT
HEURISTIC = 'POSIT'
# Enable color print mode in terminal
COLOR_ENABLE = True
def flow_free_main():
"""flow_free_main.
DESCRIPTION: main function of the solver.
INPUT: None
OUTPUT: None
"""
print("To Change to Color Mode Visualization, Set COLOR_ENABLE to True.\n")
print("Color Print Mode is Now %s\n" % ('Enabled' if COLOR_ENABLE is True else 'Disabled'))
with open(FILENAME, 'r', encoding='UTF-8') as input:
board, colors = read_board(input)
# reduce problem to sat
d_sat_var, num_vars, clauses, reduce_time = sat(board, colors)
print("\nTime Used on Reducing the Problem to SAT: %.5f seconds\n" % reduce_time)
# Get the CNF structured Formula
CNF = convert(clauses, HEURISTIC)
# input clauses into solver
# clock time
branch_count = [0, 0]
print("Solving the Problem Using DPLL with [%s] Heuristic ...\n" % HEURISTIC)
start = clock()
# branch[0] is count of failure, branch[1] is count of success
solved_cnf, branch = solve(CNF, branch_count)
solve_time = clock() - start
print("Time Used on Solving the SAT Problem: %.5f seconds\n" % solve_time)
print("Failed Splits: ", branch[0])
print("Successful Splites: ", branch[1])
# Get the solution from the returing list
solution = solved_cnf.get_solution()
# Test line
# print("SAT Solution: ", solution)
# print("Type of sol: ", type(solution))
# decode the solution
converted = decode(board, colors, solution)
# visialize the solution
print("\nVisualized Solution: \n")
visualize(converted, colors, COLOR_ENABLE)
if __name__ == '__main__':
flow_free_main()