def initialize_sudoku(): csp = CSP() N = 4 BLOCK = 2 rule = "x != y" for i in range(N): for j in range(N): csp.add_variable("(%s, %s)" % (i, j), list(range(N))) for x1 in range(N): for y1 in range(N): for x2 in range(N): for y2 in range(N): neighbors = False if x1 == x2: neighbors = True if y1 == y2: neighbors = True if x1 // BLOCK == x2 // BLOCK and y1 // BLOCK == y2 // BLOCK: neighbors = True if neighbors: csp.add_constraint("(%s, %s)" % (x1, y1), "(%s, %s)" % (x2, y2), rule) csp.export_to_json("sudoku.json") csp.all_solutions = False solution = csp.solve() board = [[0] * N for i in range(N)] print(len(board)) for key, value in solution.items(): print(key[1], key[4], value) board[int(key[1])][int(key[4])] = value[0] for i in range(N): print(board[i])
def initialzing_NQueen(N): csp = CSP() rule = "x[0] != y[0] and x[1] != y[1] and abs((x[0] - y[0]) / (x[1] - y[1])) != 1" for v in range(N): csp.add_variable(str(v), [[x, y] for x in range(N) for y in range(N)]) for x in csp.variables: for y in csp.variables: if x != y: csp.add_constraint(x, y, rule) csp.solve() csp.save_solution() # print(csp.solutions) return csp
def solve(self): X = [] V = {} C = {} # all locations are variables # locations given number have domain size 1 for i in range(0, len(self.map)): for j in range(0, len(self.map[i])): X.append((i, j)) if self.map[i][j] == 0: V[(i, j)] = [1, 2, 3, 4, 5, 6, 7, 8, 9] else: V[(i, j)] = [self.map[i][j]] # get a list of different integer pairs from 1 to 9 diff_pair = [] for i in range(1, 10): for j in range(1, 10): if not i == j: diff_pair.append((i, j)) # a variable needs to be differnet from its row, column, and 3*3 square for i in range(0, len(X)): if len(V[X[i]]) == 9: for col in range(0, 9): if not col == X[i][1]: if not (X[i], (X[i][0], col)) in C and not ((X[i][0], col), X[i]) in C: C[(X[i], (X[i][0], col))] = diff_pair for row in range(0, 9): if not row == X[i][0]: if not (X[i], (row, X[i][1])) in C and not ((row, X[i][1]), X[i]) in C: C[(X[i], (row, X[i][1]))] = diff_pair lt = ((X[i][0]//3) * 3, (X[i][1]//3) * 3) for c_diff in range(0, 3): for r_diff in range(0, 3): if not (lt[0] + r_diff, lt[1] + c_diff) == X[i]: if not (X[i], (lt[0] + r_diff, lt[1] + c_diff)) in C and not ((lt[0] + r_diff, lt[1] + c_diff), X[i]) in C: C[X[i], (lt[0] + r_diff, lt[1] + c_diff)] = diff_pair problem = CSP(X, V, C) solution = problem.solve() return solution
def solve(self): X = [] V = {} C = {} # initialize variable and domain for i in range(0, len(self.Components)): X.append(i) component_width = self.Components[i][0] component_height = self.Components[i][1] # possible lower left point of a component for x in range(0, self.width): for y in range(0, self.height): # if legal, append to V if x + component_width <= self.width and y + component_height <= self.height: if i in V: V[i].append((x, y)) else: V[i] = [(x, y)] # initialize constraints for i in range(0, len(X)): for j in range(0, len(X)): if not i == j: if not (i, j) in C and not (j, i) in C: C[(i, j)] = self.get_cons(i, j, V) #print(X) #print(V) #print(C) problem = CSP(X, V, C) # solve if self.bt_mc == 1: solution = problem.solve() else: solution = problem.min_conflicts(1000) result = {} if solution == None: return None for idx in solution: result[tuple(self.Components[idx])] = solution[idx] return result
def solve(self): X = [] V = {} C = {} for i in range(0, len(self.Components)): X.append(i) bot_width = self.Components[i][0] bot_height = self.Components[i][1] up_width = self.Components[i][2] up_height = self.Components[i][3] shift = self.Components[i][4] for x in range(0, self.width): for y in range(0, self.height): if x + shift >= 0 and x + bot_width <= self.width and x + shift + up_width <= self.width and y + bot_height + up_height <= self.height: if i in V: V[i].append((x, y)) else: V[i] = [(x, y)] for i in range(0, len(X)): for j in range(0, len(X)): if not i == j: if not (i, j) in C and not (j, i) in C: C[(i, j)] = self.get_cons(i, j, V) #print(X) #print(V) #rint(C) problem = CSP(X, V, C) solution = problem.solve() result = {} #print(solution) if solution == None: return None for idx in solution: result[tuple(self.Components[idx])] = solution[idx] return result
def solve(self): X = [] V = {} C = {} # get a list of different color pairs color_pairs = get_color_pairs(self.Color) # initialize variable and domain for i in range(0, len(self.T)): X.append(i) V[i] = [] for c in self.Color: V[i].append(c) #V[i] = self.Color # initialize constraints for t in self.Neighbors: for n in self.Neighbors[t]: ti = self.T.index(t) ni = self.T.index(n) if not (ti, ni) in C and not (ni, ti) in C: C[(ti, ni)] = color_pairs #print(X) #print(V) #print(C) problem = CSP(X, V, C) # solve if self.bt_mc == 1: solution = problem.solve() else: solution = problem.min_conflicts(101) result = {} if solution == None: return None for idx in solution: result[self.T[idx]] = solution[idx] return result