def binary_ne_grid(kenken_grid): n = kenken_grid[0][0] variables, constraints = [], [] for i in range(n): variables.append([ Variable("%d%d" % (i, j), domain=list(range(1, n + 1))) for j in range(n) ]) tuples = [t for t in permutations(list(range(1, n + 1)), 2)] for i in range(n): for j in range(n): for k in range(j + 1, n): cons = Constraint("r%d%d%d" % (i, j, k), [variables[i][j], variables[i][k]]) cons.add_satisfying_tuples(tuples) constraints.append(cons) cons = Constraint("c%d%d%d" % (i, j, k), [variables[j][i], variables[k][i]]) cons.add_satisfying_tuples(tuples) constraints.append(cons) csp = CSP("binary_ne_grid", [v for row in variables for v in row]) for cons in constraints: csp.add_constraint(cons) return csp, variables
def binary_ne_grid(kenken_grid): # Initialize dim = kenken_grid[0][0] Vars = [] Vars_1d = [] cons = [] # construct list of lists of Variable objects for i in range(dim): Vars.append([]) for j in range(dim): var = Variable("Cell_r%i_c%i"%(i+1,j+1), domain = [z for z in range(1, dim+1)]) Vars_1d.append(var) Vars[i].append(var) # construct csp for i in range(dim): for j in range(dim-1): for k in range(j+1, dim): #for row con = Constraint("ConstraintRow_r%i_c%i_c%i"%(i+1,j+1,k+1), [Vars[i][j],Vars[i][k]]) tuples = [] for Tuple in permutations(list(range(1,dim+1)),2): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) #for col con = Constraint("ConstraintCol_c%i_r%i_r%i"%(i+1,j+1,k+1), [Vars[j][i],Vars[k][i]]) tuples = [] for Tuple in permutations(list(range(1,dim+1)),2): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) csp = CSP("binary_ne_grid", Vars_1d) for con in cons: csp.add_constraint(con) return csp, Vars
def nary_ad_grid(kenken_grid): # Get the size of the grid and buid the domain of each variable size = kenken_grid[0][0] domain = list(range(1, size + 1)) # Instantiate all variables in size x size list and instantiate csp vars = [] for i in domain: vars.append([Variable(str(i) + str(j), domain) for j in domain]) csp = CSP('kenken', [var for row in vars for var in row]) # Create n-ary constraints valid = list(permutations(domain, size)) for i in range(size): cons_x = Constraint('r' + str(i), vars[i]) cons_y = Constraint('c' + str(i), [v[i] for v in vars]) cons_x.add_satisfying_tuples(valid) cons_y.add_satisfying_tuples(valid) csp.add_constraint(cons_x) csp.add_constraint(cons_y) return csp, vars
def binary_ne_grid(kenken_grid): # Get the size of the grid and buid the domain of each variable size = kenken_grid[0][0] domain = list(range(1, size + 1)) # Instantiate all variables in size x size list and instantiate csp vars = [] for i in domain: vars.append([Variable(str(i) + str(j), domain) for j in domain]) csp = CSP('kenken', [var for row in vars for var in row]) # Create binary constraints valid = list(permutations(domain, 2)) for i in range(size): for j in range(size): for k in range(j + 1, size): cons_x = Constraint('{0}{1}-{0}{2}'.format(i, j, k), [vars[i][j], vars[i][k]]) cons_y = Constraint('{1}{0}-{2}{0}'.format(i, j, k), [vars[j][i], vars[k][i]]) cons_x.add_satisfying_tuples(valid) cons_y.add_satisfying_tuples(valid) csp.add_constraint(cons_x) csp.add_constraint(cons_y) return csp, vars
def kenken_csp_model(kenken_grid): ''' Use nary_ad_grid ''' # Initialize dim = kenken_grid[0][0] Vars = [] Vars_1d = [] cons = [] # construct list of lists of Variable objects for i in range(dim): Vars.append([]) for j in range(dim): var = Variable("Cell_r%i_c%i"%(i+1,j+1), domain = [z for z in range(1, dim+1)]) Vars_1d.append(var) Vars[i].append(var) # nary_ad_grid for i in range(dim): #for row con = Constraint("ConstraintRow_r%i"%i, Vars[i]) tuples = [] for Tuple in permutations(list(range(1, dim+1))): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) #for col con = Constraint("ConstraintCol_r%i"%i, [(Vars[row][i]) for row in range(dim)]) tuples = [] for Tuple in permutations(list(range(1, dim+1))): tuples.append(Tuple) con.add_satisfying_tuples(tuples) cons.append(con) # construct kenken constraints for idx, element in enumerate(kenken_grid[1:]): if len(element) == 2: con = Constraint("KenKen_%i"%idx, [(Vars[(element[0]//10)-1][(element[0]%10)-1])]) tuples = [[element[1]]] else: con = Constraint("KenKen_%i"%idx, [(Vars[(i//10)-1][(i%10)-1]) for i in element[:-2]]) tuples = [] for comb in product(list(range(1,dim+1)), repeat = (len(element)-2)): if element[-1] == 0: oper = operator.add elif element[-1] == 1: oper = operator.sub elif element[-1] == 2: oper = operator.truediv elif element[-1] == 3: oper = operator.mul if reduce(oper, comb) == element[-2]: for permu in permutations(comb): if permu not in tuples: tuples.append(permu) con.add_satisfying_tuples(tuples) cons.append(con) csp = CSP("kenken_csp_model", Vars_1d) for con in cons: csp.add_constraint(con) return csp, Vars