def __init__( self, netfile, period, binomial, start_time=0, end_time=3600, seed=None, thru_only=True, trip_attrib=('departLane="best" departSpeed="max" ' 'departPos="random_free" ' 'speedFactor="normc(1,0.1,0.2,2)"'), ): self.netfile = netfile self.period = period # lower period means more cars self.binomial = binomial self.start_time = start_time self.end_time = end_time self.seed = seed self.thru_only = thru_only self.trip_attrib = trip_attrib self.net_dir = get_net_dir(netfile) self.net_name = get_net_name(netfile)
def define_tls_output_file( netfile, tls_subset=None, output_data_dir=None, output_file_name=None, config_dir=None, addl_file_name='tls_output.add.xml', ): if config_dir is None: config_dir = get_net_dir(netfile) if output_data_dir is None: output_data_dir = os.path.join(config_dir, 'output') if output_file_name is None: net_name = get_net_name(netfile) output_file_name = '{}_tls_output.xml'.format(net_name) output_file = os.path.join(output_data_dir, output_file_name) addl_file = os.path.join(config_dir, addl_file_name) relative_output_filename = os.path.relpath(output_file, os.path.dirname(addl_file)) tls_addl = sumolib.xml.create_document('additional') net = sumolib.net.readNet(netfile) for tls in net.getTrafficLights(): tls_xml_element = tls_addl.addChild('timedEvent') tls_xml_element.setAttribute('type', 'SaveTLSSwitchTimes') tls_xml_element.setAttribute('source', tls.getID()) tls_xml_element.setAttribute('dest', relative_output_filename) tls_addl_file_fid = open(os.path.realpath(addl_file), 'w') tls_addl_file_fid.write(tls_addl.toXML()) tls_addl_file_fid.close() if not os.path.exists(os.path.dirname(output_file)): os.makedirs(os.path.dirname(output_file)) return os.path.realpath(addl_file)
def generate_detector_set(netfile, detector_type, distance_to_tls, detector_def_file=None, detector_output_file=None, detector_length=None, frequency=60, per_detector_output_files=True, verbose=True): if detector_type not in ['e1', 'e2']: raise ValueError('Unknown detector type: {}'.format(detector_type)) if detector_length is not None and not ( len(distance_to_tls) == len(detector_length) or len(distance_to_tls) == 1 or len(detector_length) == 1): raise ValueError( ('Input size mismatch: len(distance_to_tls) = {} ' 'and len(detector_length) = {}').format(len(distance_to_tls), len(detector_length))) default_net_config_dir = get_net_dir(netfile) net_name = get_net_name(netfile) default_output_data_dir = os.path.join(default_net_config_dir, 'output') if detector_def_file is None: detector_def_file = os.path.join( default_net_config_dir, '{}_{}.add.xml'.format(net_name, detector_type)) if detector_output_file is None and per_detector_output_files == False: detector_output_file = os.path.join( default_output_data_dir, '{}_{}_output.xml'.format(net_name, detector_type)) relative_output_filename = os.path.relpath( detector_output_file, os.path.dirname(detector_def_file)) detector_tag = detector_type_tag_dict[detector_type] detectors_xml = sumolib.xml.create_document("additional") lanes_with_detectors = set() net = sumolib.net.readNet(netfile) for tls in net.getTrafficLights(): for connection in tls.getConnections(): lane = connection[0] lane_length = lane.getLength() lane_id = lane.getID() if verbose: logger.debug("Creating detector for lane %s", str(lane_id)) if lane_id in lanes_with_detectors: if verbose: logger.debug("Detectors for lane %s already generated", str(lane_id)) continue lanes_with_detectors.add(lane_id) for i, distance in enumerate(distance_to_tls): detector_xml = detectors_xml.addChild(detector_tag) detector_xml.setAttribute("freq", str(frequency)) detector_xml.setAttribute("friendlyPos", "x") detector_xml.setAttribute( "id", "{}_{}_{}".format(detector_type, lane_id, i).replace('/', '-')) if per_detector_output_files == True: detector_output_file = os.path.join( default_output_data_dir, '{}_{}_{}_{}_output.xml'.format( net_name, detector_type, lane_id, i).replace('/', '-')) relative_output_filename = os.path.relpath( detector_output_file, os.path.dirname(detector_def_file)) detector_xml.setAttribute("file", relative_output_filename) detector_xml.setAttribute("lane", str(lane_id)) if detector_type == 'e2': final_detector_length = adjust_detector_length( detector_length[i], distance, lane_length) detector_xml.setAttribute("length", str(final_detector_length)) else: final_detector_length = 0 final_detector_position = adjust_detector_position( final_detector_length, distance, lane_length) detector_xml.setAttribute("pos", str(final_detector_position)) detector_file = open(os.path.realpath(detector_def_file), 'w') detector_file.write(detectors_xml.toXML()) detector_file.close() if not os.path.exists(os.path.dirname(detector_output_file)): os.makedirs(os.path.dirname(detector_output_file)) return (os.path.realpath(detector_def_file), os.path.realpath(detector_output_file))