-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.py
62 lines (54 loc) · 2.46 KB
/
Utils.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
import os
import sys
import traci
from gym.utils import seeding
try:
sys.path.append(os.path.join(os.environ.get("SUMO_HOME"), "tools"))
from sumolib import checkBinary # noqa
except ImportError:
sys.exit(
"please declare environment variable 'SUMO_HOME' as the root directory of your sumo installation (it should contain folders 'bin', 'tools' and 'docs')")
def initSimulation(visualize=False, seed=123):
__generateRoutefile(seed)
if visualize:
sumoBinary = checkBinary('sumo-gui')
else:
sumoBinary = checkBinary('sumo')
traci.start(
[sumoBinary, "-c", os.path.join(os.path.dirname(__file__), "data/cross.sumocfg"), "--start", "--quit-on-end",
"--duration-log.statistics"])
traci.trafficlights.setPhase("0", 0)
def __generateRoutefile(seed):
npRandom, seed = seeding.np_random(seed)
N = 3600 # number of time steps
# demand per second from different directions
pWE = .1
pEW = .1
pNS = .1 # change to ".3" for second experiment
pSN = .1 # change to ".3" for second experiment
with open(os.path.join(os.path.dirname(__file__), "data/cross.rou.xml"), "w") as routes:
print("""<routes>
<vType id="typeWE" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" guiShape="passenger"/>
<route id="right" edges="51o 1i 2o 52i" />
<route id="left" edges="52o 2i 1o 51i" />
<route id="up" edges="53o 3i 4o 54i" />
<route id="down" edges="54o 4i 3o 53i" />""", file=routes)
vehNr = 0
for i in range(N):
if npRandom.uniform(low=0., high=1.) < pWE:
print(' <vehicle id="right_%i" type="typeWE" route="right" depart="%i" />' % (
vehNr, i), file=routes)
vehNr += 1
if npRandom.uniform(low=0., high=1.) < pEW:
print(' <vehicle id="left_%i" type="typeWE" route="left" depart="%i" />' % (
vehNr, i), file=routes)
vehNr += 1
if npRandom.uniform(low=0., high=1.) < pNS:
print(' <vehicle id="down_%i" type="typeWE" route="down" depart="%i" />' % (
vehNr, i), file=routes)
vehNr += 1
if npRandom.uniform(low=0., high=1.) < pSN:
print(' <vehicle id="up_%i" type="typeWE" route="up" depart="%i" />' % (
vehNr, i), file=routes)
vehNr += 1
print("</routes>", file=routes)