mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-22 03:26:30 +00:00
Added runner module
This commit is contained in:
parent
c719ba0068
commit
1e8d0b0199
@ -4,15 +4,15 @@ Created on 17 oct. 2018
|
|||||||
@author: Axel Huynh-Phuc, Thibaud Gasser
|
@author: Axel Huynh-Phuc, Thibaud Gasser
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Iterable
|
|
||||||
|
|
||||||
import traci
|
|
||||||
from model import Area, Vehicle
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
This module defines all possible actions on the simulation
|
This module defines all possible actions on the simulation
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import traci
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
|
from model import Area, Vehicle
|
||||||
|
|
||||||
|
|
||||||
def compute_edge_weight(edge_id):
|
def compute_edge_weight(edge_id):
|
||||||
"""
|
"""
|
||||||
|
@ -4,6 +4,10 @@ Created on 17 oct. 2018
|
|||||||
@author: Axel Huynh-Phuc, Thibaud Gasser
|
@author: Axel Huynh-Phuc, Thibaud Gasser
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
This module defines the global configuration for the simulation
|
||||||
|
"""
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@ -12,10 +16,6 @@ import sys
|
|||||||
|
|
||||||
from model import Emission
|
from model import Emission
|
||||||
|
|
||||||
"""
|
|
||||||
This module defines the global configuration for the simulation
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
"""
|
"""
|
||||||
|
@ -4,28 +4,29 @@ Created on 17 oct. 2018
|
|||||||
@author: Axel Huynh-Phuc, Thibaud Gasser
|
@author: Axel Huynh-Phuc, Thibaud Gasser
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
This module is used for loading simulation data
|
||||||
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import csv
|
import csv
|
||||||
import datetime
|
import datetime
|
||||||
import itertools
|
import itertools
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
|
import pprint
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from typing import List
|
|
||||||
import json
|
|
||||||
import jsonpickle
|
|
||||||
import pprint
|
|
||||||
|
|
||||||
import actions
|
|
||||||
import traci
|
import traci
|
||||||
from config import Config
|
from typing import List
|
||||||
from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission
|
|
||||||
|
import jsonpickle
|
||||||
from parse import search
|
from parse import search
|
||||||
from shapely.geometry import LineString
|
from shapely.geometry import LineString
|
||||||
|
|
||||||
"""
|
import actions
|
||||||
This module is used for loading simulation data
|
from config import Config
|
||||||
"""
|
from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission
|
||||||
|
|
||||||
|
|
||||||
class Data:
|
class Data:
|
||||||
@ -115,13 +116,13 @@ class Data:
|
|||||||
logics.append(Logic(l, phases))
|
logics.append(Logic(l, phases))
|
||||||
area.add_tl(TrafficLight(tl_id, logics))
|
area.add_tl(TrafficLight(tl_id, logics))
|
||||||
|
|
||||||
def save(self,dump_name):
|
def save(self, dump_name):
|
||||||
"""
|
"""
|
||||||
Save simulation data into a json file
|
Save simulation data into a json file
|
||||||
:param dump_name: The name of your data dump
|
:param dump_name: The name of your data dump
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
dump_dir = 'dump'
|
dump_dir = 'files/dump'
|
||||||
if not os.path.exists(dump_dir):
|
if not os.path.exists(dump_dir):
|
||||||
os.mkdir(dump_dir)
|
os.mkdir(dump_dir)
|
||||||
|
|
||||||
|
@ -4,6 +4,10 @@ Created on 17 oct. 2018
|
|||||||
@author: Axel Huynh-Phuc, Thibaud Gasser
|
@author: Axel Huynh-Phuc, Thibaud Gasser
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
This module defines the entry point of the application
|
||||||
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import csv
|
import csv
|
||||||
import datetime
|
import datetime
|
||||||
@ -11,20 +15,18 @@ import itertools
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
|
import traci
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
import jsonpickle
|
||||||
from data import Data
|
|
||||||
import actions
|
|
||||||
import traci
|
|
||||||
from config import Config
|
|
||||||
from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission
|
|
||||||
from parse import search
|
from parse import search
|
||||||
from shapely.geometry import LineString
|
from shapely.geometry import LineString
|
||||||
|
|
||||||
"""
|
import actions
|
||||||
This module defines the entry point of the application
|
from config import Config
|
||||||
"""
|
from data import Data
|
||||||
|
from model import Area, Vehicle, Lane, TrafficLight, Phase, Logic, Emission
|
||||||
|
|
||||||
|
|
||||||
def compute_vehicle_emissions(veh_id):
|
def compute_vehicle_emissions(veh_id):
|
||||||
"""
|
"""
|
||||||
@ -133,129 +135,3 @@ def export_data_to_csv(config, grid):
|
|||||||
em_for_step = (f'{a.emissions_by_step[step].value():.3f}' for a in grid)
|
em_for_step = (f'{a.emissions_by_step[step].value():.3f}' for a in grid)
|
||||||
writer.writerow(itertools.chain((step,), em_for_step))
|
writer.writerow(itertools.chain((step,), em_for_step))
|
||||||
|
|
||||||
|
|
||||||
def run(config, logger, csv_export, dump_name):
|
|
||||||
"""
|
|
||||||
Run the simulation with the configuration chosen
|
|
||||||
:param config: The simulation configuration
|
|
||||||
:param logger: The simulation logger
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
|
|
||||||
try:
|
|
||||||
traci.start(config.sumo_cmd)
|
|
||||||
logger.info(f'Loaded simulation file : {config._SUMOCFG}')
|
|
||||||
logger.info('Loading data for the simulation')
|
|
||||||
start = time.perf_counter()
|
|
||||||
|
|
||||||
data = Data(traci.simulation.getNetBoundary(), config)
|
|
||||||
data.init_grid()
|
|
||||||
data.add_data_to_areas()
|
|
||||||
data.save(dump_name)
|
|
||||||
|
|
||||||
loading_time = round(time.perf_counter() - start, 2)
|
|
||||||
logger.info(f'Data loaded ({loading_time}s)')
|
|
||||||
|
|
||||||
logger.info('Simulation started...')
|
|
||||||
step = 0
|
|
||||||
while step < config.n_steps:
|
|
||||||
traci.simulationStep()
|
|
||||||
|
|
||||||
vehicles = get_all_vehicles()
|
|
||||||
get_emissions(data.grid, vehicles, step, config, logger)
|
|
||||||
step += 1
|
|
||||||
|
|
||||||
print(f'step = {step}/{config.n_steps}', end='\r')
|
|
||||||
|
|
||||||
finally:
|
|
||||||
traci.close(False)
|
|
||||||
|
|
||||||
if csv_export:
|
|
||||||
export_data_to_csv(config, data.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()
|
|
||||||
for area in data.grid:
|
|
||||||
total_emissions += area.sum_all_emissions()
|
|
||||||
|
|
||||||
logger.info(f'Total emissions = {total_emissions.value()} mg')
|
|
||||||
|
|
||||||
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 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} %')
|
|
||||||
logger.info(f'-> CO2 emissions = {get_reduction_percentage(ref.co2, total_emissions.co2)} %')
|
|
||||||
logger.info(f'-> CO emissions = {get_reduction_percentage(ref.co, total_emissions.co)} %')
|
|
||||||
logger.info(f'-> Nox emissions = {get_reduction_percentage(ref.nox, total_emissions.nox)} %')
|
|
||||||
logger.info(f'-> HC emissions = {get_reduction_percentage(ref.hc, total_emissions.hc)} %')
|
|
||||||
logger.info(f'-> PMx emissions = {get_reduction_percentage(ref.pmx, total_emissions.pmx)} %')
|
|
||||||
|
|
||||||
|
|
||||||
def add_options(parser):
|
|
||||||
"""
|
|
||||||
Add command line options
|
|
||||||
:param parser: The command line parser
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
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("-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="Show UI")
|
|
||||||
parser.add_argument("-csv", "--csv", action="store_true",
|
|
||||||
help="Export all data emissions into a CSV file")
|
|
||||||
|
|
||||||
|
|
||||||
def main(args):
|
|
||||||
"""
|
|
||||||
The entry point of the application
|
|
||||||
:param args: Command line options
|
|
||||||
:return:
|
|
||||||
"""
|
|
||||||
parser = argparse.ArgumentParser(description="")
|
|
||||||
add_options(parser)
|
|
||||||
args = parser.parse_args(args)
|
|
||||||
|
|
||||||
config = Config()
|
|
||||||
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
|
|
||||||
logger.info(f'Reference simulation')
|
|
||||||
|
|
||||||
if args.steps:
|
|
||||||
config.n_steps = args.steps
|
|
||||||
|
|
||||||
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, csv_export, dump_name)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main(sys.argv[1:])
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"_SUMOCMD": "sumo",
|
"_SUMOCMD": "sumo",
|
||||||
"_SUMOCFG": "simulations/mulhouse_simulation/osm.sumocfg",
|
"_SUMOCFG": "files/simulations/mulhouse_simulation/osm.sumocfg",
|
||||||
|
|
||||||
"areas_number": 10,
|
"areas_number": 10,
|
||||||
"emissions_threshold": 500000,
|
"emissions_threshold": 500000,
|
Before Width: | Height: | Size: 489 KiB After Width: | Height: | Size: 489 KiB |
@ -4,17 +4,17 @@ Created on 17 oct. 2018
|
|||||||
@author: Axel Huynh-Phuc, Thibaud Gasser
|
@author: Axel Huynh-Phuc, Thibaud Gasser
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
This module defines the business model of our application
|
||||||
|
"""
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
"""
|
|
||||||
This module defines the business model of our application
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
class Lane:
|
class Lane:
|
||||||
|
152
sumo_project/runner.py
Normal file
152
sumo_project/runner.py
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
'''
|
||||||
|
Created on 19 janv. 2019
|
||||||
|
|
||||||
|
@author: Admin
|
||||||
|
'''
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import traci
|
||||||
|
|
||||||
|
import jsonpickle
|
||||||
|
|
||||||
|
from config import Config
|
||||||
|
from data import Data
|
||||||
|
import emissions
|
||||||
|
from model import Emission
|
||||||
|
|
||||||
|
|
||||||
|
def add_options(parser):
|
||||||
|
"""
|
||||||
|
Add command line options
|
||||||
|
:param parser: The command line parser
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
parser.add_argument("-new_dump", "--new_dump", metavar=('config_file', 'dump_name'), nargs=2, type=str,
|
||||||
|
required=False, help='Load an create a new data dump with the configuration file chosen')
|
||||||
|
parser.add_argument("-run", "--run", type=str, required=False,
|
||||||
|
help='Run a simulation with the dump chosen')
|
||||||
|
|
||||||
|
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="Show UI")
|
||||||
|
parser.add_argument("-csv", "--csv", action="store_true",
|
||||||
|
help="Export all data emissions into a CSV file")
|
||||||
|
|
||||||
|
|
||||||
|
def create_dump(config_file, dump_name):
|
||||||
|
|
||||||
|
config = Config()
|
||||||
|
config.import_config_file(config_file)
|
||||||
|
config.check_config()
|
||||||
|
config.init_traci()
|
||||||
|
|
||||||
|
"""with open(f'dump/{dump_name}.json', 'r') as f:
|
||||||
|
data = jsonpickle.decode(f.read())"""
|
||||||
|
|
||||||
|
traci.start(config.sumo_cmd)
|
||||||
|
if not os.path.isfile(f'files/dump/{dump_name}.json'):
|
||||||
|
start = time.perf_counter()
|
||||||
|
data = Data(traci.simulation.getNetBoundary(), config)
|
||||||
|
data.init_grid()
|
||||||
|
data.add_data_to_areas()
|
||||||
|
data.save(dump_name)
|
||||||
|
|
||||||
|
loading_time = round(time.perf_counter() - start, 2)
|
||||||
|
print(f'Data loaded ({loading_time}s)')
|
||||||
|
print(f'Dump {dump_name} created')
|
||||||
|
else:
|
||||||
|
print(f'Dump with name {dump_name} already exist')
|
||||||
|
|
||||||
|
traci.close(False)
|
||||||
|
|
||||||
|
|
||||||
|
def run(data : Data, config : Config, logger):
|
||||||
|
try:
|
||||||
|
traci.start(config.sumo_cmd)
|
||||||
|
logger.info(f'Loaded simulation file : {config._SUMOCFG}')
|
||||||
|
logger.info('Loading data for the simulation')
|
||||||
|
start = time.perf_counter()
|
||||||
|
|
||||||
|
logger.info('Simulation started...')
|
||||||
|
step = 0
|
||||||
|
while step < config.n_steps:
|
||||||
|
traci.simulationStep()
|
||||||
|
|
||||||
|
vehicles = emissions.get_all_vehicles()
|
||||||
|
emissions.get_emissions(data.grid, vehicles, step, config, logger)
|
||||||
|
step += 1
|
||||||
|
|
||||||
|
print(f'step = {step}/{config.n_steps}', end='\r')
|
||||||
|
|
||||||
|
finally:
|
||||||
|
traci.close(False)
|
||||||
|
|
||||||
|
|
||||||
|
def main(args):
|
||||||
|
"""
|
||||||
|
The entry point of the application
|
||||||
|
:param args: Command line options
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
add_options(parser)
|
||||||
|
args = parser.parse_args(args)
|
||||||
|
|
||||||
|
if args.new_dump is not None:
|
||||||
|
create_dump(args.new_dump[0], args.new_dump[1])
|
||||||
|
|
||||||
|
if args.run is not None:
|
||||||
|
dump_path = f'files/dump/{args.run}.json'
|
||||||
|
if not os.path.isfile(dump_path):
|
||||||
|
with open(dump_path, 'r') as f:
|
||||||
|
data = jsonpickle.decode(f.read())
|
||||||
|
config = data.config
|
||||||
|
logger = config.init_logger(save_logs=args.save)
|
||||||
|
logger.info(f'Running simulation dump {args.run}...')
|
||||||
|
if args.ref:
|
||||||
|
config.without_actions_mode = True
|
||||||
|
logger.info(f'Reference simulation')
|
||||||
|
if args.steps: config.n_steps = args.steps
|
||||||
|
if args.gui: config._SUMOCMD = "sumo-gui"
|
||||||
|
|
||||||
|
start = time.perf_counter()
|
||||||
|
run(data, config, logger)
|
||||||
|
|
||||||
|
if args.csv:
|
||||||
|
emissions.export_data_to_csv(config, data.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()
|
||||||
|
for area in data.grid:
|
||||||
|
total_emissions += area.sum_all_emissions()
|
||||||
|
|
||||||
|
logger.info(f'Total emissions = {total_emissions.value()} mg')
|
||||||
|
|
||||||
|
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 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} %')
|
||||||
|
logger.info(f'-> CO2 emissions = {emissions.get_reduction_percentage(ref.co2, total_emissions.co2)} %')
|
||||||
|
logger.info(f'-> CO emissions = {emissions.get_reduction_percentage(ref.co, total_emissions.co)} %')
|
||||||
|
logger.info(f'-> Nox emissions = {emissions.get_reduction_percentage(ref.nox, total_emissions.nox)} %')
|
||||||
|
logger.info(f'-> HC emissions = {emissions.get_reduction_percentage(ref.hc, total_emissions.hc)} %')
|
||||||
|
logger.info(f'-> PMx emissions = {emissions.get_reduction_percentage(ref.pmx, total_emissions.pmx)} %')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(sys.argv[1:])
|
Loading…
Reference in New Issue
Block a user