def __init__(self, fname_domain, fname_problem): """ Initialize a PDDL planner. @param domprob The domain problem definition. """ super(PDDLPlanner, self).__init__() self.fname_domain = fname_domain self.fname_problem = fname_problem # initialize domain problem definitions domprob = pddlpy.DomainProblem(self.fname_domain, self.fname_problem) self.predefined_initial_state = domprob.initialstate() self.predefined_goals = domprob.goals() self.operators = domprob.operators() # generate grounded operators dictionary # note: a new domain problem object needs to be initialized from PDDL files # everytime a new grounded operator is going to be generated, because # of the `ground_operator` bug in the `pddlpy` library. self.grounded_operators_dict = dict() for operator in self.operators: domprob = pddlpy.DomainProblem(self.fname_domain, self.fname_problem) grounded_operator = list(domprob.ground_operator(operator)) self.grounded_operators_dict[operator] = grounded_operator
def func_random_step(curr_state,seen_states): #make problem file from curr_state, goal state new_problem_file_loc = "./new_problem.pddl" replace_init_state_problem(problem_file_loc,curr_state,dest_problem_file_loc=new_problem_file_loc) starting_pddl_obj = pddlpy.DomainProblem(domain_file_loc, new_problem_file_loc) all_operators = list(starting_pddl_obj.operators()) random.shuffle(all_operators) for curr_operator in all_operators: action_instance_iter = starting_pddl_obj.ground_operator_curr_state_only(curr_operator) try: single_action = action_instance_iter.__next__() except: #there is not action instance in this state continue new_state = {x.ground({}) for x in starting_pddl_obj.initialstate()} new_state.difference_update(single_action.effect_neg) new_state.update(single_action.effect_pos) temp_state = [] for x in new_state: # prop_parts = x.predicate temp_state.append("_".join(x)) #end for loop new_state = tuple(temp_state) if new_state in seen_states: continue #else we can seen_states.add(new_state) return new_state #end return None #there was no random step to an unseen state.
def __init__(self, robot_domain_model, robot_problem, plan_file, spl_features, feat_x, feat_y, label_file): robot_dom_prob = pddlpy.DomainProblem(robot_domain_model, robot_problem) self.orig_start = self.convert_prop_tuple_list( robot_dom_prob.initialstate()) self.goal = self.convert_prop_tuple_list(robot_dom_prob.goals()) self.grounded_robot_model_map = self.convert_domain_obj_map( robot_dom_prob) self.explain_x = feat_x self.explain_y = feat_y self.spl_features = spl_features self.plan = [] self.label_seq = [] with open(plan_file) as p_fd: self.plan = [ ' '.join(a.strip().split(' ')) for a in p_fd.readlines() ] self.action_names = list( self.grounded_robot_model_map['domain'].keys()) #print (self.action_names) with open(label_file) as l_fd: self.label_seq = [l.strip() for l in l_fd.readlines()] self.beh_trace = self.execute_plan()
def convert_to_state_action_list(solution_list): """ :summary : Get the start state from the problem file. Parse each action, and apply it to the start state. Get the resultant state and repeat. Each state is converted into a list of propositions and placed in the trace. The ground actions are placed in the trace as a list of one :param solution_list: :return: """ s_a_trace = [] #get the start state using pddlpy pddl_obj = pddlpy.DomainProblem(domain_file_loc, problem_file_loc) init_state_prop_set = pddl_obj.initialstate() state_dict = {} for single_prop in init_state_prop_set: curr_prop = single_prop.predicate curr_prop = [str.lower(x) for x in curr_prop] insert_list_in_dict(curr_prop,state_dict) #---end for loop making the state dict #convert the state dict into a list of propositions curr_state = convert_dict_to_list(state_dict) prev_state = curr_state for single_action in solution_list: curr_state = domain_parser_obj.apply_action(set(curr_state),single_action) s_a_trace.append(tuple(list(prev_state) + ["ACTION_"+single_action]))#the order of propositions does not matter. They all connect to each other s_a_trace.append(tuple(list(curr_state) + ["ACTION_"+single_action]))#the order of propositions does not matter. They all connect to each other prev_state = curr_state #---end for loop return s_a_trace
def run_demo(demonumber): print('===') print(f'Run DEMO {demonumber}') domainfile = "./examples-pddl/domain-0%d.pddl" % demonumber problemfile = "./examples-pddl/problem-0%d.pddl" % demonumber domprob = pddlpy.DomainProblem(domainfile, problemfile) print() print("DOMAIN PROBLEM") print("objects") print("\t", domprob.worldobjects()) print("operators") print("\t", list( domprob.operators() )) print("init",) print("\t", domprob.initialstate()) print("goal",) print("\t", domprob.goals()) print() ops_to_test = { 1:"op2", 2:"move", 3:"move", 4:"move", 5:"A1" } op = ops_to_test[demonumber] print("ground for operator", op, "applicable if (adjacent loc1 loc2)") for o in domprob.ground_operator(op): if ("adjacent","loc1","loc2") in o.precondition_pos: print() print( "\tvars", o.variable_list ) print( "\tpre+", o.precondition_pos ) print( "\tpre-", o.precondition_neg ) print( "\teff+", o.effect_pos ) print( "\teff-", o.effect_neg ) print()
def get_state_sequence_form_with_goals(solution_list, include_static_props=True): """ :summary : Get the start state from the problem file. Parse each action, and apply it to the start state. Get the resultant state and repeat. Each state is converted into a list of propositions and placed in the trace. The ground actions are placed in the trace as a list of one :param solution_list: :return: """ state_seq_trace = [] action_seq_trace = [] #get the start state using pddlpy pddl_obj = pddlpy.DomainProblem(domain_file_loc, problem_file_loc) init_state_prop_set = pddl_obj.initialstate() state_dict = {} for single_prop in init_state_prop_set: curr_prop = single_prop.predicate curr_prop = [str.lower(x) for x in curr_prop] insert_list_in_dict(curr_prop, state_dict) #---end for loop making the state dict #convert the state dict into a list of propositions curr_state = convert_dict_to_list(state_dict) state_seq_trace.append(tuple(curr_state)) for single_action in solution_list: action_seq_trace.append(single_action) next_state = domain_parser_obj.apply_action( set(curr_state), single_action) state_seq_trace.append(tuple(next_state)) curr_state = next_state #---end for loop return tuple(state_seq_trace), tuple(action_seq_trace), tuple([ "_".join(x.predicate) for x in pddl_obj.goals() ]) # converst (at,p0,l10) to at_p0_l10
def get_domprob(domain_path, problem_path): """Returns the DomainProblem object from pddlpy. Parameters: domain_path: string, path of the domain file problem_path: string, path of the problem file """ return pddlpy.DomainProblem(domain_path, problem_path)
def create_problem(self, domain_file_path, problem_file_path): self.domprob = pddlpy.DomainProblem(domain_file_path, problem_file_path) self.pddl = to_pddl_aima_obj(self.domprob) # self.pddl = three_block_tower() self.negkb = FolKB([]) self.graphplan = MyGraphPlan(self.pddl, self.negkb) self.nx_graph = nx.DiGraph() self.is_ready = True
def __init__(self, domain_file, problem_file): print('Domain: ', domain_file) print('Problem: ', problem_file) domprob = pddlpy.DomainProblem(domain_file, problem_file) self.domain = Domain(domprob) init = set(Predicate(p.predicate) for p in domprob.initialstate()) self.init_state = State(init, 0, None, []) self.goals = init = set( Predicate(p.predicate) for p in domprob.goals()) goal_state = State(self.goals, 0, None, []) print('Goal: ', goal_state)
def create_problem(self, domain_file_path, problem_file_path): self.domprob = pddlpy.DomainProblem(domain_file_path, problem_file_path) self.pddl = to_pddl_aima_obj(self.domprob) # self.pddl = three_block_tower() self.negkb = FolKB([]) self.graphplan = GraphPlan(self.pddl, self.negkb) self.nx_graph = nx.DiGraph() self.max_depth_checking = 5 # will expand at most n equal levels self.is_ready = True
def search_explanations(xpl_folder, heuristic_flag, domain_template_file, objs_w_underscores): x = PrettyTable() title = "BlocksWorld " if "blocksworld" in xpl_folder else "PathWay " title += "w/ " if heuristic_flag else "w/o " title += "heuristic" x.title = title x.field_names = [" Num expl", "Elapsed time"] orig_xpl_folder = xpl_folder for xpls in range(2, 5): test_folder = orig_xpl_folder + str(xpls) + "/" pddl_hm = test_folder + hm_name + ".pddl" pddl_rm = test_folder + rm_name + ".pddl" pddl_prob = test_folder + prob_name pddl_rplan = test_folder + grounded_robot_plan_name human_model = pddlpy.DomainProblem(pddl_hm, pddl_prob) robot_model = pddlpy.DomainProblem(pddl_rm, pddl_prob) p = Problem(human_model, robot_model, hm_name, rm_name, pddl_rm, pddl_prob, pddl_rplan, tmp_state_file, heuristic_flag, domain_template_file, objs_w_underscores) start_time = time.time() expls = astarSearch(p) elapsed_time = time.time() - start_time x.add_row([len(expls), str(round(elapsed_time, 2)) + "s"]) print(x) print("\n--------------------------------------------")
def __init__(self, domain_model, problem, dom_templ, prob_templ): self.dom_prob = pddlpy.DomainProblem(domain_model, problem) self.original_domain_file = domain_model self.domain_template_file = dom_templ self.prob_template_file = prob_templ # get the model prop list and actions self.create_domain_set_and_action_list() self.goal_state = self.convert_prop_tuple_list(self.dom_prob.goals()) self.init_state = self.convert_prop_tuple_list( self.dom_prob.initialstate()) self.prev_goals = set() # get missing propositions with open(self.prob_template_file) as p_fd: self.prob_template_str = "\n".join( [i.strip() for i in p_fd.readlines()])
def test_example_03(): import pddlpy domprob = pddlpy.DomainProblem('examples-pddl/domain-03.pddl', 'examples-pddl/problem-03.pddl') assert {tuple(e.predicate) for e in domprob.initialstate()} == {('adjacent', 'loc2', 'loc1'), ('unloaded', 'robr'), ('atl', 'robr', 'loc1'), ('unloaded', 'robq'), ('in', 'conta', 'loc1'), ('atl', 'robq', 'loc2'), ('adjacent', 'loc1', 'loc2'), ('in', 'contb', 'loc2')} assert list(domprob.operators()) == ['move', 'load', 'unload']
def main(): fname_domain = '../PDDL/domain.pddl' fname_problem = '../PDDL/problem_gridmc.pddl' print('load planning domain and problem:') print(' - domain file: %s' % (fname_domain)) print(' - problem file: %s' % (fname_problem)) domprob = pddlpy.DomainProblem(fname_domain, fname_problem) print('') show_domprob_summary(domprob) planner = PDDLPlanner(fname_domain, fname_problem) plan = planner.find_plan() show_plan(plan) IPython.embed()
def __init__(self, robot_domain_model, robot_problem, plan_file): robot_dom_prob = pddlpy.DomainProblem(robot_domain_model, robot_problem) self.orig_start = self.convert_prop_tuple_list( robot_dom_prob.initialstate()) self.goal = self.convert_prop_tuple_list(robot_dom_prob.goals()) self.grounded_robot_model_map = self.convert_domain_obj_map( robot_dom_prob) self.plan = [] self.label_seq = [] with open(plan_file) as p_fd: self.plan = [ ' '.join(a.strip().split(' ')) for a in p_fd.readlines() ] self.action_names = list( self.grounded_robot_model_map['domain'].keys())
def __init__(self, domain_model, problem, dom_templ, prob_templ, proposition_list_file): self.original_domain_file = domain_model self.original_problem_file = problem self.dom_prob = pddlpy.DomainProblem(domain_model, problem) self.domain_template_file = dom_templ self.prob_template_file = prob_templ # get list of prop list with open(proposition_list_file) as p_fd: self.proposition_set = set(p.strip() for p in p_fd.readlines()) # get the model prop list and actions self.create_domain_set_and_action_list() # get missing propositions self.missing_predicates = self.find_missing_predicate_set() with open(self.domain_template_file) as d_fd: self.domain_template_str = "\n".join( [i.strip() for i in d_fd.readlines()]) with open(self.prob_template_file) as p_fd: self.prob_template_str = "\n".join( [i.strip() for i in p_fd.readlines()])
def test_example_ld01(): import pddlpy domprob = pddlpy.DomainProblem('examples-pddl/domain-ld01.pddl', 'examples-pddl/problem-ld01.pddl')
import pddlpy import sys dom = sys.argv[1] prob = sys.argv[2] d_pr = pddlpy.DomainProblem(dom, prob) for k in d_pr.operators(): for pr in d_pr.domain.operators[k].precondition_pos: if type(pr) == "tuple": print(k + "-has-preconditon-", " ".join(list(pr))) else: print(k + "-has-preconditon-", " ".join(list(pr.predicate))) for pr in d_pr.domain.operators[k].precondition_neg: if type(pr) == "tuple": print(k + "-has-neg-preconditon-", " ".join(list(pr))) else: print(k + "-has-neg-preconditon-", " ".join(list(pr.predicate)))
def main(): #All the robots will have 1000 availability of black color and white color. amount = 1000 #Reading from the Domain and problem file. domprob = pddlpy.DomainProblem('Domain.pddl', Size_Problems[5]) initRob = [] initState = [] targetState = [] max_robot = -300 #Checking how many robots we have in the pddl file for o in domprob.initialstate(): if ((str(o)).split(',')[0] == "('robot-at'"): robot = ((str(o)).split(',')[1])[7] Row = (((str(o)).split(',')[2]).split('-')[0]).split('_')[1] Column = (((str(o)).split(',')[2]).split('-')[1]).split("'")[0] element = [(int(Column)), (int(Row))] #In order to know how many robots we have if (int(robot) > max_robot): max_robot = int(robot) #Create a list with all the information from the robots. for i in range(0, max_robot): element = [] initRob.append(None) #Look at the initial positions of the robot. #Update the robots list. for o in domprob.initialstate(): if ((str(o)).split(',')[0] == "('robot-at'"): robot = ((str(o)).split(',')[1])[7] Row = (((str(o)).split(',')[2]).split('-')[0]).split('_')[1] Column = (((str(o)).split(',')[2]).split('-')[1]).split("'")[0] element = [(int(Column)), (int(Row))] element2 = [] element2.append(element) element2.append(0) element2.append(amount) element2.append(amount) initRob[int(robot) - 1] = element2 #Look at which color has the robot. for o in domprob.initialstate(): if ((str(o)).split(',')[0] == "('robot-has'"): robot = ((str(o)).split(',')[1])[7] color = ((str(o)).split(',')[2]) if (str(color) == " 'black')"): initRob[int(robot) - 1][1] = 0 else: initRob[int(robot) - 1][1] = 1 max_column = -3000 max_rows = -3000 #How many columns and how many rows we have in this pddl file. for o in domprob.worldobjects(): if ('tile') in o: x, column = (str(o)).split('-') if (int(column) >= int(max_column)): max_column = column z, row = (str(x)).split('_') if (int(row) >= int(max_rows) + 1): max_rows = row # Create the initial and the target state. for i in range((int(max_rows)) + 1): list = [] list2 = [] for j in range(int(max_column)): list.append(0) list2.append(0) initState.append(list) targetState.append(list2) #Update the initState with the init robot positions. for i in range(0, max_robot): row_robot, column_robot = initRob[i][0] initState[column_robot][row_robot - 1] = 3 #Update the target state using the goals defined in the pddl file. for g in domprob.goals(): goal_row = (((str(g)).split(',')[1]).split('-')[0]).split('_')[1] goal_column = (((str(g)).split(',')[1]).split('-')[1]).split("'")[0] goal_color = ((str(g)).split(',')[2]) if (str(goal_color) == " 'black')"): num_color = 2 else: num_color = 1 targetState[int(goal_row) - 1][int(goal_column) - 1] = int(num_color) #return the robot information, the init state information and also the target State information return initRob, initState, targetState
def get_goals(): pddl_obj = pddlpy.DomainProblem(domain_file_loc, problem_file_loc) return tuple(["_".join(x.predicate) for x in pddl_obj.goals()])