1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-21 19:16:30 +00:00

Externalize configuration in config.py

This commit is contained in:
Thibaud Gasser 2018-11-14 14:56:54 +01:00
parent 6c36eafe09
commit 7cbc1d85a0
2 changed files with 27 additions and 18 deletions

22
sumo_project/config.py Normal file
View File

@ -0,0 +1,22 @@
"""
Global configuration for the simulation
"""
import os
import sys
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
_SUMOCMD = 'sumo-gui'
_SUMOCFG = "mulhouse_simulation/map.sumocfg"
CELLS_NUMBER = 10
CO2_THRESHOLD = 500000
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
sumo_cmd = [sumo_binary, "-c", _SUMOCFG]

View File

@ -1,22 +1,9 @@
import os
import sys
import traci
import config
from SUMOFactory import SUMOFactory
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("please declare environment variable 'SUMO_HOME'")
import traci
from area import Area
sumoBinary = os.path.join(os.environ['SUMO_HOME'], 'bin', 'sumo-gui')
sumoCmd = [sumoBinary, "-c", "mulhouse_simulation/map.sumocfg"]
CELLS_NUMBER = 10
CO2_THRESHOLD = 500000
def init_grid(simulation_bounds, cells_number):
width = simulation_bounds[1][0] / cells_number
@ -46,7 +33,7 @@ def emission_for_area(area):
def get_emissions(areas, factory):
for area in areas:
emission_for_area(area)
if area.emissions > CO2_THRESHOLD:
if area.emissions > config.CO2_THRESHOLD:
# print(f'Threshold exceeded in {area.name} : {area.emissions}')
factory.lock_area(area)
traci.polygon.setColor(area.name, (255, 0, 0))
@ -55,8 +42,8 @@ def get_emissions(areas, factory):
def main():
try:
traci.start(sumoCmd)
grid = init_grid(traci.simulation.getNetBoundary(), CELLS_NUMBER)
traci.start(config.sumo_cmd)
grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
while traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep()
get_emissions(grid, SUMOFactory())