def main(): import config as c with tf.python_io.TFRecordWriter(c.tfrecord_file_name, c.tfrecord_options) as tfr_writer: ex_c = 0 episode = None for run_i in c.n_run: run_dir = os.path.join(c.results_dir, c.base_run_dir_fn(run_i)) object_file_name = os.path.join(run_dir, os.path.basename(c.dst_object_file_name)) abs_paths_file_name = os.path.join(run_dir, os.path.basename(c.project_output_dir), c.paths_file_name) #abs_paths_file_name = os.path.join(run_dir, c.paths_file_name) abs_simulation_info_file_name = os.path.join(run_dir, c.simulation_info_file_name) with open(abs_simulation_info_file_name) as infile: simulation_info = json.load(infile) while True: # start of episode if episode is None: episode = Episode(c.analysis_area, c.analysis_area_resolution, c.antenna_number, c.frequency, c.n_paths_to_tfrecord, simulation_info['cars_with_antenna']) else: try: episode.add_scene(object_file_name, abs_paths_file_name, simulation_info['scene_i']) # go to next scene if episode.cars_with_antenna != simulation_info['cars_with_antenna']: raise UnexpectedCarsWithAntennaChangeError('In run {}'.format(run_dir)) break # episode is over, write and start a new one except SceneNotInEpisodeSequenceError: tfr_writer.write(episode.to_example().SerializeToString()) ex_c += 1 episode = None raise Exception() print('wrote {} examples'.format(ex_c))
def main(): parser = argparse.ArgumentParser() parser.add_argument('-p', '--place-only', action='store_true', help='Run only the objects placement') parser.add_argument('-c', '--run-calcprop', action='store_true', help='Run using calcprop') parser.add_argument('-s', '--pause-each-run', action='store_true', help='Interactive run') parser.add_argument('-o', '--remove-results-dir', action='store_true', help='ONLY IF YOU KNOW WHAT YOU ARE DOING') args = parser.parse_args() insite_project = insite.InSiteProject(setup_path=c.setup_path, xml_path=c.dst_x3d_xml_path.replace( ' ', '\ '), output_dir=c.project_output_dir, calcprop_bin=c.calcprop_bin, wibatch_bin=c.wibatch_bin) with open(os.path.join(c.base_insite_project_path, "base.object")) as infile: objFile = objects.ObjectFile.from_file(infile) with open(os.path.join(c.base_insite_project_path, 'base.txrx')) as infile: txrxFile = txrx.TxRxFile.from_file(infile) x3d_xml_file = X3dXmlFile(c.base_x3d_xml_path) car = objects.RectangularPrism(*c.car_dimensions, material=c.car_material_id) car_structure = objects.Structure(name=c.car_structure_name) car_structure.add_sub_structures(car) car_structure.dimensions = car.dimensions antenna = txrxFile[c.antenna_points_name].location_list[0] try: shutil.copytree( c.base_insite_project_path, c.results_base_model_dir, ) except FileExistsError: shutil.rmtree(c.results_dir) shutil.copytree( c.base_insite_project_path, c.results_base_model_dir, ) if c.use_sumo: traci.start(c.sumo_cmd) scene_i = None episode_i = None for i in c.n_run: run_dir = os.path.join(c.results_dir, c.base_run_dir_fn(i)) #os.makedirs(run_dir) objFile.clear() if c.use_sumo: # when to start a new episode if scene_i is None or scene_i >= c.time_of_episode: if episode_i is None: episode_i = 0 else: episode_i += 1 scene_i = 0 # step time_between_episodes from the last one for count in range(c.time_between_episodes): traci.simulationStep() # ensure that there enough cars to place antennas while len(traci.vehicle.getIDList()) < c.n_antenna_per_episode: logging.error('not enough cars') traci.simulationStep() cars_with_antenna = np.random.choice(traci.vehicle.getIDList(), c.n_antenna_per_episode, replace=False) else: traci.simulationStep() structure_group, location = place_by_sumo( antenna, c.car_material_id, lane_boundary_dict=c.lane_boundary_dict, margin_dict=c.margin_dict, cars_with_antenna=cars_with_antenna) print(traci.simulation.getCurrentTime()) # no cars in the environment if location is None: logging.error( "all antennas are out of the simulation, aborting episode") scene_i = np.inf continue else: structure_group, location = place_on_line( c.line_origin, c.line_destination, c.line_dimension, c.car_distances, car_structure, antenna, c.antenna_origin) objFile.add_structure_groups(structure_group) objFile.write(c.dst_object_file_name) #shutil.copy(c.dst_object_file_name, run_dir) x3d_xml_file.add_vertice_list(location, c.dst_x3d_txrx_xpath) x3d_xml_file.write(c.dst_x3d_xml_path) #shutil.copy(c.dst_x3d_xml_path, run_dir) txrxFile[c.antenna_points_name].location_list[0] = location txrxFile.write(c.dst_txrx_file_name) #shutil.copy(c.dst_txrx_file_name, run_dir) if not args.place_only: insite_project.run_x3d(output_dir=c.project_output_dir) if not args.place_only and args.run_calcprop: insite_project.run_calcprop(output_dir=run_dir, delete_temp=True) shutil.copytree(c.base_insite_project_path, run_dir) with open(os.path.join(run_dir, c.simulation_info_file_name), 'w') as infofile: info_dict = dict( cars_with_antenna=list(cars_with_antenna), scene_i=scene_i, ) json.dump(info_dict, infofile) scene_i += 1 if args.pause_each_run: input('Enter to step') sys.stdin.readline() traci.close()
def main(): cache = True cache_file_name = 'plotbeans.npz' pos_matrix_array = None bean_array = None if cache: try: cache_file = np.load(cache_file_name) pos_matrix_array = cache_file['pos_matrix_array'] bean_array = cache_file['bean_array'] except FileNotFoundError: pass if pos_matrix_array is None: bean_array = np.zeros((20, 2)) pos_matrix_array = np.zeros((20, *c.position_matrix_shape)) for run_i in c.n_run: run_dir = os.path.join(c.results_dir, c.base_run_dir_fn(run_i)) object_file_name = os.path.join( run_dir, os.path.basename(c.dst_object_file_name)) abs_paths_file_name = os.path.join( run_dir, os.path.basename(c.project_output_dir), c.paths_file_name) pos_matrix, best_tx_rx = to_tfrecord(c.analysis_area, object_file_name, abs_paths_file_name, c.analysis_area_resolution, c.antenna_number, c.frequency) pos_matrix_array[run_i, :] = pos_matrix bean_array[run_i, :] = best_tx_rx if cache: np.savez(cache_file_name, pos_matrix_array=pos_matrix_array, bean_array=bean_array) with open('plotbeans.bin', 'wb') as outfile: outfile.write(pos_matrix_array.astype(np.float32)[:].tobytes()) outfile.write(bean_array.astype(np.float32)[:].tobytes()) bean_line = [] pos_matrix_full = np.zeros( (pos_matrix_array.shape[2], pos_matrix_array.shape[0] * (pos_matrix_array.shape[1] + 20))) pos_matrix_array = pos_matrix_array + 1 for run_i in range(pos_matrix_array.shape[0]): pos_matrix_full[:, (pos_matrix_array.shape[1] + 20) * run_i:(pos_matrix_array.shape[1]) * (run_i + 1) + run_i * 20] = pos_matrix_array[run_i].T best_tx_rx = bean_array[run_i] bean_line.append(best_tx_rx[0] * 16 + best_tx_rx[1]) #ax = plt.subplot(3, 10, 11 + run_i) #ax.imshow(pos_matrix.T, origin='lower') #bean_line = np.array(bean_line) #ax = plt.subplot(3,1,1) #ax.plot(bean_line) ax = plt.subplot(111) ax.imshow(pos_matrix_full, origin='lower') for run_i in range(pos_matrix_array.shape[0]): best_tx_rx = bean_array[run_i] l1, l2 = arrow_angle(*best_tx_rx) ax.quiver((pos_matrix_array.shape[1] + 20) * run_i + 25, 250, *l1, scale=28, width=0.004, color='b') ax.quiver((pos_matrix_array.shape[1] + 20) * run_i + 25, 250, *l2, scale=28, width=0.004, color='r') #plt.plot(*l1, '-xb') #plt.plot(*l2, '-or') #plt.xticks(range(0,20)) ax.set_yticklabels('') ax.set_xticklabels(['a', 'b']) plt.ylim(0, 320) plt.show()
EpisodeNotStartingFromZeroError from rwimodeling import objects from rwiparsing import P2mPaths from rwisimulation.datamodel import save5gmdata as fgdb last_simulation_info = None simulation_info = None session = fgdb.Session() sc_i = 0 ep_i = 0 episode = None for run_i in c.n_run: #for run_i in range(50, 100): run_dir = os.path.join(c.results_dir, c.base_run_dir_fn(run_i)) object_file_name = os.path.join(run_dir, os.path.basename(c.dst_object_file_name)) abs_paths_file_name = os.path.join(run_dir, os.path.basename(c.project_output_dir), c.paths_file_name) abs_simulation_info_file_name = os.path.join(run_dir, c.simulation_info_file_name) with open(abs_simulation_info_file_name) as infile: simulation_info = json.load(infile) # start of episode if simulation_info['scene_i'] == 0: ep_i += 1 if episode is not None: session.add(episode)
def main(): parser = argparse.ArgumentParser() parser.add_argument( '-p', '--place-only', action='store_true', help='Run only the objects placement and save files for ray-tracing') parser.add_argument( '-j', '--jump', action='store_true', help= 'Jumping runs that already have results (works only if utilized with the option \'-r\' )' ) parser.add_argument( '-r', '--ray-tracing-only', action='store_true', help='Run only ray-tracing with previoulsy generated files') parser.add_argument( '-c', '--run-calcprop', action='store_true', help='Ray-tracing with InSite calcprop instead of the default wibatch') parser.add_argument('-s', '--pause-each-run', action='store_true', help='Interactive run') parser.add_argument( '-o', '--remove-results-dir', action='store_true', help= 'ONLY IF YOU KNOW WHAT YOU ARE DOING: it will remove the whole results folder' ) parser.add_argument( '-m', '--mimo-only', action='store_true', help= 'Run only ray-tracing with native mimo from InSite previoulsy generated files' ) args = parser.parse_args() #check consistency of user input if c.use_fixed_receivers: if c.n_antenna_per_episode != 0: print( 'ERROR: if use_fixed_receivers=True, n_antenna_per_episode must be 0 but it is', c.n_antenna_per_episode) raise Exception() #setup_path=c.setup_path, xml_path=c.dst_x3d_xml_path.replace(' ', '\ ') #AK: now the constructor has fewer parameters insite_project = insite.InSiteProject(project_name='model', calcprop_bin=c.calcprop_bin, wibatch_bin=c.wibatch_bin) print('########## Start simulation #############################') if args.ray_tracing_only: if args.run_calcprop: print('Option -r is not compatible with -c') exit(-1) print( 'Will run only ray-tracing. I am assuming all files have been placed.' ) for i in c.n_run: run_dir = os.path.join(c.results_dir, c.base_run_dir_fn(i)) #Ray-tracing output folder (where InSite will store the results (Study Area name)). #They will be later copied to the corresponding output folder specified by results_dir project_output_dir = os.path.join( run_dir, c.insite_study_area_name) #output InSite folder p2mpaths_file = os.path.join( project_output_dir, c.insite_setup_name + '.paths.t001_01.r002.p2m') if not os.path.exists(p2mpaths_file) or args.remove_results_dir: xml_full_path = os.path.join( run_dir, c.dst_x3d_xml_file_name) #input InSite folder xml_full_path = xml_full_path.replace(' ', '\ ') insite_project.run_x3d(xml_full_path, project_output_dir) elif os.path.exists(p2mpaths_file) and args.jump: continue else: print("ERROR: " + p2mpaths_file + " already exists, aborting simulation!") raise Exception() print('Finished running ray-tracing') exit(1) if args.mimo_only: if args.run_calcprop: print('Option -r is not compatible with -c') exit(-1) print( 'Will run MIMO ray-tracing. I am assuming all files have been placed.' ) for i in c.n_run: run_dir = os.path.join(c.results_dir, c.base_run_dir_fn(i)) #Ray-tracing output folder (where InSite will store the results (Study Area name)). #They will be later copied to the corresponding output folder specified by results_dir project_output_dir = os.path.join( run_dir, c.insite_study_area_name) #output InSite folder db_file = os.path.join(project_output_dir, c.insite_setup_name + '.study.sqlite') if not os.path.exists(db_file) or args.remove_results_dir: xml_full_path = os.path.join( run_dir, c.dst_x3d_xml_file_name) #input InSite folder xml_full_path = xml_full_path.replace(' ', '\ ') insite_project.run_x3d(xml_full_path, project_output_dir) elif os.path.exists(p2mpaths_file) and args.jump: continue else: print("ERROR: " + db_file + " already exists, aborting simulation!") raise Exception() print('Finished running MIMO ray-tracing') exit(1) #copy files from initial (source folder) to results base folder try: shutil.copytree( c.base_insite_project_path, c.results_base_model_dir, ) except FileExistsError: if args.remove_results_dir: shutil.rmtree(c.results_dir) print('Removed folder', c.results_dir) shutil.copytree( c.base_insite_project_path, c.results_base_model_dir, ) else: print('### ERROR: folder / file exists:', c.results_base_model_dir) raise FileExistsError print('Copied folder ', c.base_insite_project_path, 'into', c.results_base_model_dir) #open InSite files that are used as the base to create each new scene / simulation with open(c.base_object_file_name) as infile: objFile = objects.ObjectFile.from_file(infile) print('Opened file with objects:', c.base_object_file_name) with open(c.base_txrx_file_name) as infile: txrxFile = txrx.TxRxFile.from_file(infile) print('Opened file with transmitters and receivers:', c.base_txrx_file_name) if c.insite_version == '3.3': x3d_xml_file = X3dXmlFile3_3(c.base_x3d_xml_path) else: x3d_xml_file = X3dXmlFile(c.base_x3d_xml_path) print('Opened file with InSite XML:', c.base_x3d_xml_path) #AK-TODO document and comment the methods below. car = objects.RectangularPrism(*c.car_dimensions, material=c.car_material_id) car_structure = objects.Structure( name=c.car_structure_name ) #AK-TODO what is the role of c.car_structure_name ? car_structure.add_sub_structures(car) car_structure.dimensions = car.dimensions antenna = txrxFile[c.antenna_points_name].location_list[0] if c.use_sumo: print('Starting SUMO Traci') traci.start(c.sumo_cmd) scene_i = None episode_i = None # Trick to start simulations from a given run as it was started from 0 if c.n_run[0] != 0: tmp_var = 0 while (tmp_var < int(c.n_run[0])): if c.use_sumo: # when to start a new episode if scene_i is None or scene_i >= c.time_of_episode: #first scene of an episode if episode_i is None: episode_i = 0 else: episode_i += 1 scene_i = 0 # step time_between_episodes from the last one for count in range(c.time_between_episodes): traci.simulationStep() traci_vehicle_IDList = traci.vehicle.getIDList() # Filter list to have only drones if c.drone_simulation: traci_vehicle_IDList = onlyDronesList( traci.vehicle.getIDList()) while len(traci_vehicle_IDList) < c.n_antenna_per_episode: traci_vehicle_IDList = traci.vehicle.getIDList() if c.drone_simulation: traci_vehicle_IDList = onlyDronesList( traci.vehicle.getIDList()) logging.warning('not enough vehicles at time ' + str(traci.simulation.getCurrentTime())) traci.simulationStep() cars_with_antenna = np.random.choice( traci_vehicle_IDList, c.n_antenna_per_episode, replace=False) else: traci.simulationStep() scene_i += 1 #update scene counter tmp_var += 1 print('Jump until the step ' + str(c.n_run[0]) + ': ' + str(int((tmp_var / c.n_run[0]) * 100)) + '%') count_nar = 0 # Number of Runs without cars with antenna while mobile for i in c.n_run: run_dir = os.path.join(c.results_dir, c.base_run_dir_fn(i - count_nar)) #Ray-tracing output folder (where InSite will store the results (Study Area name)). #They will be later copied to the corresponding output folder specified by results_dir project_output_dir = os.path.join( run_dir, c.insite_study_area_name) #output InSite folder #Disabled below because the paths will be created later on by shutil.copytree #and shutil.copytree does not support folders that already exist #if not os.path.exists(run_dir): #os.makedirs(run_dir) objFile.clear() #if it's the beginning of the episode, the code searches for a minimium number of cars. After the #episode starts, then it does not do that. But it does not simulate scenarios without vehicles if c.use_sumo: # when to start a new episode if scene_i is None or scene_i >= c.time_of_episode: #first scene of an episode if episode_i is None: episode_i = 0 else: episode_i += 1 scene_i = 0 # step time_between_episodes from the last one for count in range( c.time_between_episodes ): #AK-TODO should rename it and avoid calling "time" traci.simulationStep() if c.use_fixed_receivers: cars_with_antenna = [] else: # ensure that there enough cars to place antennas. If use_fixed_receivers, then wait to have at least # one vehicle traci_vehicle_IDList = traci.vehicle.getIDList() # Filter list to have only drones if c.drone_simulation: traci_vehicle_IDList = onlyDronesList( traci.vehicle.getIDList()) while len(traci_vehicle_IDList) < c.n_antenna_per_episode: traci_vehicle_IDList = traci.vehicle.getIDList() if c.drone_simulation: traci_vehicle_IDList = onlyDronesList( traci.vehicle.getIDList()) logging.warning('not enough vehicles at time ' + str(traci.simulation.getCurrentTime())) traci.simulationStep() cars_with_antenna = np.random.choice( traci_vehicle_IDList, c.n_antenna_per_episode, replace=False) else: traci.simulationStep() structure_group, location, str_vehicles = place_by_sumo( antenna, c.car_material_id, lane_boundary_dict=c.lane_boundary_dict, cars_with_antenna=cars_with_antenna, use_fixed_receivers=c.use_fixed_receivers, use_pedestrians=c.use_pedestrians) #if location is None: #there are not cars with antennas in this episode (all have left) # no vehicles in the environment (not only the ones without antennas, but no vehicles at all) if traci.vehicle.getIDList() is None: logging.warning("No vehicles in scene " + str(scene_i) + " time " + str(traci.simulation.getCurrentTime())) os.makedirs( run_dir + '_novehicles' ) #create an empty folder to "indicate" the situation #save SUMO information for this scene as text CSV file #sumoOutputInfoFileName = os.path.join(run_dir,'sumoOutputInfoFileName_novehicles.txt') #writeSUMOInfoIntoFile(sumoOutputInfoFileName, episode_i, scene_i, c.lane_boundary_dict, cars_with_antenna) scene_i += 1 #update scene counter continue if location is None: #there are not cars with antennas in this episode (all have left) if not c.use_fixed_receivers: count_nar = count_nar + 1 #abort, there is not reason to continue given that there will be no receivers along the whole episode logging.warning("No vehicles with antennas in scene " + str(scene_i) + " time " + str(traci.simulation.getCurrentTime())) os.makedirs( run_dir + '_noAntennaVehicles' ) #create an empty folder to "indicate" the situation scene_i = np.Infinity #update scene counter continue else: #in case we should not use SUMO to position vehicles, then get a fixed position structure_group, location = place_on_line( c.line_origin, c.line_destination, c.line_dimension, c.car_distances, car_structure, antenna, c.antenna_origin) #Prepare the files for the input folder, where InSite will find them to execute the simulation #(obs: now InSite is writing directly to the output folder) shutil.copytree(c.base_insite_project_path, run_dir) print('Copied', c.base_insite_project_path, 'into', run_dir) #Writing to the final run folder objFile.add_structure_groups(structure_group) dst_object_full_path = os.path.join(run_dir, c.dst_object_file_name) objFile.write(dst_object_full_path) #write new model of vehicles to the final folder if c.use_vehicles_template: dst_new_object_full_path = os.path.join( run_dir, c.insite_vehicles_name_model + '.object') f_dst_new_object = open(dst_new_object_full_path, 'w') f_dst_new_object.write(str_vehicles) f_dst_new_object.close() #get name of XML xml_full_path = os.path.join( run_dir, c.dst_x3d_xml_file_name) #input InSite folder xml_full_path = xml_full_path.replace(' ', '\ ') if not c.use_fixed_receivers: #Marcus' workaround if not args.mimo_only: #Ailton workaround x3d_xml_file.add_vertice_list(location, c.dst_x3d_txrx_xpath) x3d_xml_file.write(xml_full_path) txrxFile[c.antenna_points_name].location_list[0] = location # txrx modified in the RWI project dst_txrx_full_path = os.path.join(run_dir, c.dst_txrx_file_name) txrxFile.write(dst_txrx_full_path) else: x3d_xml_file.add_vertice_list(location, c.dst_x3d_txrx_xpath) x3d_xml_file.write(xml_full_path) txrxFile[c.antenna_points_name].location_list[0] = location # txrx modified in the RWI project dst_txrx_full_path = os.path.join(run_dir, c.dst_txrx_file_name) txrxFile.write(dst_txrx_full_path) #check if we should run ray-tracing if not args.place_only: if args.run_calcprop: #AK-TODO: Need to fix run_calcprop in insite.py: should not copy unless necessary (need to check) insite_project.run_calcprop(output_dir=project_output_dir, delete_temp=True) else: insite_project.run_x3d(xml_full_path, project_output_dir) with open(os.path.join(run_dir, c.simulation_info_file_name), 'w') as infofile: #if c.use_fixed_receivers: #AK-TODO take in account fixed receivers # listToBeSaved = list('only_fixed_receivers') #else: listToBeSaved = list(cars_with_antenna) info_dict = dict( cars_with_antenna=listToBeSaved, scene_i=scene_i, ) json.dump(info_dict, infofile) #write JSON infofile #save SUMO information for this scene as text CSV file sumoOutputInfoFileName = os.path.join(run_dir, 'sumoOutputInfoFileName.txt') writeSUMOInfoIntoFile(sumoOutputInfoFileName, episode_i, scene_i, c.lane_boundary_dict, cars_with_antenna, c.use_fixed_receivers, c.use_pedestrians) scene_i += 1 #update scene counter if args.pause_each_run: input('Enter to step') sys.stdin.readline() traci.close()