def create_problem(): """ Creates a blocksworld STRIPStream problem. :return: a :class:`.STRIPStreamProblem` """ # Data types BLOCK = Type() # Fluent predicates Clear = Pred(BLOCK) OnTable = Pred(BLOCK) ArmEmpty = Pred() Holding = Pred(BLOCK) On = Pred(BLOCK, BLOCK) # Free parameters B1, B2 = Param(BLOCK), Param(BLOCK) rename_easy(locals()) actions = [ Action(name='pickup', parameters=[B1], condition=And(Clear(B1), OnTable(B1), ArmEmpty()), effect=And(Holding(B1), Not(Clear(B1)), Not(OnTable(B1)), Not(ArmEmpty()))), Action(name='putdown', parameters=[B1], condition=Holding(B1), effect=And(Clear(B1), OnTable(B1), ArmEmpty(), Not(Holding(B1)))), Action(name='stack', parameters=[B1, B2], condition=And(Clear(B2), Holding(B1)), effect=And(Clear(B1), On(B1, B2), ArmEmpty(), Not(Clear(B2)), Not(Holding(B1)))), Action(name='unstack', parameters=[B1, B2], condition=And(Clear(B1), On(B1, B2), ArmEmpty()), effect=And(Clear(B2), Holding(B1), Not(Clear(B1)), Not(On(B1, B2)), Not(ArmEmpty()))), ] axioms = [] cond_streams = [] constants = [] initial_atoms = [ OnTable('A'), OnTable('B'), OnTable('C'), Clear('A'), Clear('B'), Clear('C'), ArmEmpty(), ] goal_literals = [On('A', 'B'), On('B', 'C')] return STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams, constants)
def __init__(self): block, pose, config = P('o', BLOCK), P('p', POSE), P('q', CONFIG) STRIPSAction.__init__(self, self.__class__.__name__, [block, pose, config], [ AtPose(block, pose), HandEmpty(), IsIK(pose, config), ], [ Holding(block), Not(HandEmpty()), Not(AtPose(block, pose)), ]) Refinable.__init__(self, [H0Pick1(block, pose, config)])
def __init__(self, block=P('o', BLOCK), pose=P('p', POSE), config=P('q', CONFIG)): #def __init__(self, block, pose, config): super(H0Pick1, self).__init__(self.__class__.__name__, [block, pose, config], [ AtPose(block, pose), HandEmpty(), IsIK(pose, config), AtConfig(config), ], [ Holding(block), Not(HandEmpty()), Not(AtPose(block, pose)), ])
def __init__(self): params = (P('o', BLOCK), P('p', POSE), P('q', CONFIG)) block, pose, config = params conditions = [ AtPose(block, pose), HandEmpty(), IsIK(pose, config), #IsContained(block, pose, ENV_REGION), ] if USE_BASE: conditions.append(AtConfig(config)) super(Pick, self).__init__(self.__class__.__name__, params, conditions, [ Holding(block), Not(HandEmpty()), Not(AtPose(block, pose)), ])
def __init__(self): super(Land, self).__init__(self.__class__.__name__, [], [ Flying(), AtState(State((0, 0))), ], [ Landed(), Not(Flying()), ])
def get_effects(var_map, effect_vars, assign_map): effects = [] for var in effect_vars: name, args = var_name(var), make_var_constants(var, var_map) pre_args = [assign_map.get(p, p) for p in args + [X[var]]] eff_args = [assign_map.get(p, p) for p in args + [nX[var]]] predicate = var_map[name].predicate effects += [predicate(*eff_args), Not(predicate(*pre_args))] return effects
def create_problem(time_step=.5, fuel_rate=5, start_fuel=10, goal_p=10): """ Creates the Tsiolkovsky rocket problem. :return: a :class:`.STRIPStreamProblem` """ # Data types STATE, RATE, MASS, TIME = Type(), Type(), Type(), Type() HEIGHT = Type() # Fluent predicates AtState = Pred(STATE) # Derived predicates Above = Pred(HEIGHT) # Static predicates IsBurst = Pred(STATE, RATE, TIME, STATE) IsAbove = Pred(STATE, HEIGHT) # Free parameters X1, X2 = Param(STATE), Param(STATE) Q, T = Param(RATE), Param(TIME) H = Param(HEIGHT) rename_easy(locals()) # Trick to make debugging easier #################### actions = [ Action(name='burst', parameters=[X1, Q, T, X2], condition=And(AtState(X1), IsBurst(X1, Q, T, X2)), effect=And(AtState(X2), Not(AtState(X1)))), ] axioms = [ Axiom(effect=Above(H), condition=Exists([X1], And(AtState(X1), IsAbove(X1, H)))), ] #################### # Conditional stream declarations cond_streams = [ GeneratorStream(inputs=[X1, Q, T], outputs=[X2], conditions=[], effects=[IsBurst(X1, Q, T, X2)], generator=forward_burst), TestStream(inputs=[X1, H], conditions=[], effects=[IsAbove(X1, H)], test=lambda (p, v, m), h: p >= h, eager=True), ]
def __init__(self): params = (P('q1', CONFIG), P('q2', CONFIG)) q1, q2 = params super(Move, self).__init__(self.__class__.__name__, params, [ AtConfig(q1), ], [ AtConfig(q2), Not(AtConfig(q1)), ])
def __init__(self): obj, p, prior = Param(OBJ), Param(POSE), Param(BELIEF) post = 1 super(PerfectLook, self).__init__( self.__class__.__name__, [obj, p, prior], And( #At(obj, p), BAt(obj, p, prior)), And(BAt(obj, p, 1), Not(BAt(obj, p, prior))))
def create_abstract_problem(state, goal): # Goal serialization is basically like this # Necessary and sufficient? tables, initial_poses, initial_config, holding = state # TODO: connect this to goals on the actual states # The quantification of poses and grasps is independent # Conditions are derived or atoms actions = [ Action( name='pick', parameters=[B, S], condition=And(On(B, S), HandEmpty()), #, AtConf(Q)), #condition=And(On(B, S), HandEmpty(), Not(Holding(B))), # , AtConf(Q)), effect=And(Holding(B), Not(On(B, S)), Not(HandEmpty()))), Action( name='place', parameters=[B, S], condition=And(Holding(B)), #, AtConf(Q)), effect=And(On(B, S), HandEmpty(), Not(Holding(B)))), #Action(name='move', parameters=[S1, Q2], # condition=AtConf(Q), # effect=And(AtConf(Q2), Not(AtConf(Q)))), ] axioms = [] cond_streams = [] initial_atoms = [ #AtConf(initial_config), HandEmpty() ] + [On(block, pose.sur) for block, pose in initial_poses.items()] goal_literals = [On(b, s) for b, s in goal.on.items()] if goal.holding is not None: goal_literals.append(Holding(goal.holding)) problem = STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams) return problem
def __init__(self): params = (P('x', STATE), P('p', POSITION), P('s', SATELLITE)) x, p, s = params super(Deploy, self).__init__(self.__class__.__name__, params, [ Carrying(s), AtState(x), ArePair(x, p), ], [ AtOrbit(s, p), Not(Carrying(s)), ])
def __init__(self): params = (P('x1', STATE), P('t', TIME), P('x2', STATE)) x1, t, x2 = params super(Glide, self).__init__(self.__class__.__name__, params, [ AtState(x1), IsGlide(x1, t, x2), Flying(), ], [ AtState(x2), Not(AtState(x1)), ])
def __init__(self, p_obs_t): obj, p, prior, post = Param(OBJ), Param(POSE), Param(BELIEF), Param( BELIEF) super(BackwardLook, self).__init__( self.__class__.__name__, [obj, p, prior, post], And( #At(obj, p), BAt(obj, p, prior), #BAtAbove(obj, p, prior), # Problem where we want to delete the old belief, but we need to include the reference belief IsPossible(prior, post)), And(BAt(obj, p, post), Not(BAt(obj, p, prior))))
def __init__(self, oracle): params = (P('b', BLOCK), P('p', POSE), P('g', GRASP), P('q', CONFIG), P('t', TRAJ)) b, p, g, q, t = params conditions = [ AtPose(b, p), HandEmpty(), IsPose(b, p), # NOTE - might not need these IsGrasp(b, g), IsIK(b, p, g, q, t), ] if BASE: conditions.append(AtConfig(q)) conditions += collision_conditions(oracle, b, t) effects = [ #HasGrasp(b, g), Holding(b), HasGrasp(g), Not(HandEmpty()), Not(AtPose(b, p)), ] super(Pick, self).__init__(self.__class__.__name__, params, conditions, effects)
def __init__(self, oracle): params = (P('q1', CONFIG), P('q2', CONFIG)) q1, q2 = params super(Move, self).__init__( self.__class__.__name__, params, [ AtConfig(q1), ], [ AtConfig(q2), Not(AtConfig(q1)), #Cost(???), # TODO - include cost lowerbound first #Cost(???), # NOTE - PDDL uses the last cost specified ])
def __init__(self, min_safe_p=.95): obj, p1, p2, b = Param(OBJ), Param(POSE), Param(POSE), Param(BELIEF) Action.__init__( self, self.__class__.__name__, [obj, p1, p2, b], And( #At(obj, p1), BAt(obj, p1, b), BAtAbove(obj, p1, min_safe_p)), And( #At(obj, p2), BAt(obj, p2, b), #Not(At(obj, p1)), Not(BAt(obj, p1, b))), cost=1)
def __init__(self, min_safe_p=.95): obj, d1, d2 = Param(OBJ), Param(DIST), Param(DIST) Action.__init__( self, self.__class__.__name__, [obj, d], And( BClean(obj, d1), IsClean(d1, d2), #BAtAbove(obj, p1, min_safe_p) ), And( BClean(obj, d2), #Not(At(obj, p1)), Not(BClean(obj, d1))), cost=1)
def __init__(self): params = (P('x1', STATE), P('a', ACCELERATION), P('t', TIME), P('x2', STATE)) x1, a, t, x2 = params super(Burst, self).__init__( self.__class__.__name__, params, [ AtState(x1), IsBurst(x1, a, t, x2), Flying(), ], [ #Cost(), # Delta fuel AtState(x2), Not(AtState(x1)), ])
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)), ])
def __init__(self, blocks): params = (P('o', BLOCK), P('p', POSE), P('q', CONFIG)) block, pose, config = params conditions = [ Holding(block), IsIK(pose, config), # IsPose(p), IsConfig(q), ] if COLLISIONS: conditions += [Safe(other, pose) for other in blocks ] # TODO - always self safe using equality if BASE: conditions.append(AtConfig(config)) super(Place, self).__init__(self.__class__.__name__, params, conditions, [ AtPose(block, pose), HandEmpty(), Not(Holding(block)), ])
def __init__(self, p_obs_t, p_obs_f=None, log_cost=False): #self.p_obs_t = p_obs_t #if p_obs_f is None: self.p_obs_f = 1 - p_obs_t #self.log_cost = log_cost cost = prob_cost( p_obs_t, log_cost=log_cost) # Conditions on successful observation cost = None obj, p, prior, post = Param(OBJ), Param(POSE), Param(BELIEF), Param( BELIEF) super(Look, self).__init__( self.__class__.__name__, [obj, p, prior, post], And( #At(obj, p), BAt(obj, p, prior), IsUpdate(prior, post)), And(BAt(obj, p, post), Not(BAt(obj, p, prior))), cost)
O, P, G = Param(OBJ), Param(POSE), Param(GRASP) O2, P2 = Param(OBJ), Param(POSE) Q, Q2 = Param(CONF), Param(CONF) T = Param(TRAJ) rename_easy(locals()) #################### actions = [ Action(name='pick', parameters=[O, P, G, Q, T], condition=And(PoseEq(O, P), HandEmpty(), ConfEq(Q), GraspMotion(P, G, Q, T), ForAll([O2], Or(Equal(O, O2), SafeTraj(O2, T)))), #ForAll([O2], Or(Equal(O, O2), And(SafePose(O2, P), SafeGTraj(O2, GT))))), effect=And(GraspEq(O, G), Not(HandEmpty()), Not(PoseEq(O, P)))), Action(name='place', parameters=[O, P, G, Q, T], condition=And(GraspEq(O, G), ConfEq(Q), GraspMotion(P, G, Q, T), ForAll([O2], Or(Equal(O, O2), And(SafePose(O2, P), SafeTraj(O2, T))))), effect=And(PoseEq(O, P), HandEmpty(), Not(GraspEq(O, G)))), Action(name='move', parameters=[Q, Q2, T], condition=And(ConfEq(Q), HandEmpty(), FreeMotion(Q, Q2, T), ForAll([O2], SafeTraj(O2, T))), effect=And(ConfEq(Q2), Not(ConfEq(Q)))), Action(name='move_holding', parameters=[Q, Q2, T, O, G], condition=And(ConfEq(Q), GraspEq(O, G), HoldingMotion(Q, Q2, G, T), ForAll([O2], Or(Equal(O, O2), SafeTraj(O2, T)))), effect=And(ConfEq(Q2), Not(ConfEq(Q)))),
def compile_problem(tamp_problem): """ Constructs a STRIPStream problem for the countable TMP problem. :param tamp_problem: a :class:`.TMPProblem` :return: a :class:`.STRIPStreamProblem` """ B1, B2 = Param(BLOCK), Param(BLOCK) P1, P2 = Param(POSE), Param(POSE) Q1, Q2 = Param(CONF), Param(CONF) actions = [ Action(name='pick', parameters=[B1, P1, Q1], condition=And(AtPose(B1, P1), HandEmpty(), AtConf(Q1), LegalKin(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), LegalKin(P1, Q1), ForAll([B2], Or(Equal(B1, B2), Safe(B2, P1)))), effect=And(AtPose(B1, P1), HandEmpty(), Not(Holding(B1)))), Action(name='move', parameters=[Q1, Q2], condition=AtConf(Q1), effect=And(AtConf(Q2), Not(AtConf(Q1)))), ] axioms = [ Axiom(effect=Safe(B2, P1), condition=Exists([P2], And(AtPose(B2, P2), CollisionFree(P1, P2)))), ] cond_streams = [ EasyGenStream(inputs=[], outputs=[P1], conditions=[], effects=[], generator=lambda: irange(0, NUM_POSES)), EasyGenStream(inputs=[P1], outputs=[Q1], conditions=[], effects=[LegalKin(P1, Q1)], generator=lambda p: iter([p])), EasyTestStream(inputs=[P1, P2], conditions=[], effects=[CollisionFree(P1, P2)], test=lambda p1, p2: p1 != p2, eager=EAGER_TESTS), ] constants = [] initial_atoms = [ AtConf(tamp_problem.initial_config), ] + [ AtPose(block, pose) for block, pose in tamp_problem.initial_poses.iteritems() ] if tamp_problem.initial_holding is False: initial_atoms.append(HandEmpty()) else: initial_atoms.append(Holding(tamp_problem.initial_holding, BLOCK)) goal_literals = [] if tamp_problem.goal_holding is False: goal_literals.append(HandEmpty()) elif tamp_problem.goal_holding is not None: goal_literals.append(Holding(tamp_problem.goal_holding)) for block, goal in tamp_problem.goal_poses.iteritems(): goal_literals.append(AtPose(block, goal)) return STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams, constants)
def create_problem(dt, GoalTest, ClearTest, CalcDiffSystem, InitialState): """ Creates a generic non-holonomic motion planning problem :return: a :class:`.STRIPStreamProblem` """ # Data types X, DX, U = Type(), Type(), Type() # Fluent predicates AtX = Pred(X) # Fluent predicates GoalReached = Pred() # Static predicates ComputeDX = Pred(X, U, DX) Dynamics = Pred(X, DX, X) AtGoal = Pred(X) Clear = Pred(X, X) # Free parameters X1, X2 = Param(X), Param(X) DX1 = Param(DX) U1 = Param(U) rename_easy(locals()) # Trick to make debugging easier #################### actions = [ Action(name='simulate', parameters=[X1, U1, X2, DX1], condition=And(AtX(X1), ComputeDX(X1, U1, DX1), Dynamics(X1, DX1, X2), Clear(X1, X2)), effect=And(Not(AtX(X1)), AtX(X2))), ] axioms = [ Axiom(effect=GoalReached(), condition=Exists([X1], AtGoal(X1))), ] #################### # Conditional stream declarations def ComputeDXCalculator(X1F, DX1F): X2F = X1F[:] for i in range(len(X2F)): X2F[i] = X1F[i] + dt * DX1F[i] return X2F cond_streams = [ FunctionStream(inputs=[X1, DX1], outputs=[X2], conditions=[], effects=[Dynamics(X1, DX1, X2)], function=ComputeDXCalculator), FunctionStream(inputs=[X1, U1], outputs=[DX1], conditions=[], effects=[ComputeDX(X1, U1, DX1)], function=CalcDiffSystem), TestStream(inputs=[X1, X2], conditions=[], effects=[Clear(X1, X2)], test=ClearTest, eager=True), TestStream(inputs=[X1], conditions=[], effects=[AtGoal(X1)], test=GoalTest, eager=True), ] #################### constants = [ # TODO - need to declare control inputs (or make a stream for them) ] initial_atoms = [ AtX(InitialState), ] goal_literals = [GoalReached()] problem = STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams, constants) return problem
def create_problem(goal, obstacles=(), distance=.25, digits=3): """ Creates a Probabilistic Roadmap (PRM) motion planning problem. :return: a :class:`.STRIPStreamProblem` """ # Data types POINT = Type() REGION = Type() # Fluent predicates AtPoint = Pred(POINT) # Derived predicates InRegion = Pred(REGION) #IsReachable = Pred(POINT, POINT) IsReachable = Pred(POINT) # Stream predicates IsEdge = Pred(POINT, POINT) Contained = Pred(POINT, REGION) # Free parameters P1, P2 = Param(POINT), Param(POINT) R = Param(REGION) rename_easy(locals()) # Trick to make debugging easier #################### actions = [ Action(name='move', parameters=[P1, P2], condition=And(AtPoint(P1), IsReachable(P2)), effect=And(AtPoint(P2), Not(AtPoint(P1)))) ] axioms = [ Axiom(effect=InRegion(R), condition=Exists([P1], And(AtPoint(P1), Contained(P1, R)))), Axiom(effect=IsReachable(P2), condition=Or(AtPoint(P2), Exists([P1], And(IsReachable(P1), IsEdge(P1, P2))))), ] #################### def sampler(): for _ in inf_sequence(): yield [(sample(digits), ) for _ in range(10)] roadmap = set() def test(p1, p2): if not (get_distance(p1, p2) <= distance and is_collision_free( (p1, p2), obstacles)): return False roadmap.add((p1, p2)) return True #################### # Conditional stream declarations cond_streams = [ EasyListGenStream( inputs=[], outputs=[P1], conditions=[], effects=[], generator=sampler ), # NOTE - version that only generators collision-free points GeneratorStream(inputs=[R], outputs=[P1], conditions=[], effects=[Contained(P1, R)], generator=lambda r: (sample_box(r) for _ in inf_sequence())), TestStream(inputs=[P1, P2], conditions=[], effects=[IsEdge(P1, P2), IsEdge(P2, P1)], test=test, eager=True), ] #################### constants = [] initial_atoms = [ AtPoint((0, 0)), ] goal_literals = [] if is_region(goal): goal_literals.append(InRegion(goal)) else: goal_literals.append(AtPoint(goal)) problem = STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams, constants) return problem, roadmap
def compile_problem(oracle): problem = oracle.problem for obj in problem.goal_poses: if problem.goal_poses[obj] == 'initial': problem.goal_poses[obj] = oracle.initial_poses[obj] elif problem.goal_poses[ obj] in oracle.initial_poses: # Goal names other object (TODO - compile with initial) problem.goal_poses[obj] = oracle.initial_poses[ problem.goal_poses[obj]] # oracle.initial_config = ??? # TODO - set the initial configuration transit_conf = Config(oracle.default_left_arm_config) #################### O, P, G, T = Param(OBJ), Param(POSE), Param(GRASP), Param(TRAJ) OB, R = Param(OBJ), Param(REG) BQ, MQ = Param(BASE_CONF), Param(MANIP_CONF) BQ2, MQ2 = Param(BASE_CONF), Param(MANIP_CONF) BT, MT = Param(BASE_TRAJ), Param(MANIP_TRAJ) rename_easy(locals()) # Separate arm and base configs and trajectories? # Make a fixed configuration for arm stuff # If I separate arm and base poses, I can avoid worrying about collision when the base and things # Would I ever want to actually separate these and use the same grasp at a different config? # - Maybe for two arms? # - I don't want to reuse trajectories at different base configs. That wouldn't make sense # - Although if I manipulate two configs at once, maybe it would! # - Make constant arm transit configs? # TODO - maybe make an axiom to handle BT and MT when doing these things? # TODO - I could equip a manip conf with a base conf at all times if I really don't want them separate # NOTE - put then would need to update them whenever moving the base #actions = [ # Action(name='pick', parameters=[O, P, G, BQ, MQ], # condition=And(PoseEq(O, P), HandEmpty(), Kin(O, P, G, BQ, MQ), BaseEq(BQ), ManipEq(MQ)), # #ForAll([OB], Or(Equal(O, OB), Safe(OB, T)))), # effect=And(GraspEq(O, G), Not(HandEmpty()), Not(PoseEq(O, P)))), # # Action(name='place', parameters=[O, P, G, BQ, MQ], # condition=And(GraspEq(O, G), Kin(O, P, G, BQ, MQ), BaseEq(BQ), ManipEq(MQ)), # #ForAll([OB], Or(Equal(O, OB), Safe(OB, T)))), # effect=And(PoseEq(O, P), HandEmpty(), Not(GraspEq(O, G)))), # # Action(name='move_arm', parameters=[MQ, MQ2, BQ, MT], # condition=And(ManipEq(MQ), ManipMotion(MQ, MQ2, BQ, MT)), # effect=And(ManipEq(MQ2), Not(ManipEq(MQ)))), # # Action(name='move_base', parameters=[BQ, BQ2, MQ, BT], # TODO - put the arm motion stuff in an axiom # condition=And(BaseEq(BQ), TransitManip(MQ), BaseMotion(BQ, BQ2, BT)), # effect=And(BaseEq(BQ2), Not(BaseEq(BQ)))), # ] # TODO - should I include the grasp in move backwards trajectory? actions = [ abs_action( name='pick', parameters=[O, P, G, BQ, MQ], conditions=[ And(PoseEq(O, P), HandEmpty(), Kin(O, P, G, BQ, MQ)), #ForAll([OB], Or(Equal(O, OB), Safe(OB, T)))), And(BaseEq(BQ), ManipEq(MQ)) ], effect=And(GraspEq(O, G), Not(HandEmpty()), Not(PoseEq(O, P)))), abs_action( name='place', parameters=[O, P, G, BQ, MQ], conditions=[ And(GraspEq(O, G), Kin(O, P, G, BQ, MQ)), #ForAll([OB], Or(Equal(O, OB), Safe(OB, T)))), And(BaseEq(BQ), ManipEq(MQ)) ], effect=And(PoseEq(O, P), HandEmpty(), Not(GraspEq(O, G)))), # NOTE - the hierarchical versions of this abs_action( name='move_arm', parameters=[MQ, MQ2, BQ, MT], # TODO - Or(TransitManip(MQ), TransitManip(MQ2)) conditions=[ And(ManipEq(MQ), ManipMotion(MQ, MQ2, BQ, MT), BaseEq(BQ)), #And(ManipEq(MQ), ManipMotion(MQ, MQ2, BQ, MT), BaseEq(BQ), Or(TransitManip(MQ), TransitManip(MQ2))) # This does something odd #And(ManipEq(MQ), ManipMotion(MQ, MQ2, BQ, MT), BaseEq(BQ), # Or(TransitManip(MQ), LegalConf(BQ, MQ)), # Or(TransitManip(MQ2), LegalConf(BQ, MQ2))) # This does something odd ], # TODO - put an Or(Pair(MQ, BQ), Pair(MQ2, BQ)) or something effect=And(ManipEq(MQ2), Not(ManipEq(MQ)))), abs_action(name='move_base', parameters=[BQ, BQ2, BT], conditions=[ And(BaseEq(BQ), SafeManip(BT), BaseMotion(BQ, BQ2, BT)) ], effect=And(BaseEq(BQ2), Not(BaseEq(BQ)))), #abs_action(name='move_base', parameters=[BQ, BQ2, MQ, BT], # conditions=[ # And(BaseEq(BQ), TransitManip(MQ), BaseMotion(BQ, BQ2, BT))], # effect=And(BaseEq(BQ2), Not(BaseEq(BQ)))), ] # NOTE - the parameters really aren't necessary # TODO - could remove any dependence on tests because there are so cheap with the evaluation (already can't use them with axioms) # TODO - could also just make the cost only depend on the introduced objects within the effects axioms = [ Axiom(effect=Holding(O), condition=Exists([G], And(GraspEq(O, G), LegalGrasp(O, G)))), Axiom(effect=InRegion(O, R), condition=Exists([P], And(PoseEq(O, P), Contained(R, O, P)))), #Axiom(effect=Safe(O, T), condition=Exists([P], And(PoseEq(O, P), CFree(O, P, T)))), #Axiom(effect=SafeArm(O, MQ, T), condition=Exists([P, MQ], And(PoseEq(O, P), ManipEq(MQ), CFree(O, P, T)))), Axiom(effect=SafeManip(BT), condition=Exists( [MQ], And(ManipEq(MQ), TransitManip(MQ)))), # TODO - add condition here ] # TODO - include parameters in STRIPS axiom? #################### def get_base_conf(conf): return Config(base_values_from_full_config(conf.value)) def get_arm_conf(conf): return Config( arm_from_full_config(oracle.robot.GetActiveManipulator(), conf.value)) def sample_poses(o, num_samples=1): while True: if AVOID_INITIAL: oracle.set_all_object_poses(oracle.initial_poses) else: oracle.set_all_object_poses({o: oracle.initial_poses[o]}) yield list( islice( random_region_placements(oracle, o, oracle.get_counters(), region_weights=True), num_samples)) def sample_grasps(o): yield get_grasps(oracle, o) def sample_region(o, r, num_samples=1): while True: oracle.set_all_object_poses({o: oracle.initial_poses[o]}) yield list( islice( random_region_placements(oracle, o, [r], region_weights=True), num_samples)) def sample_motion(o, p, g, max_calls=1, max_failures=50): oracle.set_all_object_poses( {o: p}) # TODO - saver for the initial state as well? if oracle.approach_collision(o, p, g): return for i in range(max_calls): pap = PickAndPlace(oracle.get_geom_hash(o), p, g) if not pap.sample(oracle, o, max_failures=max_failures, sample_vector=False, sample_arm=False, check_base=False): break #pap.obj = o yield [(get_base_conf(pap.grasp_config), get_arm_conf(pap.grasp_config))] def collision_free(o, p, t): if p is None or o == t.obj: return True holding = ObjGrasp(t.obj, t.grasp) #holding = Holding(self.oracle.get_body_name(pap.geom_hash), pap.grasp) if not DO_ARM_MOTION: return not oracle.holding_collision(t.grasp_config, o, p, holding) return not oracle.traj_holding_collision(t.approach_config, t.trajs, o, p, holding) cond_streams = [ EasyListGenStream(inputs=[O], outputs=[P], conditions=[], effects=[LegalPose(O, P)], generator=sample_poses), EasyListGenStream(inputs=[O], outputs=[G], conditions=[], effects=[LegalGrasp(O, G)], generator=sample_grasps), EasyListGenStream(inputs=[O, R], outputs=[P], conditions=[], effects=[LegalPose(O, P), Contained(R, O, P)], generator=sample_region), #MultiEasyGenStream(inputs=[O, P, G], outputs=[BQ, MQ], conditions=[LegalPose(O, P), LegalGrasp(O, G)], # effects=[Kin(O, P, G, BQ, MQ)], generator=sample_motion), EasyListGenStream(inputs=[O, P, G], outputs=[BQ, MQ], conditions=[LegalPose(O, P), LegalGrasp(O, G)], effects=[Kin(O, P, G, BQ, MQ), LegalConf(BQ, MQ)], generator=sample_motion), # TODO - this doesn't work for constants # TODO - make a condition that MQ, MQ2 both work for BQ. Otherwise, it will still create a ton of streams #EasyGenStream(inputs=[MQ, MQ2, BQ], outputs=[MT], conditions=[], # effects=[ManipMotion(MQ, MQ2, BQ, MT)], generator=lambda *args: [None], order=1), EasyGenStream(inputs=[MQ, MQ2, BQ], outputs=[MT], conditions=[TransitManip(MQ), LegalConf(BQ, MQ2)], effects=[ManipMotion(MQ, MQ2, BQ, MT)], generator=lambda *args: [None], order=1), EasyGenStream(inputs=[MQ, MQ2, BQ], outputs=[MT], conditions=[LegalConf(BQ, MQ), TransitManip(MQ2)], effects=[ManipMotion(MQ, MQ2, BQ, MT)], generator=lambda *args: [None], order=1), #EasyGenStream(inputs=[MQ, MQ2, BQ], outputs=[MT], conditions=[LegalConf(BQ, MQ), LegalConf(BQ, MQ2)], # effects=[ManipMotion(MQ, MQ2, BQ, MT)], generator=lambda *args: [None], order=1), EasyGenStream(inputs=[BQ, BQ2], outputs=[BT], conditions=[], effects=[BaseMotion(BQ, BQ2, BT)], generator=lambda *args: [None], order=1), #effects=[BaseMotion(BQ, BQ2, BT)], generator=lambda *args: [None], order=2), # NOTE - causes a bug! #EasyTestStream(inputs=[O, P, T], conditions=[LegalPose(O, P)], effects=[CFree(O, P, T)], # test=collision_free, eager=EAGER_TESTS), ] #################### #print transit_conf.value #print oracle.initial_config.value initial_base = get_base_conf(oracle.initial_config) initial_manip = get_arm_conf(oracle.initial_config) print initial_base.value print initial_manip.value constants = [] initial_atoms = [ BaseEq(initial_base), ManipEq(initial_manip), HandEmpty(), TransitManip(transit_conf), LegalConf(initial_base, initial_manip), ] for obj, pose in oracle.initial_poses.iteritems(): initial_atoms += [PoseEq(obj, pose), LegalPose(obj, pose)] # TODO - serialize goals by object name goal_literals = [] if problem.goal_holding is not None: if problem.goal_holding is False: goal_literals.append(HandEmpty()) elif isinstance(problem.goal_holding, ObjGrasp): goal_literals.append( GraspEq(problem.goal_holding.object_name, problem.goal_holding.grasp)) elif problem.goal_holding in oracle.get_objects(): goal_literals.append(Holding(problem.goal_holding)) else: raise Exception() for obj, pose in problem.goal_poses.iteritems(): goal_literals.append(PoseEq(obj, pose)) initial_atoms.append(LegalPose(obj, pose)) for obj, region in problem.goal_regions.iteritems(): goal_literals.append(InRegion(obj, region)) #goal_formula = And(*goal_literals) #goal_formula = AbsCondition(map(And, goal_literals)) # TODO - bug where goals must have And goal_formula = AbsCondition( goal_literals) # TODO - bug where goals must have And return STRIPStreamProblem(initial_atoms, goal_formula, actions + axioms, cond_streams, constants)
def create_problem(initRobotPos = (0.5, 0.5), initRobotVar = 0.01, maxMoveDist = 5.0, beaconPos = (1, 1), homePos = (0, 0), goalPosEps = 0.1, goalVar = 0.1, odoErrorRate = 0.1, obsVarPerDistFromSensor = 10.0, minObsVar = 0.001, domainSize = 20, verboseFns = True): """ :return: a :class:`.STRIPStreamProblem` """ # Data types POS = Type() # 2D position VAR = Type() # 2D variance DIST = Type() # positive scalar # Fluent predicates RobotPos = Pred(POS) RobotVar = Pred(VAR) # Derived predicates KnowYouAreHome = Pred() # Static predicates DistBetween = Pred(POS, POS, DIST) # Distance between two positions (function) LessThanV = Pred(VAR, VAR) # Less than, on distances LessThanD = Pred(DIST, DIST) # Less than, on distances OdometryVar = Pred(VAR, VAR, DIST) # How does odometry variance increase? SensorVar = Pred(VAR, VAR, DIST) # How does an observation decrease variance? LegalPos = Pred(POS) # Any legal robot pos # Free parameters RPOS1, RPOS2, RVAR1, RVAR2 = Param(POS), Param(POS), Param(VAR), Param(VAR) DIST1, DIST2 = Param(DIST), Param(DIST) def odoVarFun(rv, d): odoVar = (d * odoErrorRate)**2 result = rv + odoVar if verboseFns: print 'ovf:', rv, d, result return [result] def sensorVarFun(rv, d): obsVar = max(d / obsVarPerDistFromSensor, minObsVar) result = 1.0 / ((1.0 / rv) + (1.0 / obsVar)) if verboseFns: print 'svf:', rv, d, result return [result] def randPos(): while True: result = (random.random() * domainSize, random.random() * domainSize) print 'rp:', result yield [result] def legalTest(rp): (x, y) = rp result = (0 <= x <= domainSize) and (0 <= y <= domainSize) if not result: print 'not legal:', rp return result actions = [ Action(name='Move', parameters=[RPOS1, RPOS2, RVAR1, RVAR2, DIST1], condition = And(RobotPos(RPOS1), RobotVar(RVAR1), LegalPos(RPOS2), # Generate an intermediate pos DistBetween(RPOS1, RPOS2, DIST1), LessThanD(DIST1, maxMoveDist), OdometryVar(RVAR1, RVAR2, DIST1)), effect = And(RobotPos(RPOS2), RobotVar(RVAR2), Not(RobotPos(RPOS1)), Not(RobotVar(RVAR1)))), # Action(name='Look', # parameters=[RPOS1, RVAR1, RVAR2, DIST1], # condition = And(RobotPos(RPOS1), # RobotVar(RVAR1), # DistBetween(RPOS1, beaconPos, DIST1), # SensorVar(RVAR1, RVAR2, DIST1)), # effect = And(RobotVar(RVAR2), # Not(RobotVar(RVAR1)))) ] axioms = [ Axiom(effect = KnowYouAreHome(), condition = Exists([RPOS1, RVAR1, DIST1], And(RobotPos(RPOS1), RobotVar(RVAR1), DistBetween(RPOS1, homePos, DIST1), LessThanD(DIST1, goalPosEps), LessThanV(RVAR1, goalVar)))) ] # Conditional stream declarations cond_streams = [ TestStream(inputs = [RPOS1], conditions = [], effects = [LegalPos(RPOS1)], test = legalTest, eager = True), GeneratorStream(inputs = [], outputs = [RPOS1], conditions = [], effects = [LegalPos(RPOS1)], generator = randPos), GeneratorStream(inputs = [RPOS1, RPOS2], outputs = [DIST1], conditions = [], effects = [DistBetween(RPOS1, RPOS2, DIST1)], generator = lambda rp1, rp2: [distance(rp1, rp2)]), GeneratorStream(inputs = [RVAR1, DIST1], outputs = [RVAR2], conditions = [], effects = [OdometryVar(RVAR1, RVAR2, DIST1)], generator = odoVarFun), # GeneratorStream(inputs = [RVAR1, DIST1], # outputs = [RVAR2], # conditions = [], # effects = [SensorVar(RVAR1, RVAR2, DIST1)], # generator = sensorVarFun), TestStream(inputs = [DIST1, DIST2], conditions = [], effects = [LessThanD(DIST1, DIST2)], test = lt, eager = True), TestStream(inputs = [RVAR1, RVAR2], conditions = [], effects = [LessThanV(RVAR1, RVAR2)], test = lt, eager = True) ] #################### constants = [ ] initial_atoms = [ RobotPos(initRobotPos), RobotVar(initRobotVar), LegalPos(homePos), LegalPos((.1, .1)), LegalPos((3.0, .1)), LegalPos((6.0, .1)) ] goal_literals = [ KnowYouAreHome() ] problem = STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams, constants) return problem
def add_refined(self): self.add_conditions(Not(self.RefinedPredicate(*self.parameters))) for operator in self.refinement: operator.add_conditions( self.get_valid_atom(operator, self.parameters))
def create_problem(goal, obstacles=(), distance=.25, digits=3): """ Creates a Probabilistic Roadmap (PRM) motion planning problem. :return: a :class:`.STRIPStreamProblem` """ # Data types POINT = Type() REGION = Type() # Fluent predicates AtPoint = Pred(POINT) # Derived predicates InRegion = Pred(REGION) # Stream predicates AreNearby = Pred(POINT, POINT) IsEdge = Pred(POINT, POINT) Contained = Pred(POINT, REGION) # Functions Distance = Func(POINT, POINT) # Free parameters P1, P2 = Param(POINT), Param(POINT) R = Param(REGION) rename_easy(locals()) # Trick to make debugging easier #################### actions = [ #STRIPSAction(name='move', parameters=[P1, P2], # conditions=[AtPoint(P1), IsEdge(P1, P2)], # effects=[AtPoint(P2), Not(AtPoint(P1))], cost=1), # Fixed cost #STRIPSAction(name='move', parameters=[P1, P2], # conditions=[AtPoint(P1), IsEdge(P1, P2)], # effects=[AtPoint(P2), Not(AtPoint(P1)), Cost(Distance(P1, P2))]), # Cost depends on parameters Action(name='move', parameters=[P1, P2], condition=And(AtPoint(P1), IsEdge(P1, P2)), effect=And(AtPoint(P2), Not(AtPoint(P1))), costs=[1, Distance(P1, P2)]), ] axioms = [ #Axiom(effect=GoalReached(), condition=Exists([P1], And(AtPos(P1), Contained(P1)))), STRIPSAxiom(conditions=[AtPoint(P1), Contained(P1, R)], effects=[InRegion(R)]) ] #################### # Conditional stream declarations cond_streams = [ GeneratorStream( inputs=[], outputs=[P1], conditions=[], effects=[], generator=lambda: ((sample(digits), ) for _ in inf_sequence()) ), # NOTE - version that only generators collision-free points GeneratorStream(inputs=[R], outputs=[P1], conditions=[], effects=[Contained(P1, R)], generator=lambda r: ((sample_box(r), ) for _ in inf_sequence())), #TestStream(inputs=[P1, P2], conditions=[], effects=[AreNearby(P1, P2), AreNearby(P2, P1)], # test=lambda p1, p2: get_distance(p1, p2) <= distance, eager=True), #TestStream(inputs=[P1, P2], conditions=[AreNearby(P1, P2)], effects=[IsEdge(P1, P2), IsEdge(P2, P1)], # test=lambda p1, p2: is_collision_free((p1, p2), obstacles), eager=True), TestStream( inputs=[P1, P2], conditions=[], effects=[IsEdge(P1, P2), IsEdge(P2, P1)], #test=lambda p1, p2: is_collision_free((p1, p2), obstacles), eager=True), test=lambda p1, p2: (get_distance(p1, p2) <= distance) and is_collision_free( (p1, p2), obstacles), eager=True), #TestStream(inputs=[P1, P2], conditions=[], effects=[IsEdge(P1, P2), IsEdge(P2, P1)], # test=lambda p1, p2: get_distance(p1, p2) <= distance and is_collision_free((p1, p2), obstacles), eager=True), #TestStream(inputs=[P1, R], conditions=[], effects=[Contained(P1, R)], # test=lambda p, r: contains(p, r), eager=True), CostStream(inputs=[P1, P2], conditions=[], effects=[Distance(P1, P2), Distance(P2, P1)], function=get_distance, scale=100, eager=True), ] #################### constants = [] initial_atoms = [ AtPoint((0, 0)), ] goal_literals = [] if is_region(goal): goal_literals.append(InRegion(goal)) else: goal_literals.append(AtPoint(goal)) problem = STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams, constants) return problem
def create_problem(): """ Creates the 1D task and motion planning STRIPStream problem. :return: a :class:`.STRIPStreamProblem` """ blocks = ['block%i'%i for i in range(3)] num_poses = pow(10, 10) initial_config = 0 # the initial robot configuration is 0 initial_poses = {block: i for i, block in enumerate(blocks)} # the initial pose for block i is i goal_poses = {block: i+1 for i, block in enumerate(blocks)} # the goal pose for block i is i+1 #################### VALUE = Type() # Fluent predicates AtConf = Pred(VALUE) AtPose = Pred(VALUE, VALUE) HandEmpty = Pred() Holding = Pred(VALUE) # Derived predicates Safe = Pred(VALUE, VALUE, VALUE) # Static predicates IsBlock = Pred(VALUE) IsPose = Pred(VALUE) IsConf = Pred(VALUE) LegalKin = Pred(VALUE, VALUE) CollisionFree = Pred(VALUE, VALUE, VALUE, VALUE) # Free parameters B1, B2 = Param(VALUE), Param(VALUE) P1, P2 = Param(VALUE), Param(VALUE) Q1, Q2 = Param(VALUE), Param(VALUE) rename_easy(locals()) # Trick to make debugging easier #################### # NOTE - it would be easier to just update an in hand pose actions = [ STRIPSAction(name='pick', parameters=[B1, P1, Q1], conditions=[IsBlock(B1), IsPose(P1), IsConf(Q1), LegalKin(P1, Q1), AtPose(B1, P1), HandEmpty(), AtConf(Q1)], effects=[Holding(B1), Not(AtPose(B1, P1)), Not(HandEmpty())]), STRIPSAction(name='place', parameters=[B1, P1, Q1], conditions=[IsBlock(B1), IsPose(P1), IsConf(Q1), LegalKin(P1, Q1), Holding(B1), AtConf(Q1)] + [Safe(b2, B1, P1) for b2 in blocks], effects=[AtPose(B1, P1), HandEmpty(), Not(Holding(B1))]), STRIPSAction(name='move', parameters=[Q1, Q2], conditions=[IsConf(Q1), IsConf(Q2), AtConf(Q1)], effects=[AtConf(Q2), Not(AtConf(Q1))]), ] axioms = [ # TODO - need to combine axioms #Axiom(effect=Safe(B2, B1, P1), # condition=Exists([P2], And(AtPose(B2, P2), CollisionFree(B1, P1, B2, P2)))), # Infers B2 is at a safe pose wrt B1 at P1 ] #################### # Conditional stream declarations cond_streams = [ GeneratorStream(inputs=[], outputs=[P1], conditions=[], effects=[IsPose(P1)], generator=lambda: xrange(num_poses)), # Enumerating all the poses GeneratorStream(inputs=[P1], outputs=[Q1], conditions=[IsPose(P1)], effects=[IsConf(Q1), LegalKin(P1, Q1)], generator=lambda p: [p]), # Inverse kinematics TestStream(inputs=[B1, P1, B2, P2], conditions=[IsBlock(B1), IsPose(P1), IsBlock(B2), IsPose(P2)], effects=[CollisionFree(B1, P1, B2, P2)], test=lambda b1, p1, b2, p2: p1 != p2, eager=True), # Collision checking ] #################### constants = [] initial_atoms = [ IsConf(initial_config), AtConf(initial_config), HandEmpty() ] for block, pose in initial_poses.iteritems(): initial_atoms += [ IsBlock(block), IsPose(pose), AtPose(block, pose), ] goal_literals = [] for block, pose in goal_poses.iteritems(): initial_atoms += [ IsBlock(block), IsPose(pose), ] goal_literals.append(AtPose(block, pose)) problem = STRIPStreamProblem(initial_atoms, goal_literals, actions + axioms, cond_streams, constants) return problem