Beispiel #1
0
def _define_situation(name, predicates, signs):
    situation = Sign(name)
    sit_meaning = situation.add_meaning()
    elements = {}

    def get_or_add(sign):
        if sign not in elements:
            meaning = sign.add_meaning()
            elements[sign] = meaning
        return elements.get(sign)

    for predicate in predicates:
        pred_sign = signs[predicate.name]
        pred_meaning = pred_sign.add_meaning()
        connector = sit_meaning.add_feature(pred_meaning)
        pred_sign.add_out_meaning(connector)
        if len(predicate.signature) == 1:
            sig_sign = signs[predicate.signature[0][0]]
            sig_meaning = get_or_add(sig_sign)
            conn = sit_meaning.add_feature(sig_meaning, connector.in_order)
            sig_sign.add_out_meaning(conn)
        elif len(predicate.signature) > 1:
            for fact in predicate.signature:
                fact_sign = signs[fact[0]]
                fact_meaning = get_or_add(fact_sign)
                conn = pred_meaning.add_feature(fact_meaning)
                fact_sign.add_out_meaning(conn)

    return situation, elements
Beispiel #2
0
def _time_shift_forward(active_pm, script):
    next_pm = Sign(st.SIT_PREFIX + str(st.SIT_COUNTER))
    world_model[next_pm.name] = next_pm
    pm = next_pm.add_meaning()
    st.SIT_COUNTER += 1
    copied = {}
    for event in active_pm.cause:
        for es in script.cause:
            if event.resonate('meaning', es):
                break
        else:
            pm.add_event(event.copy(pm, 'meaning', 'meaning', copied))
    for event in script.effect:
        pm.add_event(event.copy(pm, 'meaning', 'meaning', copied))
    return pm
Beispiel #3
0
def _time_shift_backward(active_pm, script):
    next_pm = Sign(st.SIT_PREFIX + str(st.SIT_COUNTER))
    world_model[next_pm.name] = next_pm
    pm = next_pm.add_meaning()
    st.SIT_COUNTER += 1
    copied = {}
    for event in active_pm.cause:
        for es in script.effect:
            if event.resonate('meaning', es):
                break
        else:
            pm.add_event(event.copy(pm, 'meaning', 'meaning', copied))
    for event in script.cause:
        pm.add_event(event.copy(pm, 'meaning', 'meaning', copied))

    return pm
Beispiel #4
0
def _define_situation(name, predicates, signs, events):
    situation = Sign(name)
    sit_meaning = situation.add_meaning()
    elements = {}

    def get_or_add(sign):
        if sign not in elements:
            meaning = sign.add_meaning()
            elements[sign] = meaning
        return elements.get(sign)

    for predicate in predicates:
        pred_sign = signs[predicate.name]
        pred_meaning = pred_sign.add_meaning()
        connector = sit_meaning.add_feature(pred_meaning)
        pred_sign.add_out_meaning(connector)
        if len(predicate.signature) == 1:
            sig_sign = signs[predicate.signature[0][0]]
            sig_meaning = get_or_add(sig_sign)
            conn = sit_meaning.add_feature(sig_meaning, connector.in_order)
            sig_sign.add_out_meaning(conn)
        elif len(predicate.signature) > 1:
            pre_signs = set()
            for fact in predicate.signature:
                fact_signs = signs[fact[0]].find_attribute()
                for sign in fact_signs:
                    pre_signs.add(sign)
            if len(pre_signs) < len(predicate.signature):
                for fact in predicate.signature:
                    fact_sign = signs[fact[0]]
                    fact_meaning = get_or_add(fact_sign)
                    conn = pred_meaning.add_feature(fact_meaning)
                    fact_sign.add_out_meaning(conn)
            else:
                for fact in predicate.signature:
                    fact_sign = signs[fact[0]]
                    fact_meaning = get_or_add(fact_sign)
                    conn = sit_meaning.add_feature(fact_meaning,
                                                   connector.in_order)
                    fact_sign.add_out_meaning(conn)
                    con3 = pred_meaning.add_feature(fact_meaning)
                    fact_sign.add_out_meaning(con3)

    for event in events:
        sit_meaning.add_event(event)

    return situation, elements
