def rmpyl_observation_risk(): prog = RMPyL() prog *= prog.observe( { 'name': 'travel', 'ctype': 'probabilistic', 'domain': ['Short', 'Long'], 'probability': [0.7, 0.3] }, Episode(action='(cab-ride-long)', duration={ 'ctype': 'uncontrollable_probabilistic', 'distribution': { 'type': 'uniform', 'lb': 5.0, 'ub': 11.0 } }), Episode(action='(cab-ride-short)', duration={ 'ctype': 'uncontrollable_probabilistic', 'distribution': { 'type': 'uniform', 'lb': 5.0, 'ub': 10.0 } })) prog.add_overall_temporal_constraint(ctype='controllable', lb=0.0, ub=10.0) return prog
def _recursive_convert(G, prog, node, policy, constraints, constraint_fields, global_end): """Recursively converts a policy graph into an RMPyL program.""" #Collects constraints in the node constraints.update(_get_state_constraints(node, constraint_fields)) if node.terminal: stop_episode = Episode(action='__stop__') #Stop action if global_end != None: constraints.add( TemporalConstraint(start=stop_episode.end, end=global_end, ctype='controllable', lb=0.0, ub=float('inf'))) return stop_episode else: op = policy[node] #Operator at current node children = G.hyperedge_successors(node, op) obs_domain = op.properties['obs'] obs_probs = op.properties['prob'] if op.properties['episode'] == None: ep = Episode(action=op.name) else: ep = op.properties['episode'] #Probabilistic transitions with more than one option require an #observation node. if len(obs_domain) > 1: return prog.sequence( ep, prog.observe( { 'name': 'observe-' + op.name, 'ctype': 'probabilistic', 'domain': obs_domain, 'probability': obs_probs }, *[ _recursive_convert(G, prog, c, policy, constraints, constraint_fields, global_end) for c in children ])) #If there is only a single transition, no need for an observation. elif len(obs_domain) == 1: return prog.sequence( ep, _recursive_convert(G, prog, children[0], policy, constraints, constraint_fields, global_end)) else: raise ValueError( 'Cannot have a probabilitistic transition with 0 children!' )
def perform_science(self): """ Returns the episode corresponding to the vehicle performing science experiments. """ return sequence_composition( Episode(duration={'ctype':'uncontrollable_bounded','lb':9,'ub':11}, action='(drill %s)'%(self.name)), Episode(duration={'ctype':'uncontrollable_bounded','lb':10,'ub':15}, action='(collect %s)'%(self.name)), Episode(duration={'ctype':'controllable','lb':5,'ub':30}, action='(process %s)'%(self.name)))
def scan(self, lb=1, ub=10): return Episode(duration={ 'ctype': 'controllable', 'lb': lb, 'ub': ub }, action='(' + self._name + '-scan)')
def rmpyl_infeasible(): prog = RMPyL() prog *= Episode(action='(cab-ride)', duration={ 'ctype': 'controllable', 'lb': 10, 'ub': 20 }) prog.add_overall_temporal_constraint(ctype='controllable', lb=0.0, ub=5.0) return prog
def rmpyl_choice_risk(): prog = RMPyL() prog *= prog.decide( { 'name': 'transport-choice', 'domain': ['Bike', 'Car', 'Stay'], 'utility': [100, 70, 0] }, prog.observe( { 'name': 'travel', 'ctype': 'probabilistic', 'domain': ['Short', 'Long'], 'probability': [0.7, 0.3] }, Episode(action='(cab-ride-long)', duration={ 'ctype': 'uncontrollable_probabilistic', 'distribution': { 'type': 'uniform', 'lb': 5.0, 'ub': 11.0 } }), Episode(action='(cab-ride-short)', duration={ 'ctype': 'uncontrollable_probabilistic', 'distribution': { 'type': 'uniform', 'lb': 5.0, 'ub': 10.0 } })), Episode(action='(drive-car)', duration={ 'ctype': 'controllable', 'lb': 6.0, 'ub': 8.0 }), Episode(action='(stay)')) prog.add_overall_temporal_constraint(ctype='controllable', lb=0.0, ub=10.0) return prog
def rmpyl_low_risk(): prog = RMPyL() prog *= Episode(action='(cab-ride)', duration={ 'ctype': 'uncontrollable_probabilistic', 'distribution': { 'type': 'uniform', 'lb': 5.0, 'ub': 10.0 } }) prog.add_overall_temporal_constraint(ctype='controllable', lb=5.0, ub=9.9) return prog
def pick_and_place_block(prog, block, pick_loc, place_loc, manip, agent, dur_dict=None): """"Picks a block and places it somewhere.""" obj = block + 'Component' duration = dur_dict if dur_dict != None else { 'ctype': 'controllable', 'lb': 0.0, 'ub': float('inf') } return prog.sequence( say('Going to pick %s' % obj), Episode(action=('(pick %s %s %s %s)' % (obj, manip, pick_loc, agent)).lower(), duration=duration), Episode(action=('(place %s %s %s %s)' % (obj, manip, place_loc, agent)).lower(), duration=duration))
def rmpyl_breakfast(): """ Example from (Levine & Williams, ICAPS14). """ #Actions that Alice performs get_mug_ep = Episode(action='(get alice mug)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) get_glass_ep = Episode(action='(get alice glass)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) make_cofee_ep = Episode(action='(make-coffee alice)', duration={ 'ctype': 'controllable', 'lb': 3.0, 'ub': 5.0 }) pour_cofee_ep = Episode(action='(pour-coffee alice mug)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) pour_juice_glass = Episode(action='(pour-juice alice glass)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) get_bagel_ep = Episode(action='(get alice bagel)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) get_cereal_ep = Episode(action='(get alice cereal)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) toast_bagel_ep = Episode(action='(toast alice bagel)', duration={ 'ctype': 'controllable', 'lb': 3.0, 'ub': 5.0 }) add_cheese_bagel_ep = Episode(action='(add-cheese alice bagel)', duration={ 'ctype': 'controllable', 'lb': 1.0, 'ub': 2.0 }) mix_cereal_ep = Episode(action='(mix-cereal alice milk)', duration={ 'ctype': 'controllable', 'lb': 1.0, 'ub': 2.0 }) #Actions that the robot performs get_grounds_ep = Episode(action='(get grounds robot)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) get_juice_ep = Episode(action='(get juice robot)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) get_milk_ep = Episode(action='(get milk robot)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) get_cheese_ep = Episode(action='(get cheese robot)', duration={ 'ctype': 'controllable', 'lb': 0.5, 'ub': 1.0 }) prog = RMPyL() prog *= prog.sequence( prog.parallel( prog.observe( { 'name': 'observe-utensil', 'domain': ['Mug', 'Glass'], 'ctype': 'uncontrollable' }, get_mug_ep, get_glass_ep, id='observe-utensil-ep'), prog.decide( { 'name': 'choose-beverage-ingredient', 'domain': ['Grounds', 'Juice'], 'utility': [0, 0] }, get_grounds_ep, get_juice_ep, id='choose-beverage-ingredient-ep')), prog.observe( { 'name': 'observe-alice-drink', 'domain': ['Coffee', 'Juice'], 'ctype': 'uncontrollable' }, prog.sequence(make_cofee_ep, pour_cofee_ep), pour_juice_glass, id='observe-alice-drink-ep'), prog.parallel(prog.observe( { 'name': 'observe-food', 'domain': ['Bagel', 'Cereal'], 'ctype': 'uncontrollable' }, get_bagel_ep, get_cereal_ep, id='observe-food-ep'), prog.decide( { 'name': 'choose-food-ingredient', 'domain': ['Milk', 'Cheese'], 'utility': [0, 0] }, get_milk_ep, get_cheese_ep, id='choose-food-ingredient-ep'), id='parallel-food-ep'), prog.observe( { 'name': 'observe-alice-food', 'domain': ['Bagel', 'Cereal'], 'ctype': 'uncontrollable' }, prog.sequence(toast_bagel_ep, add_cheese_bagel_ep), mix_cereal_ep), id='breakfast-sequence') extra_tcs = [ TemporalConstraint( start=prog.episode_by_id('breakfast-sequence').start, end=prog.episode_by_id('observe-utensil-ep').start, ctype='controllable', lb=0.0, ub=0.0), TemporalConstraint( start=prog.episode_by_id('breakfast-sequence').start, end=prog.episode_by_id('choose-beverage-ingredient-ep').start, ctype='controllable', lb=0.2, ub=0.3), TemporalConstraint(start=prog.episode_by_id('parallel-food-ep').start, end=prog.episode_by_id('observe-food-ep').start, ctype='controllable', lb=0.0, ub=0.0), TemporalConstraint( start=prog.episode_by_id('parallel-food-ep').start, end=prog.episode_by_id('choose-food-ingredient-ep').start, ctype='controllable', lb=0.2, ub=0.3) ] for tc in extra_tcs: prog.add_temporal_constraint(tc) prog.add_overall_temporal_constraint(ctype='controllable', lb=0.0, ub=7.0) prog.simplify_temporal_constraints() return prog
def pick_and_place_block(prog,block,pick_loc,place_loc,manip,agent): """"Picks a block and places it somewhere.""" obj=block+'Component' return prog.sequence(say('Going to pick %s'%obj), Episode(action=('(pick %s %s %s %s)'%(obj,manip,pick_loc,agent)).lower()), Episode(action=('(place %s %s %s %s)'%(obj,manip,place_loc,agent)).lower()))
def say(text): """Final message to the human.""" return Episode(action=('(say \"%s\")'%text).lower())
halt_on_violation=False, verbose=1) #Searches for the optimal policy policy, explicit, performance = planner.search(b0) #Converts policy to graphical SVG format dot_policy = policy_to_dot(explicit, policy) dot_policy.write('flightgear_policy.svg', format='svg') #Converts optimal exploration policy into an RMPyL program exploration_policy = policy_to_rmpyl(explicit, policy) #The flight policy has the additional actions of taking off and landing. flight_policy = RMPyL(name='run()') flight_policy *= flight_policy.sequence(Episode(action='(takeoff plane)'), exploration_policy, Episode(action='(land plane)')) #Eliminates probabilistic choices from the policy, since Pike (in fact, the #Lisp tpn package) cannot properly handle them. for obs in flight_policy.observations: if obs.type == 'probabilistic': obs.type = 'uncontrollable' del obs.properties['probability'] #Converts the program to a TPN flight_policy.to_ptpn(filename='flightgear_rmpyl.tpn') # Writes control program to pickle file with open('flightgear_rmpyl.pickle', 'wb') as f:
def rmpyl_icaps14(): """ Example from (Santana & Williams, ICAPS14). """ prog = RMPyL() prog *= prog.decide( { 'name': 'transport-choice', 'domain': ['Bike', 'Car', 'Stay'], 'utility': [100, 70, 0] }, prog.observe( { 'name': 'slip', 'domain': [True, False], 'ctype': 'probabilistic', 'probability': [0.051, 1.0 - 0.051] }, prog.sequence( Episode(action='(ride-bike)', duration={ 'ctype': 'controllable', 'lb': 15, 'ub': 25 }), Episode(action='(change)', duration={ 'ctype': 'controllable', 'lb': 20, 'ub': 30 })), Episode(action='(ride-bike)', duration={ 'ctype': 'controllable', 'lb': 15, 'ub': 25 })), prog.observe( { 'name': 'accident', 'domain': [True, False], 'ctype': 'probabilistic', 'probability': [0.013, 1.0 - 0.013] }, prog.sequence( Episode(action='(tow-vehicle)', duration={ 'ctype': 'controllable', 'lb': 30, 'ub': 90 }), Episode(action='(cab-ride)', duration={ 'ctype': 'controllable', 'lb': 10, 'ub': 20 })), Episode(action='(drive)', duration={ 'ctype': 'controllable', 'lb': 10, 'ub': 20 })), Episode(action='(stay)')) prog.add_overall_temporal_constraint(ctype='controllable', lb=0.0, ub=30.0) return prog
def relay(self): """ Returns the episode representing the rover sending data back to a satellite. """ return Episode(duration={'ctype':'controllable','lb':5,'ub':30}, action='(relay %s)'%(self.name))
def stop(self): return Episode(action='(stop)')
def crash(self): return Episode(action='(' + self._name + '-crash)', terminal=True)