From ab551cc4298bb8238ea435b90b9be978ec8fab4b Mon Sep 17 00:00:00 2001 From: Ahp06 Date: Wed, 12 Dec 2018 10:13:33 +0100 Subject: [PATCH] Added images folder for wiki --- sumo_project/emissions.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index 399b860..dcb5792 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -28,6 +28,7 @@ def init_grid(simulation_bounds, areas_number): traci.polygon.add(area.name, ar_bounds, (0, 255, 0)) return grid + def get_all_lanes() -> List[Lane]: lanes = [] for lane_id in traci.lane.getIDList(): @@ -36,6 +37,7 @@ def get_all_lanes() -> List[Lane]: lanes.append(Lane(lane_id, polygon_lane, initial_max_speed)) return lanes + def parse_phase(phase_repr): duration = search('duration: {:f}', phase_repr) minDuration = search('minDuration: {:f}', phase_repr) @@ -47,6 +49,7 @@ def parse_phase(phase_repr): return Phase(duration[0], minDuration[0], maxDuration[0], phaseDef) + def add_data_to_areas(areas: List[Area]): lanes = get_all_lanes() for area in areas: @@ -56,12 +59,13 @@ def add_data_to_areas(areas: List[Area]): for tl_id in traci.trafficlight.getIDList(): # add traffic lights if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id): logics = [] - for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): #add logics + for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): # add logics phases = [] - for phase in traci.trafficlight.Logic.getPhases(l): #add phases to logics + for phase in traci.trafficlight.Logic.getPhases(l): # add phases to logics phases.append(parse_phase(phase.__repr__())) - logics.append(Logic(l,phases)) - area.add_tl(TrafficLight(tl_id,logics)) + logics.append(Logic(l, phases)) + area.add_tl(TrafficLight(tl_id, logics)) + def compute_vehicle_emissions(veh_id): return (traci.vehicle.getCOEmission(veh_id) @@ -80,6 +84,7 @@ def get_all_vehicles() -> List[Vehicle]: vehicles.append(vehicle) return vehicles + def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, config, logger): for area in grid: vehicle_emissions = 0 @@ -121,16 +126,16 @@ def run(config, logger): grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number) add_data_to_areas(grid) - loading_time = round(time.perf_counter() - start,2) + loading_time = round(time.perf_counter() - start, 2) logger.info(f'Data loaded ({loading_time}s)') logger.info('Start of the simulation') step = 0 - while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0: + while step < config.n_steps : # traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() vehicles = get_all_vehicles() - get_emissions(grid, vehicles,step,config,logger) + get_emissions(grid, vehicles, step, config, logger) if config.weight_routing_mode: actions.adjust_edges_weights() @@ -139,7 +144,7 @@ def run(config, logger): finally: traci.close(False) - simulation_time = round(time.perf_counter() - start,2) + simulation_time = round(time.perf_counter() - start, 2) logger.info(f'End of the simulation ({simulation_time}s)') total_emissions = 0 @@ -151,27 +156,27 @@ def run(config, logger): if not config.without_actions_mode : ref = config.get_basics_emissions() if not (ref is None): - diff_with_actions = (ref - total_emissions)/ref + diff_with_actions = (ref - total_emissions) / ref logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %') def main(args): parser = argparse.ArgumentParser(description="") - parser.add_argument("-f", "--configfile", type=str, default= 'configs/default_config.json', required=False) - parser.add_argument("-save", "--save", action = "store_true") - parser.add_argument("-ref","--ref", action = "store_true") + parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False) + parser.add_argument("-save", "--save", action="store_true") + parser.add_argument("-ref", "--ref", action="store_true") args = parser.parse_args(args) # > py ./emissions.py -f configs/config1.json -save # will load the configuration file "config1.json" and save logs into the logs directory - # > py ./emissions.py -f configs/config1.json -save & py ./emissions.py -f configs/config1.json -save -ref + # > py ./emissions.py -f configs/config1.json -save -ref & py ./emissions.py -f configs/config1.json -save # same as above but also launches a reference simulation by using -ref option config = Config() config.import_config_file(args.configfile) config.init_traci() - logger = config.init_logger(save_logs = args.save) + logger = config.init_logger(save_logs=args.save) if args.ref: config.without_actions_mode = True config.check_config() @@ -179,6 +184,7 @@ def main(args): logger.info(f'Loaded configuration file : {args.configfile}') run(config, logger) + if __name__ == '__main__': main(sys.argv[1:])