Example #1
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)
Example #2
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()
    # They_signif = They_sign.add_significance()
    connector = type_signif.add_feature(approve_signif)
    Approve.add_out_significance(connector)

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

    approve_signif = Approve.add_significance()
    executer = approve_signif.add_execution(Approve.name.lower(), effect=True)
    Send.add_out_significance(executer)
Example #3
0
def ground(map_file, object_types, agent):
    """
    :param
    :return: Task
    """
    # map recognition
    region_map, cell_map, cell_location = map_recogn(map_file, object_types,
                                                     agent)

    obj_signifs = {}
    obj_means = {}
    signs = {}
    #I, They signs
    I_sign = Sign("I")
    They_sign = Sign("They")
    obj_signifs[I_sign] = I_sign.add_significance()
    signs[I_sign.name] = I_sign
    obj_signifs[They_sign] = They_sign.add_significance()
    signs[They_sign.name] = They_sign
    # create sign map and matrix
    Map_sign = Sign("Map")
    obj_signifs[Map_sign] = Map_sign.add_significance()
    signs[Map_sign.name] = Map_sign
    # create regions signs and matrixes
    Region_sign = Sign("Region")
    region_signif = Region_sign.add_significance()
    obj_signifs[Region_sign] = region_signif
    signs[Region_sign.name] = Region_sign
    regions = []
    for role in ('?X', '?Y', '?Z'):
        Region_signs = Sign('Region' + role)
        regions.append(Region_signs)
        role_signif = Region_signs.add_significance()
        obj_signifs[Region_signs] = role_signif
        signs[Region_signs.name] = Region_signs
        connector = role_signif.add_feature(region_signif, zero_out=True)
        Region_sign.add_out_significance(connector)

    for region in region_map:
        Regions_sign = Sign(region)
        obj_signifs[Regions_sign] = Regions_sign.add_significance()
        signs[Regions_sign.name] = Regions_sign
        Region_signif = Region_sign.add_significance()
        connector = Region_signif.add_feature(obj_signifs[Regions_sign],
                                              zero_out=True)
        Regions_sign.add_out_significance(connector)

    # create cell signs and matrixes
    Cell_sign = Sign("Cell")
    obj_signifs[Cell_sign] = Cell_sign.add_significance()
    signs[Cell_sign.name] = Cell_sign

    Cellx_sign = Sign("Cell?X")
    obj_signifs[Cellx_sign] = Cellx_sign.add_significance()
    signs[Cellx_sign.name] = Cellx_sign

    Celly_sign = Sign("Cell?Y")
    obj_signifs[Celly_sign] = Celly_sign.add_significance()
    signs[Celly_sign.name] = Celly_sign

    Celly_signif = Celly_sign.add_significance()
    con = Celly_signif.add_feature(obj_signifs[Cell_sign], zero_out=True)
    Cell_sign.add_out_significance(con)

    for cell, value in cell_map.items():
        Cells_sign = Sign(cell)
        obj_signifs[Cells_sign] = Cells_sign.add_significance()
        signs[Cells_sign.name] = Cells_sign
        Cell_signif = Cell_sign.add_significance()
        connector = Cell_signif.add_feature(obj_signifs[Cells_sign],
                                            zero_out=True)
        Cells_sign.add_out_significance(connector)
        if agent in value:
            Cellx_signif = Cellx_sign.add_significance()
            con = Cellx_signif.add_feature(obj_signifs[Cells_sign],
                                           zero_out=True)
            Cells_sign.add_out_significance(con)

    # create objects signs
    Object_sign = Sign('Object')
    obj_signifs[Object_sign] = Object_sign.add_significance()
    signs[Object_sign.name] = Object_sign
    obj_signs = []
    Block_sign = Sign('Block')
    obj_signs.append(Block_sign)
    Obstacle_sign = Sign('Obstacle')
    obj_signs.append(Obstacle_sign)
    Border_sign = Sign('Border')
    obj_signs.append(Border_sign)
    Nothing_sign = Sign('Nothing')
    obj_signs.append(Nothing_sign)
    Agent_sign = Sign('Agent')
    obj_signs.append(Agent_sign)
    Table_sign = Sign('Table')
    obj_signs.append(Table_sign)
    for s in obj_signs:
        obj_signifs[s] = s.add_significance()
        signs[s.name] = s
        Object_sign_signif = Object_sign.add_significance()
        connector = Object_sign_signif.add_feature(obj_signifs[s],
                                                   zero_out=True)
        s.add_out_significance(connector)

    dirs = []
    Direction_sign = Sign('Direction')
    dirs.append(Direction_sign)
    obj_signifs[Direction_sign] = Direction_sign.add_significance()
    signs[Direction_sign.name] = Direction_sign

    Direction_start = Sign('Dir-start')
    dirs.append(Direction_start)
    Dir_signif = Direction_start.add_significance()
    obj_signifs[Direction_start] = Dir_signif
    signs[Direction_start.name] = Direction_start
    connector = Dir_signif.add_feature(obj_signifs[Direction_sign],
                                       zero_out=True)
    Direction_sign.add_out_significance(connector)

    Direction_finish = Sign('Dir-finish')
    dirs.append(Direction_finish)
    Dir_signif = Direction_finish.add_significance()
    obj_signifs[Direction_finish] = Dir_signif
    signs[Direction_finish.name] = Direction_finish
    connector = Dir_signif.add_feature(obj_signifs[Direction_sign],
                                       zero_out=True)
    Direction_sign.add_out_significance(connector)

    directions = [
        'Above', 'Below', 'Left', 'Right', 'Above-left', 'Above-right',
        'Below-left', 'Below-right'
    ]

    for dir in directions:
        dir_sign = Sign(dir)
        dir_signif = dir_sign.add_significance()
        obj_signifs[dir_sign] = dir_signif
        signs[dir_sign.name] = dir_sign
        Direction_signif = Direction_sign.add_significance()
        connector = Direction_signif.add_feature(dir_signif, zero_out=True)
        dir_sign.add_out_significance(connector)

    distance = []
    # locate objects: include
    Include_sign = Sign('Include')
    distance.append(Include_sign)
    signs[Include_sign.name] = Include_sign
    obj_signifs[Include_sign] = Include_sign.add_significance()
    for obj in obj_signs:
        Include_signif = Include_sign.add_significance()
        connector = Include_signif.add_feature(obj_signifs[Region_sign],
                                               effect=False,
                                               zero_out=True)
        Region_sign.add_out_significance(connector)
        connector = Include_signif.add_feature(obj_signifs[obj],
                                               effect=True,
                                               zero_out=True)
        obj.add_out_significance(connector)
    #TODO test random cell generation place
    for region, cells in cell_location.items():
        region_sign = signs[region]
        for cell in cells:
            Include_signif = Include_sign.add_significance()
            connector = Include_signif.add_feature(obj_signifs[region_sign],
                                                   effect=False,
                                                   zero_out=True)
            region_sign.add_out_significance(connector)
            cell_sign = signs[cell]
            connector = Include_signif.add_feature(obj_signifs[cell_sign],
                                                   effect=True,
                                                   zero_out=True)
            cell_sign.add_out_significance(connector)
    #include cellX cellY
    Include_X_signif = Include_sign.add_significance()
    connector = Include_X_signif.add_feature(obj_signifs[Region_sign],
                                             effect=False,
                                             zero_out=True)
    Region_sign.add_out_significance(connector)
    connector = Include_X_signif.add_feature(obj_signifs[Cellx_sign],
                                             effect=True,
                                             zero_out=True)
    Cellx_sign.add_out_significance(connector)
    Include_Y_signif = Include_sign.add_significance()
    connector = Include_Y_signif.add_feature(obj_signifs[Region_sign],
                                             effect=False,
                                             zero_out=True)
    Region_sign.add_out_significance(connector)
    connector = Include_Y_signif.add_feature(obj_signifs[Celly_sign],
                                             effect=True,
                                             zero_out=True)
    Celly_sign.add_out_significance(connector)
    # locate objects: contain
    ag_cell = []
    Contain_sign = Sign('Contain')
    contain_signif = Contain_sign.add_significance()
    obj_signifs[Contain_sign] = contain_signif
    for cell, objs in cell_map.items():
        cell_sign = signs[cell]
        if len(objs) > 1 and 0 in objs:
            for obj in objs:
                if obj != 0:
                    contain_signif = Contain_sign.add_significance()
                    connector = contain_signif.add_feature(
                        obj_signifs[cell_sign], effect=False, zero_out=True)
                    cell_sign.add_out_significance(connector)
                    if obj in signs:
                        obj_sign = signs[obj]
                    else:
                        obj_sign = Sign(obj)
                        if 'agent' in obj:
                            ag_cell.append(cell)
                        signs[obj_sign.name] = obj_sign
                        obj_signifs[obj_sign] = obj_sign.add_significance()
                        obj_role = obj.title()[:-1]
                        role_names = [s.name for s in obj_signs]
                        if obj_role in role_names:
                            obj_role_sign = signs[obj_role]
                            orss = obj_role_sign.add_significance()
                            con = orss.add_feature(obj_signifs[obj_sign],
                                                   zero_out=True)
                            obj_sign.add_out_significance(con)
                    connector = contain_signif.add_feature(
                        obj_signifs[obj_sign], effect=True, zero_out=True)
                    obj_sign.add_out_significance(connector)

        else:
            contain_signif = Contain_sign.add_significance()
            connector = contain_signif.add_feature(obj_signifs[cell_sign],
                                                   effect=False,
                                                   zero_out=True)
            cell_sign.add_out_significance(connector)
            connector = contain_signif.add_feature(obj_signifs[Nothing_sign],
                                                   effect=True,
                                                   zero_out=True)
            Nothing_sign.add_out_significance(connector)

    # closely
    Closely_sign = Sign('Closely')
    distance.append(Closely_sign)
    signs[Closely_sign.name] = Closely_sign
    Closely_signif = Closely_sign.add_significance()
    obj_signifs[Closely_sign] = Closely_signif
    # locate cells: closely
    Closely_cells_signif = Closely_sign.add_significance()
    connector = Closely_cells_signif.add_feature(obj_signifs[Cellx_sign])
    Cellx_sign.add_out_significance(connector)
    connector = Closely_cells_signif.add_feature(obj_signifs[Celly_sign])
    Celly_sign.add_out_significance(connector)
    # locate regions: closely
    combinations = list(itertools.combinations(regions, 2))
    closely_regions = []
    for combination in combinations:
        Closely_signif = Closely_sign.add_significance()
        closely_regions.append(Closely_signif)
        connector = Closely_signif.add_feature(obj_signifs[combination[0]])
        combination[0].add_out_significance(connector)
        connector = Closely_signif.add_feature(obj_signifs[combination[1]])
        combination[1].add_out_significance(connector)
    # locate cell and regions: closely
    closely_regcells = []
    for reg in regions:
        Closely_signif = Closely_sign.add_significance()
        closely_regcells.append(Closely_signif)
        connector = Closely_signif.add_feature(obj_signifs[Cellx_sign])
        Cellx_sign.add_out_significance(connector)
        connector = Closely_signif.add_feature(obj_signifs[reg])
        reg.add_out_significance(connector)
    for reg in regions:
        Closely_signif = Closely_sign.add_significance()
        closely_regcells.append(Closely_signif)
        connector = Closely_signif.add_feature(obj_signifs[Celly_sign])
        Celly_sign.add_out_significance(connector)
        connector = Closely_signif.add_feature(obj_signifs[reg])
        reg.add_out_significance(connector)
    # nearly
    Nearly_sign = Sign('Nearly')
    distance.append(Nearly_sign)
    signs[Nearly_sign.name] = Nearly_sign
    Nearly_signif = Nearly_sign.add_significance()
    obj_signifs[Nearly_sign] = Nearly_signif
    # locate cell and regions: nearly
    cell_y_reg = [
        cm for cm in closely_regcells if Celly_sign in cm.get_signs()
    ]
    for cm in cell_y_reg:
        Nearly_signif = Nearly_sign.add_significance()
        con = Nearly_signif.add_feature(Closely_cells_signif)
        Closely_sign.add_out_significance(con)
        conn = Nearly_signif.add_feature(cm)
        Closely_sign.add_out_significance(conn)

    # locate regions: nearly
    Nearly_signif_reg = Nearly_sign.add_significance()
    Region_X_sign = signs['Region?X']
    Region_Y_sign = signs['Region?Y']
    Region_Z_sign = signs['Region?Z']
    closely_XZ = [
        cm for cm in closely_regions
        if Region_X_sign in cm.get_signs() and Region_Z_sign in cm.get_signs()
    ][0]
    closely_ZY = [
        cm for cm in closely_regions
        if Region_Z_sign in cm.get_signs() and Region_Y_sign in cm.get_signs()
    ][0]
    con = Nearly_signif_reg.add_feature(closely_XZ)
    Closely_sign.add_out_significance(con)
    conn = Nearly_signif_reg.add_feature(closely_ZY)
    Closely_sign.add_out_significance(conn)

    # faraway
    Faraway_sign = Sign('Faraway')
    distance.append(Faraway_sign)
    signs[Faraway_sign.name] = Faraway_sign
    obj_signifs[Faraway_sign] = Faraway_sign.add_significance()
    Faraway_signif = Faraway_sign.add_significance()
    con = Faraway_signif.add_feature(Nearly_signif_reg)
    Nearly_sign.add_out_significance(con)
    conn = Faraway_signif.add_feature(Include_X_signif)
    Nearly_sign.add_out_significance(conn)

    # distance
    Distance_sign = Sign('Distance')
    signs[Distance_sign.name] = Distance_sign
    Distance_signif = Distance_sign.add_significance()
    obj_signifs[Distance_sign] = Distance_signif
    for dist in distance:
        Distance_signif = Distance_sign.add_significance()
        connector = Distance_signif.add_feature(obj_signifs[dist])
        dist.add_out_significance(connector)
    # location
    Location_sign = Sign('Location')
    signs[Location_sign.name] = Location_sign
    obj_signifs[Location_sign] = Location_sign.add_significance()
    for dir in dirs:
        Location_signif = Location_sign.add_significance()
        conn = Location_signif.add_feature(obj_signifs[Distance_sign],
                                           effect=False)
        Distance_sign.add_out_significance(conn)
        con = Location_signif.add_feature(obj_signifs[dir], effect=True)
        dir.add_out_significance(con)