def rmpyl_nested_uav(): hello = UAV('hello') uav = UAV('uav') prog = RMPyL() prog.plan = prog.sequence( hello.scan(), uav.scan(), prog.decide( { 'name': 'UAV-choice', 'domain': ['Hello', 'UAV'], 'utility': [7, 5] }, prog.sequence( hello.fly(), prog.observe( { 'name': 'hello-success', 'domain': ['Success', 'Failure'], 'ctype': 'probabilistic', 'probability': [0.8, 0.2] }, prog.decide( { 'name': 'hello-assert-success', 'domain': ['Success'], 'utility': [10] }, hello.stop()), prog.decide( { 'name': 'hello-assert-failure', 'domain': ['Failure'], 'utility': [0] }, hello.stop()))), prog.sequence( uav.fly(), prog.observe( { 'name': 'uav-success', 'domain': ['Success', 'Failure'], 'ctype': 'probabilistic', 'probability': [0.95, 0.05] }, prog.decide( { 'name': 'uav-assert-success', 'domain': ['Success'], 'utility': [10] }, uav.stop()), prog.decide( { 'name': 'uav-assert-failure', 'domain': ['Failure'], 'utility': [0] }, uav.stop()))))) return prog
def rmpyl_original_verbose(hello,uav): """ Implementation of the original RMPL using a more verbose syntax and adding a chance constraint. ##### Original RMPL class UAV { value on; value off; primitive method fly() [3,10]; primitive method scan() [1,10]; } class Main { UAV helo; UAV uav; method run () { [0, 18] sequence { parallel { sequence { helo.scan(); helo.fly(); } sequence { uav.fly(); uav.scan(); } } choose { with reward: 5 {helo.fly();} with reward: 7 {uav.fly();} } } } } """ prog = RMPyL() prog.plan = prog.sequence( prog.parallel( prog.sequence( hello.scan(), hello.fly()), prog.sequence( uav.fly(), uav.scan())), prog.decide({'name':'UAV-choice','domain':['Hello','UAV'],'utility':[5,7]}, hello.fly(), uav.fly())) overall_tc = prog.add_overall_temporal_constraint(ctype='controllable',lb=0.0,ub=18.0) cc_time = ChanceConstraint(constraint_scope=[overall_tc],risk=0.1) prog.add_chance_constraint(cc_time) return prog
def rmpyl_single_episode(hello,uav): """Extremely simple plan composed of a single primitive episode.""" prog = RMPyL() prog.plan = uav.scan() healthy = StateVariable(name='Healthy', domain_dict={'type':'finite-discrete', 'domain':['True','False','Maybe']}) assig_sc = AssignmentStateConstraint(scope=[healthy],values=['True']) prog.add_overall_state_constraint(assig_sc) return prog
def pysat_planner(dom_file,prob_file,max_steps,duration_func): """ Uses PySAT as the planner. """ py_sat = PySAT(dom_file,prob_file,precompute_steps=max_steps,remove_static=True, write_dimacs=False,verbose=True) domain,problem,task = model_parser(dom_file,prob_file,remove_static=True) print('\n##### Determining optimal plan length!\n') start = time.time() min_steps = len(task.goals-task.initial_state) plans = py_sat.plan(task.initial_state,task.goals,time_steps=max_steps, find_shortest=True,min_steps=min_steps) elapsed = time.time()-start print('\n##### All solving took %.4f s'%(elapsed)) if len(plans)>0: plan = plans[0] print('\n##### Plan found!\n') for t,action in enumerate(plan): print('%d: %s'%(t,action)) prog = RMPyL(name='run()') if duration_func!=None: prog.plan = prog.sequence(*[Episode(start=Event(name='start-of-'+op), end=Event(name='end-of-'+op), action=op, duration=duration_func(op)) for op in plan]) else: prog.plan = prog.sequence(*[Episode(start=Event(name='start-of-'+op), end=Event(name='end-of-'+op), action=op) for op in plan]) else: prog = None return {'policy':None,'explicit':None,'performance':None,'rmpyl':prog}
def rmpyl_uav(): hello = UAV('hello') uav = UAV('uav') prog = RMPyL() # prog *= hello.fly() prog.plan = prog.sequence( hello.scan(), hello.fly(), uav.fly(), uav.scan(), prog.decide( { 'name': 'UAV-choice', 'domain': ['Hello', 'UAV'], 'utility': [5, 7] }, hello.fly(), uav.fly())) prog.add_overall_temporal_constraint(ctype='controllable', lb=0.0, ub=18.0) return prog
def rmpyl_parallel_uav(): hello = UAV('hello') uav = UAV('uav') prog = RMPyL() prog.plan = prog.parallel( prog.sequence( prog.decide( { 'name': 'hello-action', 'domain': ['Fly', 'Scan'], 'utility': [0, 1] }, hello.fly(), hello.scan()), prog.decide( { 'name': 'hello-action', 'domain': ['Fly', 'Scan'], 'utility': [0, 1] }, hello.fly(), hello.scan()), prog.decide( { 'name': 'hello-action', 'domain': ['Fly', 'Scan'], 'utility': [0, 1] }, hello.fly(), hello.scan())), prog.sequence( prog.decide( { 'name': 'uav-action', 'domain': ['Fly', 'Scan'], 'utility': [0, 1] }, uav.fly(), uav.scan()), prog.decide( { 'name': 'uav-action', 'domain': ['Fly', 'Scan'], 'utility': [0, 1] }, uav.fly(), uav.scan()), prog.decide( { 'name': 'uav-action', 'domain': ['Fly', 'Scan'], 'utility': [0, 1] }, uav.fly(), uav.scan()))) return prog
def rmpyl_uav(): hello = UAV('hello') uav = UAV('uav') prog = RMPyL() # prog *= hello.fly() prog.plan = prog.sequence( hello.scan(), hello.fly(), uav.fly(), uav.scan(), prog.decide({'name':'UAV-choice','domain':['Hello','UAV'], 'utility':[5,7]}, hello.fly(), uav.fly())) prog.add_overall_temporal_constraint(ctype='controllable',lb=0.0,ub=18.0) return prog
def rmpyl_nested_uav(): hello = UAV('hello') uav = UAV('uav') prog = RMPyL() prog.plan = prog.sequence( hello.scan(), uav.scan(), prog.decide( {'name':'UAV-choice','domain':['Hello','UAV'], 'utility':[7,5]}, prog.sequence( hello.fly(), prog.observe( {'name':'hello-success','domain':['Success','Failure'], 'ctype':'probabilistic','probability':[0.8,0.2]}, prog.decide( {'name':'hello-assert-success', 'domain':['Success'], 'utility':[10]}, hello.stop()), prog.decide( {'name':'hello-assert-failure', 'domain':['Failure'], 'utility':[0]}, hello.stop()))), prog.sequence( uav.fly(), prog.observe( {'name':'uav-success','domain':['Success','Failure'], 'ctype':'probabilistic','probability':[0.95,0.05]}, prog.decide( {'name':'uav-assert-success', 'domain':['Success'], 'utility':[10]}, uav.stop()), prog.decide( {'name':'uav-assert-failure', 'domain':['Failure'], 'utility':[0]}, uav.stop()))))) return prog
def _fake_plan_path(sites,start_site,goal_site,risk,velocities,duration_type,agent,**kwargs): """ Fakes a chance-constrained path from a start location to a goal location. Specific parameters are given as keyword arguments. """ start = sites[start_site]['coords'] goal = sites[goal_site]['coords'] line_dist = la.norm(goal-start) dist = line_dist*1.2 if risk<0.005 else line_dist*1.1 lb_duration = dist/velocities['max'] ub_duration = dist/velocities['avg'] if duration_type=='uncontrollable_bounded': duration_dict={'ctype':'uncontrollable_bounded', 'lb':lb_duration,'ub':ub_duration} elif duration_type=='uniform': duration_dict={'ctype':'uncontrollable_probabilistic', 'distribution':{'type':'uniform','lb':lb_duration,'ub':ub_duration}} elif duration_type=='gaussian': duration_dict={'ctype':'uncontrollable_probabilistic', 'distribution':{'type':'gaussian', 'mean':(lb_duration+ub_duration)/2.0, 'variance':((ub_duration-lb_duration)**2)/36.0}} elif duration_type=='no_constraint': duration_dict={'ctype':'controllable','lb':0.0,'ub':float('inf')} else: raise ValueError('Duration type %s currently not supported in Fake Planner.'%duration_type) path_episode = Episode(duration=duration_dict, action='(go-from-to %s %s %s)'%(agent,start_site,goal_site), distance=dist,**kwargs) path_episode.properties['distance']=dist path_episode.properties['start_coords']=start path_episode.properties['goal_coords']=goal prog_path = RMPyL(); prog_path.plan = path_episode return prog_path
def rmpyl_parallel_uav(): hello = UAV('hello') uav = UAV('uav') prog = RMPyL() prog.plan = prog.parallel( prog.sequence( prog.decide({'name':'hello-action','domain':['Fly','Scan'], 'utility':[0,1]}, hello.fly(), hello.scan()), prog.decide({'name':'hello-action','domain':['Fly','Scan'], 'utility':[0,1]}, hello.fly(), hello.scan()), prog.decide({'name':'hello-action','domain':['Fly','Scan'], 'utility':[0,1]}, hello.fly(), hello.scan()) ), prog.sequence( prog.decide({'name':'uav-action','domain':['Fly','Scan'], 'utility':[0,1]}, uav.fly(), uav.scan()), prog.decide({'name':'uav-action','domain':['Fly','Scan'], 'utility':[0,1]}, uav.fly(), uav.scan()), prog.decide({'name':'uav-action','domain':['Fly','Scan'], 'utility':[0,1]}, uav.fly(), uav.scan()) )) return prog
# print("---------plan: %d" % len(sat_plans)) if len(sat_plans)>0: plan = sat_plans[0] # get the first plan (default returns a list with one plan) for t,op in enumerate(plan): print('%d: %s'%(t,op)) elapsed = time.time()-start print('\n##### All solving took %.4f s'%(elapsed)) prog = RMPyL(name='run()') pddl_episodes = [Episode(id=make_episode_id(t,op), start=Event(name='start-of-%d-%s'%(t,op)), end=Event(name='end-of-%d-%s'%(t,op)), action=op, duration=rss_duration_model_func(op)) for t,op in enumerate(plan)] prog.plan = prog.sequence(*pddl_episodes) # prog.add_overall_temporal_constraint(ctype='controllable',lb=0.0,ub=2000.0) #Adds temporal window to the plan for t,op in enumerate(plan): bounds, tc_type = rss_time_window_model_func(op) for tc in time_window_constraints(tc_type,bounds,prog.first_event,prog.episode_by_id(make_episode_id(t,op))): prog.add_temporal_constraint(tc) #Dummy episodes that enable transmissions activation_episodes=[] activation_tcs=[] global_start=Event(name='global-start') for op_name,op_param_dict in time_windows['time_windows'].items(): for arg_set,window_dict in op_param_dict.items(): for ev_type,time_bound in window_dict.items():
start = time.time() sat_plans = py_sat.plan(task.initial_state, task.goals, time_steps=18) elapsed = time.time() - start print('\n##### All solving took %.4f s' % (elapsed)) if len(sat_plans) > 0: plan = sat_plans[0] print('\n##### Plan found!\n') for t, action in enumerate(plan): print('%d: %s' % (t, action)) prog = RMPyL(name='run()') prog.plan = prog.sequence(*[ Episode(start=Event(name='start-of-' + op), end=Event(name='end-of-' + op), action=op, duration=rss_duration_func(op)) for op in plan ]) prog.add_overall_temporal_constraint(ctype='controllable', lb=0.0, ub=2000.0) prog.to_ptpn(filename='rss_pysat_before_stnu_reform.tpn') paris = PARIS() risk_bound, sc_sched = paris.stnu_reformulation(prog, makespan=True, cc=0.001) if risk_bound != None: risk_bound = min(risk_bound, 1.0) print( '\nSuccessfully performed STNU reformulation with scheduling risk %f %%!'
def _fake_plan_path(sites, start_site, goal_site, risk, velocities, duration_type, agent, **kwargs): """ Fakes a chance-constrained path from a start location to a goal location. Specific parameters are given as keyword arguments. """ start = sites[start_site]['coords'] goal = sites[goal_site]['coords'] line_dist = la.norm(goal - start) dist = line_dist * 1.2 if risk < 0.005 else line_dist * 1.1 lb_duration = dist / velocities['max'] ub_duration = dist / velocities['avg'] if duration_type == 'uncontrollable_bounded': duration_dict = { 'ctype': 'uncontrollable_bounded', 'lb': lb_duration, 'ub': ub_duration } elif duration_type == 'uniform': duration_dict = { 'ctype': 'uncontrollable_probabilistic', 'distribution': { 'type': 'uniform', 'lb': lb_duration, 'ub': ub_duration } } elif duration_type == 'gaussian': duration_dict = { 'ctype': 'uncontrollable_probabilistic', 'distribution': { 'type': 'gaussian', 'mean': (lb_duration + ub_duration) / 2.0, 'variance': ((ub_duration - lb_duration)**2) / 36.0 } } elif duration_type == 'no_constraint': duration_dict = { 'ctype': 'controllable', 'lb': 0.0, 'ub': float('inf') } else: raise ValueError( 'Duration type %s currently not supported in Fake Planner.' % duration_type) path_episode = Episode(duration=duration_dict, action='(go-from-to %s %s %s)' % (agent, start_site, goal_site), distance=dist, **kwargs) path_episode.properties['distance'] = dist path_episode.properties['start_coords'] = start path_episode.properties['goal_coords'] = goal prog_path = RMPyL() prog_path.plan = path_episode return prog_path
for t, op in enumerate(plan): print('%d: %s' % (t, op)) elapsed = time.time() - start print('\n##### All solving took %.4f s' % (elapsed)) prog = RMPyL(name='run()') pddl_episodes = [ Episode(id=make_episode_id(t, op), start=Event(name='start-of-%d-%s' % (t, op)), end=Event(name='end-of-%d-%s' % (t, op)), action=op, duration=rss_duration_model_func(op)) for t, op in enumerate(plan) ] prog.plan = prog.sequence(*pddl_episodes) # prog.add_overall_temporal_constraint(ctype='controllable',lb=0.0,ub=2000.0) #Adds temporal window to the plan for t, op in enumerate(plan): bounds, tc_type = rss_time_window_model_func(op) for tc in time_window_constraints( tc_type, bounds, prog.first_event, prog.episode_by_id(make_episode_id(t, op))): prog.add_temporal_constraint(tc) #Dummy episodes that enable transmissions activation_episodes = [] activation_tcs = [] global_start = Event(name='global-start') for op_name, op_param_dict in time_windows['time_windows'].items():