def test_box_size(self): """ Test we get correct boxsize if N is square and large enough. """ self.assertEqual(3, Problem("." * 81, N=9).get_box_size()) self.assertEqual(2, Problem("." * 16, N=4).get_box_size()) self.assertEqual(0, Problem("." * 4, N=2).get_box_size()) self.assertEqual(0, Problem("." * 100, N=10).get_box_size())
def test_nentries(self): """ Check the Problem correctly counts the number of clue entries. """ sudoku = Problem('1' + '.' * (self.N**2 - 1), N=self.N) self.assertEqual(1, sudoku.num_entries()) rp = random_puzzle().strip() nentries_rp = len([e for e in rp if e != '.']) sudoku = Problem(rp) self.assertEqual(nentries_rp, sudoku.num_entries())
def test_box_digits_constraint(self): """ Check the matrix constraint that ensures each box contains all digits """ # - Construct a Problem with all cells filled # - Get the all_cells matrix # - Multiply it with the indicator vector # - check the result is all ones # - blank a cell, repeat # - check the result is not all ones N = 9 sudoku = Problem("123123123" "456456456" "789789789" "123123123" "456456456" "789789789" "123123123" "456456456" "789789789") v = sudoku.get_result(box_digits=True) self.assertOnes(v) v = self.sudoku.get_result(box_digits=True) self.assertNotOnes(v)
def make_problem(char, pos, N=9): """ Make a test problem with a single entry. """ N2 = N**2 s = "." * pos + char + "." * (N2 - pos - 1) return Problem(s)
def test_clues_matrix(self): kp = KillerProblem("aabb", {'a': 2, 'b': 4}, 2) answer = Problem("1122", N=2) v = kp.get_clues_matrix() * answer.to_indicator_vector() print kp.get_clues_matrix() print v self.assertTrue(all_ones(v))
def count_success(): results = {} for p in puzzles(): results[p] = not '_' in unicode(Problem(p).solve()) print("OK: %d" % results.values().count(True)) print("Fail: %d" % results.values().count(False)) return results
def test_solve(self): """ Test solving the Problem. """ # Easy test self.sudoku = Problem( '.81.749....4.193.7379.85.14..7831...238456179..69274..843562791762198543..5743862' ) answer = self.sudoku.solve() checkAnswer = Problem( '681374925524619387379285614497831256238456179156927438843562791762198543915743862' ) self.assertEqual(unicode(checkAnswer), unicode(answer)) answer = self.sudoku.solve() checkAnswer = Problem( '681374925524619387379285614497831256238456179156927438843562791762198543915743862' ) self.assertEqual(unicode(checkAnswer), unicode(answer))
def test_all_cells_constraint(self): """ Check the matrix constraint that ensures all cells are filled. """ # - Construct a Problem with all cells filled # - Get the all_cells matrix # - Multiply it with the indicator vector # - check the result is all ones # - blank a cell, repeat # - check the result is not all ones N = self.N sudoku = Problem("1" * N**2) v = sudoku.get_result(all_cells=True) self.assertOnes(v) v = self.sudoku.get_result(all_cells=True) self.assertNotOnes(v)
def test_col_digits_constraint(self): """ Check the matrix constraint that ensures each row contains all digits """ # - Construct a Problem with all cells filled # - Get the all_cells matrix # - Multiply it with the indicator vector # - check the result is all ones # - blank a cell, repeat # - check the result is not all ones N = 9 entries = reduce( operator.__add__, [list(itertools.repeat(i, N)) for i in range(1, N + 1)]) sudoku = Problem(entries) v = sudoku.get_result(col_digits=True) self.assertOnes(v) v = self.sudoku.get_result(col_digits=True) self.assertNotOnes(v)
def solve_board(self, algorithm): # Retrieve user input board = self.get_board() if board == None: return # Solve board method = 'blind' if algorithm == 'DFS' else 'heuristic' problem = Problem(board, method) solver = Solver(problem, algorithm) solution = solver.solve() # Display result for row in range(9): for col in range(9): address = str(row) + str(col) self.cells[address].delete(0, 'end') self.cells[address].insert(0, solution[row][col]) # Display solved message message = "Elapsed time = {} seconds\nVisited nodes = {}".format( solver.elapsed_time, solver.visited_nodes) self.message_label.config(text=message)
def setUp(self): """ Instantiate one Problem at random from TOP95 """ self.sudoku = Problem(random_puzzle()) self.N = 9