mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-22 03:26:30 +00:00
Reformat code (#3)
* Reformat code * Replace global variable areas with local one
This commit is contained in:
parent
f603a51bb3
commit
3c681652ec
@ -14,28 +14,31 @@ 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 lanes_in_area(area):
|
||||||
for lane_id in traci.lane.getIDList():
|
for lane_id in traci.lane.getIDList():
|
||||||
polygon_lane = LineString(traci.lane.getShape(lane_id))
|
polygon_lane = LineString(traci.lane.getShape(lane_id))
|
||||||
if area.rectangle.intersects(polygon_lane):
|
if area.rectangle.intersects(polygon_lane):
|
||||||
yield lane_id
|
yield lane_id
|
||||||
|
|
||||||
def computeEdgeWeight(edge_id):
|
|
||||||
return (traci.edge.getCOEmission(edge_id)
|
|
||||||
+ traci.edge.getNOxEmission(edge_id)
|
|
||||||
+ traci.edge.getHCEmission(edge_id)
|
|
||||||
+ traci.edge.getPMxEmission(edge_id)
|
|
||||||
+ traci.edge.getCO2Emission(edge_id))
|
|
||||||
|
|
||||||
def adjustEdgesWeight():
|
|
||||||
for edge_id in traci.edge.getIDList():
|
|
||||||
weight = computeEdgeWeight(edge_id) #by default edges weight = length/mean speed
|
|
||||||
traci.edge.adaptTraveltime(edge_id, weight)
|
|
||||||
|
|
||||||
def lock_area(area: Area, vehicles: Iterable[Vehicle]):
|
def compute_edge_weight(edge_id):
|
||||||
|
return (traci.edge.getCOEmission(edge_id)
|
||||||
|
+ traci.edge.getNOxEmission(edge_id)
|
||||||
|
+ traci.edge.getHCEmission(edge_id)
|
||||||
|
+ traci.edge.getPMxEmission(edge_id)
|
||||||
|
+ traci.edge.getCO2Emission(edge_id))
|
||||||
|
|
||||||
|
|
||||||
|
def adjust_edges_weights():
|
||||||
|
for edge_id in traci.edge.getIDList():
|
||||||
|
weight = compute_edge_weight(edge_id) # by default edges weight = length/mean speed
|
||||||
|
traci.edge.adaptTraveltime(edge_id, weight)
|
||||||
|
|
||||||
|
|
||||||
|
def lock_area(area: Area):
|
||||||
max_speed = 30
|
max_speed = 30
|
||||||
print(f'Setting max speed into {area.name} to {max_speed} km/h')
|
print(f'Setting max speed into {area.name} to {max_speed} km/h')
|
||||||
area.locked = True
|
area.locked = True
|
||||||
for lane in area._lanes:
|
for lane in area._lanes:
|
||||||
traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6)
|
traci.lane.setMaxSpeed(lane.lane_id, max_speed / 3.6)
|
||||||
|
|
||||||
|
@ -8,9 +8,9 @@ import config
|
|||||||
import sys
|
import sys
|
||||||
from model import Area, Vehicle, Lane
|
from model import Area, Vehicle, Lane
|
||||||
|
|
||||||
areas = list()
|
|
||||||
|
|
||||||
def init_grid(simulation_bounds, cells_number):
|
def init_grid(simulation_bounds, cells_number):
|
||||||
|
grid = list()
|
||||||
width = simulation_bounds[1][0] / cells_number
|
width = simulation_bounds[1][0] / cells_number
|
||||||
height = simulation_bounds[1][1] / cells_number
|
height = simulation_bounds[1][1] / cells_number
|
||||||
for i in range(cells_number):
|
for i in range(cells_number):
|
||||||
@ -20,17 +20,19 @@ def init_grid(simulation_bounds, cells_number):
|
|||||||
((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height))
|
((i + 1) * width, (j + 1) * height), ((i + 1) * width, j * height))
|
||||||
area = Area(ar_bounds)
|
area = Area(ar_bounds)
|
||||||
area.name = 'area ({},{})'.format(i, j)
|
area.name = 'area ({},{})'.format(i, j)
|
||||||
areas.append(area)
|
grid.append(area)
|
||||||
traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
|
traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
|
||||||
return areas
|
return grid
|
||||||
|
|
||||||
|
|
||||||
def compute_vehicle_emissions(veh_id):
|
def compute_vehicle_emissions(veh_id):
|
||||||
return (traci.vehicle.getCOEmission(veh_id)
|
return (traci.vehicle.getCOEmission(veh_id)
|
||||||
+ traci.vehicle.getNOxEmission(veh_id)
|
+ traci.vehicle.getNOxEmission(veh_id)
|
||||||
+ traci.vehicle.getHCEmission(veh_id)
|
+ traci.vehicle.getHCEmission(veh_id)
|
||||||
+ traci.vehicle.getPMxEmission(veh_id)
|
+ traci.vehicle.getPMxEmission(veh_id)
|
||||||
+ traci.vehicle.getCO2Emission(veh_id))
|
+ traci.vehicle.getCO2Emission(veh_id))
|
||||||
|
|
||||||
|
|
||||||
def get_all_vehicles() -> List[Vehicle]:
|
def get_all_vehicles() -> List[Vehicle]:
|
||||||
vehicles = list()
|
vehicles = list()
|
||||||
for veh_id in traci.vehicle.getIDList():
|
for veh_id in traci.vehicle.getIDList():
|
||||||
@ -55,8 +57,8 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
|
|||||||
for vehicle in vehicles:
|
for vehicle in vehicles:
|
||||||
if vehicle.pos in area:
|
if vehicle.pos in area:
|
||||||
area.emissions += vehicle.emissions
|
area.emissions += vehicle.emissions
|
||||||
if config.lock_mode == True and area.emissions > config.EMISSIONS_THRESHOLD and area.locked == False:
|
if config.lock_mode and area.emissions > config.EMISSIONS_THRESHOLD and not area.locked:
|
||||||
actions.lock_area(area, vehicles)
|
actions.lock_area(area)
|
||||||
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)
|
||||||
|
|
||||||
@ -70,40 +72,39 @@ def add_lanes_to_areas(areas: List[Area]):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
grid = list()
|
||||||
try:
|
try:
|
||||||
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
|
||||||
|
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()
|
||||||
get_emissions(grid, vehicles)
|
get_emissions(grid, vehicles)
|
||||||
|
|
||||||
if config.routing_mode == True:
|
if config.routing_mode:
|
||||||
actions.adjustEdgesWeight()
|
actions.adjust_edges_weights()
|
||||||
actions.rerouteAllVehicles()
|
# actions.rerouteAllVehicles()
|
||||||
|
|
||||||
step += 1
|
step += 1
|
||||||
sys.stdout.write(f'Simulation step = {step}/{config.n_steps}'+'\r')
|
sys.stdout.write(f'Simulation step = {step}/{config.n_steps}' + '\r')
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
traci.close(False)
|
traci.close(False)
|
||||||
|
|
||||||
total_emissions = 0
|
total_emissions = 0
|
||||||
for area in areas:
|
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(f'\n**** Total emissions = {total_emissions} mg ****')
|
||||||
diff_with_lock = (total_emissions200 - total_emissions)/total_emissions200
|
diff_with_lock = (total_emissions200 - total_emissions) / total_emissions200
|
||||||
print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n')
|
print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n')
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,10 +56,9 @@ class Area:
|
|||||||
class Vehicle:
|
class Vehicle:
|
||||||
|
|
||||||
def __init__(self, veh_id: int, pos: Tuple[float, float]):
|
def __init__(self, veh_id: int, pos: Tuple[float, float]):
|
||||||
self.emissions: float = None
|
self.emissions: float = 0.0
|
||||||
self.veh_id = veh_id
|
self.veh_id = veh_id
|
||||||
self.pos = Point(pos)
|
self.pos = Point(pos)
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return str(self.__dict__)
|
return str(self.__dict__)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user