This repository has been archived by the owner on Dec 5, 2022. It is now read-only.
/
SUMOscope.py
executable file
·163 lines (134 loc) · 5.49 KB
/
SUMOscope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import absolute_import
from __future__ import print_function
import json
import os
from subprocess import call
import sys
import time
import requests
try:
sys.path.append(os.path.join(os.path.dirname(
__file__), '..', '..', '..', '..', "tools")) # tutorial in tests
sys.path.append(os.path.join(os.environ.get("SUMO_HOME", os.path.join(
os.path.dirname(__file__), "..", "..", "..")), "tools")) # tutorial in docs
from sumolib import checkBinary # noqa
except ImportError:
sys.exit("please declare environment variable 'SUMO_HOME'")
from sumolib import checkBinary # noqa
from sumolib import net # noqa
import traci # noqa
import randomTrips # noqa
class SUMOScope():
'''sumo'''
def __init__(self):
self.network_filename = 'paris'
self.osm_path = 'data/osm/'+self.network_filename+'.map.osm'
self.trips_list = []
self.scatterplot_list = []
# max sim time
self.max_sim_length = 1000
# how much time did the sim took
self.actual_sim_length = 0
self.vehicles_count = 500
self.current_dir = os.path.dirname(os.path.abspath(__file__)) + "/"
def osm_to_sumo_net(self):
self.netconvertBinary = checkBinary('netconvert')
call([self.netconvertBinary, '--osm-files',
self.osm_path,
'-o',
'data/network/net.net.xml',
'--geometry.remove',
'--ramps.guess',
'--keep-edges.by-vclass', 'private, public_transport, public_emergency, public_authority, public_army, vip',
'--junctions.join',
'--tls.guess-signals',
'--tls.discard-simple',
'--tls.join'])
def create_random_demand(self):
''' create random flows'''
randomTrips.main(randomTrips.get_options([
'--flows', str(self.vehicles_count),
'-b', '0',
'-e', '1',
'-n', 'data/network/net.net.xml',
'-o', 'data/demand/demand.xml',
'--jtrrouter',
'--trip-attributes', 'departPos="random" departSpeed="max"']))
def create_routes(self):
'''create routes'''
self.jtrrouterBinary = checkBinary('jtrrouter')
call([self.jtrrouterBinary, '-c',
'data/routes/routes_config.jtrrcfg'])
def existing_car_bool(self, veh_id):
'''returns if a car is in list already'''
return any(i['id'] == [str(veh_id)] for i in self.trips_list)
def export_sim_to_json(self):
'''compute sim results from traci'''
self.sumoBinary = checkBinary('sumo')
traci.start([self.sumoBinary, "-c", self.current_dir +
'data/sumo.sumocfg', "-v"])
#
self.run_simulation_loop()
speed_results = {'meta':
{'sim_length': self.actual_sim_length,
'vehicles_count': self.vehicles_count},
'scatterplot': self.scatterplot_list}
trips_results = self.trips_list
with open(self.current_dir+"trips.json", 'w') as outfile:
json.dump(trips_results, outfile)
with open(self.current_dir+"speed.json", 'w') as outfile:
json.dump(speed_results, outfile)
traci.close()
sys.stdout.flush()
def run_simulation_loop(self):
'''compute'''
print('\nrun_simulation_loop...\n')
step = 0
while step < self.max_sim_length and traci.simulation.getMinExpectedNumber() > 0:
print('sim step:', step)
traci.simulationStep()
for veh_id in traci.vehicle.getIDList():
x, y = traci.vehicle.getPosition(veh_id)
lon, lat = traci.simulation.convertGeo(x, y)
car_loc = [lon, lat]
speed = traci.vehicle.getSpeed(veh_id)
max_speed = traci.vehicle.getMaxSpeedLat(veh_id)
# check if behicle is in list alreay
# if so, add locations and timestamps
if self.existing_car_bool(veh_id):
for i in self.trips_list:
if i['id'] == [str(veh_id)]:
i['path'].append(car_loc)
i['timestamps'].append(step)
# else create new vehicle
else:
self.trips_list.append(
{"id": [veh_id], "path": [car_loc], "timestamps": [step]})
self.scatterplot_list.append({
'name': str(veh_id), 'coordinates': car_loc, 'speed': speed, 'maxSpeed': max_speed})
step += 1
self.actual_sim_length = step
def control_vehicle_behavior(vehicle, speed, max_speed, new_taget):
'''
control the behavior of each vehicle
'''
try:
# some time parameter for now
if speed/max_speed < 0.5 and self.actual_sim_length > 100:
traci.vehicle.changeTarget(vehicle, new_taget)
elif self.actual_sim_length > self.max_sim_length/2:
traci.vehicle.changeTarget(vehicle, new_taget)
return vehicle
except traci.TraCIException:
pass
if __name__ == "__main__":
# init the class
sumo = SUMOScope()
# # make network
sumo.osm_to_sumo_net()
# # make random trips
sumo.create_random_demand()
# # make routes
sumo.create_routes()
# # run and save to json
sumo.export_sim_to_json()