def test_no_solution(self): def callback(solutions, s): self.assertTrue(False) problem = array([[]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True) problem = array([[0]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True) problem = array([[0, 0], [0, 0]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True) problem = array([[0, 1]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True) problem = array([[1, 0, 0], [0, 0, 0], [0, 0, 1]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True)
def main(): if(len(sys.argv) != 2): print("usage: python3 solveitup.py <problem description>") return numvars, clauses = getproblem(sys.argv[1]) grid = buildgrid(numvars, clauses) dlx = DLX(SparseMatrix(grid)) results = dlx.search() printresults(results)
def test_problem_1(self): def callback(solutions, s): self.validate([1], solutions, s) problem = array([[1]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True)
def test_problem_2(self): def callback(solutions, s): self.validate([1, 4, 5], solutions, s) problem = array([[0, 0, 1, 0, 1, 1, 0], [1, 0, 0, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 0], [1, 0, 0, 1, 0, 0, 0], [0, 1, 0, 0, 0, 0, 1], [0, 0, 0, 1, 1, 0, 1]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True)
def __call__(self, sudoku): board, metadata = SudokuSolver.parse(sudoku) result = DLX.solve(board) if result is None: return None # Draw back the solution. solution = np.zeros((9, 9)) for row in result: cell = metadata[row] solution[cell.row, cell.col] = cell.val return solution
def solve(self, line): """ Return list of solutions from specified line. Return empty list if no solutions are found and return at most one solution if validation is enabled or all solutions if validation is disabled. It is possible for a Sudoku challenge to have more than one solution but such challenge is considered to be an invalid. """ grid = self.build_challenge(line) dlx = DLX.from_sudoku(grid, self.result) dlx.run() return self.grids
def test_problem_3(self): def callback(solutions, s): self.validate([2, 4, 6], solutions, s) problem = array([[1, 0, 0, 1, 0, 0, 1], [1, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1], [0, 0, 1, 0, 1, 1, 0], [0, 1, 1, 0, 0, 1, 1], [0, 1, 0, 0, 0, 0, 1]]) dlx = DLX.from_matrix(problem, callback) dlx.run(True)
def solve(self, line): """Return list of solutions from specified line. Return empty list if no solutions are found and return at most one solution if validation is enabled or all solutions if validation is disabled. It is possible for a Sudoku challenge to have more than one solution but such challenge is concidered to be an invalid. """ grid = self.build_challenge(line) self.validate_challenge(grid) self.grids = [] dlx = DLX.from_sudoku(grid, self.result) dlx.run(self.validate) return self.grids
X="#BB99DD", Y="#CC88CC", Z="#DD99BB") # Grid for Scott's pentomino problem: 8x8 with a 2x2 hole in the middle GRID = Grid((8, 8), holes=[(3, 3), (3, 4), (4, 3), (4, 4)]) # Generate all pentominoes that can be placed on the grid POLYMINOES = generate_all(PENTOMINOES, GRID) # convert to list of lists POLYMINOES = [polymino.aslist for polymino in POLYMINOES] # calculate labels for DLX LABELS = list(set([element for polymino in POLYMINOES for element in polymino])) LABELS = sorted(LABELS, key=sortkey) COVER = DLX(LABELS, POLYMINOES) SOLUTIONS = [] for i, SOLUTION in enumerate(COVER.generate_solutions()): print(i) SOLUTIONS.append(Grid.from_DLX(SOLUTION)) DISTINCT_SOLUTIONS = unique_grids(SOLUTIONS) solutions_svg(DISTINCT_SOLUTIONS, filename='scott_distinct_solutions1.svg', columns=13, colour=COLOURS.get) # removing X pentominoes POLYMINOES = [pol for pol in POLYMINOES if not (pol[0] == 'X' and pol[1] not in [(5, 3), (5, 2), (4, 2)])] COVER = DLX(LABELS, POLYMINOES)