default='DepthFirst', help='the search method to be used') # problem-specific arguments parser.add_argument('-i', '--initial', required=True, nargs=2, type=int, metavar=('row', 'col'), help='row and col where the knight is initially placed') settings = parser.parse_args() # state class, problem and method row, col = settings.initial if settings.method in searchtoy.blind_methods: state_class = tourState problem = searchtoy.Problem(state_class(row, col), state_class.is_complete) method = getattr(searchtoy, settings.method)() else: state_class = WarnsdorfState problem = searchtoy.Problem(state_class(row, col), state_class.is_complete) method = getattr(searchtoy, settings.method)(evaluator=Warnsdorf) # solve (a single solution is sufficient) solution = problem.solve(method) print(solution.state, end="\n\n") print("explored", method.nb_explored, "states")
help='the type of solution required') # problem-specific parser.add_argument('-f', '--filename', required=True, help='the text file containing the initial puzzle') settings = parser.parse_args() # state class state_class = tilesState # problem problem = searchtoy.Problem(tilesState.fromFile(settings.filename), state_class.is_target) # method if settings.method in searchtoy.blind_methods: method = getattr(searchtoy, settings.method)() else: method = getattr(searchtoy, settings.method)(evaluator=manhattanEvaluator) # solve, according to solution type required if settings.solution_type == 'all': for solution in problem.solutions(method): for state, operation in solution.path: print(state, operation, end='\n\n') print(solution.state, " [", solution.cost, "]", sep="")
required=True, help='the name of the file containing the puzzle') parser.add_argument('--most-constrained', dest='most_constrained', help='flag, use domains to make branching decisions', action='store_true') settings = parser.parse_args() # state class state_class = sudokuState # generator if settings.most_constrained: state_class.attach(MostConstrainedGenerator) else: state_class.attach(SequentialGenerator) # problem problem = searchtoy.Problem(state_class.from_file(settings.filename), state_class.is_complete) # method method = getattr(searchtoy, settings.method)() # solve (a single solution is sufficient) solution = problem.solve(method) print(solution.state) print("explored", method.nb_explored, "states")
is_valid() method. """ yield self.operators.cross() for what in ("wolf", "cabbage", "sheep"): if self.positions[what] == self.positions["farmer"]: yield self.operators.carry(what) # command-line arguments parser = argparse.ArgumentParser( description="Solves the wolf, cabbage and goat problem.") # generic parser.add_argument('--method', choices=searchtoy.blind_methods, default='DepthFirst', help='the search method to be used') settings = parser.parse_args() # problem and method crossing = searchtoy.Problem(crossState(), crossState.all_across) method = getattr(searchtoy, settings.method)() # single solution solution = crossing.optimize(method) for operation in solution.path.operations: print(operation) print(solution.state) print("explored", method.nb_explored, "states")
metavar='CAPACITY', help='the capacities of the buckets') parser.add_argument('--target', type=int, required=True, help='the target amount of water in any bucket') settings = parser.parse_args() # state class state_class = bucketState # problem problem = searchtoy.Problem( state_class(settings.buckets), lambda state: settings.target in state.buckets.values()) # method method = getattr(searchtoy, settings.method)() # solve if settings.solution_type == 'all': for solution in problem.solutions(method): for state, operation in solution.path: print(state, operation) print(solution.state, solution.cost, end="\n\n") print("explored", method.nb_explored, "states") print("found", method.nb_solutions, "solutions")
'--domains', help='flag, use column domains to make branching decisions', action='store_true') settings = parser.parse_args() # problem and method # state class if settings.domains: state_class = QueensDomainsState else: state_class = QueensState # problem problem = searchtoy.Problem(state_class(settings.size), state_class.is_complete) # method method = getattr(searchtoy, settings.method)() # solve, according to solution type required if settings.solution_type == 'all': for solution in problem.solutions(method): print(solution.state, end="\n\n") print("explored", method.nb_explored, "states") print("found", method.nb_solutions, "solutions") else:
settings = parser.parse_args() # state class state_class = crossState # generator if settings.generator == 'solo': state_class.attach(SoloFirstGenerator) elif settings.generator == 'pairs': state_class.attach(PairsFirstGenerator) else: state_class.attach(AdaptiveGenerator) # problem problem = searchtoy.Problem(state_class(), state_class.all_across) # method method = getattr(searchtoy, settings.method)() # solve, according to solution type required if settings.solution_type == 'all': for solution in problem.solutions(method, upper_bound=settings.upper_bound): for state, operation in solution.path: print(state, operation, end='\n\n') print(solution.state, " [", solution.cost, "]", sep="") print("explored", method.nb_explored, "states") print("found", method.nb_solutions, "solutions")
help='flag, try jumps before sliding', action='store_true') settings = parser.parse_args() # state class state_class = swapState # generator if settings.jumpy: state_class.attach(jumpyGenerator) else: state_class.attach(obviousGenerator) # problem problem = searchtoy.Problem(state_class(settings.size), state_class.is_target) # method if settings.method in searchtoy.blind_methods: method = getattr(searchtoy, settings.method)() else: method = getattr(searchtoy, settings.method)(evaluator=distance) # solve, according to solution type required if settings.solution_type == 'all': for solution in problem.solutions(method): for state, operation in solution.path: print(state, operation) print(solution.state, solution.cost, end="\n\n")