def RSELoad(self, filename, CV): """ Reads in RSE file :param filename: The RSE filename :param CV: Dictionary of control file inputs :return: A list of errors if any occurred """ errors = [] #loading file if CV.Control["OutputLevel"] > 0: logger.info("Loading RSE file: %s" % (filename)) with open(filename, 'r') as f: header = f.readline() for line in f.readlines(): try: RSEinfo = line.split(',') RSE = {} RSE["name"] = RSEinfo[0] RSE["x"] = float(RSEinfo[1]) * self.unit_conversion RSE["y"] = float(RSEinfo[2]) * self.unit_conversion RSE["latency"] = 0.0 RSE["loss_rate"] = 0.0 if len(RSEinfo) > 3: RSE["latency"] = int(RSEinfo[3]) if len(RSEinfo) > 4: RSE["loss_rate"] = int(RSEinfo[4]) self.RSEList[RSEinfo[0]] = RSE except: errors.append("Error Reading in RSE File line: %s" % line) if CV.Control["OutputLevel"] > 0: logger.info("Number of RSE = %s" % (len(self.RSEList))) return errors
def AAPIFinish(): global enabled if enabled: global CF global Algorithm global LastTP logger.info('End Run') if len(Algorithm.BSM.BSM_list) > 0 : Algorithm.BSM.Write(clear_buffer=True) if len(Algorithm.CAM.CAM_list) > 0 : Algorithm.CAM.Write(clear_all=True) if len(Algorithm.PDM.PDM_list) > 0 : Algorithm.PDM.Write(clear_buffer=True, LastTP = LastTP) if len(Algorithm.SPOT.Travelmsgs) > 0: Algorithm.SPOT.Write(clear_all=True) if CF.Control["OutputLevel"] > 0: Algorithm.tbl.list_veh_counts() if CF.Control["OutputLevel"] > 0: end_time = time.time() logger.info("End time %s (%f)" % (time.strftime('%X', time.localtime(end_time)), (end_time - program_st) )) logger.info("************ End Program *******************") del Algorithm return 0
def SPOTLoad(self, filename, CV): """ Reads in SPOT RSE file :param filename: The SPOT RSE filename :param CV: Dictionary of control file inputs :return: A list of errors if any occurred """ errors = [] #loading file if CV.Control["OutputLevel"] > 0: logger.info("Loading SPOT file: %s" % (filename)) with open(filename, 'r') as f: header = f.readline() for line in f.readlines(): try: SPOTinfo = line.split(',') SPOT = {} SPOT["name"] = SPOTinfo[0] SPOT["x"] = float(SPOTinfo[1]) * self.unit_conversion SPOT["y"] = float(SPOTinfo[2]) * self.unit_conversion self.SPOTList[SPOTinfo[0]] = SPOT # Add the entry to the master dictionary except: errors.append("Error Reading in SPOT File line: %s" % line) if CV.Control["OutputLevel"] > 0: logger.info("Number of SPOT devices = %s" % (len(self.SPOTList))) return errors
def Load_Strategy(self): """ Load and checks strategy file values """ if self.control_values['OutputLevel'][0] >= 1: logger.info("Loading strategy_values File %s" % (self.control_values["StrategyFile"][0])) try: tree = ET.parse(self.control_values["StrategyFile"][0]) root = tree.getroot() except: if self.control_file != 'not a file.xml': logger.info( "Error: cannot find or invalid format for strategy_values file %s" % self.control_values["StrategyFile"][0]) print raise for key, value in self.strategy_values.iteritems(): if root.find(value[4]) != None: if value[3] == 'int': self.strategy_values[key][0], self.strategy_values[key][ 2] = self.int_check(root.find(value[4]).text, key) elif value[3] == 'file': self.strategy_values[key][0] = self.file_check( root.find(value[4]).text) elif value[3] == 'float': self.strategy_values[key][0], self.strategy_values[key][ 2] = self.float_check(root.find(value[4]).text, key) else: self.strategy_values[key][0] = root.find(value[4]).text self.strategy_values[key][1] = 'User_Defined'
def list_veh_counts(self): """ Outputs the number of vehicles with each type of equippage to the log file """ logger.info('Total Number Vehicles %s (Equipped Vehicles %s)' % (str(self.veh_count), str(self.equipped_veh_count))) logger.info('PDM only %s (DSRC %s, Celluar %s, Dual %s) ' % (str(self.PDM_count), str(self.PDM_DSRC_count), str(self.PDM_cell_count), str(self.PDM_both_count))) logger.info('BSM only %s (DSRC %s, Celluar %s, Dual %s) ' % (str(self.BSM_count), str(self.BSM_DSRC_count), str(self.BSM_cell_count), str(self.BSM_both_count))) logger.info( 'PDM and BSM %s (DSRC %s, Celluar %s, Dual %s) ' % (str(self.BSM_PDM_count), str(self.BSM_PDM_DSRC_count), str(self.BSM_PDM_cell_count), str(self.BSM_PDM_both_count))) logger.info('CAM only %s (DSRC %s, Celluar %s, Dual %s) ' % (str(self.CAM_count), str(self.CAM_DSRC_count), str(self.CAM_cell_count), str(self.CAM_both_count))) logger.info('SPOT only %s (DSRC %s) ' % (str(self.SPOT_count), str(self.SPOT_count)))
def Load_Control(self): """ Loads and checks control file values """ try: tree = ET.parse(self.control_file) root = tree.getroot() except: if self.control_file != curdir + 'not a file.xml': logger.info( "Error: cannot find or invalid format for control_values file %s" % self.control_file) raise for key, value in self.control_values.iteritems(): if root.find(value[4]) != None: if value[3] == 'int': self.control_values[key][0], self.control_values[key][ 2] = self.int_check(root.find(value[4]).text, key) elif value[3] == 'List_int': if len(str(root.find(value[4]).text).split(',')) > 0: self.control_values[key][0] = str( root.find(value[4]).text).split(',') try: self.control_values[key][0] = [ int(i) for i in self.control_values[key][0] ] except: pass elif value[3] == 'file': if root.find(value[4]).text != 'None': self.control_values[key][0] = self.file_check( root.find(value[4]).text) elif value[3] == 'Upper': self.control_values[key][0] = root.find( value[4]).text.upper() else: self.control_values[key][0] = root.find(value[4]).text self.control_values[key][1] = 'User_Defined' if self.control_values["OutputLevel"][0] != 0: if self.control_file == "TCAinput.xml": logger.info("Using default file name: TCAinput.xml") logger.info("TCA Version 2.3 created by Noblis") logger.info("Start time %s" % (time.strftime('%X', time.localtime(time.time())))) logger.info("Loading control_values file %s" % (self.control_file)) #Addtional Error checking if self.control_values["OutputLevel"][0] not in [0, 1, 2, 3]: self.control_values["OutputLevel"][ 2] = 'Error: OutputLevel can only be values 0,1,2,3' if (self.control_values["PDMMarketPenetration"][0] < 0 or self.control_values["PDMMarketPenetration"][0] > 100) \ and (self.control_values["PDMMarketPenetration"][0] != 0): self.control_values["PDMMarketPenetration"][ 2] = 'Error: PDMMarketPenetration is less than 0 or greater than 100' if (self.control_values["BSMMarketPenetration"][0] < 0 or self.control_values["BSMMarketPenetration"][0] > 100) \ and (self.control_values["BSMMarketPenetration"][0] != 0): self.control_values["BSMMarketPenetration"][ 2] = 'Error: BSMMarketPenetration is less than 0 or greater than 100' if (self.control_values["PDMDSRCMarketPenetration"][0] < 0 or self.control_values["PDMDSRCMarketPenetration"][0] > 100) \ and (self.control_values["PDMDSRCMarketPenetration"][0] != 0): self.control_values["PDMDSRCMarketPenetration"][ 2] = 'Error: PDMDSRCMarketPenetration is less than 0 or greater than 100' if (self.control_values["CAMMarketPenetration"][0] < 0 or self.control_values["CAMMarketPenetration"][0] > 100) \ and (self.control_values["CAMMarketPenetration"][0] != 0): self.control_values["CAMMarketPenetration"][ 2] = 'Error: CAMMarketPenetration is less than 0 or greater than 100' if (self.control_values["SPOTMarketPenetration"][0] < 0 or self.control_values["SPOTMarketPenetration"][0] > 100) \ and (self.control_values["SPOTMarketPenetration"][0] != 0): self.control_values["SPOTMarketPenetration"][ 2] = 'Error: SPOTMarketPenetration is less than 0 or greater than 100' if (self.control_values["PDMCellularMarketPenetration"][0] < 0 or self.control_values["PDMCellularMarketPenetration"][0] > 100) \ and (self.control_values["PDMCellularMarketPenetration"][0] != None): self.control_values["PDMCellularMarketPenetration"][ 2] = 'Error: PDMCellularMarketPenetration is less than 0 or greater than 100' if (self.control_values["PDMDualCommMarketPenetration"][0] < 0 or self.control_values["PDMDualCommMarketPenetration"][0] > 100) \ and (self.control_values["PDMDualCommMarketPenetration"][0] != None): self.control_values["PDMDualCommMarketPenetration"][ 2] = 'Error: PDMDualCommMarketPenetration is less than 0 or greater than 100' if (self.control_values["BSMDSRCMarketPenetration"][0] < 0 or self.control_values["BSMDSRCMarketPenetration"][0] > 100) \ and (self.control_values["BSMDSRCMarketPenetration"][0] != None): self.control_values["BSMDSRCMarketPenetration"][ 2] = 'Error: BSMDSRCMarketPenetration is less than 0 or greater than 100' if (self.control_values["BSMCellularMarketPenetration"][0] < 0 or self.control_values["BSMCellularMarketPenetration"][0] > 100) \ and (self.control_values["BSMCellularMarketPenetration"][0] != None): self.control_values["BSMCellularMarketPenetration"][ 2] = 'Error: BSMCellularMarketPenetration is less than 0 or greater than 100' if (self.control_values["BSMDualCommMarketPenetration"][0] < 0 or self.control_values["BSMDualCommMarketPenetration"][0] > 100) \ and (self.control_values["BSMDualCommMarketPenetration"][0] != None): self.control_values["BSMDualCommMarketPenetration"][ 2] = 'Error: BSMDualCommMarketPenetration is less than 0 or greater than 100' if self.control_values["BSMMarketPenetration"][ 0] != 0 and self.control_values["PDMMarketPenetration"][0] != 0: if (self.control_values["BSMMarketPenetration"][0] + self.control_values["PDMMarketPenetration"][0]) > 100: self.control_values["BSMMarketPenetration"][ 2] = 'Error: BSM and PDM equipage market penetration is more than 100%' self.control_values["PDMMarketPenetration"][ 2] = 'Error: BSM and PDM equipage market penetration is more than 100%' if self.control_values["PDMMarketPenetration"][0] != 0: if (len(self.control_values["PDMDSRCVehicleTypes"][0]) > 0) or (len(self.control_values["PDMDSRCVehicleIDs"][0]) > 0) or \ (len(self.control_values["PDMCellularVehicleTypes"][0]) > 0) or (len(self.control_values["PDMCellularVehicleIDs"][0]) > 0): self.control_values["PDMDSRCVehicleTypes"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' self.control_values["PDMDSRCVehicleIDs"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' self.control_values["PDMCellularVehicleTypes"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' self.control_values["PDMCellularVehicleIDs"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' if self.control_values["BSMMarketPenetration"][0] != 0: if (len(self.control_values["BSMDSRCVehicleTypes"][0]) > 0) or (len(self.control_values["BSMDSRCVehicleIDs"][0]) > 0) or \ (len(self.control_values["BSMCellularVehicleTypes"][0]) > 0) or (len(self.control_values["BSMCellularVehicleIDs"][0]) > 0): self.control_values["BSMDSRCVehicleTypes"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' self.control_values["BSMDSRCVehicleIDs"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' self.control_values["BSMCellularVehicleTypes"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' self.control_values["BSMCellularVehicleIDs"][ 2] = 'Error: Must use ONLY MarketPenetration, VehicleTypes, or VehicleIDs' if (self.TCA_version == 'standalone') and (self.control_values["FileType"][0] == 'CSV') and \ ((self.control_values["XColumn"][0] == None) or (self.control_values["YColumn"][0] == None) or \ (self.control_values["SpdColumn"][0] == None) or (self.control_values["IDColumn"][0] == None) or \ (self.control_values["TimeColumn"][0] == None)): self.control_values["XColumn"][ 2] = 'Error: Missing either XColumn YColumn SpdColumn IDColumn or TimeColumn xml tag' self.control_values["YColumn"][ 2] = 'Error: Missing either XColumn YColumn SpdColumn IDColumn or TimeColumn xml tag' self.control_values["SpdColumn"][ 2] = 'Error: Missing either XColumn YColumn SpdColumn IDColumn or TimeColumn xml tag' self.control_values["IDColumn"][ 2] = 'Error: Missing either XColumn YColumn SpdColumn IDColumn or TimeColumn xml tag' self.control_values["TimeColumn"][ 2] = 'Error: Missing either XColumn YColumn SpdColumn IDColumn or TimeColumn xml tag' if (self.TCA_version == 'standalone') and (self.control_values["PDMMarketPenetration"][0] == 0) and \ (self.control_values["PDMVehicleIDs"][0] == None) and (self.control_values["PDMVehicleTypes"][0] == None) and \ (self.control_values["BSMVehicleIDs"][0] == None) and (self.control_values["BSMVehicleTypes"][0] == None) and \ (self.control_values["BSMMarketPenetration"][0] == None) and (self.control_values["CAMMarketPenetration"][0] == 0) and \ (self.control_values["CAMVehicleIDs"][0] == None) and (self.control_values["CAMVehicleTypes"][0] == None) and \ (self.control_values["SPOTMarketPenetration"][0] == 0) and (self.control_values["SPOTVehicleIDs"][0] == None) and \ (self.control_values["SPOTVehicleTypes"][0] == None) and (self.control_values["DualPDMBSMMarketPenetration"][0] == 0) and \ (self.control_values["DualPDMBSMVehicleIDs"][0] == None) and (self.control_values["DualPDMBSMVehicleTypes"][0] == None): self.control_values["PDMMarketPenetration"][2] = 'Error: No vehicle equipage detected. Must select either include MarketPenetration, VehicleIDs, ' \ 'or VehicleTypes to define equipped vehicles' self.control_values["BSMMarketPenetration"][2] = 'Error: No vehicle equipage detected. Must select either include MarketPenetration, VehicleIDs, ' \ 'or VehicleTypes to define equipped vehicles' self.control_values["DualPDMBSMMarketPenetration"][2] = 'Error: No vehicle equipage detected. Must select either include MarketPenetration, VehicleIDs, ' \ 'or VehicleTypes to define equipped vehicles' self.control_values["CAMMarketPenetration"][2] = 'Error: No vehicle equipage detected. Must select either include MarketPenetration, VehicleIDs, ' \ 'or VehicleTypes to define equipped vehicles' self.control_values["SPOTMarketPenetration"][2] = 'Error: No vehicle equipage detected. Must select either include MarketPenetration, VehicleIDs, ' \ 'or VehicleTypes to define equipped vehicles' if (self.TCA_version != 'standalone'): color_values = ['BSMTransColor','PDMPeriodicColor','PDMStopColor','PDMStartColor','PDMDSRCTransColor','PDMCellularTransColor','SpotBehaviorColor','SpotTravelColor',\ 'SpotTransColor','DualPDMBSMColor','PDMDefaultColor','BSMDefaultColor','DefaultColor'] for color in color_values: if len(self.control_values[color][0]) != 3: self.control_values[color][ 2] = 'Error: Vehicle color must be specified as a list of three integers RGB values'
def AAPIPostManage(time, timeSta, timeTrans, acycle): global enabled if enabled: global CF global changecolor global Regions global RSE_Tree global Algorithm global LastTP global Snapshots global headerBSM global headerCAM headerBSM = True headerCAM = True if CF.Control["OutputLevel"] > 0: if time % 1000 == 0: logger.info("Time Step: %d" % (time)) vehicles = list() nba = AKIInfNetNbSectionsANG() for i in range(nba): id = AKIInfNetGetSectionANGId(i) nb = AKIVehStateGetNbVehiclesSection(id,True) for j in range(nb): vehicles.append(AKIVehStateGetVehicleInfSection(id,j)) # TODO: vehicles in junctions #nbj = AKIInfNetNbJunctions() #for i in range(nbj): # id = AKIInfNetGetJunctionId(i) # nb = AKIVehStateGetNbVehiclesJunction(id) # for j in range(nb): # vehicles.append(AKIVehStateGetVehicleInfJunction(id,j)) if len (vehicles) == 0: return 0 vehicles_ids = [] for veh in vehicles: active_veh = {'vehicle_ID' : veh.idVeh } vehicles_ids.append(active_veh) # Remove vehicle data of vehicles not seen in the concurrent timestep Algorithm.tbl.remove_non_active_vehicles(vehicles_ids) vehicles_list = [] for veh in vehicles: vehicle = { 'vehicle_ID': veh.idVeh, 'time': float(time), 'type': AKIVehTypeGetIdVehTypeANG(veh.type), 'accel_instantaneous': (veh.CurrentSpeed - veh.PreviousSpeed)*5280/3600/acycle, # ft/s^2 'speed': float(veh.CurrentSpeed), # mph 'location_x': float(veh.xCurrentPos), # ft 'location_y': float(veh.yCurrentPos), # ft 'link' : veh.idSection, } vehicles_list.append(vehicle) vehicles_data = Algorithm.pull_veh_data(vehicles_list, time) if RSE_Tree is not None: range_data = RSE_Tree.find_ranges(vehicles_data) for i,veh_data in enumerate(vehicles_data): #if vehicle equipped if veh_data is not None: if veh_data['BSM_equipped']: Algorithm.BSM.CheckBrakes(veh_data) if CF.Control['RegionsFile'] is not None: Regions.CheckRegions(veh_data, time) #if SPOT equipped if veh_data['SPOT_equipped']: Algorithm.SPOT.CheckMessage(veh_data, time) Algorithm.SPOT.CheckRange(veh_data, time) #if PDM equipped #if (time % 1 ==0) and (veh_data['PDM_equipped']): # TODO: transmit every second if veh_data['PDM_equipped']: # TODO: transmit every second Algorithm.PDM.CheckMessage(veh_data, time) if veh_data['DSRC_enabled'] and CF.Control['RSELocationFile'] != None: Algorithm.CheckDSRC(veh_data, time, range_data) Algorithm.CheckCellular(veh_data, time) #if (time % 1 ==0) and (veh_data['PDM_equipped']): # TODO: transmit every second if veh_data['PDM_equipped']: # TODO: transmit every second Algorithm.PDM.PSNCheck(veh_data, time) if veh_data['BSM_equipped']: Algorithm.BSM.tmp_ID_check(veh_data, time) #if changecolor: # color_display_time = CF.Control['ColorDisplayDuration'] # change_vehicle_color(vehicles[i], veh_data, time, color_display_time) Algorithm.BSM.Write() Algorithm.CAM.Write() Algorithm.tbl.previous_values(veh_data) LastTP = time return 0