-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkenken_csp.py
More file actions
215 lines (159 loc) · 6.74 KB
/
Copy pathkenken_csp.py
File metadata and controls
215 lines (159 loc) · 6.74 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
'''
All models need to return a CSP object, and a list of lists of Variable objects
representing the board. The returned list of lists is used to access the
solution.
For example, after these three lines of code
csp, var_array = kenken_csp_model(board)
solver = BT(csp)
solver.bt_search(prop_FC, var_ord)
var_array[0][0].get_assigned_value() should be the correct value in the top left
cell of the KenKen puzzle.
The grid-only models do not need to encode the cage constraints.
1. binary_ne_grid (worth 10/100 marks)
- A model of a KenKen grid (without cage constraints) built using only
binary not-equal constraints for both the row and column constraints.
2. nary_ad_grid (worth 10/100 marks)
- A model of a KenKen grid (without cage constraints) built using only n-ary
all-different constraints for both the row and column constraints.
3. kenken_csp_model (worth 20/100 marks)
- A model built using your choice of (1) binary binary not-equal, or (2)
n-ary all-different constraints for the grid.
- Together with KenKen cage constraints.
'''
from cspbase import *
import itertools
from functools import reduce
def binary_ne_grid(kenken_grid):
# ((3),(11,12,13,6,0),(21,22,31,2,2),....)
N = kenken_grid[0][0]
dom = []
for i in range(N):
dom.append(i+1)
vars = [["" for x in range(N)] for y in range(N)]
for i in dom:
for j in dom:
vars[i-1][j-1] = Variable('KenCSP{}{}'.format(i, j), dom)
cons = []
for qi in range(len(dom)):
for qj in range(len(dom) - 1):
for qk in range(qj+1, len(dom)):
con = Constraint("C(K{}{},K{}{})".format(qi+1,qj+1, qi+1,qk+1),[vars[qi][qj], vars[qj][qk]])
con_col = Constraint("C(K{}{},K{}{})".format(qj+1,qi+1, qk+1,qi+1),[vars[qj][qi], vars[qk][qi]])
sat_tuples = []
for t in itertools.product(dom, dom):
if t[0] != t[1]:
sat_tuples.append(t)
con.add_satisfying_tuples(sat_tuples)
con_col.add_satisfying_tuples(sat_tuples)
cons.append(con)
cons.append(con_col)
vars_flat = list(itertools.chain(*vars)) ## flatten 2d array
csp = CSP("{}-KenCSP".format(N), vars_flat)
for c in cons:
csp.add_constraint(c)
return csp, vars
def nary_ad_grid(kenken_grid):
N = kenken_grid[0][0]
dom = []
for i in range(N):
dom.append(i+1)
vars = [["" for x in range(N)] for y in range(N)]
for i in dom:
for j in dom:
vars[i-1][j-1] = Variable('KenCSP{}{}'.format(i, j), dom)
cons = []
i = 0
for row in vars:
i += 1
con = Constraint("C(Row{})".format(i),row)
## sat_tuples generation
sat_tuples = []
for t in itertools.permutations(range(N),N):
addOne = tuple(x+1 for x in t)
sat_tuples.append(addOne)
con.add_satisfying_tuples(sat_tuples)
cons.append(con)
for i in range(N):
col = []
for row in vars:
col.append(row[i])
con_col = Constraint("C(Column{})".format(i+1),col)
con_col.add_satisfying_tuples(sat_tuples)
cons.append(con_col)
vars_flat = list(itertools.chain(*vars)) ## flatten 2d array
csp = CSP("{}-KenCSP".format(N), vars_flat)
for c in cons:
csp.add_constraint(c)
return csp, vars
def kenken_csp_model(kenken_grid):
N = kenken_grid[0][0]
dom = []
grid_vars = kenken_grid[1:]
for i in range(N):
dom.append(i+1)
vars = [["" for x in range(N)] for y in range(N)]
for i in dom:
for j in dom:
vars[i-1][j-1] = Variable('KenCSP{}{}'.format(i, j), dom)
## n-ary
new_cons = []
i = 0
for row in vars:
i += 1
con = Constraint("C(Row{})".format(i),row)
## sat_tuples generation
sat_tuples = []
for t in itertools.permutations(range(N),N):
addOne = tuple(x+1 for x in t)
sat_tuples.append(addOne)
con.add_satisfying_tuples(sat_tuples)
new_cons.append(con)
for i in range(N):
col = []
for row in vars:
col.append(row[i])
con_col = Constraint("C(Column{})".format(i+1),col)
con_col.add_satisfying_tuples(sat_tuples)
new_cons.append(con_col)
index = 0
new_cons_cage = []
for cage in grid_vars:
operation = cage[-1]
result = cage[-2]
cells = cage[ :len(cage) - 2]
if len(cells) != 0:
scope = []
for cell in cells:
str_cell = str(cell)
i = int(str_cell[0])
j = int(str_cell[1])
scope.append(vars[i - 1][j - 1])
sat_tuples = []
options = list(itertools.product(list(range(1, N+1)), repeat = len(cells)))
con = Constraint("KenKen_%i" % index, scope)
for option in options:
if(operation == 0):
func = reduce((lambda x, y: x + y), option)
elif(operation == 1):
func = reduce((lambda x, y: x - y), option)
elif(operation == 2):
func = reduce((lambda x, y: x / y), option)
elif(operation == 3):
func = reduce((lambda x, y: x * y), option)
if func == result:
for perms in itertools.permutations(option):
if perms not in sat_tuples:
sat_tuples.append(perms)
con.add_satisfying_tuples(sat_tuples)
# new_cons_test.append(con)
new_cons_cage.append(con)
vars_flat = list(itertools.chain(*vars)) ## flatten 2d array
csp = CSP("{}-KenCSP".format(N), vars_flat)
for c in new_cons:
csp.add_constraint(c)
for c in new_cons_cage:
csp.add_constraint(c)
return csp, vars
# kenken_grid = [[5],[11,12,21,22,10,0],[13,14,23,24,34,18,0],[15,25,35,2,1],[31,32,33,1,1],[41,42,43,51,52,53,600,3],[44,54,55,2,2],[45,3]]
# # kenken_grid = [[6],[11,12,13,2,2],[14,15,3,1],[16,26,36,11,0],[21,22,23,2,2],[24,25,34,35,40,3],[31,41,51,61,14,0],[32,33,42,43,52,53,3600,3],[44,54,64,120,3],[45,46,55,56,1,1],[62,63,5,1],[65,66,5,0]]
# kenken_csp_model(kenken_grid)