'..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..' ) # ------ harder # puzzle = Sudoku('4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......') # ------ hardest # puzzle = Sudoku('1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..') puzzle.display(puzzle.infer_assignment()) start = time() # 2. Solve the puzzle; select ONLY one of the following algorithms # depth_first_graph_search(puzzle) backtracking_search(puzzle) # AC3(puzzle) # min_conflicts(puzzle) # 3. Print the results print("") if puzzle.goal_test(puzzle.infer_assignment()): print("Solution:") puzzle.display(puzzle.infer_assignment()) else: print("Failed - domains: ") print(str(puzzle.curr_domains)) # 4. Print elapsed time end = time() elapsed = end - start
): # Ensures that there is no block on proximity and that they are assigned the same "Block" return False elif (int(a[1]) == int(b[1]) - 1) and a[1:2] != b[ 1: 2]: # Checks for block continuity and avoids multiple duos. return True if ( int(a[2]) <= challengeColoursColumn[col][index] and int(a[2]) <= challengeColoursRow[row][index] ): # Ensures that values can be put if they are still some spots left return True if (a[0] == "N"): if (a == b): return False if (int(a[1]) <= challengeColoursColumn[col][3] and int(a[1]) <= challengeColoursRow[row][3] ): # Ensures only the maximum available blank spaces return False challengeColoursRow = [[2, 0, 0, 3], [1, 1, 1, 2], [4, 0, 0, 1], [3, 1, 0, 1], [2, 0, 0, 3]] challengeColoursColumn = [[2, 0, 0, 3], [1, 1, 1, 2], [4, 0, 0, 1], [3, 1, 0, 1], [2, 0, 0, 3]] maxLengthRow = [[2, 0, 0], [1, 1, 1], [2, 0, 0], [1, 1, 0], [2, 0, 0]] maxLengthColumn = [[2, 0, 0], [1, 1, 1], [2, 0, 0], [1, 1, 0], [2, 0, 0]] game = PicPix() print(csp.backtracking_search(game)) print(game.nassigns)
def now(): return secondsToStr(time()) # 1. Set up the problem and starting time n = 5 print("\nStarting at at " + now()[12:20]) print("problem with n =", n) start = time() problem = NQueensCSP(n) # 2. Solve the problem solution = backtracking_search(problem) # solution = AC3(problem); # solution = min_conflicts(problem) # 3. Print the results # Handle AC3 solutions (boolean values) first, they behave differently. if type(solution) is bool: if solution and problem.goal_test(problem.infer_assignment()): print("AC3 Solution:") print(problem.curr_domains) else: print("AC3 Failure") print(problem.curr_domains) # Handle other solutions next elif problem.goal_test(solution):
@version 14feb2013 """ import time from aima.csp import Zebra, min_conflicts, backtracking_search, AC3 from aima.search import depth_first_graph_search def print_solution(result): """A CSP solution printer copied from csp.py.""" for h in range(1, 6): print('House', h) for (var, val) in result.items(): if val == h: print('\t', var) puzzle = Zebra() # result = depth_first_graph_search(puzzle) # result = AC3(puzzle) result = backtracking_search(puzzle) # result = min_conflicts(puzzle, max_steps=1000) if puzzle.goal_test(puzzle.infer_assignment()): print("Solution:\n") print_solution(result) else: print("failed...") print(puzzle.curr_domains) puzzle.display(puzzle.infer_assignment())