def solve_sudoku_extra(puzzle): # for testing, always initialize the pseudorandom number generator to output the same sequence # of values: random.seed(41) puzzle_name = str(puzzle) sol_filename = puzzle_name[:-4] + ".sol" sat = SAT(puzzle) start = time.time() result = sat.WalkSAT() total = time.time() - start if puzzle_name[-4:] == ".sud": print("original sudoku puzzle: ") sud = Sudoku() sud.load(puzzle) print(sud) if result: sat.sol_file(sol_filename) print("solution found in " + str(sat.runs) + " iterations of SAT") print("the code took " + str(total) + " seconds to run.") display_sudoku_solution(sol_filename) else: print("no solution found by SAT in " + str(sat.limit) + " iterations of SAT")
def __str__(self): print('\n') self.write_solution() string = '' string += '^^------%s with %s Solution------^^\n\n' % ( self.problem_name, self.analysis_type) string += 'Threshold: %s \nIterations: %s \nRuntime: %s\nAverage Loop Runtime: %s' % ( self.threshold, self.iterations, self.total_runtime, self.avg_loop_time) print('Sudoku Solution Board:\n') display_sudoku_solution(self.sat_problem.solution_filename) return string
Writes a general-purpose solver with GSAT and WALKSAT algorithms for propositional logic satisfiability problems. To test the solver, sudoku logic puzzles are turned into conjunctive normal forms (CNF) and are thus modeled as satisfiability problems. Note that sudoku puzzles are used as an example here. GSAT and WALKSAT algorithms can be applied to other satisfiability problems as well. This script: Implements the main program of the sudoku solver (provided code) """ from display import display_sudoku_solution import random, sys from SAT import SAT if __name__ == "__main__": # for testing, always initialize the pseudorandom number generator to output the same sequence # of values: random.seed(1) puzzle_name = str(sys.argv[1][:-4]) sol_filename = puzzle_name + ".sol" sat = SAT(sys.argv[1]) result = sat.gsat() if result: sat.write_solution(sol_filename) display_sudoku_solution(sol_filename)
def display_solution(self, filename): """Displays the sudoku problem based on the solution in the file""" display_sudoku_solution(filename)