diff --git a/sumo_project/actions.py b/sumo_project/actions.py index 484d7f5..4a7a854 100644 --- a/sumo_project/actions.py +++ b/sumo_project/actions.py @@ -13,17 +13,22 @@ from model import Area, Vehicle def compute_edge_weight(edge_id): - return (traci.edge.getCOEmission(edge_id) - + traci.edge.getNOxEmission(edge_id) - + traci.edge.getHCEmission(edge_id) - + traci.edge.getPMxEmission(edge_id) - + traci.edge.getCO2Emission(edge_id))/(traci.edge.getLaneNumber(edge_id)) - -def adjust_edges_weights(): - for edge_id in traci.edge.getIDList(): + + co2 = traci.edge.getCO2Emission(edge_id) + co = traci.edge.getCOEmission(edge_id) + nox = traci.edge.getNOxEmission(edge_id) + hc = traci.edge.getHCEmission(edge_id) + pmx = traci.edge.getPMxEmission(edge_id) + + return (co2 + co + nox + hc + pmx)/traci.edge.getLaneNumber(edge_id) + +def adjust_edges_weights(area): + area.weight_adjusted = True + for lane in area._lanes: + edge_id = traci.lane.getEdgeID(lane.lane_id) weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed traci.edge.setEffort(edge_id, weight) - + for veh_id in traci.vehicle.getIDList(): traci.vehicle.rerouteEffort(veh_id) @@ -31,7 +36,7 @@ def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], speed_rf): area.limited_speed = True for lane in area._lanes: traci.lane.setMaxSpeed(lane.lane_id, speed_rf * lane.initial_max_speed) - + def modifyLogic(logic, rf): #rf for "reduction factor" new_phases = [] for phase in logic._phases: diff --git a/sumo_project/emissions.py b/sumo_project/emissions.py index dcb5792..b0da161 100644 --- a/sumo_project/emissions.py +++ b/sumo_project/emissions.py @@ -13,7 +13,7 @@ from config import Config from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic -def init_grid(simulation_bounds, areas_number): +def init_grid(simulation_bounds, areas_number,window_size): grid = list() width = simulation_bounds[1][0] / areas_number height = simulation_bounds[1][1] / areas_number @@ -22,10 +22,10 @@ def init_grid(simulation_bounds, areas_number): # bounds coordinates for the area : (xmin, ymin, xmax, ymax) ar_bounds = ((i * width, j * height), (i * width, (j + 1) * height), ((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height)) - area = Area(ar_bounds) - area.name = 'Area ({},{})'.format(i, j) + name = 'Area ({},{})'.format(i, j) + area = Area(ar_bounds, name, window_size) grid.append(area) - traci.polygon.add(area.name, ar_bounds, (0, 255, 0)) + traci.polygon.add(area.name, ar_bounds, (255, 0, 0)) return grid @@ -108,11 +108,14 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi logger.info(f'Action - {area.name} blocked') actions.lock_area(area) - traci.polygon.setColor(area.name, (255, 0, 0)) + if config.weight_routing_mode and not area.weight_adjusted: + actions.adjust_edges_weights(area) + traci.polygon.setFilled(area.name, True) else: actions.reverse_actions(area) + traci.polygon.setFilled(area.name, False) def run(config, logger): @@ -123,25 +126,23 @@ def run(config, logger): logger.info('Loading data for the simulation') start = time.perf_counter() - grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number) + grid = init_grid(traci.simulation.getNetBoundary(), config.areas_number, config.window_size) add_data_to_areas(grid) loading_time = round(time.perf_counter() - start, 2) logger.info(f'Data loaded ({loading_time}s)') - logger.info('Start of the simulation') + logger.info('Simulation started...') step = 0 while step < config.n_steps : # traci.simulation.getMinExpectedNumber() > 0: traci.simulationStep() - + vehicles = get_all_vehicles() get_emissions(grid, vehicles, step, config, logger) - - if config.weight_routing_mode: - actions.adjust_edges_weights() - step += 1 + print(f'step = {step}/{config.n_steps}', end='\r') + finally: traci.close(False) simulation_time = round(time.perf_counter() - start, 2) @@ -158,33 +159,45 @@ def run(config, logger): if not (ref is None): diff_with_actions = (ref - total_emissions) / ref logger.info(f'Reduction percentage of emissions = {diff_with_actions*100} %') - + +def add_options(parser): + parser.add_argument("-f", "--configfile", type=str, default='configs/default_config.json', required=False, + help='Choose your configuration file from your working directory') + parser.add_argument("-save", "--save", action="store_true", + help = 'Save the logs into the logs folder') + parser.add_argument("-steps", "--steps", type= int, default= 200, required = False, + help='Choose the simulated time (in seconds)') + parser.add_argument("-ref", "--ref", action="store_true", + help='Launch a reference simulation (without acting on areas)') + parser.add_argument("-gui", "--gui", action ="store_true", + help= "Set GUI mode") 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") + add_options(parser) 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 -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) + if args.ref: config.without_actions_mode = True - config.check_config() logger.info(f'Reference simulation') - logger.info(f'Loaded configuration file : {args.configfile}') + if args.steps: + config.n_steps = args.steps + + if args.gui: + config._SUMOCMD = "sumo-gui" + + config.check_config() + + logger.info(f'Loaded configuration file : {args.configfile}') + logger.info(f'Simulated time : {args.steps}') run(config, logger) - if __name__ == '__main__': main(sys.argv[1:]) diff --git a/sumo_project/model.py b/sumo_project/model.py index e910840..68101b4 100644 --- a/sumo_project/model.py +++ b/sumo_project/model.py @@ -5,6 +5,8 @@ from shapely.geometry import Point, LineString from shapely.geometry import Polygon from shapely.geometry.base import BaseGeometry +import collections + class Lane: @@ -45,13 +47,15 @@ class TrafficLight: class Area: - def __init__(self, coords, name=''): + def __init__(self, coords, name, window_size): self.limited_speed = False self.locked = False self.tls_adjusted = False + self.weight_adjusted = False self.rectangle = Polygon(coords) self.name = name - self.emissions_by_step = [] + self.emissions_by_step = [] + self.window = collections.deque(maxlen = window_size) self._lanes: Set[Lane] = set() self._tls: Set[TrafficLight] = set() @@ -83,11 +87,12 @@ class Area: sum += emission return sum - def sum_emissions_into_window(self, current_step, window_size): + def sum_emissions_into_window(self, current_step, window_size): + + self.window.appendleft(self.emissions_by_step[current_step]) sum = 0 - q = current_step // window_size #Returns the integral part of the quotient - for i in range(q*window_size, current_step): - sum += self.emissions_by_step[i] + for i in range(self.window.__len__()): + sum += self.window[i] return sum @classmethod