Beispiel #5
0
    def closure_function(self):
        cell_map = self.cell_map
        #cell_coords = self.cell_coords
        #signs = self.signs
        dummy = []
        for key in cell_map:
            dummy.append(cell_map[key])
        dummy_set = set()

        for i in range(len(dummy)):
            dummy_set.update(dummy[i])

        items = list(
            dummy_set -
            {0, 'agent', 'border-1', 'border-2', 'border-3', 'border-4'})

        closure_sign = Sign('closure')
        closure_meaning = closure_sign.add_meaning()
        for i in items:
            new_event = self.direction_reas(i)
            self.add_to_closure(closure_sign, closure_meaning, new_event)

        return closure_sign
Beispiel #6
0
    def save_signs(self, plan):
        def __is_role(pm):
            chains = pm.spread_down_activity('meaning', 6)
            for chain in chains:
                if len(chain[-1].sign.significances[1].cause) != 0:
                    break
            else:
                return False
            return True

        I_obj = None
        logging.info('Plan preparation to save...')
        if plan:
            logging.info('\tCleaning SWM...')
            pms_sit = [pm[0] for pm in plan[:-1]]
            pms_act = [pm[2] for pm in plan[:-1]]
            deleted = []
            for name, s in self.signs.copy().items():
                signif = list(s.significances.items())
                if name.startswith(SIT_PREFIX):
                    for index, pm in s.meanings.copy().items():
                        if pm not in pms_sit:
                            s.remove_meaning(pm)  # delete all situations
                            deleted.append(pm)
                    self.signs.pop(name)
                elif len(signif):
                    if len(signif[0][1].cause) and len(
                            signif[0][1].effect
                    ):  #delete action's meanings that are not in plan
                        for index, pm in s.meanings.copy().items():
                            # if pm not in pms_act:
                            #     s.remove_meaning(pm)
                            if __is_role(
                                    pm):  # delete only fully signed actions
                                break
                            else:
                                if pm not in pms_act:
                                    s.remove_meaning(pm)

            They_signs = [
                con.in_sign for con in self.signs["They"].out_significances
            ]
            I_obj = [
                con.in_sign for con in self.signs["I"].out_significances
                if con.out_sign.name == "I"
            ]

            for agent in itertools.chain(They_signs, I_obj):
                for connector in list(agent.out_meanings.copy()):
                    pm = connector.in_sign.meanings[connector.in_index]
                    if pm not in pms_act:
                        if __is_role(pm):  # delete only fully signed actions
                            break
                        else:
                            agent.out_meanings.remove(connector)

            logging.info('\tSaving precedent...')
            self.start_situation.name += self.name
            self.goal_situation.name += self.name
            dm = self.start_situation.meanings[1].copy('meaning', 'image')
            self.start_situation.add_image(dm)
            dm = self.goal_situation.meanings[1].copy('meaning', 'image')
            self.goal_situation.add_image(dm)
            # in start and goal sit out_meanings insert connector to plan sign
            plan_sign = Sign(PLAN_PREFIX + self.name)
            plan_mean = plan_sign.add_meaning()
            connector = plan_mean.add_feature(self.start_situation.meanings[1])
            self.start_situation.add_out_meaning(connector)
            conn = plan_mean.add_feature(self.goal_situation.meanings[1],
                                         effect=True)
            self.goal_situation.add_out_meaning(conn)

            plan_image = plan_sign.add_image()

            for _, name, cm, agent, _ in plan[:-1]:
                im = cm.sign.add_image()
                #im = cm.copy('meaning', 'image')
                connector = plan_image.add_feature(im)
                cm.sign.add_out_image(
                    connector
                )  # add connector to plan_sign threw images to out_image

            self.signs[plan_sign.name] = plan_sign
            self.signs[self.start_situation.name] = self.start_situation
            self.signs[self.goal_situation.name] = self.goal_situation
        else:
            for name, sign in self.signs.copy().items():
                if name.startswith(SIT_PREFIX):
                    self.signs.pop(name)
                else:
                    sign.meanings = {}
                    sign.out_meanings = []
        if I_obj:
            I_obj = "_" + I_obj[0].name
        else:
            I_obj = 'I'
        file_name = DEFAULT_FILE_PREFIX + datetime.datetime.now().strftime(
            '%m_%d_%H_%M') + I_obj + DEFAULT_FILE_SUFFIX
        logging.info('Start saving to {0}'.format(file_name))
        logging.info('\tDumping SWM...')
        pickle.dump(self.signs, open(file_name, 'wb'))
        logging.info('\tDumping SWM finished')
        return file_name
