-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.py
More file actions
92 lines (74 loc) · 3.77 KB
/
main.py
File metadata and controls
92 lines (74 loc) · 3.77 KB
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
'''
This test file needs the following files:
STR_SUMO.py, RouteController.py, Util.py, test.net.xml, test.rou.xml, myconfig.sumocfg and corresponding SUMO libraries.
'''
from core.STR_SUMO import StrSumo
import os
import sys
from xml.dom.minidom import parse, parseString
from core.Util import *
from controller.RouteController import *
from controller.DijkstraController import DijkstraPolicy
from core.target_vehicles_generation_protocols import *
if 'SUMO_HOME' in os.environ:
tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
sys.path.append(tools)
else:
sys.exit("No environment variable SUMO_HOME!")
from sumolib import checkBinary
import traci
# use vehicle generation protocols to generate vehicle list
def get_controlled_vehicles(route_filename, connection_info, \
num_controlled_vehicles=10, num_uncontrolled_vehicles=20, pattern = 1):
'''
:param @route_filename <str>: the name of the route file to generate
:param @connection_info <object>: an object that includes the map inforamtion
:param @num_controlled_vehicles <int>: the number of vehicles controlled by the route controller
:param @num_uncontrolled_vehicles <int>: the number of vehicles not controlled by the route controller
:param @pattern <int>: one of four possible patterns. FORMAT:
-- CASES BEGIN --
#1. one start point, one destination for all target vehicles
#2. ranged start point, one destination for all target vehicles
#3. ranged start points, ranged destination for all target vehicles
-- CASES ENDS --
'''
vehicle_dict = {}
print(connection_info.net_filename)
generator = target_vehicles_generator(connection_info.net_filename)
# list of target vehicles is returned by generate_vehicles
vehicle_list = generator.generate_vehicles(num_controlled_vehicles, num_uncontrolled_vehicles, \
pattern, route_filename, connection_info.net_filename)
for vehicle in vehicle_list:
vehicle_dict[str(vehicle.vehicle_id)] = vehicle
return vehicle_dict
def test_dijkstra_policy(vehicles):
print("Testing Dijkstra's Algorithm Route Controller")
scheduler = DijkstraPolicy(init_connection_info)
run_simulation(scheduler, vehicles)
def run_simulation(scheduler, vehicles):
simulation = StrSumo(scheduler, init_connection_info, vehicles)
traci.start([sumo_binary, "-c", "./configurations/myconfig.sumocfg", \
"--tripinfo-output", "./configurations/trips.trips.xml", \
"--fcd-output", "./configurations/testTrace.xml"])
total_time, end_number, deadlines_missed = simulation.run()
print("Average timespan: {}, total vehicle number: {}".format(str(total_time/end_number),\
str(end_number)))
print(str(deadlines_missed) + ' deadlines missed.')
if __name__ == "__main__":
sumo_binary = checkBinary('sumo-gui')
# sumo_binary = checkBinary('sumo')#use this line if you do not want the UI of SUMO
# parse config file for map file name
dom = parse("./configurations/myconfig.sumocfg")
net_file_node = dom.getElementsByTagName('net-file')
net_file_attr = net_file_node[0].attributes
net_file = net_file_attr['value'].nodeValue
init_connection_info = ConnectionInfo("./configurations/"+net_file)
route_file_node = dom.getElementsByTagName('route-files')
route_file_attr = route_file_node[0].attributes
route_file = "./configurations/"+route_file_attr['value'].nodeValue
vehicles = get_controlled_vehicles(route_file, init_connection_info, 10, 50)
#print the controlled vehicles generated
for vid, v in vehicles.items():
print("id: {}, destination: {}, start time:{}, deadline: {};".format(vid, \
v.destination, v.start_time, v.deadline))
test_dijkstra_policy(vehicles)