コード例 #1
0
def sum_solver(W: int, L: List[int]):
    class SumPS(PartialSolutionWithVisitedControl):
        def __init__(self, solution, pending):
            self.solution = solution
            self.n = len(solution)
            self.pending = pending

        def successors(self) -> Iterable["PartialSolutionWithVisitedControl"]:
            if self.n < len(L):
                if self.pending >= L[self.n]:
                    yield SumPS(self.solution + (1, ),
                                self.pending - L[self.n])
                yield SumPS(self.solution + (0, ), self.pending)

        def state(self) -> State:
            return self.n, self.pending

        def is_solution(self) -> bool:
            return self.n == len(L) and self.pending == 0

        def get_solution(self) -> Solution:
            return self.solution

    initialPS = SumPS((), W)
    return BacktrackingVCSolver.solve(initialPS)
コード例 #2
0
def solver(conjunto, A, B):
    class SumaDosEnterosPS(PartialSolutionWithVisitedControl):
        def __init__(self, solucion, sumA, sumB):
            self.solucion = solucion
            self.n = len(self.solucion)
            self.sumA = sumA
            self.sumB = sumB

        def is_solution(self):
            if self.n == len(conjunto):
                return self.sumA == A and self.sumB == B

        def get_solution(self):
            return self.solucion

        def successors(self):
            if self.n < len(conjunto):
                num = conjunto[self.n]

                if self.sumA + num <= A:
                    yield SumaDosEnterosPS(self.solucion + (1,), self.sumA + num, self.sumB)

                if self.sumB + num <= B:
                    yield SumaDosEnterosPS(self.solucion + (2,), self.sumA, self.sumB + num)

                yield SumaDosEnterosPS(self.solucion + (0,), self.sumA, self.sumB)

        def state(self):
            return self.n, self.sumA, self.sumB

    initialPS = SumaDosEnterosPS((), 0, 0)
    return BacktrackingVCSolver.solve(initialPS)
