1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-25 13:06:30 +00:00

Added images folder for wiki

This commit is contained in:
Ahp06 2018-12-12 10:13:33 +01:00
parent 2ab280b94e
commit ab551cc429

View File

@ -28,6 +28,7 @@ def init_grid(simulation_bounds, areas_number):
traci.polygon.add(area.name, ar_bounds, (0, 255, 0)) traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
return grid return grid
def get_all_lanes() -> List[Lane]: def get_all_lanes() -> List[Lane]:
lanes = [] lanes = []
for lane_id in traci.lane.getIDList(): 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)) lanes.append(Lane(lane_id, polygon_lane, initial_max_speed))
return lanes return lanes
def parse_phase(phase_repr): def parse_phase(phase_repr):
duration = search('duration: {:f}', phase_repr) duration = search('duration: {:f}', phase_repr)
minDuration = search('minDuration: {: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) return Phase(duration[0], minDuration[0], maxDuration[0], phaseDef)
def add_data_to_areas(areas: List[Area]): def add_data_to_areas(areas: List[Area]):
lanes = get_all_lanes() lanes = get_all_lanes()
for area in areas: 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 for tl_id in traci.trafficlight.getIDList(): # add traffic lights
if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id): if lane.lane_id in traci.trafficlight.getControlledLanes(tl_id):
logics = [] logics = []
for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): #add logics for l in traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id): # add logics
phases = [] 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__())) phases.append(parse_phase(phase.__repr__()))
logics.append(Logic(l,phases)) logics.append(Logic(l, phases))
area.add_tl(TrafficLight(tl_id,logics)) area.add_tl(TrafficLight(tl_id, logics))
def compute_vehicle_emissions(veh_id): def compute_vehicle_emissions(veh_id):
return (traci.vehicle.getCOEmission(veh_id) return (traci.vehicle.getCOEmission(veh_id)
@ -80,6 +84,7 @@ def get_all_vehicles() -> List[Vehicle]:
vehicles.append(vehicle) vehicles.append(vehicle)
return vehicles return vehicles
def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, config, logger): def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, config, logger):
for area in grid: for area in grid:
vehicle_emissions = 0 vehicle_emissions = 0
@ -121,16 +126,16 @@ def run(config, logger):
grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number) grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number)
add_data_to_areas(grid) 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(f'Data loaded ({loading_time}s)')
logger.info('Start of the simulation') logger.info('Start of the simulation')
step = 0 step = 0
while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0: while step < config.n_steps : # traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep() traci.simulationStep()
vehicles = get_all_vehicles() vehicles = get_all_vehicles()
get_emissions(grid, vehicles,step,config,logger) get_emissions(grid, vehicles, step, config, logger)
if config.weight_routing_mode: if config.weight_routing_mode:
actions.adjust_edges_weights() actions.adjust_edges_weights()
@ -139,7 +144,7 @@ def run(config, logger):
finally: finally:
traci.close(False) 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)') logger.info(f'End of the simulation ({simulation_time}s)')
total_emissions = 0 total_emissions = 0
@ -151,27 +156,27 @@ def run(config, logger):
if not config.without_actions_mode : if not config.without_actions_mode :
ref = config.get_basics_emissions() ref = config.get_basics_emissions()
if not (ref is None): 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} %') logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %')
def main(args): def main(args):
parser = argparse.ArgumentParser(description="") parser = argparse.ArgumentParser(description="")
parser.add_argument("-f", "--configfile", type=str, default= 'configs/default_config.json', required=False) 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("-save", "--save", action="store_true")
parser.add_argument("-ref","--ref", action = "store_true") parser.add_argument("-ref", "--ref", action="store_true")
args = parser.parse_args(args) args = parser.parse_args(args)
# > py ./emissions.py -f configs/config1.json -save # > py ./emissions.py -f configs/config1.json -save
# will load the configuration file "config1.json" and save logs into the logs directory # 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 # same as above but also launches a reference simulation by using -ref option
config = Config() config = Config()
config.import_config_file(args.configfile) config.import_config_file(args.configfile)
config.init_traci() config.init_traci()
logger = config.init_logger(save_logs = args.save) logger = config.init_logger(save_logs=args.save)
if args.ref: if args.ref:
config.without_actions_mode = True config.without_actions_mode = True
config.check_config() config.check_config()
@ -180,5 +185,6 @@ def main(args):
run(config, logger) run(config, logger)
if __name__ == '__main__': if __name__ == '__main__':
main(sys.argv[1:]) main(sys.argv[1:])