def simulator_factory( config_dict, benchmark_os_path, property_checker, plt, plant_pvt_init_data, parallel=False, test_params=None, sim_args=None ): # ##!!##logger.debug('requested simulator creation') sim_type = config_dict['plant_description'] if sim_type == 'matlab': logger.info('creating matlab simulator') # get the m_file's path m_file_path = config_dict['plant_path'] abs_m_file_path = fp.construct_path(m_file_path, benchmark_os_path) if fp.validate_file_names([abs_m_file_path]): # return MatlabSim(m_file_path, benchmark_os_path, parallel) return MEngPy(m_file_path, benchmark_os_path, parallel, sim_args) else: raise err.FileNotFound('file does not exist: ' + m_file_path) elif sim_type == 'simulink': return SimulinkSim() elif sim_type == 'python': logger.info('creating Native Python simulator') # get the file path python_file_path = config_dict['plant_path'] #(module_name, file_ext) = python_file_path.split('.') (module_name, file_ext) = fp.split_filename_ext(python_file_path) if file_ext != 'py': raise err.Fatal('Python file extension py expected, found: {}'.format(file_ext)) module_path = fp.construct_path(python_file_path, benchmark_os_path) if fp.validate_file_names([module_path]): return NativeSim(module_name, module_path, property_checker, plt, plant_pvt_init_data, parallel) else: raise err.FileNotFound('file does not exist: ' + python_file_path) elif sim_type == 'test': return TestSim(test_params) else: raise err.Fatal('unknown sim type : {}'.format(sim_type))
def parse(file_path, plant_pvt_init_data): test_des_file = file_path path = fp.get_abs_base_path(file_path) sys_name = fp.split_filename_ext(fp.get_file_name_from_path(file_path))[0] test_dir = fp.get_abs_base_path(test_des_file) SYS.path.append(test_dir) try: sut = imp.load_source('test_des', test_des_file) except IOError as e: print('File not found: {}'.format(test_des_file)) raise e sut.plant_pvt_init_data = plant_pvt_init_data try: assert (len(sut.initial_set) == 2) assert (len(sut.error_set) == 2) assert (len(sut.ci) == 2) assert (len(sut.pi) == 2) assert (len(sut.ci[0]) == len(sut.ci[1])) assert (len(sut.pi[0]) == len(sut.pi[1])) assert (len(sut.initial_set[0]) == len(sut.initial_set[1])) assert (len(sut.error_set[0]) == len(sut.error_set[1])) assert (len(sut.initial_set[0]) == len(sut.error_set[0])) assert (len(sut.initial_set[0]) == len(sut.grid_eps)) num_dims = NumDims( si=len(sut.initial_controller_integer_state), sf=len(sut.initial_controller_float_state), s=len(sut.initial_controller_integer_state) + len(sut.initial_controller_float_state), x=len(sut.initial_set[0]), u=sut.num_control_inputs, ci=len(sut.ci[0]), pi=len(sut.pi[0]), d=len(sut.initial_discrete_state), pvt=0, ) if not hasattr(sut, 'ci_grid_eps') or len(sut.ci_grid_eps) == 0: sut.ci_grid_eps = [] # if hasattr(sut, 'ci_grid_eps'): # if len(sut.ci_grid_eps) == 0: # sut.ci_grid_eps = None # else: # sut.ci_grid_eps = None if not hasattr(sut, 'pi_grid_eps') or len(sut.pi_grid_eps) == 0: sut.pi_grid_eps = [] # if hasattr(sut, 'pi_grid_eps'): # if len(sut.pi_grid_eps) == 0: # sut.pi_grid_eps = None # else: # sut.pi_grid_eps = None if num_dims.ci == 0: ci = None else: ci = cns.IntervalCons(np.array(sut.ci[0]), np.array(sut.ci[1])) if num_dims.pi == 0: pi = None else: pi = cns.IntervalCons(np.array(sut.pi[0]), np.array(sut.pi[1])) assert ((pi is None and sut.pi_grid_eps == []) or (pi is not None and sut.pi_grid_eps != [])) assert ((ci is None and sut.ci_grid_eps == []) or (ci is not None and sut.ci_grid_eps != [])) if pi is not None: assert (len(sut.pi[0]) == len(sut.pi_grid_eps)) if ci is not None: assert (len(sut.ci[0]) == len(sut.ci_grid_eps)) plant_config_dict = { 'plant_description': sut.plant_description, 'plant_path': sut.plant_path, #'refinement_factor': sut.refinement_factor, 'eps': np.array(sut.grid_eps, dtype=float), #'pi_eps': np.array(sut.pi_grid_eps, dtype=float), 'num_samples': int(sut.num_samples), 'delta_t': float(sut.delta_t), #'type': 'string', 'type': 'value', } #print(plant_config_dict) #for k, d in plant_config_dict.iteritems(): # plant_config_dict[k] = str(d) # TODO: fix the list hack, once the controller ifc matures init_cons = cns.IntervalCons(np.array(sut.initial_set[0]), np.array(sut.initial_set[1])) init_cons_list = [init_cons] final_cons = cns.IntervalCons(np.array(sut.error_set[0]), np.array(sut.error_set[1])) T = sut.T delta_t = sut.delta_t if sut.controller_path is not None: controller_object_str = fp.split_filename_ext( sut.controller_path)[0] controller_path_dir_path = fp.construct_path( sut.controller_path_dir_path, path) else: controller_object_str = None controller_path_dir_path = None initial_discrete_state = sut.initial_discrete_state initial_controller_state = (sut.initial_controller_integer_state + sut.initial_controller_float_state) MAX_ITER = sut.MAX_ITER num_segments = int(np.ceil(T / delta_t)) controller_path = sut.controller_path plant_pvt_init_data = sut.plant_pvt_init_data min_smt_sample_dist = sut.min_smt_sample_dist ci_grid_eps = sut.ci_grid_eps pi_grid_eps = sut.pi_grid_eps #num_sim_samples = sut.num_sim_samples #METHOD = sut.METHOD #symbolic_analyzer = sut.symbolic_analyzer #plot = sut.plot #MODE = sut.MODE #opts = Options(plot, MODE, num_sim_samples, METHOD, symbolic_analyzer) except AttributeError as e: raise MissingSystemDefError(e) # ROI is not mandatory and defaults to xi \in (-inf, inf) try: ROI = cns.IntervalCons(*sut.ROI) assert (ROI.dim == num_dims.x) except AttributeError: ROI = cns.top2ic(num_dims.x) sys = System(sys_name, controller_path, num_dims, plant_config_dict, delta_t, controller_path_dir_path, controller_object_str, path, plant_pvt_init_data, min_smt_sample_dist, ci_grid_eps, pi_grid_eps) prop = Property(T, init_cons_list, init_cons, final_cons, ci, pi, initial_discrete_state, initial_controller_state, MAX_ITER, num_segments, ROI) print('system loaded...') return sys, prop
def parse(file_path, plant_pvt_init_data): test_des_file = file_path path = fp.get_abs_base_path(file_path) sys_name = fp.split_filename_ext( fp.get_file_name_from_path( file_path) )[0] test_dir = fp.get_abs_base_path(test_des_file) SYS.path.append(test_dir) try: sut = imp.load_source('test_des', test_des_file) except IOError as e: print('File not found: {}'.format(test_des_file)) raise e sut.plant_pvt_init_data = plant_pvt_init_data try: assert(len(sut.initial_set) == 2) assert(len(sut.error_set) == 2) assert(len(sut.ci) == 2) assert(len(sut.pi) == 2) assert(len(sut.ci[0]) == len(sut.ci[1])) assert(len(sut.pi[0]) == len(sut.pi[1])) assert(len(sut.initial_set[0]) == len(sut.initial_set[1])) assert(len(sut.error_set[0]) == len(sut.error_set[1])) assert(len(sut.initial_set[0]) == len(sut.error_set[0])) assert(len(sut.initial_set[0]) == len(sut.grid_eps)) num_dims = NumDims( si=len(sut.initial_controller_integer_state), sf=len(sut.initial_controller_float_state), s=len(sut.initial_controller_integer_state)+len(sut.initial_controller_float_state), x=len(sut.initial_set[0]), u=sut.num_control_inputs, ci=len(sut.ci[0]), pi=len(sut.pi[0]), d=len(sut.initial_discrete_state), pvt=0, ) if not hasattr(sut, 'ci_grid_eps') or len(sut.ci_grid_eps) == 0: sut.ci_grid_eps = [] # if hasattr(sut, 'ci_grid_eps'): # if len(sut.ci_grid_eps) == 0: # sut.ci_grid_eps = None # else: # sut.ci_grid_eps = None if not hasattr(sut, 'pi_grid_eps') or len(sut.pi_grid_eps) == 0: sut.pi_grid_eps = [] # if hasattr(sut, 'pi_grid_eps'): # if len(sut.pi_grid_eps) == 0: # sut.pi_grid_eps = None # else: # sut.pi_grid_eps = None if num_dims.ci == 0: ci = None else: ci = cns.IntervalCons(np.array(sut.ci[0]), np.array(sut.ci[1])) if num_dims.pi == 0: pi = None else: pi = cns.IntervalCons(np.array(sut.pi[0]), np.array(sut.pi[1])) assert((pi is None and sut.pi_grid_eps == []) or (pi is not None and sut.pi_grid_eps != [])) assert((ci is None and sut.ci_grid_eps == []) or (ci is not None and sut.ci_grid_eps != [])) if pi is not None: assert(len(sut.pi[0]) == len(sut.pi_grid_eps)) if ci is not None: assert(len(sut.ci[0]) == len(sut.ci_grid_eps)) plant_config_dict = {'plant_description': sut.plant_description, 'plant_path': sut.plant_path, #'refinement_factor': sut.refinement_factor, 'eps': np.array(sut.grid_eps, dtype=float), #'pi_eps': np.array(sut.pi_grid_eps, dtype=float), 'num_samples': int(sut.num_samples), 'delta_t': float(sut.delta_t), #'type': 'string', 'type': 'value', } #print(plant_config_dict) #for k, d in plant_config_dict.iteritems(): # plant_config_dict[k] = str(d) # TODO: fix the list hack, once the controller ifc matures init_cons = cns.IntervalCons(np.array(sut.initial_set[0]), np.array(sut.initial_set[1])) init_cons_list = [init_cons] final_cons = cns.IntervalCons(np.array(sut.error_set[0]), np.array(sut.error_set[1])) T = sut.T delta_t = sut.delta_t if sut.controller_path is not None: controller_object_str = fp.split_filename_ext(sut.controller_path)[0] controller_path_dir_path = fp.construct_path(sut.controller_path_dir_path, path) else: controller_object_str = None controller_path_dir_path = None initial_discrete_state = sut.initial_discrete_state initial_controller_state = (sut.initial_controller_integer_state + sut.initial_controller_float_state) MAX_ITER = sut.MAX_ITER num_segments = int(np.ceil(T / delta_t)) controller_path = sut.controller_path plant_pvt_init_data = sut.plant_pvt_init_data min_smt_sample_dist = sut.min_smt_sample_dist ci_grid_eps = sut.ci_grid_eps pi_grid_eps = sut.pi_grid_eps #num_sim_samples = sut.num_sim_samples #METHOD = sut.METHOD #symbolic_analyzer = sut.symbolic_analyzer #plot = sut.plot #MODE = sut.MODE #opts = Options(plot, MODE, num_sim_samples, METHOD, symbolic_analyzer) except AttributeError as e: raise MissingSystemDefError(e) sys = System(sys_name, controller_path, num_dims, plant_config_dict, delta_t, controller_path_dir_path, controller_object_str, path, plant_pvt_init_data, min_smt_sample_dist, ci_grid_eps, pi_grid_eps) prop = Property(T, init_cons_list, init_cons, final_cons, ci, pi, initial_discrete_state, initial_controller_state, MAX_ITER, num_segments) print('system loaded...') return sys, prop