-
Notifications
You must be signed in to change notification settings - Fork 6
/
helper.py
126 lines (98 loc) · 3.36 KB
/
helper.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
from xml.etree import ElementTree as et
import os
import random
import numpy as np
import matplotlib.pyplot as plt
from pymongo import MongoClient
from dbFunction import getRunCount
client = MongoClient()
db = client['tl']
def getDBName(options):
name = 'tl'
if (options.learn == '0'):
return name + '_preTimed'
elif (options.learn == '1'):
name += '_Q-Lrn'
else:
name += '_SARSA'
if (options.stateRep == '1'):
name += '_queue'
else:
name += '_delay'
if (options.actionSel == '1'):
name += '_greedy'
else:
name += '_softmx'
if (options.phasing == '1'):
name += '_fix'
else:
name += '_var'
return name
def updateVehDistribution():
fileDir = os.path.dirname(os.path.realpath('__file__'))
src = et.parse(os.path.join(fileDir, 'data/cross.src.xml'))
dst = et.parse(os.path.join(fileDir, 'data/cross.dst.xml'))
srcEdges = src.findall('*/edge')
dstEdges = dst.findall('*/edge')
# generate uniformly dstributed random numbers that sum to 1
listRandSrc = [0, 1]
listRandDst = [0, 1]
for i in range(len(srcEdges) - 1):
listRandSrc.append(round(random.uniform(0, 1), 4))
listRandDst.append(round(random.uniform(0, 1), 4))
listRandSrc.sort()
listRandDst.sort()
for i, edge in enumerate(srcEdges):
edge.set('value', str(listRandSrc[i + 1] - listRandSrc[i]))
for i, edge in enumerate(dstEdges):
edge.set('value', str(listRandDst[i + 1] - listRandDst[i]))
src.write(os.path.join(fileDir, 'data/cross.src.xml'))
dst.write(os.path.join(fileDir, 'data/cross.dst.xml'))
# fringeFactor=10
# this uses randomtrips.py to generate a routefile with random traffic
def generate_routefile(options):
# generating route file using randomTrips.py
if (os.name == "posix"):
vType = '\"\'typedist1\'\"'
else:
vType = '\'typedist1\''
fileDir = os.path.dirname(os.path.realpath('__file__'))
filename = os.path.join(fileDir, 'data/cross.net.xml')
os.system("python randomTrips.py -n " + filename
+ " --weights-prefix " + os.path.join(fileDir, 'data/cross')
+ " -e " + str(options.numberCars)
+ " -p 4" + " -r " + os.path.join(fileDir, 'data/cross.rou.xml')
# + " --fringe-factor " + str(fringeFactor)
+ " --trip-attributes=\"type=\"" + vType + "\"\""
+ " --additional-file " + \
os.path.join(fileDir, 'data/type.add.xml')
+ " --edge-permission emergency passenger taxi bus truck motorcycle bicycle"
)
def plotGraph(xVar, yVar):
hl.set_xdata(np.append(hl.get_xdata(), xVar))
hl.set_ydata(np.append(hl.get_ydata(), yVar))
ax.relim()
ax.autoscale_view()
plt.draw()
# plt.pause(0.0001)
return
def savePlot(dbName):
ax.set_title(dbName)
plt.tight_layout()
plt.grid(alpha=0.8)
plt.savefig('outputs/ql' + dbName + '.png')
plt.savefig('outputs/ql' + dbName + '.pdf')
if __name__ != "__main__":
global fig, ax, hl
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('Time Step (x300)')
ax.set_ylabel('Average Queue Length')
hl, = ax.plot([], [])
plt.ion()
# plt.show()
else:
# Runs when helper is directly executed
# For testing purposes
random.seed(42)
updateVehDistribution()