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

Added some shell commands

This commit is contained in:
Ahp06 2018-12-10 16:26:14 +01:00
parent 91eb76e7b3
commit b8edadb5c8
2 changed files with 28 additions and 23 deletions

View File

@ -76,7 +76,7 @@ class Config:
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', self._SUMOCMD)
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()
current_date = now.strftime("%Y_%m_%d_%H_%M_%S")
@ -87,11 +87,14 @@ class Config:
logger = logging.getLogger("sumo_logger")
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")
if save_logs :
file_handler = logging.FileHandler(log_filename)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

View File

@ -13,9 +13,6 @@ import sys
from model import Area, Vehicle, Lane , TrafficLight , Phase , Logic
from traci import trafficlight
config = Config()
logger = config.init_logger(save_logs = True)
def init_grid(simulation_bounds, areas_number):
grid = list()
width = simulation_bounds[1][0] / areas_number
@ -83,7 +80,7 @@ def get_all_vehicles() -> List[Vehicle]:
vehicles.append(vehicle)
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:
vehicle_emissions = 0
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:
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)
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:
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)
@ -108,23 +103,17 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle], current_step):
logger.info(f'Action - {area.name} blocked')
actions.lock_area(area)
traci.polygon.setColor(area.name, (255, 0, 0))
traci.polygon.setFilled(area.name, True)
else:
actions.reverse_actions(area)
def main(args):
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()
def run(config, logger):
grid = list()
try:
traci.start(config.sumo_cmd)
logger.info(f'Loaded configuration file : {args.configfile}')
logger.info(f'Loaded simulation file : {config._SUMOCFG}')
logger.info('Loading data for the simulation')
start = time.perf_counter()
@ -141,7 +130,7 @@ def main(args):
traci.simulationStep()
vehicles = get_all_vehicles()
get_emissions(grid, vehicles,step)
get_emissions(grid, vehicles,step,config,logger)
if config.weight_routing_mode:
actions.adjust_edges_weights()
@ -166,6 +155,19 @@ def main(args):
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__':
main(sys.argv[1:])