def run(self, cycle, verbose=2): state = self.mem.get(self.mem.STATE) if not state: states = self.mem.get(self.mem.STATES) if states: state = states[-1] else: if verbose >= 1: print("No world state loaded. Skipping planning.") return #now state is the most recent (or only) state and is non-null try: goals = self.mem.get(self.mem.CURRENT_GOALS)[-1] except: goals = [] if not goals: if verbose >= 2: print("No goals received by planner. Skipping planning.") return plan = self.get_old_plan(state, goals, verbose) if verbose > 2: if plan: print("Will not replan") else: print("Will replan") if plan: return if not plan: plan = self.get_new_modified_plan(state, goals, verbose) if not plan: return midcaPlan = plans.Plan(plan, goals) asynchPlan = asynch_3d.asynch_plan(self.mem, midcaPlan) if verbose >= 1: print("Planning complete.") if verbose >= 2: print("Plan: ", asynchPlan) #save new plan if asynchPlan != None: self.mem.get(self.mem.GOAL_GRAPH).addPlan(asynchPlan)
def run(self, cycle, verbose=2): state = self.mem.get(self.mem.STATE) if not state: states = self.mem.get(self.mem.STATES) if states: state = states[-1] else: if verbose >= 1: print "No world state loaded. Skipping planning." return #now state is the most recent (or only) state and is non-null try: goals = self.mem.get(self.mem.CURRENT_GOALS)[-1] except: goals = [] if not goals: if verbose >= 2: print "No goals received by planner. Skipping planning." return plan = self.get_old_plan(state, goals, verbose) if verbose >= 2: if plan: print "Will not replan" else: print "Planning from scratch" if not plan: plan = self.get_new_plan(state, goals, verbose) if not plan and plan != []: return #convert to MIDCA plan format plan = plans.Plan( [plans.Action(action[0], *action[1:]) for action in plan], goals) if verbose >= 1: print "Planning complete." if verbose >= 2: print "Plan: ", plan #save new plan if plan != None: self.mem.get(self.mem.GOAL_GRAPH).addPlan(plan)
def run(self, cycle, verbose=2): verbose = self.verbose state = self.mem.get(self.mem.STATE) if not state: states = self.mem.get(self.mem.STATES) if states: state = states[-1] else: if verbose >= 1: print 'No world state loaded. Skipping planning.' return goals = self.mem.get(self.mem.CURRENT_GOALS) if not goals: if verbose >= 2: print 'No goals received by planner. Skipping planning.' return else: plan = self.get_old_plan(state, goals, verbose) if verbose >= 2: if plan: print 'Will not replan' else: print 'Planning from scratch' if not plan: plan = self.get_new_plan(state, goals, verbose) if not plan and plan != []: return plan = plans.Plan([plans.Action(action[0], *action[1:]) for action in plan ], goals) if verbose >= 1: print 'Planning complete.' if verbose >= 2: print 'Plan: ', plan if plan is not None: self.mem.get(self.mem.GOAL_GRAPH).addPlan(plan) return
def run(self, cycle, verbose=2): world = self.world try: goals = self.mem.get(self.mem.CURRENT_GOALS)[-1] except: goals = [] trace = self.mem.trace if trace: trace.add_module(cycle, self.__class__.__name__) trace.add_data("WORLD", copy.deepcopy(world)) trace.add_data("GOALS", copy.deepcopy(goals)) if not goals: if verbose >= 2: print "No goals received by planner. Skipping planning." return try: midcaPlan = self.mem.get( self.mem.GOAL_GRAPH).getMatchingPlan(goals) except AttributeError: midcaPlan = None if midcaPlan: if verbose >= 2: print "Old plan retrieved. Checking validity...", valid = self.get_old_plan(goals) if not valid: midcaPlan = None #if plan modification is added to MIDCA, do it here. if verbose >= 2: print "invalid." elif verbose >= 2: print "valid." #ensure goals is a collection to simplify things later. if not isinstance(goals, collections.Iterable): goals = [goals] if not midcaPlan: #use jshop to generate new plan if verbose >= 2: print "Planning..." try: jshopState = self.jshop_state_from_world( world, self.state_file) except Exception: print "Could not generate a valid jshop state from current world state. Skipping planning" try: jshopTasks = self.jshop_tasks_from_goals( goals, jshopState, self.state_file) except Exception: print "Could not generate a valid jshop task from current goal set. Skipping planning" try: self.mem.set(self.mem.PLANNING_COUNT, 1 + self.mem.get(self.mem.PLANNING_COUNT)) jshopPlan = JSHOP.jshop(jshopTasks, self.domain_file, self.state_file) except Exception: jshopPlan = None if not jshopPlan and jshopPlan != []: if verbose >= 1: print "Planning failed for ", for goal in goals: print goal, " ", print if trace: trace.add_data("PLAN", jshopPlan) return #change from jshop plan to MIDCA plan midcaPlan = plans.Plan([ plans.Action(action[0], *list(action[1:])) for action in jshopPlan ], goals) asynchPlan = asynch_3d_test.asynch_plan(self.mem, midcaPlan) if verbose >= 1: print "Planning complete." if verbose >= 2: print "Plan: " #, midcaPlan for a in midcaPlan: print(" " + str(a)) #save new plan # save new plan if asynchPlan != None: self.mem.get(self.mem.GOAL_GRAPH).addPlan(asynchPlan) if trace: trace.add_data("PLAN", midcaPlan)
def run(self, cycle, verbose=2): world = self.mem.get(self.mem.STATES)[-1] try: goals = self.mem.get(self.mem.CURRENT_GOALS)[-1] except: goals = [] trace = self.mem.trace if trace: trace.add_module(cycle, self.__class__.__name__) trace.add_data("WORLD", copy.deepcopy(world)) trace.add_data("GOALS", copy.deepcopy(goals)) if not goals: if verbose >= 2: print("No goals received by planner. Skipping planning.") return try: midcaPlan = self.mem.get( self.mem.GOAL_GRAPH).getMatchingPlan(goals) except AttributeError: midcaPlan = None if midcaPlan: if verbose >= 2: print("Old plan retrieved. Checking validity...", end=' ') valid = world.plan_correct(midcaPlan) if not valid: midcaPlan = None #if plan modification is added to MIDCA, do it here. if verbose >= 2: print("invalid.") elif verbose >= 2: print("valid.") if valid: if verbose >= 2: print("checking to see if all goals are achieved...", end=' ') achieved = world.plan_goals_achieved(midcaPlan) if verbose >= 2: if len(achieved) == len(midcaPlan.goals): print("yes") else: print("no. Goals achieved: " + str({str(goal) for goal in achieved})) if len(achieved) != len(midcaPlan.goals): midcaPlan = None #triggers replanning. #ensure goals is a collection to simplify things later. if not isinstance(goals, collections.Iterable): goals = [goals] if not midcaPlan: #use pyhop to generate new plan if verbose >= 2: print("Planning...") # try: jshopState = self.jshop_state_from_world(world, self.state_file) # except Exception: # print "Could not generate a valid pyhop state from current world state. Skipping planning" # try: jshopTasks = self.jshop_tasks_from_goals(goals, jshopState, self.state_file) # except Exception: # print "Could not generate a valid pyhop task from current goal set. Skipping planning" try: self.mem.set(self.mem.PLANNING_COUNT, 1 + self.mem.get(self.mem.PLANNING_COUNT)) jshopPlan = JSHOP2.jshop(jshopTasks, self.domain_file, self.state_file) except Exception: jshopPlan = None if not jshopPlan and jshopPlan != []: if verbose >= 1: print("Planning failed for ", end=' ') for goal in goals: print(goal, " ", end=' ') print() if trace: trace.add_data("PLAN", jshopPlan) return #change from jshop plan to MIDCA plan midcaPlan = plans.Plan([ plans.Action(action[0], *list(action[1:])) for action in jshopPlan ], goals) if verbose >= 1: print("Planning complete.") if verbose >= 2: print("Plan: ") #, midcaPlan for a in midcaPlan: print((" " + str(a))) #save new plan if midcaPlan != None: self.mem.get(self.mem.GOAL_GRAPH).addPlan(midcaPlan) if trace: trace.add_data("PLAN", midcaPlan)
def run(self, cycle, verbose=2): self.verbose = verbose try: goals = self.mem.get(self.mem.CURRENT_GOALS)[-1] except: goals = [] midcaPlan = None if not goals: if verbose >= 2: print("No goals received by planner. Skipping planning.") return try: midcaPlan = self.mem.get( self.mem.GOAL_GRAPH).getMatchingPlan(goals) # check to see that midcaPlan has not been finished if midcaPlan.finished(): # remove from goals and trigger replanning self.mem.get(self.mem.GOALGRAPH).removePlan(midcaPlan) if self.verbose >= 1: print("Old plan finished, will re-plan") midcaPlan = None except AttributeError: midcaPlan = None if verbose >= 2: print("Did not retrieve plan, will plan from scratch") # ensure goals is a collection to simplify things later. if not isinstance(goals, collections.Iterable): goals = [goals] if midcaPlan: if self.verbose >= 1: print("Retrieved current plan. Skipping planning.") return if not midcaPlan: #use pyhop to generate new plan if verbose >= 2: print("Planning...") #try: self.mem.set(self.mem.PLANNING_COUNT, 1 + self.mem.get(self.mem.PLANNING_COUNT)) #print "Goals are "+str(map(str,goals)) hsp_plan = self.heuristic_search( goals, decompose=self.brute_force_decompose_nbeacons) if self.verbose >= 1: print("planning finished: ") for p in hsp_plan: print(" " + str(p.operator.name) + "(" + str([o.name for o in p.args]) + ")") midcaPlan = plans.Plan([ plans.Action(action.operator.name, * [o.name for o in action.args]) for action in hsp_plan ], goals) if verbose >= 2: print("Plan: ") #, midcaPlan for a in midcaPlan: print((" " + str(a))) if midcaPlan != None: self.mem.get(self.mem.GOAL_GRAPH).addPlan(midcaPlan)
def run(self, cycle, verbose=2): #world = self.mem.get(self.mem.STATES)[-1] world = self.mem.get(self.mem.STATE) goals = self.mem.get(self.mem.CURRENT_GOALS) trace = self.mem.trace if trace: trace.add_module(cycle, self.__class__.__name__) trace.add_data("WORLD", copy.deepcopy(world)) trace.add_data("GOALS", copy.deepcopy(goals)) trace.add_data("PLAN", None) if not goals: if verbose >= 2: print "No goals received by planner. Skipping planning." return try: midcaPlan = self.mem.get( self.mem.GOAL_GRAPH).getMatchingPlan(goals) except AttributeError: midcaPlan = None if midcaPlan: if verbose >= 2: print "Old plan retrieved. Checking validity...", valid = world.plan_correct(midcaPlan) if not valid: midcaPlan = None #if plan modification is added to MIDCA, do it here. if verbose >= 2: print "invalid." elif verbose >= 2: print "valid." if valid: if verbose >= 2: print "checking to see if all goals are achieved...", achieved = world.plan_goals_achieved(midcaPlan) if verbose >= 2: if len(achieved) == len(midcaPlan.goals): print "yes" else: print "no. Goals achieved: " + str( {str(goal) for goal in achieved}) if len(achieved) != len(midcaPlan.goals): midcaPlan = None #triggers replanning. #ensure goals is a collection to simplify things later. if not isinstance(goals, collections.Iterable): goals = [goals] if not midcaPlan: #use pyhop to generate new plan if verbose >= 2: print "Planning..." try: pyhopState = self.pyhop_state_from_world(world) except Exception: print "Could not generate a valid pyhop state from current world state. Skipping planning" try: pyhopTasks = self.pyhop_tasks_from_goals(goals, pyhopState) except Exception as e: print e print "Could not generate a valid pyhop task from current goal set. Skipping planning" try: pyhopPlan = pyhop.pyhop(pyhopState, pyhopTasks, verbose=0) except Exception: pyhopPlan = None if not pyhopPlan and pyhopPlan != []: if verbose >= 1: print "Planning failed for ", for goal in goals: print goal, " ", print if trace: trace.add_data("PLAN", pyhopPlan) return #change from pyhop plan to MIDCA plan midcaPlan = plans.Plan([ plans.Action(action[0], *list(action[1:])) for action in pyhopPlan ], goals) if verbose >= 1: print "Planning complete." if verbose >= 2: print "Plan: ", midcaPlan #save new plan if midcaPlan != None: self.mem.get(self.mem.GOAL_GRAPH).addPlan(midcaPlan) if trace: trace.add_data("PLAN", midcaPlan)