def write_sas_task(sas_task, temp_dir): translate_path = os.path.join(temp_dir, TRANSLATE_OUTPUT) #clear_dir(temp_dir) safe_remove(translate_path) ensure_dir(translate_path) with open(os.path.join(temp_dir, TRANSLATE_OUTPUT), "w") as output_file: sas_task.output(output_file) return translate_path
def run_search(temp_dir, planner=DEFAULT_PLANNER, max_planner_time=DEFAULT_MAX_TIME, max_cost=INF, debug=False): max_time = convert_cost(max_planner_time) max_cost = convert_cost(scale_cost(max_cost)) start_time = time() search = os.path.join(FD_BIN, SEARCH_COMMAND) if planner == 'cerberus': planner_config = SEARCH_OPTIONS[planner] # Check if max_time, max_cost exist else: planner_config = SEARCH_OPTIONS[planner] % (max_time, max_cost) command = search.format(temp_dir + SEARCH_OUTPUT, planner_config, temp_dir + TRANSLATE_OUTPUT) temp_path = os.path.join(os.getcwd(), TEMP_DIR) domain_path = os.path.abspath(os.path.join(temp_dir, DOMAIN_INPUT)) problem_path = os.path.abspath(os.path.join(temp_dir, PROBLEM_INPUT)) if USE_FORBID: command = FORBID_COMMAND.format(num=2, domain=domain_path, problem=problem_path) if debug: print('Search command:', command) # os.popen is deprecated # run, call, check_call, check_output #with subprocess.Popen(command.split(), stdout=subprocess.PIPE, shell=True, cwd=None) as proc: # output = proc.stdout.read() # CalledProcessError #try: # output = subprocess.check_output(command, shell=True, cwd=None) #, timeout=None) #except subprocess.CalledProcessError as e: # print(e) for filename in os.listdir(temp_path): if filename.startswith(SEARCH_OUTPUT): safe_remove(os.path.join(temp_path, filename)) proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, cwd=None, close_fds=True) output, error = proc.communicate() if USE_FORBID: for filename in os.listdir(FORBID_PATH): if filename.startswith(SEARCH_OUTPUT): os.rename(os.path.join(FORBID_PATH, filename), os.path.join(temp_path, filename)) if debug: print(output.decode(encoding='UTF-8')[:-1]) print('Search runtime:', time() - start_time) plan_files = sorted(f for f in os.listdir(temp_path) if f.startswith(SEARCH_OUTPUT)) print('Plans:', plan_files) return parse_solutions(temp_path, plan_files)
def run_search(temp_dir, planner=DEFAULT_PLANNER, max_planner_time=DEFAULT_MAX_TIME, max_cost=INF, debug=False): """ Runs FastDownward's search phase on translated SAS+ problem TRANSLATE_OUTPUT :param temp_dir: the directory for temporary FastDownward input and output files :param planner: a keyword for the FastDownward search configuration in SEARCH_OPTIONS :param max_planner_time: the maximum runtime of FastDownward :param max_cost: the maximum FastDownward plan cost :param debug: If True, print the FastDownward search output :return: a tuple (plan, cost) where plan is a sequence of PDDL actions (or None) and cost is the cost of the plan (INF if no plan) """ max_time = convert_value(max_planner_time) max_cost = convert_value(scale_cost(max_cost)) start_time = time() search = os.path.join(FD_BIN, SEARCH_COMMAND) if planner == 'cerberus': planner_config = SEARCH_OPTIONS[ planner] # Check if max_time, max_cost exist else: planner_config = SEARCH_OPTIONS[planner] % (max_time, max_cost) command = search.format(temp_dir + SEARCH_OUTPUT, planner_config, temp_dir + TRANSLATE_OUTPUT) temp_path = os.path.join(os.getcwd(), TEMP_DIR) domain_path = os.path.abspath(os.path.join(temp_dir, DOMAIN_INPUT)) problem_path = os.path.abspath(os.path.join(temp_dir, PROBLEM_INPUT)) if USE_FORBID: command = FORBID_COMMAND.format(num=2, domain=domain_path, problem=problem_path) if debug: print('Search command:', command) # os.popen is deprecated # run, call, check_call, check_output #with subprocess.Popen(command.split(), stdout=subprocess.PIPE, shell=True, cwd=None) as proc: # output = proc.stdout.read() # CalledProcessError #try: # output = subprocess.check_output(command, shell=True, cwd=None) #, timeout=None) #except subprocess.CalledProcessError as e: # print(e) for filename in os.listdir(temp_path): if filename.startswith(SEARCH_OUTPUT): safe_remove(os.path.join(temp_path, filename)) proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True, cwd=None, close_fds=True) output, error = proc.communicate() if USE_FORBID: for filename in os.listdir(FORBID_PATH): if filename.startswith(SEARCH_OUTPUT): os.rename(os.path.join(FORBID_PATH, filename), os.path.join(temp_path, filename)) if debug: print(output.decode(encoding='UTF-8')[:-1]) print('Search runtime: {:.3f}'.format(elapsed_time(start_time))) plan_files = sorted(f for f in os.listdir(temp_path) if f.startswith(SEARCH_OUTPUT)) print('Plans:', plan_files) return parse_solutions(temp_path, plan_files)