def search_plan_mod(prob, search, heuristic_class): """ Copied from Pyperplan, modified to take a parsed problem rather than two filenames """ task = plan._ground(prob) heuristic = None if not heuristic_class is None: heuristic = heuristic_class(task) search_start_time = time.clock() solution = plan._search(task, search, heuristic) plan.logging.info( 'Wall-clock search time: {0:.2}'.format(time.clock() - search_start_time)) return solution
import os import py from task import Task, Operator import pyperplan as planner from search import breadth_first_search, searchspace 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
Initialize pyperplan and parse the domain and task files ''' par = plan.Parser('class/slide-domain-grid.pddl', 'class/task3x3-grid.pddl') dom = par.parse_domain() pddlProb = par.parse_problem(dom) ''' Replace the board in the parsed PDDL problem with the random one and compute the optimal solution using BFS ''' replaceBoard(pddlProb, state) sol = search_plan_mod(pddlProb, plan.SEARCHES['bfs'], None) ''' Ground the problem again and make a root node that contains the random state This is the input for the heuristic functions ''' task = plan._ground(pddlProb) root = searchspace.make_root_node(task.initial_state) ''' initialize heuristic functions and compute them on the root node ''' hff = plan.HEURISTICS['hff'](task) hmax = plan.HEURISTICS['hsa'](task) hadd = plan.HEURISTICS['hadd'](task) hmax = plan.HEURISTICS['hmax'](task) init_hff = hff(root) init_hadd = hadd(root) init_hmax = hmax(root) print("BFS solution is length : " + str(len(sol)))
def ground_problem(problem_file): problem = parse_problem(problem_file) print('Grounding', problem.name) task = planner._ground(problem) assert task is not None return task
def ground_problem(problem_file): problem = parse_problem(problem_file) print("Grounding", problem.name) task = planner._ground(problem) assert task is not None return task
import os import py from task import Task, Operator import pyperplan as planner from search import breadth_first_search, searchspace 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