mirror of
https://github.com/Ahp06/SUMO_Emissions.git
synced 2024-11-24 20:46:29 +00:00
Changed code structure
This commit is contained in:
parent
3c681652ec
commit
7972f4d794
@ -15,13 +15,6 @@ def remove_vehicle(veh_id):
|
||||
traci.vehicle.remove(veh_id, traci.constants.REMOVE_PARKING)
|
||||
|
||||
|
||||
def lanes_in_area(area):
|
||||
for lane_id in traci.lane.getIDList():
|
||||
polygon_lane = LineString(traci.lane.getShape(lane_id))
|
||||
if area.rectangle.intersects(polygon_lane):
|
||||
yield lane_id
|
||||
|
||||
|
||||
def compute_edge_weight(edge_id):
|
||||
return (traci.edge.getCOEmission(edge_id)
|
||||
+ traci.edge.getNOxEmission(edge_id)
|
||||
@ -36,9 +29,14 @@ def adjust_edges_weights():
|
||||
traci.edge.adaptTraveltime(edge_id, weight)
|
||||
|
||||
|
||||
def lock_area(area: Area):
|
||||
max_speed = 30
|
||||
def limit_speed_into_area(area: Area, vehicles: Iterable[Vehicle], max_speed):
|
||||
print(f'Setting max speed into {area.name} to {max_speed} km/h')
|
||||
area.locked = True
|
||||
for lane in area._lanes:
|
||||
traci.lane.setMaxSpeed(lane.lane_id, max_speed/3.6)
|
||||
|
||||
|
||||
def adjust_traffic_light_phase_duration():
|
||||
'''for tl_id in traci.trafficlight.getIDList():
|
||||
print(traci.trafficlight.getCompleteRedYellowGreenDefinition(tl_id))'''
|
||||
|
@ -23,4 +23,9 @@ routing_mode = False
|
||||
sumo_binary = os.path.join(os.environ['SUMO_HOME'], 'bin', _SUMOCMD)
|
||||
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'))
|
||||
|
||||
|
@ -39,7 +39,6 @@ def get_all_vehicles() -> List[Vehicle]:
|
||||
veh_pos = traci.vehicle.getPosition(veh_id)
|
||||
vehicle = Vehicle(veh_id, veh_pos)
|
||||
vehicle.emissions = compute_vehicle_emissions(veh_id)
|
||||
traci.vehicle.setRoutingMode(veh_id, traci.constants.ROUTING_MODE_AGGREGATED)
|
||||
vehicles.append(vehicle)
|
||||
return vehicles
|
||||
|
||||
@ -58,7 +57,7 @@ def get_emissions(grid: List[Area], vehicles: List[Vehicle]):
|
||||
if vehicle.pos in area:
|
||||
area.emissions += vehicle.emissions
|
||||
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.setFilled(area.name, True)
|
||||
|
||||
@ -78,6 +77,8 @@ def main():
|
||||
grid = init_grid(traci.simulation.getNetBoundary(), config.CELLS_NUMBER)
|
||||
add_lanes_to_areas(grid)
|
||||
|
||||
actions.adjust_traffic_light_phase_duration()
|
||||
|
||||
step = 0
|
||||
while step < config.n_steps : #traci.simulation.getMinExpectedNumber() > 0:
|
||||
traci.simulationStep()
|
||||
@ -90,7 +91,8 @@ def main():
|
||||
# actions.rerouteAllVehicles()
|
||||
|
||||
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()
|
||||
|
||||
finally:
|
||||
@ -103,10 +105,11 @@ def main():
|
||||
#Total of emissions of all pollutants in mg for 200 steps of simulation without locking areas
|
||||
total_emissions200 = 43970763.15084749
|
||||
|
||||
print(f'\n**** Total emissions = {total_emissions} mg ****')
|
||||
print("\n**** RESULTS ****")
|
||||
print(f'Total emissions = {total_emissions} mg')
|
||||
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} %')
|
||||
print("With the configuration :\n" + str(config.showConfig()))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Reference in New Issue
Block a user