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

Changed vehicle class

This commit is contained in:
Ahp06 2018-11-20 13:52:23 +01:00
parent 77d196d6c3
commit 4c0ffc3778
4 changed files with 22 additions and 16 deletions

View File

@ -23,15 +23,14 @@ def lanes_in_area(area):
def computeEdgeWeight(edge_id): def computeEdgeWeight(edge_id):
return traci.edge.getCO2Emission(edge_id) return traci.edge.getCO2Emission(edge_id)
#Useless because vehicles reroute automatically by travelTime def rerouteEffortVehicles():
def rerouteVehicles():
for veh_id in traci.vehicle.getIDList(): for veh_id in traci.vehicle.getIDList():
traci.vehicle.rerouteTraveltime(veh_id,True) traci.vehicle.rerouteEffort(veh_id)
def adjustEdgesWeight(): def adjustEdgesWeight():
for edge_id in traci.edge.getIDList(): for edge_id in traci.edge.getIDList():
weight = computeEdgeWeight(edge_id) #by default edges weight = length/mean speed weight = computeEdgeWeight(edge_id) #by default edges weight = length/mean speed
traci.edge.adaptTraveltime(edge_id, weight) traci.edge.setEffort(edge_id, weight)
def lock_area(area: Area, vehicles: Iterable[Vehicle]): def lock_area(area: Area, vehicles: Iterable[Vehicle]):
max_speed = 30 max_speed = 30

View File

@ -15,7 +15,7 @@ _SUMOCMD = 'sumo' # use 'sumo-gui' cmd for UI
_SUMOCFG = "mulhouse_simulation/osm.sumocfg" _SUMOCFG = "mulhouse_simulation/osm.sumocfg"
CELLS_NUMBER = 10 CELLS_NUMBER = 10
CO2_THRESHOLD = 500000 CO2_THRESHOLD = 500000
n_steps = 400 n_steps = 200
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]

View File

@ -24,13 +24,19 @@ def init_grid(simulation_bounds, cells_number):
traci.polygon.add(area.name, ar_bounds, (0, 255, 0)) traci.polygon.add(area.name, ar_bounds, (0, 255, 0))
return areas return areas
def compute_vehicle_emissions(veh_id):
return (traci.vehicle.getCOEmission(veh_id)
+ traci.vehicle.getNOxEmission(veh_id)
+ traci.vehicle.getHCEmission(veh_id)
+ traci.vehicle.getPMxEmission(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():
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.co2 = traci.vehicle.getCO2Emission(vehicle.veh_id) vehicle.emissions = compute_vehicle_emissions(veh_id)
vehicles.append(vehicle) vehicles.append(vehicle)
return vehicles return vehicles
@ -47,7 +53,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
for area in grid: for area in grid:
for vehicle in vehicles: for vehicle in vehicles:
if vehicle.pos in area: if vehicle.pos in area:
area.emissions += vehicle.co2 area.emissions += vehicle.emissions
if area.emissions > config.CO2_THRESHOLD and area.locked == False: if area.emissions > config.CO2_THRESHOLD and area.locked == False:
actions.lock_area(area, vehicles) actions.lock_area(area, vehicles)
traci.polygon.setColor(area.name, (255, 0, 0)) traci.polygon.setColor(area.name, (255, 0, 0))
@ -75,7 +81,7 @@ def main():
vehicles = get_all_vehicles() vehicles = get_all_vehicles()
get_emissions(grid, vehicles) get_emissions(grid, vehicles)
#actions.adjustEdgesWeight() #actions.adjustEdgesWeight()
#actions.rerouteEffortVehicles()
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')
@ -86,12 +92,12 @@ def main():
for area in areas: for area in areas:
total_emissions += area.emissions total_emissions += area.emissions
#For 200 steps, total emissions = 42816869.054364316 mg #Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas
#For 400 steps, total emissions = 136020579.71122485 mg total_emissions200 = 43970763.15084749
print(f'\n**** Total emissions (CO2) = {total_emissions} mg') print(f'\n**** Total emissions = {total_emissions} mg ****')
diff_with_lock = (136020579.71122485 - total_emissions)/136020579.71122485 diff_with_lock = (total_emissions200 - total_emissions)/total_emissions200
print(f'**** Reduction percentage of CO2 emissions = {diff_with_lock*100} % ****\n') print(f'**** Reduction percentage of emissions = {diff_with_lock*100} % ****\n')
traci.close(False) traci.close(False)

View File

@ -56,9 +56,10 @@ 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.co2: float = None self.emissions: float = None
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__)