mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-22 03:26:30 +00:00
Added some shell commands
This commit is contained in:
parent
91eb76e7b3
commit
b8edadb5c8
@ -76,7 +76,7 @@ class Config:
|
|||||||
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', self._SUMOCMD)
|
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', self._SUMOCMD)
|
||||||
self.sumo_cmd = [sumo_binary, "-c", self._SUMOCFG]
|
self.sumo_cmd = [sumo_binary, "-c", self._SUMOCFG]
|
||||||
|
|
||||||
def init_logger(self, save_logs = True):
|
def init_logger(self, save_logs = False):
|
||||||
now = datetime.datetime.now()
|
now = datetime.datetime.now()
|
||||||
current_date = now.strftime("%Y_%m_%d_%H_%M_%S")
|
current_date = now.strftime("%Y_%m_%d_%H_%M_%S")
|
||||||
|
|
||||||
@ -87,11 +87,14 @@ class Config:
|
|||||||
|
|
||||||
logger = logging.getLogger("sumo_logger")
|
logger = logging.getLogger("sumo_logger")
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
if save_logs :
|
|
||||||
handler = logging.FileHandler(log_filename)
|
|
||||||
else:
|
|
||||||
handler = logging.StreamHandler()
|
|
||||||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
||||||
|
|
||||||
|
if save_logs :
|
||||||
|
file_handler = logging.FileHandler(log_filename)
|
||||||
|
file_handler.setFormatter(formatter)
|
||||||
|
logger.addHandler(file_handler)
|
||||||
|
|
||||||
|
handler = logging.StreamHandler()
|
||||||
handler.setFormatter(formatter)
|
handler.setFormatter(formatter)
|
||||||
logger.addHandler(handler)
|
logger.addHandler(handler)
|
||||||
|
|
||||||
|
@ -13,9 +13,6 @@ import sys
|
|||||||
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic
|
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic
|
||||||
from traci import trafficlight
|
from traci import trafficlight
|
||||||
|
|
||||||
config = Config()
|
|
||||||
logger = config.init_logger(save_logs = True)
|
|
||||||
|
|
||||||
def init_grid(simulation_bounds, areas_number):
|
def init_grid(simulation_bounds, areas_number):
|
||||||
grid = list()
|
grid = list()
|
||||||
width = simulation_bounds[1][0] / areas_number
|
width = simulation_bounds[1][0] / areas_number
|
||||||
@ -83,7 +80,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):
|
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
|
||||||
for vehicle in vehicles:
|
for vehicle in vehicles:
|
||||||
@ -97,8 +94,6 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step):
|
|||||||
if config.limit_speed_mode and not area.limited_speed:
|
if config.limit_speed_mode and not area.limited_speed:
|
||||||
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)
|
||||||
traci.polygon.setColor(area.name, (255, 0, 0))
|
|
||||||
traci.polygon.setFilled(area.name, True)
|
|
||||||
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)
|
||||||
@ -107,24 +102,18 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step):
|
|||||||
if actions.count_vehicles_in_area(area):
|
if actions.count_vehicles_in_area(area):
|
||||||
logger.info(f'Action - {area.name} blocked')
|
logger.info(f'Action - {area.name} blocked')
|
||||||
actions.lock_area(area)
|
actions.lock_area(area)
|
||||||
|
|
||||||
|
traci.polygon.setColor(area.name, (255, 0, 0))
|
||||||
|
traci.polygon.setFilled(area.name, True)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
actions.reverse_actions(area)
|
actions.reverse_actions(area)
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
def run(config, logger):
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description="")
|
|
||||||
parser.add_argument("-f", "--configfile", type=str, default= 'configs/default_config.json', required=False)
|
|
||||||
args = parser.parse_args(args)
|
|
||||||
|
|
||||||
config.import_config_file(args.configfile)
|
|
||||||
config.init_traci()
|
|
||||||
|
|
||||||
grid = list()
|
grid = list()
|
||||||
try:
|
try:
|
||||||
traci.start(config.sumo_cmd)
|
traci.start(config.sumo_cmd)
|
||||||
logger.info(f'Loaded configuration file : {args.configfile}')
|
|
||||||
logger.info(f'Loaded simulation file : {config._SUMOCFG}')
|
logger.info(f'Loaded simulation file : {config._SUMOCFG}')
|
||||||
logger.info('Loading data for the simulation')
|
logger.info('Loading data for the simulation')
|
||||||
start = time.perf_counter()
|
start = time.perf_counter()
|
||||||
@ -141,7 +130,7 @@ def main(args):
|
|||||||
traci.simulationStep()
|
traci.simulationStep()
|
||||||
|
|
||||||
vehicles = get_all_vehicles()
|
vehicles = get_all_vehicles()
|
||||||
get_emissions(grid, vehicles,step)
|
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()
|
||||||
@ -165,7 +154,20 @@ def main(args):
|
|||||||
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):
|
||||||
|
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")
|
||||||
|
args = parser.parse_args(args)
|
||||||
|
|
||||||
|
config = Config()
|
||||||
|
config.import_config_file(args.configfile)
|
||||||
|
config.init_traci()
|
||||||
|
logger = config.init_logger(save_logs = args.save)
|
||||||
|
|
||||||
|
logger.info(f'Loaded configuration file : {args.configfile}')
|
||||||
|
run(config, logger)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main(sys.argv[1:])
|
main(sys.argv[1:])
|
||||||
|
Loading…
Reference in New Issue
Block a user