Beispiel #1
0
 def pddl(self, costs, durative=False):
     if durative:
         return self.durative_pddl()
     parameters_pddl = ' '.join(param.typed_pddl()
                                for param in self.parameters)
     augmented_effect = self.effect
     if costs and (
             self.cost is not None
     ) and not self.cost_included:  # Can only have one cost effect (uses last effect parsed)
         augmented_effect = And(augmented_effect, Cost(self.cost))
     return OPERATOR_PDDL.format(self._pddl_name, self.name,
                                 parameters_pddl, self.condition.pddl(),
                                 augmented_effect.pddl())
 def __init__(self, oracle):
     params = (P('q1', CONF), P('q2', CONF))
     q1, q2 = params
     super(Move, self).__init__(
         self.__class__.__name__,
         params,
         [
             AtConfig(q1),
             #MoveCost(q1, q2),
             #Initialize(MoveCost(q1, q2)), # TODO - should I define this here?
             IsCollisionFree(q1, q2),
         ],
         [
             AtConfig(q2),
             Not(AtConfig(q1)),
             Cost(MoveCost(q1, q2)),
         ])
Beispiel #3
0
def get_stream_functions(universe):
    action_to_function = {}
    for action in universe.name_to_action.values():
        if get_cost_atoms(action):
            continue
        if any(atom.predicate in universe.stream_predicates
               for atom in action.condition.get_atoms()):
            # TODO - assert that the atom is used positively
            # TODO - alternatively, could make a stream action that has achieves the cost and plan with it
            action_to_function[action] = Function(
                FUNCTION_TEMPLATE % action.name,
                [param.type for param in action.parameters])
            universe.add_function(action_to_function[action])
            function = action_to_function[action](*action.parameters)
            action.effect = And(action.effect, Cost(function))
            action.cost_included = True
    return action_to_function
