def sudoku_search_benchmarks(initial_state, runs=3): puzzle = Sudoku(initial_state) print("Start:") puzzle.display(puzzle.infer_assignment()) options = [ backtracking_search, AC3, min_conflicts, depth_first_graph_search ] exec_times = { "backtracking_search": [], 'AC3': [], 'min_conflicts': [], 'depth_first_graph_search': [] } for run in range(runs): for i in range(3): i = 0 start = time() options[i](puzzle) exec_time = time() - start search_name = options[i].__name__ exec_times[search_name].append(exec_time) # print_result(puzzle, options[i].__name__, exec_time, False) for key in exec_times: num_runs = len(exec_times[key]) if num_runs: print(time_str(sum(exec_times[key]) / num_runs), "average for", key, "(", end='') for ms_time in exec_times[key]: print(time_str(ms_time), end=', ') print(")")
from csp import Sudoku, backtracking_search import time easy = '003020600900305001001806400008102900700000008006708200002609500800203009005010300' medium = '000012300000400000401005600705000010600020007030000806003600109000001000006590000' hard = '417369805030000000000700000020000060000080400000010000000603070500200000104000000' for grid in [easy, medium, hard]: prob = Sudoku(grid) start = time.time() Sudoku.display(backtracking_search(prob)) print(f"Blank cell = {grid.count('0')}") print(f"Time to execute: {(time.time()-start)*1000:.2f}ms") print()
@version 14feb2013 """ from csp import Sudoku, easy1, AC3, harder1, backtracking_search, mrv, \ forward_checking, min_conflicts from search import depth_first_graph_search # 1. Set up the puzzle. #puzzle = Sudoku('483921657967345821251876493548132976729564138136798245372689514814253769695417382') # solved (Figure 6.4.b) #puzzle = Sudoku('..3.2.6..9..3.5..1..18.64....81.29..7.......8..67.82....26.95..8..2.3..9..5.1.3..') # easy (Figure 6.4.a) #puzzle = Sudoku('4173698.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......') # harder (csp.py) puzzle = Sudoku( '1....7.9..3..2...8..96..5....53..9...1..8...26....4...3......1..4......7..7...3..' ) # hardest (AI Escargot) print('Start:') puzzle.display(puzzle.infer_assignment()) # 2. Solve the puzzle. #depth_first_graph_search(puzzle) #AC3(puzzle) backtracking_search(puzzle, select_unassigned_variable=mrv, inference=forward_checking) #min_conflicts(puzzle) # 3. Print the results. if puzzle.goal_test(puzzle.infer_assignment()): print('Solution:') puzzle.display(puzzle.infer_assignment()) else: print('Failed - domains: ' + str(puzzle.curr_domains))