1
0
mirror of https://github.com/Ahp06/SUMO_Emissions.git synced 2024-11-22 03:26:30 +00:00

Changed code structure

This commit is contained in:
Ahp06 2018-11-23 13:53:20 +01:00
parent 3c681652ec
commit 7972f4d794
3 changed files with 91 additions and 85 deletions

View File

@ -1,44 +1,42 @@
""" """
Created on 17 oct. 2018 Created on 17 oct. 2018
@author: Axel Huynh-Phuc, Thibaud Gasser @author: Axel Huynh-Phuc, Thibaud Gasser
""" """
from typing import Iterable from typing import Iterable
import traci import traci
from shapely.geometry.linestring import LineString from shapely.geometry.linestring import LineString
from model import Area, Vehicle from model import Area, Vehicle
def remove_vehicle(veh_id): def remove_vehicle(veh_id):
traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING) traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING)
def lanes_in_area(area): def compute_edge_weight(edge_id):
for lane_id in traci.lane.getIDList(): return (traci.edge.getCOEmission(edge_id)
polygon_lane = LineString(traci.lane.getShape(lane_id)) + traci.edge.getNOxEmission(edge_id)
if area.rectangle.intersects(polygon_lane): + traci.edge.getHCEmission(edge_id)
yield lane_id + traci.edge.getPMxEmission(edge_id)
+ traci.edge.getCO2Emission(edge_id))
def compute_edge_weight(edge_id):
return (traci.edge.getCOEmission(edge_id) def adjust_edges_weights():
+ traci.edge.getNOxEmission(edge_id) for edge_id in traci.edge.getIDList():
+ traci.edge.getHCEmission(edge_id) weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed
+ traci.edge.getPMxEmission(edge_id) traci.edge.adaptTraveltime(edge_id, weight)
+ traci.edge.getCO2Emission(edge_id))
def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], max_speed):
def adjust_edges_weights(): print(f'Setting max speed into {area.name} to {max_speed} km/h')
for edge_id in traci.edge.getIDList(): area.locked = True
weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed for lane in area._lanes:
traci.edge.adaptTraveltime(edge_id, weight) traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6)
def lock_area(area: Area): def adjust_traffic_light_phase_duration():
max_speed = 30 '''for tl_id in traci.trafficlight.getIDList():
print(f'Setting max speed into {area.name} to {max_speed} km/h') print(traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id))'''
area.locked = True
for lane in area._lanes:
traci.lane.setMaxSpeed(lane.lane_id, max_speed / 3.6)

View File

@ -1,26 +1,31 @@
""" """
Global configuration for the simulation Global configuration for the simulation
""" """
import os import os
import sys import sys
if 'SUMO_HOME' in os.environ: if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools') tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools) sys.path.append(tools)
else: else:
sys.exit("please declare environment variable 'SUMO_HOME'") sys.exit("please declare environment variable 'SUMO_HOME'")
_SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI _SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI
_SUMOCFG = "mulhouse_simulation/osm.sumocfg" _SUMOCFG = "mulhouse_simulation/osm.sumocfg"
CELLS_NUMBER = 10 CELLS_NUMBER = 10
EMISSIONS_THRESHOLD = 500000 EMISSIONS_THRESHOLD = 500000
n_steps = 200 n_steps = 200
lock_mode = True lock_mode = True
routing_mode = False routing_mode = False
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD) sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
sumo_cmd = [sumo_binary, "-c", _SUMOCFG] sumo_cmd = [sumo_binary, "-c", _SUMOCFG]
def showConfig():
return (str(f'Grid : {CELLS_NUMBER}x{CELLS_NUMBER}\n')
+ str(f'step number = {n_steps}\n')
+ str(f'lock mode = {lock_mode}\n')
+ str(f'routing mode = {routing_mode}\n'))

View File

@ -39,7 +39,6 @@ def get_all_vehicles() -> List[Vehicle]:
veh_pos = traci.vehicle.getPosition(veh_id) veh_pos = traci.vehicle.getPosition(veh_id)
vehicle = Vehicle(veh_id, veh_pos) vehicle = Vehicle(veh_id, veh_pos)
vehicle.emissions = compute_vehicle_emissions(veh_id) vehicle.emissions = compute_vehicle_emissions(veh_id)
traci.vehicle.setRoutingMode(veh_id, traci.constants.ROUTING_MODE_AGGREGATED)
vehicles.append(vehicle) vehicles.append(vehicle)
return vehicles return vehicles
@ -58,7 +57,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
if vehicle.pos in area: if vehicle.pos in area:
area.emissions += vehicle.emissions area.emissions += vehicle.emissions
if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked: if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked:
actions.lock_area(area) actions.limit_speed_into_area(area, vehicles,30)
traci.polygon.setColor(area.name, (255, 0, 0)) traci.polygon.setColor(area.name, (255, 0, 0))
traci.polygon.setFilled(area.name, True) traci.polygon.setFilled(area.name, True)
@ -77,9 +76,11 @@ def main():
traci.start(config.sumo_cmd) traci.start(config.sumo_cmd)
grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER) grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
add_lanes_to_areas(grid) add_lanes_to_areas(grid)
step = 0 actions.adjust_traffic_light_phase_duration()
while step < config.n_steps: # traci.simulation.getMinExpectedNumber() > 0:
step = 0
while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
traci.simulationStep() traci.simulationStep()
vehicles = get_all_vehicles() vehicles = get_all_vehicles()
@ -90,7 +91,8 @@ def main():
# actions.rerouteAllVehicles() # actions.rerouteAllVehicles()
step += 1 step += 1
sys.stdout.write(f'Simulation step = {step}/{config.n_steps}' + '\r') progress = round(step/config.n_steps*100,2)
sys.stdout.write(f'Progress : {progress}%'+'\r')
sys.stdout.flush() sys.stdout.flush()
finally: finally:
@ -99,14 +101,15 @@ def main():
total_emissions = 0 total_emissions = 0
for area in grid: for area in grid:
total_emissions += area.emissions total_emissions += area.emissions
# Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas #Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas
total_emissions200 = 43970763.15084749 total_emissions200 = 43970763.15084749
print(f'\n**** Total emissions = {total_emissions} mg ****') print("\n**** RESULTS ****")
diff_with_lock = (total_emissions200 - total_emissions) / total_emissions200 print(f'Total emissions = {total_emissions} mg')
print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n') diff_with_lock = (total_emissions200 - total_emissions)/total_emissions200
print(f'Reduction percentage of emissions = {diff_with_lock*100} %')
print("With the configuration :\n" + str(config.showConfig()))
if __name__ == '__main__': if __name__ == '__main__':
main() main()