Example #1
0
 def update_single(facts):
     for fact in facts:
         role_name = fact[1][0].name + fact[0]
         fact_name = fact[1][0].name
         if role_name not in signs:
             role_sign = Sign(role_name)
             signs[role_name] = role_sign
         else:
             role_sign = signs[role_name]
         if fact_name not in signs:
             fact_sign = Sign(fact_name)
             signs[fact_name] = fact_sign
         else:
             fact_sign = signs[fact_name]
         role_signif = role_sign.add_significance()
         fact_signif = fact_sign.add_significance()
         connector = role_signif.add_feature(fact_signif, zero_out=True)
         fact_sign.add_out_significance(connector)
Example #2
0
def ground(problem, plagent, exp_signs=None):
    # ground I and They
    domain = problem.domain
    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

    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()

    others = set()
    for id in range(1, len(signs['agent'].significances) + 1):
        other_ag = signs['agent'].significances[id].get_signs()
        if signs[plagent] not in other_ag:
            others |= other_ag

    for subagent in others:
        if subagent.name != plagent:
            if not They_sign in subagent.significances[1].get_signs():
                signif = obj_signifs[They_sign]
                if signif.is_empty():
                    They_signif = signif
                else:
                    They_signif = They_sign.add_significance()
                connector = subagent.significances[1].add_feature(
                    They_signif, zero_out=True)
                They_sign.add_out_significance(connector)
                obj_means[subagent] = subagent.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, problem.constraints, 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')

    # 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], domain)
            connector = htn_mean.add_feature(cm)
            cm.sign.add_out_meaning(connector)
        subtasks.append(htn_sign)

    from mapmulti.grounding.pddl_grounding import signify_connection
    signify_connection(signs)

    return Task(problem.name, signs, start, goal, subtasks)
Example #3
0
def define_map(map_name, region_map, cell_location, near_loc, regions_struct,
               signs):
    signs[map_name] = Sign(map_name)
    map_image = signs[map_name].add_image()
    elements = {}
    contain_sign = signs['contain']
    region_sign = signs['region']
    few_sign = signs['few']
    noth_sign = signs['nothing']
    location_signif = \
    [matr for _, matr in signs['location'].significances.items() if signs['direction'] in matr.get_signs()][0]
    contain_reg = [
        region for region, cells in cell_location.items() if 'cell-4' in cells
    ][0]

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

    for region, objects in region_map.items():
        region_x = signs[region]
        flag = False
        if 0 in objects:
            flag = True
            objects.remove(0)
            objects.add("nothing")
            cont_signif = [
                signif for _, signif in contain_sign.significances.items()
                if signs['region'] in signif.get_signs()
                and noth_sign in signif.get_signs()
            ][0]
        else:
            cont_signif = [
                signif for _, signif in contain_sign.significances.items()
                if signs['region'] in signif.get_signs()
                and signs['object'] in signif.get_signs()
            ][0]
        connectors = []
        for object in objects:
            if object in signs:
                ob_sign = signs[object]
            else:
                ob_sign = Sign(object)
                signs[object] = ob_sign
                for s_name, sign in signs.items():
                    if s_name in object and s_name != object:
                        obj_signif = ob_sign.add_significance()
                        tp_signif = sign.add_significance()
                        connector = tp_signif.add_feature(obj_signif,
                                                          zero_out=True)
                        ob_sign.add_out_significance(connector)
                        break

            ob_image = get_or_add(ob_sign)
            pm = cont_signif.copy('significance', 'image')

            region_image = region_x.add_image()
            pm.replace('image', region_sign, region_image)
            pm.replace('image', signs['object'], ob_image)
            if not flag:
                few_image = few_sign.add_image()
                pm.replace('image', signs['amount'], few_image)

            if connectors:
                con = connectors[0]
                connector = map_image.add_feature(pm, con.in_order)
            else:
                connector = map_image.add_feature(pm)
            contain_sign.add_out_image(connector)
            if not connectors:
                connectors.append(connector)

        loc_sign = get_reg_location(cell_location, near_loc, region, signs)
        connector = connectors[0]
        am = None
        for id, signif in loc_sign.significances.items():
            if resonated(signif, regions_struct, region, contain_reg, signs):
                am = signif.copy('significance', 'image')
                break
        if not am:
            print('Did not find applicable map')
            sys.exit(1)
        cell_image = signs["cell-4"].add_image()
        am.replace('image', signs["cell?x"], cell_image)
        inner_matrices = am.spread_down_activity('image', 3)
        for lmatrice in inner_matrices:
            if lmatrice[-1].sign.name == "region?z":
                reg_image = signs[region].add_image()
                am.replace('image', signs["region?z"], reg_image)
                break
        else:
            for lmatrice in inner_matrices:
                if lmatrice[-1].sign.name == "region?y":
                    reg_image = signs[region].add_image()
                    am.replace('image', signs["region?y"], reg_image)
        reg_image = signs[contain_reg].add_image()
        am.replace('image', signs["region?x"], reg_image)

        # direction matrice
        if contain_reg != region:
            dir_sign = signs[regions_struct[contain_reg][region][1]]
        else:
            dir_sign = signs["inside"]
        dir_matr = dir_sign.add_image()

        # location matrice
        location_am = location_signif.copy('significance', 'image')
        location_am.replace('image', signs['distance'], am)
        location_am.replace('image', signs['direction'], dir_matr)
        con = map_image.add_feature(location_am, connector.in_order)
        loc_sign.add_out_image(con)

    return map_image