Beispiel #7
0
def ground(problem, agent, subjects, logic, exp_signs=None):
    domain = problem.domain
    actions = domain.actions.values()
    predicates = domain.predicates.values()
    constraints = problem.constraints

    # Objects
    objects = problem.objects
    objects.update(domain.constants)
    logging.debug('Objects:\n%s' % objects)

    # Remove old type_map
    if exp_signs:
        objects = _update_exp_signs(exp_signs, objects)

    # Create a map from types to objects
    type_map = _create_type_map(objects)
    logging.debug("Type to object map:\n%s" % type_map)

    # Create type subtype map
    subtype_map = _create_subtype(domain.types)

    obj_signifs = {}
    obj_means = {}
    events = []

    # Sign world model
    if exp_signs:
        signs = exp_signs
        finish = exp_signs[[
            key for key in exp_signs.keys() if "finish" in key
        ][0]]
        finish_cm = finish.meanings.get(1)
        for event in finish_cm.cause:
            for connector in event.coincidences:
                if not connector.in_sign == finish:
                    events.append(event)
        I_sign = signs['I']
        They_sign = signs['They']
        obj_means[I_sign] = I_sign.meanings[1]
        obj_signifs[I_sign] = I_sign.significances[1]
        obj_means[They_sign] = They_sign.meanings[1]
        obj_signifs[They_sign] = They_sign.significances[1]
    else:
        signs = {}
        I_sign = Sign("I")
        They_sign = Sign("They")
        obj_means[I_sign] = I_sign.add_meaning()
        obj_signifs[I_sign] = I_sign.add_significance()
        signs[I_sign.name] = I_sign
        obj_means[They_sign] = They_sign.add_meaning()
        obj_signifs[They_sign] = They_sign.add_significance()
        signs[They_sign.name] = They_sign

    for obj in objects:
        obj_sign = Sign(obj)
        obj_signifs[obj] = obj_sign.add_significance()
        obj_means[obj] = obj_sign.add_meaning()
        signs[obj] = obj_sign
        if obj_sign.name == agent:
            connector = obj_signifs[obj].add_feature(obj_signifs[I_sign],
                                                     zero_out=True)
            I_sign.add_out_significance(connector)

    for tp, objects in type_map.items():
        if exp_signs:
            tp_sign = signs[tp.name]
        else:
            tp_sign = Sign(tp.name)
        for obj in objects:
            obj_signif = obj_signifs[obj]
            tp_signif = tp_sign.add_significance()
            connector = tp_signif.add_feature(obj_signif, zero_out=True)
            signs[obj].add_out_significance(connector)
        if not exp_signs:
            signs[tp.name] = tp_sign

    for sub in subjects:
        if sub != agent:
            if not They_sign in signs[sub].significances[1]:
                connector = signs[sub].significances[1].add_feature(
                    obj_signifs[They_sign], zero_out=True)
                They_sign.add_out_significance(connector)

    if not exp_signs:
        updated_predicates = _update_predicates(predicates, actions)
        signify_predicates(predicates, updated_predicates, signs, subtype_map)
        signify_actions(actions, constraints, signs, agent, events, obj_means)
        signify_connection(signs)

    start_situation, pms = _define_situation('*start*', problem.initial_state,
                                             signs, events)
    goal_situation, pms = _define_situation('*finish*', problem.goal, signs,
                                            events)
    if problem.name.startswith("blocks"):
        list_signs = task_signs(problem)
        _expand_situation_ma_blocks(goal_situation, signs, pms,
                                    list_signs)  # For task
    elif problem.name.startswith("logistics"):
        _expand_situation_ma_logistics(goal_situation, signs, pms)
    return Task(problem.name, signs, constraints, start_situation,
                goal_situation, logic, None, None, None)