1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-24 20:46:29 +00:00

Refactored some code and changed logs filename

This commit is contained in:
Ahp06 2019-01-18 23:01:38 +01:00
parent 5b812d2dc1
commit fef374c47f
3 changed files with 36 additions and 32 deletions

View File

@ -13,6 +13,7 @@ from model import Area, Vehicle
This module defines all possible actions on the simulation
"""
def compute_edge_weight(edge_id):
"""
Sum the different pollutant emissions on the edge with the identifier edge_id
@ -100,7 +101,9 @@ def count_vehicles_in_area(area):
def lock_area(area):
"""
Prohibits access to the area to a particular vehicle class
NOT FIXED : Some vehicles continue to go into the area if they can not turn around and stay there
NOT FIXED : Some vehicles continue to go into the area
if they can not turn around and then will stay blocked there
as long as it will not be reversed
:param area: The Area object
:return:
"""

View File

@ -41,22 +41,9 @@ class Config:
with open(config_file, 'r') as f:
data = json.load(f)
self._SUMOCMD = data["_SUMOCMD"]
self._SUMOCFG = data["_SUMOCFG"]
self.areas_number = data["areas_number"]
self.emissions_threshold = data["emissions_threshold"]
self.n_steps = data["n_steps"]
self.window_size = data["window_size"]
self.without_actions_mode = data["without_actions_mode"]
self.limit_speed_mode = data["limit_speed_mode"]
self.speed_rf = data["speed_rf"]
self.adjust_traffic_light_mode = data["adjust_traffic_light_mode"]
self.trafficLights_duration_rf = data["trafficLights_duration_rf"]
self.weight_routing_mode = data["weight_routing_mode"]
self.lock_area_mode = data["lock_area_mode"]
for option in data:
self.__setattr__(option, data[option])
self.config_filename = os.path.basename(f.name)
self.check_config()
def check_config(self):
@ -64,13 +51,13 @@ class Config:
Check the relevance of user configuration choices
:return:
"""
# Weight routing mode cannot be combinated with other actions
# Weight routing mode cannot be combined with other actions
if self.weight_routing_mode:
self.limit_speed_mode = False
self.adjust_traffic_light_mode = False
self.lock_area_mode = False
# If without_actions_mode is choosen
# If without_actions_mode is chosen
if self.without_actions_mode:
self.limit_speed_mode = False
self.adjust_traffic_light_mode = False
@ -118,7 +105,7 @@ class Config:
if not os.path.exists('logs'):
os.makedirs('logs')
log_filename = f'logs/sumo_logs_{current_date}.log'
log_filename = f'logs/sumo_logs_{current_date}_{self.config_filename}.log'
logger = logging.getLogger("sumo_logger")
logger.setLevel(logging.INFO)

View File

@ -24,6 +24,7 @@ from shapely.geometry import LineString
This module defines the entry point of the application
"""
def init_grid(simulation_bounds, areas_number, window_size):
"""
Initialize the grid of the loaded map from the configuration
@ -43,7 +44,7 @@ def init_grid(simulation_bounds, areas_number, window_size):
name = 'Area ({},{})'.format(i, j)
area = Area(ar_bounds, name, window_size)
grid.append(area)
traci.polygon.add(area.name, ar_bounds, (255, 0, 0))
traci.polygon.add(area.name, ar_bounds, (255, 0, 0)) # Add polygon for UI
return grid
@ -105,7 +106,7 @@ def add_data_to_areas(areas: List[Area]):
def compute_vehicle_emissions(veh_id):
"""
Recover the emissions of different pollutants from a vehicle and create an Emission instance
:param veh_id:
:param veh_id: The vehicle ID
:return: A new Emission instance
"""
co2 = traci.vehicle.getCO2Emission(veh_id)
@ -148,8 +149,10 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi
if vehicle.pos in area:
total_emissions += vehicle.emissions
# Adding of the total of emissions pollutant at the current step into memory
area.emissions_by_step.append(total_emissions)
# If the sum of pollutant emissions (in mg) exceeds the threshold
if area.sum_emissions_into_window(current_step) >= config.emissions_threshold:
if config.limit_speed_mode and not area.limited_speed:
@ -178,8 +181,8 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step, confi
def get_reduction_percentage(ref, total):
"""
Return the reduction percentage of total emissions between reference and an other simulation
:param ref:
:param total:
:param ref: The sum of all pollutant emissions (in mg) for the simulation of reference
:param total: The sum of all pollutant emissions (in mg) for the current simulation launched
:return:
"""
return (ref - total) / ref * 100
@ -208,7 +211,7 @@ def export_data_to_csv(config, grid):
writer.writerow(itertools.chain((step,), em_for_step))
def run(config, logger):
def run(config, logger, csv_export):
"""
Run the simulation with the configuration chosen
:param config: The simulation configuration
@ -230,7 +233,7 @@ def run(config, logger):
logger.info('Simulation started...')
step = 0
while step < config.n_steps: # traci.simulation.getMinExpectedNumber() > 0:
while step < config.n_steps:
traci.simulationStep()
vehicles = get_all_vehicles()
@ -241,10 +244,15 @@ def run(config, logger):
finally:
traci.close(False)
if csv_export:
export_data_to_csv(config, grid)
logger.info(f'Exported data into the csv folder')
simulation_time = round(time.perf_counter() - start, 2)
logger.info(f'End of the simulation ({simulation_time}s)')
# 1 step is equal to one second simulated
logger.info(f'Real-time factor : {config.n_steps / simulation_time}')
total_emissions = Emission()
@ -253,9 +261,9 @@ def run(config, logger):
logger.info(f'Total emissions = {total_emissions.value()} mg')
if not config.without_actions_mode:
if not config.without_actions_mode: # If it's not a simulation without actions
ref = config.get_ref_emissions()
if not (ref is None):
if not (ref is None): # If a reference value exist (add yours into config.py)
global_diff = (ref.value() - total_emissions.value()) / ref.value()
logger.info(f'Global reduction percentage of emissions = {global_diff * 100} %')
@ -281,7 +289,9 @@ def add_options(parser):
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")
help="Show UI")
parser.add_argument("-csv", "--csv", action="store_true",
help="Export all data emissions into a CSV file")
def main(args):
@ -295,9 +305,10 @@ def main(args):
args = parser.parse_args(args)
config = Config()
config.import_config_file(args.configfile)
config.import_config_file(args.configfile) # By default the configfile is default_config.json
config.init_traci()
logger = config.init_logger(save_logs=args.save)
csv_export = False
if args.ref:
config.without_actions_mode = True
@ -309,11 +320,14 @@ def main(args):
if args.gui:
config._SUMOCMD = "sumo-gui"
if args.csv:
csv_export = True
config.check_config()
logger.info(f'Loaded configuration file : {args.configfile}')
logger.info(f'Simulated time : {args.steps}s')
run(config, logger)
run(config, logger, csv_export)
if __name__ == '__main__':