Example #4
0
def signify_connection(signs):
    Send = Sign("Send")
    send_signif = Send.add_significance()
    Broadcast = Sign("Broadcast")
    brdct_signif = Broadcast.add_significance()
    connector = brdct_signif.add_feature(send_signif)
    Send.add_out_significance(connector)
    Approve = Sign("Approve")
    approve_signif = Approve.add_significance()
    connector = approve_signif.add_feature(send_signif)
    Send.add_out_significance(connector)
    signs[Send.name] = Send
    signs[Broadcast.name] = Broadcast
    signs[Approve.name] = Approve

    They_sign = signs["They"]
    agents = They_sign.spread_up_activity_obj("significance", 1)
    agents_type = []
    for agent in agents:
        agents_type.append({
            cm.sign
            for cm in agent.sign.spread_up_activity_obj("significance", 1)
        })
    types = []
    if agents_type:
        types = [
            t for t in reduce(lambda x, y: x & y, agents_type)
            if t != signs["object"]
        ]
    if types and len(agents):
        type = types[0]
    else:
        type = signs["I"]

    They_signif = They_sign.add_significance()
    brdct_signif = Broadcast.add_significance()
    connector = They_signif.add_feature(brdct_signif)
    Broadcast.add_out_significance(connector)
    type_signif = type.add_significance()
    approve_signif = Approve.add_significance()

    connector = type_signif.add_feature(approve_signif)
    Approve.add_out_significance(connector)

    brdct_signif = Broadcast.add_significance()
    executer = brdct_signif.add_feature(Broadcast.name.lower(),
                                        effect=True,
                                        actuator=True)
    Send.add_out_significance(executer)

    approve_signif = Approve.add_significance()
    executer = approve_signif.add_feature(Approve.name.lower(),
                                          effect=True,
                                          actuator=True)
    Send.add_out_significance(executer)
Example #5
0
def signify_connection(signs):
    Send = Sign("Send")
    send_signif = Send.add_significance()
    Broadcast = Sign("Broadcast")
    brdct_signif = Broadcast.add_significance()
    connector = brdct_signif.add_feature(send_signif)
    Send.add_out_significance(connector)
    Approve = Sign("Approve")
    approve_signif = Approve.add_significance()
    connector = approve_signif.add_feature(send_signif)
    Send.add_out_significance(connector)
    signs[Send.name] = Send
    signs[Broadcast.name] = Broadcast
    signs[Approve.name] = Approve

    They_sign = signs["They"]

    They_signif = They_sign.add_significance()
    brdct_signif = Broadcast.add_significance()
    connector = They_signif.add_feature(brdct_signif)
    Broadcast.add_out_significance(connector)

    approve_signif = Approve.add_significance()
    They_signif = They_sign.add_significance()
    connector = They_signif.add_feature(approve_signif)
    Approve.add_out_significance(connector)

    brdct_signif = Broadcast.add_significance()
    executer = brdct_signif.add_feature(Broadcast.name.lower(),
                                        effect=True,
                                        actuator=True)
    Send.add_out_significance(executer)

    approve_signif = Approve.add_significance()
    executer = approve_signif.add_feature(Approve.name.lower(),
                                          effect=True,
                                          actuator=True)
    Send.add_out_significance(executer)
