File: runner.py

package info (click to toggle)
sumo 0.15.0~dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 31,740 kB
  • sloc: cpp: 109,360; xml: 49,743; ansic: 41,570; python: 20,769; java: 17,071; sh: 10,413; makefile: 1,377; perl: 450
file content (84 lines) | stat: -rwxr-xr-x 3,021 bytes parent folder | download
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
#!/usr/bin/env python
"""
@file    runner.py
@author  Lena Kalleske
@author  Daniel Krajzewicz
@author  Michael Behrisch
@date    2009-03-26
@version $Id: runner.py 11671 2012-01-07 20:14:30Z behrisch $

Tutorial for traffic light control via the TraCI interface.

SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
Copyright (C) 2009-2012 DLR/TS, Germany
All rights reserved
"""

import os, subprocess, sys, random
sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..', '..', 'tools'))
sys.path.append(os.path.join(os.environ.get("SUMO_HOME", os.path.join(os.path.dirname(__file__), '..', '..', '..')), 'tools'))
from sumolib import checkBinary
import traci

PORT = 8813

NSGREEN = "GrGr" 
NSYELLOW = "yryr"
WEGREEN = "rGrG" 
WEYELLOW = "ryry"

PROGRAM = [WEYELLOW,WEYELLOW,WEYELLOW,NSGREEN,NSGREEN,NSGREEN,NSGREEN,NSGREEN,NSGREEN,NSGREEN,NSGREEN,NSYELLOW,NSYELLOW,WEGREEN]

if not traci.isEmbedded():
    N = 9000
    pWE = 1./10
    pEW = 1./11
    pNS = 1./30
    routes = open("data/cross.rou.xml", "w")
    print >> routes, """<routes>
    <vType id="typeWE" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" guiShape="passenger"/>
    <vType id="typeNS" accel="0.8" decel="4.5" sigma="0.5" length="17" minGap="3" maxSpeed="25" guiShape="bus"/>

    <route id="right" edges="51o 1i 2o 52i" />
    <route id="left" edges="52o 2i 1o 51i" />
    <route id="down" edges="54o 4i 3o 53i" />"""
    lastVeh = 0
    vehNr = 0
    for i in range(N):
        if random.uniform(0,1) < pWE:
            print >> routes, '    <vehicle id="%i" type="typeWE" route="right" depart="%i" />' % (vehNr, i)
            vehNr += 1
            lastVeh = i
        if random.uniform(0,1) < pEW:
            print >> routes, '    <vehicle id="%i" type="typeWE" route="left" depart="%i" />' % (vehNr, i)
            vehNr += 1
            lastVeh = i
        if random.uniform(0,1) < pNS:
            print >> routes, '    <vehicle id="%i" type="typeNS" route="down" depart="%i" color="1,0,0"/>' % (vehNr, i)
            vehNr += 1
            lastVeh = i
    print >> routes, "</routes>"
    routes.close()

    sumoBinary = checkBinary('sumo')
    sumoConfig = "data/cross.sumocfg"
    if len(sys.argv) > 1:
        retCode = subprocess.call("%s -c %s --python-script %s" % (sumoBinary, sumoConfig, __file__), shell=True, stdout=sys.stdout)
        sys.exit(retCode)
    else:
        sumoProcess = subprocess.Popen("%s -c %s" % (sumoBinary, sumoConfig), shell=True, stdout=sys.stdout)
        traci.init(PORT)

programPointer = len(PROGRAM)-1
step = 0
while step == 0 or traci.simulation.getMinExpectedNumber() > 0:
    traci.simulationStep()
    programPointer = min(programPointer+1, len(PROGRAM)-1)
    no = traci.inductionloop.getLastStepVehicleNumber("0")
    if no > 0:
        programPointer = (0 if programPointer == len(PROGRAM)-1 else 3)
    traci.trafficlights.setRedYellowGreenState("0", PROGRAM[programPointer])
    step += 1

traci.close()
sys.stdout.flush()