-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
86 lines (71 loc) · 3.68 KB
/
solver.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
83
from domains import extract_domains
class solver:
def __init__(self, board, constraints):
self.board = board
self.constraints = constraints
self.constraints_location = self.generate_constraint_location()
self.constraint_domains, self.constraints_lengths = self.generate_constraint_domains()
self.constraints_effected = self.generate_constraints_effected()
# generate location of constraints
def generate_constraint_location(self):
constraints_location = {
}
for i in range(len(self.board)):
for j in range(len(self.board)):
if self.board[i][j] in self.constraints:
constraints_location[self.board[i][j]] = (i, j)
return constraints_location
# generate length of constraints for ordering
# and also generate domains for each constraint for backtracking
def generate_constraint_domains(self):
constraint_lengths = {}
constraint_domains = {}
for cs_name in self.constraints:
row = self.constraints_location[cs_name][0]
col = self.constraints_location[cs_name][1]
if 'down' in self.constraints[cs_name]:
length = 0
for i in range(row+1, len(self.board)):
if self.board[i][self.constraints_location[cs_name][1]] == 0:
length += 1
else:
break
constraint_lengths[tuple([cs_name, 'down'])] = length
value = self.constraints[cs_name]['down']
constraint_domains[tuple([cs_name, 'down'])] = extract_domains(value=value, length=length)
if 'right' in self.constraints[cs_name]:
length = 0
for j in range(col+1, len(self.board)):
if self.board[self.constraints_location[cs_name][0]][j] == 0:
length += 1
else:
break
constraint_lengths[tuple([cs_name, 'right'])] = length
value = self.constraints[cs_name]['right']
constraint_domains[tuple([cs_name, 'right'])] = extract_domains(value=value, length=length)
return constraint_domains, constraint_lengths
# for finding with constraints are effected on (i,j)
# to change its domain of the constraints
def generate_constraints_effected(self):
constraints_effected = {}
for cs_name in self.constraints:
constraints_effected[cs_name] = []
row = self.constraints_location[cs_name][0]
col = self.constraints_location[cs_name][1]
if 'down' in self.constraints[cs_name]:
for i in range(row+1, len(self.board)):
if self.board[i][col] == '#' or self.board[i][col] in self.constraints:
break
if self.board[i][col] == 0:
if tuple([i, col]) not in constraints_effected:
constraints_effected[tuple([i, col])] = []
constraints_effected[tuple([i, col])].append(tuple([cs_name, 'down']))
if 'right' in self.constraints[cs_name]:
for j in range(col+1, len(self.board)):
if self.board[row][j] == '#' or self.board[row][j] in self.constraints:
break
if self.board[row][j] == 0:
if tuple([row, j]) not in constraints_effected:
constraints_effected[tuple([row, j])] = []
constraints_effected[tuple([row, j])].append(tuple([cs_name, 'right']))
return constraints_effected