def run_test_plan(world_spec, plan, expression_spec, result, sets={}): global run, passed run += 1 try: world = make_world(world_spec, sets) i = 1 for p in plan: run += 1 (pre, eff, subst) = p preexp = make_expression(pre) effexp = make_expression(eff) for s in subst: (var, val) = s preexp = substitute(preexp, var, val) effexp = substitute(effexp, var, val) if not models(world, preexp): print("precondition failed at step", i) return False world = apply(world, effexp) passed += 1 i += 1 exp = make_expression(expression_spec) res = models(world, exp) if res == result: passed += 1 return True except Exception: traceback.print_exc() return False
def get_neighbors(self): # for each grounded action for ac in groundedActions: # if the action is modeled in the state if expressions.models(self.state, expressions.make_expression( ac.precondition)): ## apply the effect to the world newstate = expressions.apply( self.state, expressions.make_expression(ac.effect)) ## add state to the neighbours list as an edge nt = PlanNode(ac.name, newstate, self.objs) ne = graph.Edge(nt, 1, ac.name) self.neighbours.append(ne) return self.neighbours
def run_test_substitute(world_spec, expression_spec, subst, result, sets={}): global run, passed run += 1 try: world = make_world(world_spec, sets) action = make_expression(action_spec) world1 = apply(world, action) exp = make_expression(expression_spec) res = models(world1, exp) if res == result: passed += 1 return True except Exception: traceback.print_exc() return False
def parse_problem(fname): """ Parses a PDDL problem file contained in the file fname The return value of this function is passed to planner.plan, and does not have to follow any particular format """ stack = get_stack_from_pddl(fname) pddl_objects = {} pddl_init_exp = [] pddl_goal_exp = [] for element in stack: for subelement in element: if subelement[0] == ":objects": pddl_objects = process_parameters(subelement[1:]) if subelement[0] == ":init": pddl_init_exp = subelement[1:] if subelement[0] == ":goal": pddl_goal_exp = subelement[1] logger.info("PDDL Objects: %s" % pddl_objects) logger.info("PDDL Init: %s" % pddl_init_exp) logger.info("PDDL Goal: %s" % pddl_goal_exp) return pddl_objects, pddl_init_exp, expressions.make_expression( pddl_goal_exp)
def plan(domain, problem, useheuristic=True): """ Find a solution to a planning problem in the given domain The parameters domain and problem are exactly what is returned from pddl.parse_domain and pddl.parse_problem. If useheuristic is true, a planning heuristic (developed in task 4) should be used, otherwise use pathfinding.default_heuristic. This allows you to compare the effect of your heuristic vs. the default one easily. The return value of this function should be a 4-tuple, with the exact same elements as returned by pathfinding.astar: - A plan, which is a sequence of graph.Edge objects that have to be traversed to reach a goal state from the start. Each Edge object represents an action, and the edge's name should be the name of the action, consisting of the name of the operator the action was derived from, followed by the parenthesized and comma-separated parameter values e.g. "move(agent-1,sq-1-1,sq-2-1)" - distance is the number of actions in the plan (i.e. each action has cost 1) - visited is the total number of nodes that were added to the frontier during the execution of the algorithm - expanded is the total number of nodes that were expanded (i.e. whose neighbors were added to the frontier) """ goal = expressions.make_expression(problem[2][0]) def heuristic(state, action): num = 1 num = expressions.goalsAchieved(action.target.world, goal.getRoot(), num) num = 1 / num #print(num) neighNum = len(state.neighbors) #print(neighNum) return num def isgoal(state): return True #Problem = [typesDictionary, initialStates, rawGoals, ExpressionGoal] #Domain = [actionsDictionary, typesDictionary] #print(problem[1]) #expressions.printNAryTree(goal.getRoot()) allPossibleActions = [] #expressions.printNAryTree(goal.getRoot()) start = graph.PlanNode(problem[1], domain[1], problem[0], [], 'Root') allPossibleActions = start.set_possibleActions(domain[0]) start.set_initialStates() start.get_neighbors(allPossibleActions, domain[0], useheuristic) #for n in start.neighbors: # print(n.name) #print(allPossibleActions) #astar(start, heuristic, goal, allActions, actionsDictionary) #input("Press Enter to continue...") #print(useheuristic) edgesPassed, totalPathCost, numFront, numExpandedNodes = pathfinding.astar( start, heuristic if useheuristic else pathfinding.default_heuristic, goal, allPossibleActions, domain[0], useheuristic) #returns priorityNode.edgesPassed, priorityNode.currentPathCost, numFrontier, expandedNodes return edgesPassed, len(edgesPassed), numFront, numExpandedNodes
def run_test_planp(world_spec, plan, expression_spec, result, sets={}): global run, passed run += 1 try: world = make_world(world_spec, sets) i = 1 for p in plan: run += 1 (pre, eff, subst) = p preexp = make_expression(sub(pre, subst)) effexp = make_expression(sub(eff, subst)) printNAryTree(effexp.getRoot()) if not models(world, preexp): print("precondition failed at step", i, preexp) return False world = apply(world, effexp) passed += 1 i += 1 exp = make_expression(expression_spec) res = models(world, exp) if res == result: passed += 1 return True else: print('Not pass planp') print('world') print(world_spec) print('plan') print(plan) print('exp') print(expression_spec) print('result') print(result) print('set') print(sets) except Exception: print('error 5') traceback.print_exc() return False
def computeRPG(actions, start, isgoal): fluents = [] actionsApplied = [] executedActionEffects = {} #initialize fluents.append(copy.deepcopy(start)) actionsApplied.append([]) t = 0 while not isgoal(fluents[t]): # while goal is not in the layer t += 1 actionsApplied.append([]) for rac in groundedActions: # find the actions that can be applied if expressions.models( fluents[t - 1].state, expressions.make_expression(rac.precondition)): actionsApplied[t].append(rac) fluents.append(copy.deepcopy( fluents[t - 1])) # copy the fluents of last layer for act in actionsApplied[t]: #apply the actions to this layer actionEffects = expressions.apply( fluents[t].state, expressions.make_expression(act.effect, True)) aef = [] for atm in actionEffects.formulas: if atm not in fluents[t].state.formulas: aef.append(atm) fluents[t].state.formulas.append(atm) if len(aef) > 0: executedActionEffects[str(t) + act.name] = aef if t > 0 and fluents[t].state == fluents[ t - 1].state: # if the new state is no different than the last one, fail return None, None, None return fluents, actionsApplied, executedActionEffects
def run_test_simple(world_spec, expression_spec, result, sets={}): global run, passed run += 1 try: world = make_world(world_spec, sets) exp = make_expression(expression_spec) res = models(world, exp) if res == result: passed += 1 return True except Exception: traceback.print_exc() return False
def run_test_apply(world_spec, action_spec, expression_spec, result, sets={}): global run, passed run += 1 try: world = make_world(world_spec, sets) action = make_expression(action_spec) world1 = apply(world, action) exp = make_expression(expression_spec) res = models(world1, exp) if res == result: passed += 1 return True else: print('Not pass apply') print(world_spec) print(action_spec) print(expression_spec) print(sets) print(result) except Exception: print('error 2') traceback.print_exc() return False
def expand_action(action, substitutions_per_action): """ Expands each action as many times as parameters it has to process for it """ expressions_to_expand = [] # create and initial WHEN expression to expand and wrap it in ExpandedExpression when_expression_list = ["when", action.precondition, action.effect] when_expression = expressions.make_expression(when_expression_list) expanded_expression = ExpandedExpression(action.name, when_expression) expressions_to_expand.append(expanded_expression) # expand each expression in expressions_to_expand as many times as parameters we have for the action for substitution_per_action in substitutions_per_action: expressions_to_expand = expand_expressions(substitution_per_action, expressions_to_expand) return expressions_to_expand
def plan(domain, problem, useheuristic=True): # get all objects applying the typinh hierarchy allobjs = mergeObjs(domain, problem) # get all grounded actions groundActions(domain.actions, allobjs) goalExp = expressions.make_expression(problem.goal) def isgoal(state): return expressions.models(state.state, goalExp) def heuristic(state, action): return SuperHeuristic(state, action, problem.init, problem.goal, allobjs, isgoal) # pathfinding.default_heuristic start = PlanNode("init", expressions.make_world(problem.init, allobjs), allobjs) return pathfinding.astar( start, heuristic if useheuristic else pathfinding.default_heuristic, isgoal)
def createNeighbors_PlanNode(self, allPossibleActions, actionsDictionary, useheuristic): allKeys = allPossibleActions.keys() actionsKeys = actionsDictionary.keys() #print('') #print(allPossibleActions) #print(actionsKeys) for key in allKeys: #print(key) #print(actionsDictionary[key][1]) #print("--------------------------") #print(actionsDictionary[key][2]) expTree = expressions.make_expression(actionsDictionary[key][1]) if len(allPossibleActions[key]) < 1: neighName = '' neighName = '' + key + '()' precondition = [] #print(neighName) if expressions.models(self.world, expTree): #print('models') tempworld = deepcopy(self.world) if len(actionsDictionary[key][2]) == 1: expEffect = actionsDictionary[key][2] expressions.applyToWorld(tempworld, expEffect, useheuristic) self.neighbors.append(Edge(PlanNode(self.initialStates, self.domainTypes, None, tempworld, neighName), 1, neighName)) else: expEffect = expressions.make_expression(actionsDictionary[key][2]) for tuple in precondition: expressions.substitute(expEffect, tuple[0], tuple[1]) expressions.applyToWorld(tempworld, expEffect, useheuristic) self.neighbors.append(Edge(PlanNode(self.initialStates, self.domainTypes, None, tempworld, neighName), 1, neighName)) #else: #print(self.world) #print('Does not models') else: for action in allPossibleActions[key]: #print(actionsDictionary[key][1]) precondition = [] neighName = '' neighName = '' + key + '(' first = True second = True allActionKeys = action.keys() for ak in allActionKeys: if first: neighName = neighName + action[ak] + ', ' precondition.append((ak,action[ak])) first = False elif second: neighName = neighName + action[ak] precondition.append((ak,action[ak])) second = False else: neighName = neighName+ ', ' + action[ak] precondition.append((ak,action[ak])) neighName = neighName + ')' tempcopy = deepcopy(expTree) for tuple in precondition: expressions.substitute(tempcopy, tuple[0], tuple[1]) #print(neighName) if expressions.models(self.world, tempcopy): #print('') #print(neighName) #print(precondition) #print(self.world) tempworld = deepcopy(self.world) #print('Applying: ') if len(actionsDictionary[key][2]) == 1: expEffect = actionsDictionary[key][2] #print(tempworld) expressions.applyToWorld(tempworld, expEffect, useheuristic) #print(tempworld) self.neighbors.append(Edge(PlanNode(self.initialStates, self.domainTypes, None, tempworld, neighName), 1, neighName)) else: #print(tempworld) expEffect = expressions.make_expression(actionsDictionary[key][2]) for tuple in precondition: expressions.substitute(expEffect, tuple[0], tuple[1]) expressions.applyToWorld(tempworld, expEffect, useheuristic) #print(tempworld) self.neighbors.append(Edge(PlanNode(self.initialStates, self.domainTypes, None, tempworld, neighName), 1, neighName))