-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsat.py
More file actions
135 lines (104 loc) · 3.56 KB
/
sat.py
File metadata and controls
135 lines (104 loc) · 3.56 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""sat file converting problem to sat."""
from cnf import get_dir_var, form_color_cnf, get_cell_cIndex, form_dir_cnf
import time
import sys
from utility import ANSI_RESET, ANSI_FORMAT, COLOR_ANSI
def sat(board, colors):
"""sat.
DESCRIPTION: reduce the board to SAT problem in conjunctive normal form
INPUT:
board: Game board
colors: dictionary of colors
OUTPUT:
dvars: direction variables
num_vars: total number of variables
cnf: complete CNF
t: time of reducing to SAT
"""
num_colors = len(colors)
# get dimensions of the board
width = len(board[0])
height = len(board)
num_cells = width * height
num_cvars = num_cells * num_colors
# start clocking
start = time.clock()
# CNF of directions
d_sat_var, num_dvars = get_dir_var(board, num_cvars)
dir_clauses = form_dir_cnf(board, colors, d_sat_var)
# CNF of colors
col_clauses = form_color_cnf(board, colors)
# end time
t = time.clock() - start
# calculate the total number of variables
num_vars = num_cvars + num_dvars
# get total CNF
clauses = col_clauses + dir_clauses
# print("CNF: ", clauses)
# print("d_sat_var:", d_sat_var)
return d_sat_var, num_vars, clauses, t
def decode(board, colors, path):
"""decode.
DESCRIPTION: convert back from solved sat to readable solutions.
INPUT:
board: Game board
colors: dictionary of colors
path: solution path of the Game
OUTPUT:
converted: a converted understandable list of the solution path.
"""
pathCP = []
# converted passed in path into a list of int
for element in path:
pathCP.append(int(element))
# make a set of solution path
path = set(pathCP)
converted = []
width = len(board[0])
# height = len(board)
# get direction variables
dvar, _ = get_dir_var(board, len(colors))
for i, row in enumerate(board):
# for each row create empty array
rows = []
for j, cell in enumerate(row):
curr_cell_color = -1
for c in range(len(colors)):
if get_cell_cIndex(width, i, j, colors, c) in path:
curr_cell_color = c
curr_cell_direction = -1
if not cell.isalpha():
for direction, value in dvar[i, j].items():
if value in path:
curr_cell_direction = direction
rows.append((curr_cell_color, curr_cell_direction))
converted.append(rows)
return converted
def visualize(converted, colors, mode_enable):
"""visualize.
DESCRIPTION: visualize the decoded solution
INPUT: converted: decoded solutions
colors: color dictionary
"""
# get the keys of colors dictionary
# list of NONEs ready for assigning colors
color_keys = len(colors)*[None]
for key, color in colors.items():
color_keys[color] = key
# if color mode is enabled
paint = mode_enable and key in COLOR_ANSI
for row in converted:
for (c, d) in row:
color_cell = color_keys[c]
display_cell = color_cell
# for cells that are not endpoints
if paint:
if color_cell in COLOR_ANSI:
ansi = ANSI_FORMAT.format(COLOR_ANSI[color_cell])
else:
ansi = ANSI_RESET
sys.stdout.write(ansi)
sys.stdout.write(display_cell)
if mode_enable:
sys.stdout.write(ANSI_RESET)
sys.stdout.write('\n')