mirror of
				https://github.com/Ahp06/SUMO_Emissions.git
				synced 2025-11-04 03:59:19 +00:00 
			
		
		
		
	Reformat code 🚀
This commit is contained in:
		@@ -12,13 +12,13 @@ from model import Emission
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Config:
 | 
					class Config:
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas
 | 
					    # Total of emissions of all pollutants in mg for n steps of simulation without acting on areas
 | 
				
			||||||
    # These constants are simulation dependant, you must change them according to your simulation 
 | 
					    # These constants are simulation dependant, you must change them according to your simulation 
 | 
				
			||||||
    ref200 = Emission(co2=42816869.05436445,co=1128465.0343051048,nox=18389.648337283958,hc=6154.330914019103,pmx=885.0829265236318)
 | 
					    ref200 = Emission(co2=42816869.05436445, co=1128465.0343051048, nox=18389.648337283958, hc=6154.330914019103,
 | 
				
			||||||
 | 
					                      pmx=885.0829265236318)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
        '''Default constructor'''
 | 
					        """Default constructor"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def import_config_file(self, config_file):
 | 
					    def import_config_file(self, config_file):
 | 
				
			||||||
        with open(config_file, 'r') as f:
 | 
					        with open(config_file, 'r') as f:
 | 
				
			||||||
@@ -56,16 +56,17 @@ class Config:
 | 
				
			|||||||
            self.weight_routing_mode = False
 | 
					            self.weight_routing_mode = False
 | 
				
			||||||
            self.lock_area_mode = False
 | 
					            self.lock_area_mode = False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                
 | 
					 | 
				
			||||||
    def __repr__(self) -> str:
 | 
					    def __repr__(self) -> str:
 | 
				
			||||||
        return (str(f'grid : {self.areas_number}x{self.areas_number}\n')
 | 
					        return (
 | 
				
			||||||
        + str(f'step number = {self.n_steps}\n')
 | 
					            f'grid : {self.areas_number}x{self.areas_number}\n'
 | 
				
			||||||
        + str(f'window size = {self.window_size}\n')
 | 
					            f'step number = {self.n_steps}\n'
 | 
				
			||||||
        + str(f'weight routing mode = {self.weight_routing_mode}\n')
 | 
					            f'window size = {self.window_size}\n'
 | 
				
			||||||
        + str(f'lock area mode = {self.lock_area_mode}\n')
 | 
					            f'weight routing mode = {self.weight_routing_mode}\n'
 | 
				
			||||||
        + str(f'limit speed mode = {self.limit_speed_mode}, RF = {self.speed_rf*100}%\n')
 | 
					            f'lock area mode = {self.lock_area_mode}\n'
 | 
				
			||||||
        + str(f'adjust traffic light mode = {self.adjust_traffic_light_mode} , RF = {self.trafficLights_duration_rf*100}%\n'))
 | 
					            f'limit speed mode = {self.limit_speed_mode}, RF = {self.speed_rf * 100}%\n'
 | 
				
			||||||
                
 | 
					            f'adjust traffic light mode = {self.adjust_traffic_light_mode},'
 | 
				
			||||||
 | 
					            'RF = {self.trafficLights_duration_rf * 100}%\n'
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def init_traci(self):
 | 
					    def init_traci(self):
 | 
				
			||||||
        if 'SUMO_HOME' in os.environ:
 | 
					        if 'SUMO_HOME' in os.environ:
 | 
				
			||||||
@@ -104,5 +105,3 @@ class Config:
 | 
				
			|||||||
    def get_ref_emissions(self):
 | 
					    def get_ref_emissions(self):
 | 
				
			||||||
        if self.n_steps == 200:
 | 
					        if self.n_steps == 200:
 | 
				
			||||||
            return self.ref200
 | 
					            return self.ref200
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,14 +1,13 @@
 | 
				
			|||||||
import argparse
 | 
					import argparse
 | 
				
			||||||
import csv
 | 
					import csv
 | 
				
			||||||
 | 
					import datetime
 | 
				
			||||||
import itertools
 | 
					import itertools
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
from traci import trafficlight
 | 
					 | 
				
			||||||
import traci
 | 
					 | 
				
			||||||
from typing import List
 | 
					from typing import List
 | 
				
			||||||
import datetime
 | 
					 | 
				
			||||||
import os
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import traci
 | 
				
			||||||
from parse import search
 | 
					from parse import search
 | 
				
			||||||
from shapely.geometry import LineString
 | 
					from shapely.geometry import LineString
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -47,14 +46,16 @@ def get_all_lanes() -> List[Lane]:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
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)
 | 
					    min_duration = search('min_duration: {:f}', phase_repr)
 | 
				
			||||||
    maxDuration = search('maxDuration: {:f}', phase_repr)
 | 
					    max_duration = search('max_duration: {:f}', phase_repr)
 | 
				
			||||||
    phaseDef = search('phaseDef: {}\n', phase_repr)
 | 
					    phase_def = search('phase_def: {}\n', phase_repr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if phaseDef is None: phaseDef = ''
 | 
					    if phase_def is None:
 | 
				
			||||||
    else : phaseDef = phaseDef[0]
 | 
					        phase_def = ''
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        phase_def = phase_def[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return Phase(duration[0], minDuration[0], maxDuration[0], phaseDef)
 | 
					    return Phase(duration[0], min_duration[0], max_duration[0], phase_def)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def add_data_to_areas(areas: List[Area]):
 | 
					def add_data_to_areas(areas: List[Area]):
 | 
				
			||||||
@@ -75,7 +76,6 @@ def add_data_to_areas(areas: List[Area]):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def compute_vehicle_emissions(veh_id):
 | 
					def compute_vehicle_emissions(veh_id):
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    co2 = traci.vehicle.getCO2Emission(veh_id)
 | 
					    co2 = traci.vehicle.getCO2Emission(veh_id)
 | 
				
			||||||
    co = traci.vehicle.getCOEmission(veh_id)
 | 
					    co = traci.vehicle.getCOEmission(veh_id)
 | 
				
			||||||
    nox = traci.vehicle.getNOxEmission(veh_id)
 | 
					    nox = traci.vehicle.getNOxEmission(veh_id)
 | 
				
			||||||
@@ -84,6 +84,7 @@ def compute_vehicle_emissions(veh_id):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    return Emission(co2, co, nox, hc, pmx)
 | 
					    return Emission(co2, co, nox, hc, pmx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_all_vehicles() -> List[Vehicle]:
 | 
					def get_all_vehicles() -> List[Vehicle]:
 | 
				
			||||||
    vehicles = list()
 | 
					    vehicles = list()
 | 
				
			||||||
    for veh_id in traci.vehicle.getIDList():
 | 
					    for veh_id in traci.vehicle.getIDList():
 | 
				
			||||||
@@ -109,7 +110,8 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi
 | 
				
			|||||||
                logger.info(f'Action - Decreased max speed into {area.name} by {config.speed_rf * 100}%')
 | 
					                logger.info(f'Action - Decreased max speed into {area.name} by {config.speed_rf * 100}%')
 | 
				
			||||||
                actions.limit_speed_into_area(area, vehicles, config.speed_rf)
 | 
					                actions.limit_speed_into_area(area, vehicles, config.speed_rf)
 | 
				
			||||||
                if config.adjust_traffic_light_mode and not area.tls_adjusted:
 | 
					                if config.adjust_traffic_light_mode and not area.tls_adjusted:
 | 
				
			||||||
                    logger.info(f'Action - Decreased traffic lights duration by {config.trafficLights_duration_rf*100}%')
 | 
					                    logger.info(
 | 
				
			||||||
 | 
					                        f'Action - Decreased traffic lights duration by {config.trafficLights_duration_rf * 100}%')
 | 
				
			||||||
                    actions.adjust_traffic_light_phase_duration(area, config.trafficLights_duration_rf)
 | 
					                    actions.adjust_traffic_light_phase_duration(area, config.trafficLights_duration_rf)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if config.lock_area_mode and not area.locked:
 | 
					            if config.lock_area_mode and not area.locked:
 | 
				
			||||||
@@ -126,6 +128,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi
 | 
				
			|||||||
            actions.reverse_actions(area)
 | 
					            actions.reverse_actions(area)
 | 
				
			||||||
            traci.polygon.setFilled(area.name, False)
 | 
					            traci.polygon.setFilled(area.name, False)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_reduction_percentage(ref, total):
 | 
					def get_reduction_percentage(ref, total):
 | 
				
			||||||
    return (ref - total) / ref * 100
 | 
					    return (ref - total) / ref * 100
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -211,8 +214,8 @@ def add_options(parser):
 | 
				
			|||||||
    parser.add_argument("-gui", "--gui", action="store_true",
 | 
					    parser.add_argument("-gui", "--gui", action="store_true",
 | 
				
			||||||
                        help="Set GUI mode")
 | 
					                        help="Set GUI mode")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def main(args):
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def main(args):
 | 
				
			||||||
    parser = argparse.ArgumentParser(description="")
 | 
					    parser = argparse.ArgumentParser(description="")
 | 
				
			||||||
    add_options(parser)
 | 
					    add_options(parser)
 | 
				
			||||||
    args = parser.parse_args(args)
 | 
					    args = parser.parse_args(args)
 | 
				
			||||||
@@ -238,5 +241,6 @@ def main(args):
 | 
				
			|||||||
    logger.info(f'Simulated time : {args.steps}s')
 | 
					    logger.info(f'Simulated time : {args.steps}s')
 | 
				
			||||||
    run(config, logger)
 | 
					    run(config, logger)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    main(sys.argv[1:])
 | 
					    main(sys.argv[1:])
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,10 +1,10 @@
 | 
				
			|||||||
import collections
 | 
					import collections
 | 
				
			||||||
from traci._trafficlight import Logic as SUMO_Logic
 | 
					 | 
				
			||||||
from typing import Tuple, Set
 | 
					from typing import Tuple, Set
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from shapely.geometry import Point, LineString
 | 
					from shapely.geometry import Point, LineString
 | 
				
			||||||
from shapely.geometry import Polygon
 | 
					from shapely.geometry import Polygon
 | 
				
			||||||
from shapely.geometry.base import BaseGeometry
 | 
					from shapely.geometry.base import BaseGeometry
 | 
				
			||||||
 | 
					from traci._trafficlight import Logic as SUMO_Logic
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Lane:
 | 
					class Lane:
 | 
				
			||||||
@@ -18,6 +18,7 @@ class Lane:
 | 
				
			|||||||
        """Overrides the default implementation"""
 | 
					        """Overrides the default implementation"""
 | 
				
			||||||
        return hash(self.lane_id)
 | 
					        return hash(self.lane_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Phase:
 | 
					class Phase:
 | 
				
			||||||
    def __init__(self, duration: float, minDuration: float, maxDuration: float, phaseDef: str):
 | 
					    def __init__(self, duration: float, minDuration: float, maxDuration: float, phaseDef: str):
 | 
				
			||||||
        self.duration = duration
 | 
					        self.duration = duration
 | 
				
			||||||
@@ -29,11 +30,13 @@ class Phase:
 | 
				
			|||||||
        repr = f'Phase(duration:{self.duration},minDuration:{self.minDuration},maxDuration:{self.maxDuration},phaseDef:{self.phaseDef})'
 | 
					        repr = f'Phase(duration:{self.duration},minDuration:{self.minDuration},maxDuration:{self.maxDuration},phaseDef:{self.phaseDef})'
 | 
				
			||||||
        return str(repr)
 | 
					        return str(repr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Logic:
 | 
					class Logic:
 | 
				
			||||||
    def __init__(self, logic: SUMO_Logic, phases: Set[Phase]):
 | 
					    def __init__(self, logic: SUMO_Logic, phases: Set[Phase]):
 | 
				
			||||||
        self._logic = logic
 | 
					        self._logic = logic
 | 
				
			||||||
        self._phases: Set[Phase] = phases
 | 
					        self._phases: Set[Phase] = phases
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class TrafficLight:
 | 
					class TrafficLight:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, tl_id: str, logics: Set[Logic]):
 | 
					    def __init__(self, tl_id: str, logics: Set[Logic]):
 | 
				
			||||||
@@ -44,6 +47,7 @@ class TrafficLight:
 | 
				
			|||||||
        """Overrides the default implementation"""
 | 
					        """Overrides the default implementation"""
 | 
				
			||||||
        return hash(self.tl_id)
 | 
					        return hash(self.tl_id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Emission:
 | 
					class Emission:
 | 
				
			||||||
    def __init__(self, co2=0, co=0, nox=0, hc=0, pmx=0):
 | 
					    def __init__(self, co2=0, co=0, nox=0, hc=0, pmx=0):
 | 
				
			||||||
        self.co2 = co2
 | 
					        self.co2 = co2
 | 
				
			||||||
@@ -53,8 +57,8 @@ class Emission:
 | 
				
			|||||||
        self.pmx = pmx
 | 
					        self.pmx = pmx
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __add__(self, other):
 | 
					    def __add__(self, other):
 | 
				
			||||||
        return Emission(self.co2 + other.co2, self.co + other.co, self.nox + other.nox, self.hc + other.hc, self.pmx + other.pmx)
 | 
					        return Emission(self.co2 + other.co2, self.co + other.co, self.nox + other.nox, self.hc + other.hc,
 | 
				
			||||||
        
 | 
					                        self.pmx + other.pmx)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def value(self):
 | 
					    def value(self):
 | 
				
			||||||
        return self.co2 + self.co + self.nox + self.hc + self.pmx
 | 
					        return self.co2 + self.co + self.nox + self.hc + self.pmx
 | 
				
			||||||
@@ -63,6 +67,7 @@ class Emission:
 | 
				
			|||||||
        repr = f'Emission(co2={self.co2},co={self.co},nox={self.nox},hc={self.hc},pmx={self.pmx})'
 | 
					        repr = f'Emission(co2={self.co2},co={self.co},nox={self.nox},hc={self.hc},pmx={self.pmx})'
 | 
				
			||||||
        return str(repr)
 | 
					        return str(repr)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Area:
 | 
					class Area:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, coords, name, window_size):
 | 
					    def __init__(self, coords, name, window_size):
 | 
				
			||||||
@@ -122,6 +127,7 @@ class Area:
 | 
				
			|||||||
            (xmax, ymax),
 | 
					            (xmax, ymax),
 | 
				
			||||||
            (xmax, ymin)))
 | 
					            (xmax, ymin)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Vehicle:
 | 
					class Vehicle:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, veh_id: int, pos: Tuple[float, float]):
 | 
					    def __init__(self, veh_id: int, pos: Tuple[float, float]):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user