Beispiel #4
0
def compile_problem(estimator, task):
    # Data types
    CONF = Type()
    SURFACE = Type()  # Difference between fixed and movable objects
    ITEM = Type()
    POSE = Type()
    CLASS = Type()

    # Fluent predicates
    AtConf = Pred(CONF)
    HandEmpty = Pred()
    Holding = Pred(ITEM)
    AtPose = Pred(ITEM, POSE)
    Supported = Pred(POSE, SURFACE)  # Fluent
    Localized = Pred(OBJECT)
    Measured = Pred(OBJECT)

    # Static predicates
    IsKin = Pred(POSE, CONF)
    IsClass = Pred(OBJECT, CLASS)
    IsVisible = Pred(SURFACE, CONF)
    IsSupported = Pred(POSE, SURFACE)  # Static

    # Functions
    ScanRoom = Func(SURFACE)
    #ScanTable = Func(SURFACE, TYPE)
    ScanTable = Func(
        SURFACE, ITEM)  # TODO: could include more specific vantage point costs
    Distance = Func(CONF, CONF)

    # Derived
    On = Pred(ITEM, SURFACE)
    ComputableP = Pred(POSE)
    ComputableQ = Pred(CONF)

    # Free parameters
    Q1, Q2 = Param(CONF), Param(CONF)
    S1 = Param(SURFACE)
    B1, B2 = Param(ITEM), Param(ITEM)
    P1, P2 = Param(POSE), Param(POSE)

    rename_easy(locals())  # Trick to make debugging easier

    # TODO: could just do easier version of this that doesn't require localized to start

    actions = [
        Action(
            name='pick',
            parameters=[B1, P1, Q1],  # TODO: Visibility constraint
            condition=And(Localized(B1), AtPose(B1, P1), HandEmpty(),
                          AtConf(Q1), IsKin(P1, Q1)),
            effect=And(Holding(B1), Not(AtPose(B1, P1)), Not(HandEmpty()))),
        Action(name='place',
               parameters=[B1, P1, Q1],
               condition=And(Holding(B1), AtConf(Q1), IsKin(P1, Q1)),
               effect=And(AtPose(B1, P1), HandEmpty(), Not(Holding(B1)))),
        Action(name='move',
               parameters=[Q1, Q2],
               condition=And(AtConf(Q1), ComputableQ(Q2)),
               effect=And(AtConf(Q2), Not(AtConf(Q1)), Cost(Distance(Q1,
                                                                     Q2)))),
        Action(name='scan_room',
               parameters=[S1],
               condition=Not(Localized(S1)),
               effect=And(Localized(S1), Cost(ScanRoom(S1)))),
        # TODO: need to set later poses to be usable or not to constrain order
        Action(name='scan_table',
               parameters=[S1, B1, P1, Q1],
               condition=And(Localized(S1), AtConf(Q1), IsVisible(S1, Q1),
                             Not(Localized(B1))),
               effect=And(Localized(B1), Measured(P1), Supported(P1, S1),
                          Cost(ScanTable(S1, B1)))),
    ]

    axioms = [
        # TODO: axiom for on? Might need a stream that generates initial fluents for On
        # TODO: axiom that says that all fake values depending on a certain one now are usable
        # TODO: could use stream predicates as fluents (as long as it doesn't break anything...)
        Axiom(effect=On(B1, S1),
              condition=Exists([P1],
                               And(AtPose(B1, P1),
                                   Or(IsSupported(P1, S1), Supported(P1,
                                                                     S1))))),

        # TODO: compile automatically
        Axiom(effect=ComputableQ(Q1),
              condition=Or(Measured(Q1),
                           Exists([P1], And(IsKin(P1, Q1), ComputableP(P1))),
                           Exists([S1], And(IsVisible(S1, Q1),
                                            Localized(S1))))),
        Axiom(effect=ComputableP(P1),
              condition=Or(
                  Measured(P1),
                  Exists([S1], And(IsSupported(P1, S1), Localized(S1))))),
    ]

    #####

    surface_types = estimator.surface_octomaps.keys()
    item_types = estimator.object_octomaps.keys()
    names_from_type = defaultdict(list)
    known_poses = {}
    holding = None

    def add_type(cl):
        name = '{}{}'.format(cl, len(names_from_type[cl]))
        names_from_type[cl].append(name)
        return name

    # TODO: this is all very similar to the generic open world stuff
    if estimator.holding is not None:
        holding = add_type(estimator.holding)
    for cl, octomap in estimator.surface_octomaps.items(
    ):  # TODO: generic surface object
        for pose in octomap.get_occupied():
            known_poses[add_type(cl)] = pose
    for cl, octomap in estimator.object_octomaps.items():
        for pose in octomap.get_occupied():
            known_poses[add_type(cl)] = pose
    print dict(names_from_type), known_poses

    # Human tells you to move block -> at least one block
    # At least one block -> at least one surface
    # TODO: generate fake properties about these fake values?
    goal_objects, goal_surfaces = entities_from_task(task)
    for cl in surface_types:
        add_type(cl)
    #for cl in goal_surfaces:
    #  for i in xrange(len(names_from_type[cl]), goal_surfaces[cl]):
    #    add_type(cl)
    #for cl in item_types:
    for cl in goal_objects:
        for i in xrange(len(names_from_type[cl]), goal_objects[cl]):
            add_type(cl)

    #####

    initial_atoms = [
        AtConf(estimator.robot_conf),
        Measured(CONF(estimator.robot_conf))
    ]
    if holding is None:
        initial_atoms.append(HandEmpty())
    else:
        initial_atoms.append(Holding(holding))

    class_from_name = {
        name: ty
        for ty in names_from_type for name in names_from_type[ty]
    }
    for name, ty in class_from_name.iteritems():
        ENTITY = SURFACE if ty in surface_types else ITEM
        initial_atoms.append(IsClass(ENTITY(name), ty))
        if name in known_poses:
            initial_atoms.append(Localized(ENTITY(name)))
            if ENTITY == ITEM:
                pose = known_poses[name]
                initial_atoms += [AtPose(name, pose), Measured(POSE(pose))]
        else:
            if ENTITY == ITEM:
                pose = 'p_init_{}'.format(
                    name
                )  # The object should always be at this pose (we just can't do anything about it yet)
                initial_atoms += [AtPose(name, pose)]

    goal_literals = []
    if task.holding is None:
        goal_literals.append(HandEmpty())
    elif task.holding is not False:
        goal_literals.append(
            Exists([B1], And(Holding(B1), IsClass(B1, task.holding))))
    for obj, surface in task.object_surfaces:
        goal_literals.append(
            Exists([B1, S1],
                   And(On(B1, S1), IsClass(B1, obj), IsClass(S1, surface))))
    goal_formula = And(*goal_literals)

    ####################

    TOLERANCE = 0.1

    def is_visible(table, conf):
        x, y = conf
        pose = known_poses[table]
        #return (pose == x) and (y == 2)
        #return (pose == x) and (y == 2)
        return (pose == x) and (abs(y - 2) < TOLERANCE)

    def is_kinematic(pose, conf):
        x, y = conf
        #return (pose == x) and (y == 1)
        return (pose == x) and (abs(y - 1) < TOLERANCE)

    ####################

    def sample_visible(table):  # TODO: could generically do this with poses
        if table in known_poses:
            y = 2
            #y += round(uniform(-TOLERANCE, TOLERANCE), 3)
            conf = (known_poses[table], y)
            assert is_visible(table, conf)
        else:
            conf = 'q_vis_{}'.format(table)
        yield (conf, )

    def inverse_kinematics(pose):  # TODO: list stream that uses ending info
        # TODO: only do if localized as well?
        # TODO: is it helpful to have this even if the raw value is kind of wrong (to steer the search)
        if type(pose) != str:
            y = 1
            #y += round(uniform(-TOLERANCE, TOLERANCE), 3)
            conf = (pose, y)
            assert is_kinematic(pose, conf)
        else:
            conf = 'q_ik_{}'.format(pose)
        yield (conf, )

    def sample_table(table):
        if table in known_poses:
            pose = known_poses[table]
        else:
            pose = 'p_{}'.format(table)
        yield (pose, )

    ####################

    MAX_DISTANCE = 10

    # TODO: maybe I don't need to worry about normalizing. I can just pretend non-parametric again for planning
    def scan_surface_cost(
            surface, obj):  # TODO: what about multiple scans of the belief?
        fail_cost = 100
        surface_cl = class_from_name[surface]
        obj_cl = class_from_name[obj]
        prob = 1.0
        if obj_cl in estimator.object_prior:
            prob *= estimator.object_prior[obj_cl].get(surface_cl, 0)
        if surface in known_poses:
            prob *= estimator.object_octomaps[obj_cl].get_prob(
                known_poses[surface])
        else:
            prob *= 0.1  # Low chance if you don't even know the table exists
            # TODO: could even include the probability the table exists
        #return expected_cost(1, fail_cost, prob)
        return mdp_cost(1, fail_cost, prob)

    def scan_room_cost(surface):
        # TODO: try to prove some sort of bound on the cost to recover will suffice?
        fail_cost = 100
        cl = class_from_name[surface]
        occupied_poses = {
            known_poses[n]
            for n in names_from_type[cl] if n in known_poses
        }
        p_failure = 1.0
        for pose in estimator.poses:
            if pose not in occupied_poses:
                p_failure *= (1 -
                              estimator.surface_octomaps[cl].get_prob(pose))
        return 1 * (1 - p_failure) + fail_cost * p_failure

    def distance_cost(q1, q2):
        if str in (type(q1), type(q2)):
            return MAX_DISTANCE  # TODO: take the max possible pose distance
        # TODO: can use the info encoded within these to obtain better bounds
        return np.linalg.norm(np.array(q2) - np.array(q1))

    ####################

    # TODO: could add measured as the output to these
    streams = [
        GeneratorStream(inputs=[P1],
                        outputs=[Q1],
                        conditions=[],
                        effects=[IsKin(P1, Q1)],
                        generator=inverse_kinematics),
        GeneratorStream(inputs=[S1],
                        outputs=[Q1],
                        conditions=[],
                        effects=[IsVisible(S1, Q1)],
                        generator=sample_visible),
        GeneratorStream(inputs=[S1],
                        outputs=[P1],
                        conditions=[],
                        effects=[IsSupported(P1, S1)],
                        generator=sample_table),
        CostStream(inputs=[S1, B1],
                   conditions=[],
                   effects=[ScanTable(S1, B1)],
                   function=scan_surface_cost),
        CostStream(inputs=[Q1, Q2],
                   conditions=[],
                   effects=[Distance(Q1, Q2),
                            Distance(Q2, Q1)],
                   function=distance_cost),
        CostStream(inputs=[S1],
                   conditions=[],
                   effects=[ScanRoom(S1)],
                   function=scan_room_cost),

        # TODO: make an is original precondition and only apply these to original values?
        # I suppose I could apply to all concrete things but that's likely not useful
        #TestStream(inputs=[S1, Q1], conditions=[IsOriginal(Q1)], effects=[IsVisible(S1, Q1)],
        #           test=is_visible, eager=True),
        #TestStream(inputs=[P1, Q1], conditions=[IsOriginal(Q1), IsOriginal(Q1)], effects=[IsKin(P1, Q1)],
        #           test=is_kinematic, eager=True),

        #GeneratorStream(inputs=[P1], outputs=[Q1], conditions=[], effects=[IsVisible(P1, Q1)],
        #                generator=sample_visible),
    ]

    problem = STRIPStreamProblem(initial_atoms, goal_formula, actions + axioms,
                                 streams, [])

    def command_from_action((action, args)):
        if action.name == 'scan_room':
            return simulate_scan, []
        if action.name in ('scan_table', 'look_block'):
            return simulate_look, []
        if action.name == 'move':
            q1, q2 = map(get_value, args)
            return simulate_move, [q2]
        if action.name == 'pick':
            o, p, q = map(get_value, args)
            return simulate_pick, [class_from_name[o]]
        if action.name == 'place':
            return simulate_place, []
        raise ValueError(action.name)

    return problem, command_from_action