コード例 #3
0
def cuerda_solver(L, P, M):
    """
	Funcion que devuelve la cantidad de vallas de longitud L a usar

	Parameters:
	L (list): longitud cuerdas
	P (list): cantidad cuerdas disponibles
	M (int): longitud de la cuerdas a comprobar

	"""
    class CuerdasPS(PartialSolutionWithVisitedControl):
        def __init__(self, ds, lcuerdas):
            self.ds = ds
            self.n = len(ds)
            self.lcuerdas = lcuerdas

        def is_solution(self):
            return self.n == len(L) and self.lcuerdas == 0

        def get_solution(self):
            return self.ds

        def state(self):
            return self.n, self.lcuerdas

        def successors(self):
            if self.n < len(L):
                for c in range(min(P[self.n], self.lcuerdas // L[self.n]) + 1):
                    yield CuerdasPS(self.ds + (c, ),
                                    self.lcuerdas - (L[self.n] * c))

    initial_ps = CuerdasPS((), M)
    return BacktrackingVCSolver.solve(initial_ps)
コード例 #4
0
def subconjuntos_disjuntos(C, A, B):
    class SubconjuntosPS(PartialSolutionWithVisitedControl):
        def __init__(self, sa, sb, s: Set):
            self.sa = sa
            self.sb = sb
            self.s = s
            self.n = len(s)

        def successors(self) -> Iterable["PartialSolutionWithVisitedControl"]:
            if self.n > 0:
                setcopia = self.s.copy()
                elem = setcopia.pop()
                yield SubconjuntosPS(self.sa, self.sb, setcopia)
                if sum(self.sa) + elem <= A:
                    acopia = self.sa.copy()
                    acopia.add(elem)
                    yield SubconjuntosPS(acopia, self.sb, setcopia)
                if sum(self.sb) + elem <= B:
                    bcopia = self.sb.copy()
                    bcopia.add(elem)
                    yield SubconjuntosPS(self.sa, bcopia, setcopia)

        def state(self) -> State:
            return self.n, sum(self.sa), sum(self.sb)

        def is_solution(self) -> bool:
            return sum(self.sa) == A and sum(self.sb) == B

        def get_solution(self) -> Solution:
            return self.sa, self.sb

    initialPS = SubconjuntosPS(set(), set(), C)
    return BacktrackingVCSolver.solve(initialPS)
コード例 #5
0
def bricker_vc_solve(level: Level):
    class BrikerVC_PS(PartialSolutionWithVisitedControl):
        def __init__(self, block: Block, decisions: Tuple[Move, ...]):
            self.block = block
            self.decisions = decisions

        def is_solution(self) -> bool:
            return self.block.is_standing_at_pos(level.get_targetpos())

        def get_solution(self) -> Solution:
            return self.decisions

        def successors(self) -> Iterable["BrikerVC_PS"]:
            for movement in self.block.valid_moves(level.is_valid):
                print(self.decisions)
                yield BrikerVC_PS(self.block.move(movement),
                                  self.decisions + (movement, ))

        def state(self) -> State:
            return self.block

    # TODO: crea initial_ps y llama a BacktrackingVCSolver.solve
    initial_ps = BrikerVC_PS(Block(level.get_startpos(), level.get_startpos()),
                             ())
    return BacktrackingVCSolver.solve(initial_ps)
コード例 #6
0
def problema(C: List[int], A: int, B: int):
    class SumaDosEnterosPS(PartialSolutionWithVisitedControl):
        def __init__(self, quedan_a: int, quedan_b: int, sa: List[int],
                     sb: List[int], index):
            self.quedan_a = quedan_a
            self.quedan_b = quedan_b
            self.sa = sa
            self.sb = sb
            self.index = index

        def is_solution(self) -> bool:
            return self.quedan_a == 0 and self.quedan_b == 0

        def get_solution(self) -> Solution:
            return self.sa, self.sb

        def successors(self) -> Iterable["PartialSolution"]:
            print(self.index)
            print("A:", self.quedan_a)
            print("B:", self.quedan_b)
            if not self.is_solution() and self.index < len(C):
                if self.quedan_a - C[self.index] >= 0:
                    self.quedan_a -= C[self.index]
                    self.sa.append(C[self.index])
                elif self.quedan_b - C[self.index] >= 0:
                    self.quedan_b -= C[self.index]
                    self.sb.append(C[self.index])

                yield SumaDosEnterosPS(self.quedan_a, self.quedan_b, self.sa,
                                       self.sb, self.index + 1)

        def state(self) -> State:
            return self.quedan_a, self.quedan_b, self.index

    return BacktrackingVCSolver.solve(SumaDosEnterosPS(A, B, [], [], 0))
コード例 #7
0
def bricker_vc_solve(level: Level):
    class BrikerVC_PS(PartialSolutionWithVisitedControl):
        def __init__(self, block: Block, decisions: Tuple[Move, ...]):
            self.block = block
            self.decisions = decisions
            self.n = len(decisions)

        def is_solution(self) -> bool:
            return self.block.is_standing_at_pos(level.get_targetpos())

        def get_solution(self) -> Solution:
            return self.decisions

        def successors(self) -> Iterable["BrikerVC_PS"]:
            # if not self.is_solution():
            for elem in self.block.valid_moves(level.is_valid):
                yield BrikerVC_PS(self.block.move(elem),
                                  self.decisions + (elem, ))

        def state(self) -> State:
            return self.block

    # TODO: crea initial_ps y llama a BacktrackingVCSolver.solve
    bloque = Block(level.get_startpos(), level.get_startpos())
    return BacktrackingVCSolver.solve(BrikerVC_PS(bloque, ()))
コード例 #8
0
def solver(C, A, B):
	class PartialSolution(BacktrackingVCSolver):
		def __init__(self, ds, SA, SB):
			self.ds = ds
			self.n = len(ds)
			self.SA = SA
			self.SB = SB

		def is_solution(self):
			return self.n == len(C) and self.SA == A and self.SB == B

		def get_solution(self):
			return self.ds

		def state(self):
			return self.n, self.SA, self.SB

		def successors(self):
			if self.n < len(C):
				nr = C[self.n]

				if nr + self.SA <= A:
					yield PartialSolution(self.ds + (1,), self.SA + nr, self.SB)

				if nr + self.SB <= B:
					yield PartialSolution(self.ds + (2,), self.SA, self.SB + nr)

				yield PartialSolution(self.ds + (0,), self.SA, self.SB)

	initialPS = PartialSolution((), 0, 0)
	return BacktrackingVCSolver.solve(initialPS)