def save_plan(self, start, finish, actions, plan_name): # Creating plan action for further use if not plan_name: plan_name = 'plan_' + self.name if not start.sign.meanings: scm = start.copy('image', 'meaning') start.sign.add_meaning(scm) for con in start.sign.out_meanings.copy(): if con.in_sign.name == plan_name: start.sign.out_meanings.remove(con) if not finish.sign.meanings: fcm = finish.copy('image', 'meaning') finish.sign.add_meaning(fcm) for con in finish.sign.out_meanings.copy(): if con.in_sign.name == plan_name: finish.sign.out_meanings.remove(con) plan_sign = Sign(plan_name + self.name) plan_mean = plan_sign.add_meaning() connector = plan_mean.add_feature(start.sign.meanings[1]) start.sign.add_out_meaning(connector) conn = plan_mean.add_feature(finish.sign.meanings[1], effect=True) finish.sign.add_out_meaning(conn) self.signs[plan_sign.name] = plan_sign # Adding Sequence of actions to plan image plan_image = plan_sign.add_image() for act in actions: im = act.sign.add_image() connector = plan_image.add_feature(im) act.sign.add_out_image(connector) # Adding scenario vs partly concrete actions to the plan sign scenario = self.scenario_builder(start, finish, actions) plan_signif = plan_sign.add_significance() for act in scenario: connector = plan_signif.add_feature(act) act.sign.add_out_significance(connector) return [plan_sign, start.sign, finish.sign]
def save_plan(self, start, finish, actions, plan_name, subplans=None): # Creating plan action for further use if not plan_name: plan_name = 'plan_' + self.name if not start.sign.meanings: scm = start.copy('image', 'meaning') start.sign.add_meaning(scm) if not finish.sign.meanings: fcm = finish.copy('image', 'meaning') finish.sign.add_meaning(fcm) plan_sign = Sign(plan_name) plan_mean = plan_sign.add_meaning() connector = plan_mean.add_feature(start.sign.meanings[1]) start.sign.add_out_meaning(connector) conn = plan_mean.add_feature(finish.sign.meanings[1], effect=True) finish.sign.add_out_meaning(conn) self.signs[plan_sign.name] = plan_sign # Adding Sequence of actions to plan image plan_image = plan_sign.add_image() iter = -1 if not subplans: for act in actions: im = act[2].sign.add_image() connector = plan_image.add_feature(im) act[2].sign.add_out_image( connector ) # add connector to plan_sign threw images to out_image else: # ots = {subplans.index(it): range(it[1], it[2]+1) for it in subplans} # for act in actions: # iter += 1 # flag = False # for index, value in ots.items(): # if iter == value[0]: # if subplans[index][0].images: # im = subplans[index][0].images[1] # else: # im = subplans[index][0].add_image() # connector = plan_image.add_feature(im) # subplans[index][0].add_out_image(connector) # flag = True # break # elif iter in value: # flag = True # break # if flag: # continue # else: # im = act[2].sign.add_image() # connector = plan_image.add_feature(im) # act[2].sign.add_out_image(connector) for sub in subplans: if sub[2].sign.images: connector = plan_image.add_feature(sub[2].sign.images[1]) else: they_im = sub[2].sign.add_image() connector = plan_image.add_feature(they_im) sub[2].sign.add_out_image( connector ) # add connector to plan_sign threw images to out_image # Adding scenario vs partly concrete actions to the plan sign scenario = self.scenario_builder(start, finish, actions) plan_signif = plan_sign.add_significance() for act in scenario: connector = plan_signif.add_feature(act) act.sign.add_out_significance(connector) return [plan_sign, start.sign, finish.sign]
def create_subplan(subplan, agent, signs, plan_sit, plan_map, start=False): elem_acts_im = [] if agent == 'I': ag_sign = I_obj[0] whose = agent else: ag_sign = signs[agent] whose = 'They' act_st = subplan[0] if isinstance(act_st[-1], dict): descr1 = act_st[-1] else: descr1 = act_st[-1][0] # Remake situations coze they could be synthesized by other agent act_sit_start, act_map_start, size = create_situation( descr1, ag_sign, whose) if agent == 'I': I_sign = signs['I'] I_im = I_sign.add_image() act_sit_start.replace('image', ag_sign, I_im) plan_map.append(act_map_start.sign) act_fn = subplan[-1] if isinstance(act_fn[-1], dict): logging.debug('cant find goal sit for subplan') descr2 = act_fn[-1] else: descr2 = act_fn[-1][1] # Change goal sit size ag_x = descr1['objects'][ag_sign.name]['x'] ag_y = descr1['objects'][ag_sign.name]['y'] for el in subplan: if el[1] == 'Clarify': new_size = (size[2] - size[0]) // 6 size = [ ag_x - new_size, ag_y - new_size, ag_x + new_size, ag_y + new_size ] elif el[1] == 'Abstract': new_size = size[2] - size[0] size = [ size[0] - new_size, size[1] - new_size, size[2] + new_size, size[3] + new_size ] act_sit_finish, act_map_finish, _ = create_situation( descr2, ag_sign, whose, size) plan_map.append(act_map_finish.sign) if agent == 'I': I_sign = signs['I'] I_im = I_sign.add_image() act_sit_finish.replace('image', ag_sign, I_im) active_sit_start_mean = act_sit_start.copy('image', 'meaning') plan_sit.append(act_sit_start.sign) active_sit_finish_mean = act_sit_finish.copy('image', 'meaning') plan_sit.append(act_sit_finish.sign) global SUBPLAN_COUNTER SUBPLAN_COUNTER += 1 if agent != 'I': plan_sign = Sign(SUBPLAN_PREFIX + 'they_' + agent + '_' + str(SUBPLAN_COUNTER)) plan_mean = plan_sign.add_meaning() connector = plan_mean.add_feature(active_sit_start_mean) active_sit_start_mean.sign.add_out_meaning(connector) conn = plan_mean.add_feature(active_sit_finish_mean, effect=True) active_sit_finish_mean.sign.add_out_meaning(conn) signs[plan_sign.name] = plan_sign else: plan_sign = Sign(SUBPLAN_PREFIX + 'I_' + str(SUBPLAN_COUNTER)) plan_mean = plan_sign.add_meaning() connector = plan_mean.add_feature(active_sit_start_mean) active_sit_start_mean.sign.add_out_meaning(connector) conn = plan_mean.add_feature(active_sit_finish_mean, effect=True) active_sit_finish_mean.sign.add_out_meaning(conn) plan_image = plan_sign.add_image() for act in subplan: act_sign = self.signs[act[1]] im = act_sign.add_image() connector = plan_image.add_feature(im) act_sign.add_out_image(connector) elem_acts_im.append(im) signs[plan_sign.name] = plan_sign ## changing start and finish if start == True: self.start_situation = act_sit_start self.start_map = act_map_start self.goal_situation = act_sit_finish self.goal_map = act_map_finish return [ act_sit_start, plan_sign.name, plan_mean, act_st[3], (None, None), (act_map_start, None), (descr1, descr2) ], signs, plan_sit, plan_map, elem_acts_im
def ground(problem, plagent, exp_signs=None): domain = problem.domain # ground I and They I_sign = Sign("I") obj_means[I_sign] = I_sign.add_meaning() obj_signifs[I_sign] = I_sign.add_significance() signs[I_sign.name] = I_sign They_sign = Sign("They") obj_means[They_sign] = They_sign.add_meaning() obj_signifs[They_sign] = They_sign.add_significance() signs[They_sign.name] = They_sign situation = Sign('situation') signs['situation'] = situation for type, stype in domain['types']: stype_sign = __add_sign(stype) stype_signif = stype_sign.add_significance() type_sign = __add_sign(type) connector = stype_signif.add_feature(obj_signifs[type_sign], zero_out=True) type_sign.add_out_significance(connector) for obj, type in problem.objects: obj_sign = __add_sign(obj) obj_means[obj_sign] = obj_sign.add_meaning() type_sign = signs[type] tp_signif = type_sign.add_significance() connector = tp_signif.add_feature(obj_signifs[obj_sign], zero_out=True) obj_sign.add_out_significance(connector) if obj_sign.name == plagent: connector = obj_signifs[obj_sign].add_feature(obj_signifs[I_sign], zero_out=True) I_sign.add_out_significance(connector) obj_means[obj_sign] = obj_sign.add_meaning() for predicate in domain['predicates']: _ground_predicate(predicate.name, predicate.signature) for action in domain['actions']: _ground_action(action.name, action.parameters, action.preconditions, action.effect, plagent=plagent) for task in domain['tasks']: __add_sign(task.name, False) methods = sorted(domain['methods'], key=lambda method: len(method.subtasks)) for method in methods: __ground_method(method.parameters, method.subtasks, method.ordering, method.task, domain, 2) #Ground Init start = __add_sign('*start %s*' % str(problem.name), False) sit_im = start.add_image() for predicate in problem.init: pred_im = _ground_htn_predicate(predicate.name, predicate.signature, plagent) connector = sit_im.add_feature(pred_im) pred_im.sign.add_out_image(connector) sit_im.copy('image', 'meaning') global_cm = situation.add_image() connector = global_cm.add_feature(sit_im) getattr(start, 'add_out_image')(connector) #Ground htns to meanings goal = None subtasks = [] for htn in problem.htns: htn_name = 'htn_' + str(problem.htns.index(htn)) htn_sign = __add_sign(htn_name, False) htn_mean = htn_sign.add_meaning() for task in htn.ordering: subtask = htn.subtasks[task] cm = __ground_htn_subtask(subtask[0], subtask[1], problem) connector = htn_mean.add_feature(cm) cm.sign.add_out_meaning(connector) subtasks.append(htn_sign) return Task(problem.name, signs, start, goal, subtasks)