Ejemplo n.º 1
0
class SubsetSumSolver2:
    def __init__(self):
        self.enumerator = BacktrackingEnumerator(createSolution=lambda space, i, d, f: f)
    
    def solve(self, w, W):
        space = SubsetSumStateSpace2(w, W)
        return next(self.enumerator.enumerate(space))#]s2
Ejemplo n.º 2
0
class DLXSolver:#[solver
    def __init__(self):
        self.solver = BacktrackingEnumerator(createSolution=lambda space, i, d, f: f)
        
    def solve(self, ones):
        space = DLX0(ones)
        return(next(self.solver.enumerate(space)))#]solver
Ejemplo n.º 3
0
class NQueensDLXSolver:  #[solver
    def __init__(self):
        self.solver = BacktrackingEnumerator()

    def solve(self, N: "int") -> "IList<int> or None":
        M, row_coding = self.nqueens_to_matrix(N)
        space = DLX2(M, set(range(2 * N)))
        self.solver.createSolution = \
           lambda space, i, d, s: self.selected_nqueens(s.selected, row_coding)
        return next(self.solver.enumerate(space), None)

    def nqueens_to_matrix(self, N: "int") -> "matrix, IList<(int, int)>":
        M = []
        rows_coding = []
        for row in range(N):
            for col in range(N):
                M.append([
                    row, N + col, 2 * N + col - row + N - 1,
                    2 * N + 2 * N - 1 + col + row
                ])
                rows_coding.append((row, col))
        return M, rows_coding

    def selected_nqueens(self, selected: "IList<int>", row_coding: "IList<(int, int)>") \
            -> "IList<int> or None":
        if selected != None:
            return tuple(q[1]
                         for q in sorted(row_coding[row] for row in selected))
        else:
            return None  #]solver
Ejemplo n.º 4
0
class PerfectHashFinder:
    def __init__(self):
        self.enumerator = BacktrackingEnumerator(
            createSolution=lambda space, i, d, f: f)

    def find(self, words, maxvalue):
        space = PerfectHashStateSpace(words, maxvalue)
        return next(self.enumerator.enumerate(space))  #]full
Ejemplo n.º 5
0
class HamiltonianCycleSolver:  #[solver
    def __init__(self):
        self.enumerator = BacktrackingEnumerator(
            createSolution=lambda space, i, d, f: f + (f[0], ))

    def solve(self, G):
        space = HamiltonianCycleStateSpace(G)
        return next(self.enumerator.enumerate(space))  #]solver
Ejemplo n.º 6
0
class ExactCoverSolver:
    def __init__(self):
        self.solver = BacktrackingEnumerator(
            createSolution=lambda space, i, d, f: d)

    def solve(self, matrix):
        space = ExactCoverStateSpace(matrix)
        return (next(self.solver.enumerate(space)))
Ejemplo n.º 7
0
class SubsetSumSolver3:
    def __init__(self):
        self.enumerator = BacktrackingEnumerator(
            createSolution=lambda space, i, d, f: d + [0]*(space.n-len(d)))
    
    def solve(self, w, W):
        space = SubsetSumStateSpace3(w, W)
        return next(self.enumerator.enumerate(space))
Ejemplo n.º 8
0
class PolyominoesSolver:
    def __init__(self, ):
        self.enumerator = BacktrackingEnumerator(
            createSolution=lambda space, i, d, f: f)

    def solve(self, board, tetrominos):
        space = PolyominoesStateSpace(board, tetrominos)
        return next(self.enumerator.enumerate(space))
class HamiltonianCycleSolver:#[solver
    def __init__(self):
        self.enumerator = BacktrackingEnumerator(createSolution
                                                 =lambda space, i, d, f: f + (f[0],))
    
    def solve(self, G):
        space = HamiltonianCycleStateSpace(G)
        return self.enumerator.enumerate(space) #]solver
    def find(self,S):
        sol=HamiltonianCycleSolver().solve(S)
        return sol
Ejemplo n.º 10
0
class WolfGoatCabbageSolver:
    def __init__(self):
        self.solver = BacktrackingEnumerator(
            createSet=lambda space: StateSet(),
            createSolution=lambda space, i, d, f: d)

    def solve(self) -> "IList<str>":
        space = WolfGoatCabbageSpaceState()
        return next(self.solver.enumerate(space))


#> full
Ejemplo n.º 11
0
class PolyominoesSolver:  #[dlx
    def __init__(self):
        self.enumerator = BacktrackingEnumerator(
            createSolution=lambda space, i, d, f: f)

    def solve(self, board, polyominoes):
        matrix, row_coding = self.polyominos_matrix_and_rows_coding(
            board, polyominoes)
        space = DLX(matrix)
        selected_rows = next(self.enumerator.enumerate(space))
        return self.solution_as_string(selected_rows, board, row_coding)

    def polyominos_matrix_and_rows_coding(self, board, polyominoes):
        polyomino_column = {}
        col = 0
        for polyomino in polyominoes:
            symbol, pieces = polyomino
            polyomino_column[symbol] = col
            col += 1

        board = [list(row) for row in board.split('\n')]

        xy_column = {}
        for y in range(len(board)):
            for x in range(len(board[y])):
                if board[y][x] == '.':
                    xy_column[x, y] = col
                    col += 1

        matrix = []
        rows_coding = []

        for polyomino in polyominoes:
            symbol, pieces = polyomino
            for piece in pieces:
                for y in range(len(board)):
                    for x in range(len(board[y])):
                        if all(0<=x+i<len(board[y]) and 0<=y+j<len(board) and \
                               board[y+j][x+i] == '.' for (i,j) in piece):
                            rows_coding.append((symbol, piece, x, y))
                            matrix.append([polyomino_column[symbol]])
                            for (i, j) in piece:
                                matrix[-1].append(xy_column[x + i, y + j])
        return matrix, rows_coding

    def solution_as_string(self, selected_rows, board, rows_coding):
        board = [list(row) for row in board.split('\n')]
        for selected_row in selected_rows:
            symbol, piece, x, y = rows_coding[selected_row]
            for i, j in piece:
                board[y + j][x + i] = symbol
        return ('\n'.join(''.join(row) for row in board)).replace("*",
                                                                  " ")  #]dlx
Ejemplo n.º 12
0
 def __init__(self):
     self.enumerator = BacktrackingEnumerator(createSolution
                                              =lambda space, i, d, f: f + (f[0],))
Ejemplo n.º 13
0
 def __init__(self):
     self.solver = BacktrackingEnumerator()
Ejemplo n.º 14
0
 def __init__(self):
     self.enumerator = BacktrackingEnumerator(
         createSolution=lambda space, i, d, f: d + [0]*(space.n-len(d)))
Ejemplo n.º 15
0
 def __init__(self):
     self.solver = BacktrackingEnumerator(
         createSolution=lambda space, i, d, f: d)
Ejemplo n.º 16
0
 def __init__(self, ):
     self.enumerator = BacktrackingEnumerator(
         createSolution=lambda space, i, d, f: f)
Ejemplo n.º 17
0
 def match(self, x: "str") -> "bool":
     space = RegularExpressionMatchingStateSpace(self.nfa, x)
     return BacktrackingEnumerator().first(space) != None  #]match