Example #6
0
def ground(problem, plagent, 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 = {}
    # add agents, because on significances they have a inner link to I and They signs
    obj_means = {}

    # Check logic in exp
    if exp_signs:
        signs = exp_signs
        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]
        signs['situation'] = exp_signs['situation']
    else:
        signs = {}
        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
        signs['situation'] = Sign('situation')

    for obj in objects:
        obj_sign = Sign(obj)
        obj_signifs[obj_sign] = obj_sign.add_significance()
        signs[obj] = obj_sign
        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 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[signs[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

    others = set()
    for id in range(1, len(signs['agent'].significances) + 1):
        other_ag = signs['agent'].significances[id].get_signs()
        if signs[plagent] not in other_ag:
            others |= other_ag

    for subagent in others:
        if subagent.name != plagent:
            if not They_sign in subagent.significances[1].get_signs():
                signif = obj_signifs[They_sign]
                if signif.is_empty():
                    They_signif = signif
                else:
                    They_signif = They_sign.add_significance()
                connector = subagent.significances[1].add_feature(
                    They_signif, zero_out=True)
                They_sign.add_out_significance(connector)
                obj_means[subagent] = subagent.add_meaning()

    if not exp_signs:
        updated_predicates = _update_predicates(predicates, actions)
        signify_predicates(predicates, updated_predicates, signs, subtype_map,
                           domain.constants)
        signify_actions(actions, constraints, signs, plagent, obj_means,
                        obj_signifs)
        signify_connection(signs)

    start_situation, pms = _define_situation('*start*', problem.initial_state,
                                             signs, 'image')
    goal_situation, pms = _define_situation('*finish*', problem.goal, signs,
                                            'image')
    # if problem.name.startswith("blocks"):
    #     list_signs = task_signs(problem)
    #     _expand_situation_ma_blocks(goal_situation, signs, pms, list_signs)  # For task
    return MaPlanningTask(problem.name, signs, start_situation, goal_situation)
Example #7
0
def spatial_ground(problem, plagent, agents, exp_signs=None, backward = False):
    global signs
    initial_state = problem.initial_state
    initial_state.update(problem.map)
    goal_state = problem.goal_state
    goal_state.update(problem.map)

    # Prepare states to plagent
    init_state = {key: value for key, value in deepcopy(initial_state).items() if key != plagent}
    init_state['I'] = initial_state[plagent]
    init_state['objects']['I'] = init_state['objects'].pop(plagent)
    go_state = {key: value for key, value in deepcopy(goal_state).items() if key != plagent}
    go_state['I'] = goal_state[plagent]
    go_state['objects']['I'] = go_state['objects'].pop(plagent)
    agents.remove(plagent)
    agents.append('I')

    obj_signifs = {}
    obj_means = {}
    # Create agents and communications
    agent_type = None

    types = problem.domain['types']
    roles = problem.domain['roles']

    if exp_signs:
        signs, types = ut._clear_model(exp_signs, signs, types)
        signs, roles = ut._clear_model(exp_signs, signs, roles)
        I_sign = exp_signs['I']
        signs['I'] = I_sign
        They_sign = exp_signs['They']
        signs['They'] = They_sign
        Clarify = exp_signs['Clarify']
        signs['Clarify'] = Clarify
        Abstract = exp_signs['Abstract']
        signs['Abstract'] = Abstract
        signs['situation'] = exp_signs['situation']
    else:
        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
        Clarify = Sign('Clarify')
        signs[Clarify.name] = Clarify
        Abstract = Sign('Abstract')
        signs[Abstract.name] = Abstract
        signs['situation'] = Sign('situation')

    # ground types
    for type_name, smaller in types.items():
        if not type_name in signs and not exp_signs:
            type_sign = Sign(type_name)
            signs[type_name] = type_sign
        else:
            if exp_signs:
                type_sign = exp_signs[type_name]
            else:
                type_sign = signs[type_name]
        if smaller:
            for obj in smaller:
                if not obj in signs:
                    obj_sign = Sign(obj)
                    signs[obj] = obj_sign
                else:
                    obj_sign = signs[obj]

                obj_signif = obj_sign.add_significance()
                obj_signifs[obj_sign] = obj_signif
                tp_signif = type_sign.add_significance()
                connector = tp_signif.add_feature(obj_signif, zero_out=True)
                obj_sign.add_out_significance(connector)
                # Assign I to agent
                if obj_sign.name == plagent:
                    connector = obj_signif.add_feature(obj_signifs[I_sign], zero_out=True)
                    I_sign.add_out_significance(connector)
                    agent_type = type_name
        else:
            obj_signifs[type_sign] = type_sign.add_significance()

    # Assign other agents
    others = {signs[ag] for ag in agents if ag != 'I'}

    for subagent in others:
        if not They_sign in subagent.significances[1].get_signs():
            signif = obj_signifs[They_sign]
            if signif.is_empty():
                They_signif = signif
            else:
                They_signif = They_sign.add_significance()
            connector = subagent.significances[1].add_feature(They_signif, zero_out=True)
            They_sign.add_out_significance(connector)
            obj_means[subagent] = subagent.add_meaning()
    # Signify roles
    for role_name, smaller in roles.items():
        role_sign = Sign(role_name)
        signs[role_name] = role_sign

        for object in smaller:
            obj_sign = signs[object]
            obj_signif = obj_sign.significances[1]
            role_signif = role_sign.add_significance()
            connector = role_signif.add_feature(obj_signif, zero_out=True)
            obj_sign.add_out_significance(connector)
            if object == agent_type:
                agent_type = role_name
    # Find and signify walls
    if exp_signs:
        if not 'wall' in signs:
            signs['wall'] = exp_signs['wall']
    ws = signs['wall']
    views = []
    if ws.images:
        for num, im in ws.images.items():
            if len(im.cause):
                for view in im.cause[0].coincidences:
                    if view.view:
                        views.append(view.view)
    if 'wall' in problem.map:
        for wall in problem.map['wall']:
            if wall not in views:
                cimage = ws.add_image()
                cimage.add_feature(wall, effect=False, view = True)
    else:
        logging.warning('There are no walls around! Check your task!!!')
        sys.exit(1)
    # Ground predicates and actions
    if not exp_signs:
        ut._ground_predicates(problem.domain['predicates'], signs)
        ut._ground_actions(problem.domain['actions'], obj_means, problem.constraints, signs, agents, agent_type)
    else:
        #TODO check if new action or predicate variation appeared in new task
        for pred in problem.domain['predicates'].copy():
            if pred in exp_signs:
                signs[pred] = exp_signs[pred]
                problem.domain['predicates'].pop(pred)
        for act in problem.domain['actions'].copy():
            if act in exp_signs:
                signs[act] = exp_signs[act]
                problem.domain['actions'].pop(act)
        ut._ground_predicates(problem.domain['predicates'], signs)
        ut._ground_actions(problem.domain['actions'], obj_means, problem.constraints, signs, agents, agent_type)
        #Copy all experience subplans and plans
        for sname, sign in exp_signs.items():
            if sname not in signs:
                signs[sname] = sign

    # Define start situation
    maps = {}
    if backward:
        maps[0] = problem.goal_state
    else:
        maps[0] = problem.initial_state
    ms = maps[0].pop('map-size')
    walls = maps[0].pop('wall')
    static_map = {'map-size': ms, 'wall': walls}

    # Create maps and situations for planning agents
    regions_struct = ut.get_struct()
    additions = []
    additions.extend([maps, regions_struct, static_map])
    cells = {}
    agent_state = {}

    for agent in agents:
        region_map, cell_map_start, cell_location, near_loc, cell_coords, size, cl_lv_init = ut.signs_markup(init_state, static_map,
                                                                                               agent)
        agent_state_start = ut.state_prediction(signs[agent], init_state, signs)
        start_situation = ut.define_situation('*start-sit*-'+agent, cell_map_start, problem.initial_state['conditions'], agent_state_start, signs)
        start_map = ut.define_map('*start-map*-'+agent, region_map, cell_location, near_loc, regions_struct, signs)
        ut.state_fixation(start_situation, cell_coords, signs, 'cell')

        # Define goal situation
        region_map, cell_map_goal, cell_location, near_loc, cell_coords, size, cl_lv_goal = ut.signs_markup(go_state, static_map,
                                                                                               agent)
        agent_state_finish = ut.state_prediction(signs[agent], go_state, signs)
        goal_situation = ut.define_situation('*goal-sit*-'+agent, cell_map_goal, problem.goal_state['conditions'], agent_state_finish, signs)
        goal_map = ut.define_map('*goal-map*-'+agent, region_map, cell_location, near_loc, regions_struct, signs)
        ut.state_fixation(goal_situation, cell_coords, signs, 'cell')

        #fixation map
        map_size = ut.scale(ms)
        rmap = [0, 0, map_size[0], map_size[1]]
        region_location, _ = ut.locater('region-', rmap, initial_state['objects'], walls)
        ut.state_fixation(start_map, region_location, signs, 'region')
        ut.signify_connection(signs)
        if agent == 'I':
            additions[0][0][plagent]['cl_lv_init'] = cl_lv_init
            additions[0][0][plagent]['cl_lv_goal'] = cl_lv_goal
        else:
            additions[0][0][agent]['cl_lv_init'] = cl_lv_init
            additions[0][0][agent]['cl_lv_goal'] = cl_lv_goal
        if backward:
            cell_map_goal['cell-4'] = {plagent}
            cells[agent] = {0:cell_map_goal}
        else:
            cell_map_start['cell-4'] = {plagent}
            cells[agent] = {0: cell_map_start}
        agent_state[agent] = {'start-sit':start_situation.sign, 'goal-sit': goal_situation.sign, 'start-map':start_map.sign,
                              'goal-map': goal_map.sign}

    additions.insert(2, cells)

    return SpTask(problem.name, signs, agent_state, additions, problem.initial_state,
                  {key:value for key, value in problem.goal_state.items() if key not in static_map}, static_map, plagent)
Example #8
0
def ground(problem, agent, exp_signs=None):
    domain = problem.domain
    actions = domain.actions.values()
    predicates = domain.predicates.values()

    # 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 = {}

    # Check logic in exp
    if exp_signs:
        signs = exp_signs
        I_sign = signs['I']
        obj_means[I_sign] = I_sign.meanings[1]
        obj_signifs[I_sign] = I_sign.significances[1]
    else:
        signs = {}
        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
        signs['situation'] = Sign('situation')

    for obj in objects:
        obj_sign = Sign(obj)
        obj_signifs[obj] = obj_sign.add_significance()
        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

    if not exp_signs:
        updated_predicates = _update_predicates(predicates, actions)
        signify_predicates(predicates, updated_predicates, signs, subtype_map,
                           domain.constants)
        agent_roles = {}
        pred_names = {pred.name for pred in predicates}
        if agent:
            agent_role_cms = signs[agent].spread_up_activity_obj(
                'significance', 3)
            roles = {
                cm.sign
                for cm in agent_role_cms if cm.sign.name not in pred_names
            }
            agent_roles[signs[agent]] = roles
        signify_actions(actions, signs, obj_means, agent_roles)

    start_situation, pms = _define_situation('*start*', problem.initial_state,
                                             signs, 'image')
    goal_situation, pms = _define_situation('*finish*', problem.goal, signs,
                                            'image')

    return Task(problem.name, signs, start_situation, goal_situation)
Example #9
0
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)