O, L1, L2, S = Param(OBJ), Param(LOC), Param(LOC), Param(STATE)

rename_easy(locals())

#UNKNOWN_LOC = 'unknown'

# NOTE - I could validate safe by inspecting all objects or just the goal location

# TODO - could do costs by either specifying them upfront or using STRIPStream

actions = [
  Action(name='find', parameters=[O, L1], # Policy to find the object or an inspection?
    #condition=And(At(O, UNKNOWN_LOC)),
    #effect=And(At(O, L1), Not(At(O, UNKNOWN_LOC)))),
    condition=And(UnsureLoc(O), Not(NotAtLoc(O, L1))),
    effect=And(At(O, L1), Not(Clear(L1)), Not(UnsureLoc(O)), Cost(FindCost(O, L1)))), # TODO - can even adjust the cost of these to bias search towards more likely

  Action(name='inspect_loc', parameters=[O, L1], # NOTE - O is not needed except for the execution
    condition=And(UnsureClear(L1)),
    effect=And(Clear(L1), Not(UnsureClear(L1)), Cost(InspectLocCost(L1)))),

  Action(name='inspect_state', parameters=[O, L1, S],
    condition=And(UnsureState(O), At(O, L1)), # Probably should know where it is before you worry about its state
    #condition=And(UnsureState(O), Not(UnsureLoc(O))), # Probably should know where it is before you worry about its state
    effect=And(HasState(O, S), Not(UnsureState(O)), Cost(InspectStateCost(O, S)))), # TODO - should I factor loc in the cost?

  Action(name='transport', parameters=[O, L1, L2], # NOTE - Leslie and Tomas call this Move
    #condition=And(At(O, L1)),
    condition=And(At(O, L1), Clear(L2)),
    effect=And(At(O, L2), Clear(L1), Not(At(O, L1)), Not(Clear(L2))),
    cost=COST_SCALE*1),
Beispiel #6
0
##################################################

# NOTE - the location here is like a control parameter

actions = [
    Action(
        name='transport',
        parameters=[O, L, L2, B, B2],  # NOTE - Leslie and Tomas call this Move
        condition=And(BAt(O, B), At(O, L), IsMoveUpdate(L, B, L2, B2)),
        effect=And(
            BAt(O, B2),
            At(O, L2),
            Not(At(O, L)),
            Not(BAt(O, B)),  #)),
            Cost(MoveCost(L, B)))),

    #Action(name='transport', parameters=[O, B, L, B2], # NOTE - I could include belief preconditions for safety or effectiveness
    #  condition=And(BAt(O, B), BAtAbove(L, B2, ), IsMoveUpdate(B, L, B2)),
    #  effect=And(BAt(O, B2), Not(BAt(O, B)))), # NOTE - Leslie and Tomas call this Move
    Action(
        name='find',
        parameters=[O, L, B, B2],
        condition=And(BAt(O, B), At(O, L), IsLookUpdate(B, L, B2)),
        effect=And(
            BAt(O, B2),
            Not(BAt(O, B)),  #)),
            Cost(LookCost(B, L)))),
    Action(
        name='infer_at',
        parameters=[O, L],