def _paris_service_cb(self,req): """Handler of PARIS service requests.""" resp = PARISSrvResponse() tcs = ut.ros_pstnu_to_rmpyl_temporal_constraints(req.pstnu) paris = PARIS(req.gaussian_div,req.gaussian_optimize_partition, req.gaussian_lr,req.gaussian_tol,req.gaussian_max_iter) if paris.set_minimum_risk_strong_controllability(tcs): setup_success = True if req.makespan: rospy.loginfo('Setting up makespan optimization.') paris.set_makespan_optimization() if req.cc>=0.0: rospy.loginfo('Setting up chance-constrained optimization.') paris.set_chance_constraint(req.cc) rospy.loginfo('Calling PARIS to solve scheduling problem.') squeeze_dict,risk_bound,sc_schedule = paris.solve() else: setup_success = False squeeze_dict=None if not setup_success: rospy.logwarn('Failed to set up strong controllability problem.') if squeeze_dict != None: resp.success = True resp.schedule = ut.schedule_to_schedule_msg(sc_schedule) resp.squeezes = ut.squeeze_dict_to_squeeze_msgs(squeeze_dict) resp.risk_upper_bound = risk_bound rospy.loginfo('Strong schedule found!') else: resp.success = False rospy.logwarn('Failed to solve strong controllability problem.') resp.header.stamp = rospy.Time.now() #Records time when solution was obtained return resp
def check_cc_strong_controllability(tcs,example_name='',write_tpns=False, gaussian_approx='smart_piecewise',gaussian_div=5, gaussian_optimize_partition=False,gaussian_lr=0.05, gaussian_tol=1e-4,gaussian_max_iter=1000, cc=-1.0,makespan=False): """ Checks the chance-constrained strong controllability of a pSTN. """ pt = PyTemporal() start_time = time.time() paris = PARIS(gaussian_div,gaussian_optimize_partition,gaussian_lr, gaussian_tol,gaussian_max_iter) paris.init_model() if paris.set_minimum_risk_strong_controllability(tcs): if makespan: paris.set_makespan_optimization() if cc>=0.0: paris.set_chance_constraint(cc) squeeze_dict,risk_bound,sc_schedule = paris.solve() else: squeeze_dict=None if squeeze_dict!=None: risk_bound = min(risk_bound,1.0) #The bound must saturate at 1 (almost sure to fail) elapsed = time.time()-start_time print('\n***** '+example_name) if squeeze_dict!=None: print('Found SC reformulation in %.3f ms!'%(elapsed*1000.0)) print('These are the squeezings:') prob_success=1.0 for tc,tc_dict in squeeze_dict.items(): print('\t%s [%.1f,%.1f]->[%.1f,%.1f]:\tRisk=%.4f%%'%(tc.name,tc.lb,tc.ub,tc_dict['lb'],tc_dict['ub'],tc_dict['risk']*100.0)) prob_success*=(1.0-tc_dict['risk']) print('\nThis is the schedule:') for ev_name, ev_time in sc_schedule.items(): print('\t%s: %.2f s'%(ev_name,ev_time)) risk_indep = (1.0-prob_success) print('\nTotal scheduling risk (assuming independent stochastic durations): %.4f%%'%(risk_indep*100.0)) print('Linear risk bound: %.4f%%'%(risk_bound*100.0)) if risk_bound<risk_indep: raise ValueError('Boole\'s bound %f less than (independent) risk %f'%(risk_bound,risk_indep)) else: risk_gap = risk_bound-risk_indep print('Bound gap: %f%%'%((risk_gap)*100.0)) if write_tpns: sc_stnu=[] for tc in tcs: if tc.type=='controllable': sc_stnu.append(tc) else: if tc in squeeze_dict: lb = tc_dict['lb'] ub = tc_dict['ub'] else: lb = tc.lb ub = tc.ub sc_stnu.append(TemporalConstraint(start=tc.start,end=tc.end, ctype='uncontrollable_bounded', lb=lb, ub=ub)) prog = pt.to_rmpyl(sc_stnu) prog.to_ptpn(filename='paris_sc_%s.tpn'%(example_name)) else: prob_success=risk_bound=risk_gap=-1 print('Strongly controllable reformulation failed in %.3f ms...'%(elapsed*1000.0)) if write_tpns: prog = pt.to_rmpyl(tcs) prog.to_ptpn(filename='paris_pstn_%s.tpn'%(example_name)) return prob_success,elapsed,risk_bound,risk_gap