def test_simple_exact_cover(): """ ArthurKantor's 1st test case """ data = np.array([[1, 0, 0], [0, 1, 1]], dtype=np.int32) expected = np.array([0, 1]) actual = get_exact_cover(data) np.testing.assert_array_equal(actual, expected)
def test_single_row_exact_cover(): """ ArthurKantor's 2nd test case """ data = np.array([[1, 1, 1]], dtype=np.int32) expected = np.array([0]) actual = get_exact_cover(data) np.testing.assert_array_equal(actual, expected)
def test_exact_cover_with_solution(array_data): rowcount = len(array_data) actual = get_exact_cover(array_data) assert actual.size > 0 assert actual.size <= rowcount assert all(actual < rowcount) check_cover = array_data[actual, :].sum(axis=0) assert all(check_cover == 1)
def test_exact_cover(array_data): rowcount = len(array_data) try: actual = get_exact_cover(array_data) except NoSolution: assert True return assert actual.size <= rowcount assert all(actual < rowcount)
def test_solution_to_split_problem_solves_original_problem(a, n): try: result = list(split_problem(a, n)) for sub in result: s = get_exact_cover(sub) if s.size > 0: assert is_solution(s, sub) assert is_solution(s, a) except NoSolution: assert True except CannotSplitFurther: assert True
def solve(self): # NOTES: # 1. No error-checking; data is clean at this point. # 2. We translate the puzzle once into the constraint matrix below, and then we # translate again into a sparse matrix in get_exact_cover. This duplicates # some work, but it allows get_exact_cover to be a more generally applicable # function. logging.debug(self._constraint_matrix) cover = ec.get_exact_cover(self._constraint_matrix) logging.debug(cover) solution = None if cover.size > 0 and ( (cover[0] != 0) or (cover[1] != 0)): # an array of 0's implies no solution. solution = Sudoku(self._size) solution._sudo = self._translate_exact_cover_into_sudoku(cover) return solution
def solve_unfolding(unfolding, max_copies): n = len(unfolding[0]) # Try cube-ish box dimensions first, seems to make solutions likely to be # found early. boxes = sorted( iter_box_sizes(n, len(unfolding), max_copies), key=lambda x: sorted(x, reverse=True)) for box in boxes: matrix, transformed_unfoldings = generate_matrix(unfolding, box) solution = exact_cover.get_exact_cover(matrix) if len(solution): print('Solution:') print(f'Unit cell dimensions: {list(box)}') for i in solution: print(json.dumps(transformed_unfoldings[i])) return print('No solution found. :(')
def test_solve_medium_sudoku_problem(): matrix = load_problem("tests/files/medium_sudoku.csv") solution = get_exact_cover(matrix) assert is_solution(solution, matrix)
def test_is_solution_fails_for_proper_subset(a): s = get_exact_cover(a) assert not is_solution(s[:-1], a)
def test_is_solution_fails_for_extra_rows(a, x): s = get_exact_cover(a) assert not is_solution(list(s) + [x], a)
def test_list_is_solution(a): s = list(get_exact_cover(a)) assert is_solution(s, a)
def test_is_solution(a): s = get_exact_cover(a) assert is_solution(s, a)
def test_solve_very_hard_sudoku_problem_2(): matrix = load_problem("tests/files/very_hard_sudoku_2.csv") solution = get_exact_cover(matrix) assert is_solution(solution, matrix)
def run_debug(): logging.basicConfig(level=logging.DEBUG) data = np.genfromtxt("tests/files/pentominos_chessboard.csv", dtype=np.int32) logging.info(data.shape) solution = get_exact_cover(data, True) logging.info(solution)
def test_exact_cover_without_solution(array_data): with pytest.raises(NoSolution): get_exact_cover(array_data)
def test_no_solution(data): with pytest.raises(NoSolution): get_exact_cover(data)
def test_complex_exact_cover_problem(): data = np.array(polyomino_problem, dtype=np.int32) actual = get_exact_cover(data) assert actual.shape == (13,)
def test_exact_cover(): data = np.array([[1, 0, 0], [0, 1, 0], [0, 1, 1], [0, 0, 1]], dtype=np.int32) expected = np.array([0, 1, 3]) actual = get_exact_cover(data) np.testing.assert_array_equal(actual, expected)
def test_exact_cover_no_solution(): data = np.array([[1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 0, 0]], dtype=np.int32) with pytest.raises(NoSolution): get_exact_cover(data)
def test_solve_impossible_sudoku_problem(): matrix = load_problem("tests/files/impossible_sudoku.csv") with pytest.raises(NoSolution): get_exact_cover(matrix)
def solve(self): self.make_problem() solution = get_exact_cover(self.array) tiling = [self.key[s] for s in solution] if tiling: return Solution(tiling, self.board)