Example #1
0
def main():
    # Commandline parsing
    log_levels = ["debug", "info", "warning", "error"]

    # get pretty print names for the search algorithms:
    # use the function/class name and strip off '_search'
    def get_callable_names(callables, omit_string):
        names = [c.__name__ for c in callables]
        names = [n.replace(omit_string, "").replace("_", " ") for n in names]
        return ", ".join(names)

    search_names = get_callable_names(SEARCHES.values(), "_search")

    argparser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    argparser.add_argument(dest="domain", nargs="?")
    argparser.add_argument(dest="problem")
    argparser.add_argument("-l",
                           "--loglevel",
                           choices=log_levels,
                           default="info")
    argparser.add_argument(
        "-H",
        "--heuristic",
        choices=HEURISTICS.keys(),
        help="Select a heuristic",
        default="hff",
    )
    argparser.add_argument(
        "-s",
        "--search",
        choices=SEARCHES.keys(),
        help=f"Select a search algorithm from {search_names}",
        default="bfs",
    )
    args = argparser.parse_args()

    logging.basicConfig(
        level=getattr(logging, args.loglevel.upper()),
        format="%(asctime)s %(levelname)-8s %(message)s",
        stream=sys.stdout,
    )

    hffpo_searches = ["gbf", "wastar", "ehs"]
    if args.heuristic == "hffpo" and args.search not in hffpo_searches:
        print(
            "ERROR: hffpo can currently only be used with %s\n" %
            hffpo_searches,
            file=sys.stderr,
        )
        argparser.print_help()
        sys.exit(2)

    args.problem = os.path.abspath(args.problem)
    if args.domain is None:
        args.domain = find_domain(args.problem)
    else:
        args.domain = os.path.abspath(args.domain)

    search = SEARCHES[args.search]
    heuristic = HEURISTICS[args.heuristic]

    if args.search in ["bfs", "ids", "sat"]:
        heuristic = None

    logging.info("using search: %s" % search.__name__)
    logging.info("using heuristic: %s" %
                 (heuristic.__name__ if heuristic else None))
    use_preferred_ops = args.heuristic == "hffpo"
    solution = search_plan(
        args.domain,
        args.problem,
        search,
        heuristic,
        use_preferred_ops=use_preferred_ops,
    )

    if solution is None:
        logging.warning("No solution could be found")
    else:
        solution_file = args.problem + ".soln"
        logging.info("Plan length: %s" % len(solution))
        write_solution(solution, solution_file)
        validate_solution(args.domain, args.problem, solution_file)
Example #2
0
def run_planner(problem_file):
    domain_file = planner.find_domain(problem_file)
    print("Searching solution for", domain_file, problem_file)
    assert (planner.search_plan(domain_file, problem_file,
                                breadth_first_search, None) is not None)
Example #3
0
finisher1-stack-letter sheet1 dummy-sheet
"""
optimal_plan = [op.strip() for op in optimal_plan.splitlines()]

import os

from pyperplan import planner
from pyperplan.search import breadth_first_search, searchspace
from pyperplan.task import Operator, Task

benchmarks = os.path.abspath(
    os.path.join(os.path.abspath(__file__), "../../../../benchmarks"))

# Collect problem files
problem_file = os.path.join(benchmarks, "parcprinter", "task01.pddl")
domain_file = planner.find_domain(problem_file)

problem = planner._parse(domain_file, problem_file)
task = planner._ground(problem)

# Manually do the "search"
node = searchspace.make_root_node(task.initial_state)
for step, op_name in enumerate(optimal_plan, start=1):
    for op, successor_state in task.get_successor_states(node.state):
        if not op.name.strip("()") == op_name:
            continue
        node = searchspace.make_child_node(node, op, successor_state)

# Check that we reached the goal
assert len(task.goals - node.state) == 0
Example #4
0
def parse_problem(problem_file, domain_file=None):
    if domain_file is None:
        domain_file = planner.find_domain(problem_file)
    print("Parsing", problem_file)
    problem = planner._parse(domain_file, problem_file)
    return problem