Hi
I am trying to implement dynamic Dijkstra algorithm in sumo simulator using
Traci and python script and I execute my code using python hay2.py in
command window. I get the error below.
hayder@hayder-OptiPlex-7020:~/sumo-0.22.0/dijkstra_hayder/test$ python
hay2.py
Loading configuration... done.
Traceback (most recent call last):
File "hay2.py", line 183, in <module>
main()
File "hay2.py", line 160, in main
traci.route.add("1",route)
File "/home/hayder/sumo-0.22.0/tools/traci/route.py", line 82, in add
1+4+sum(map(len, edges))+4*len(edges))
AttributeError: Edge instance has no attribute '__len__'
Below is code
import os, sys
import optparse
import subprocess
import random
from Queue import PriorityQueue
import xml.sax
from xml.sax import saxutils, parse, make_parser, handler
from copy import copy
from itertools import *
SUMO_HOME = "/home/hayder/sumo-0.22.0"
try:
sys.path.append(os.path.join(SUMO_HOME, "tools"))
# import the library
import sumolib
from sumolib import checkBinary
from sumolib.net import Net
from sumolib.net import NetReader
from sumolib.net import Lane
from sumolib.net import Edge
from sumolib.net import Node
from sumolib.net import Connection
from sumolib.net import Roundabout
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')")
graph = sumolib.net.readNet('Dijkstra1.net.xml')
vertex = graph.getNodes()
edge = graph.getEdges()
import traci
# the port used for communicating with your sumo instance
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]
class priorityDictionary(dict):
def __init__(self):
'''Initialize priorityDictionary by creating binary heap
of pairs (value,key). Note that changing or removing a dict
entry will
not remove the old pair from the heap until it is found by
smallest() or
until the heap is rebuilt.'''
self.__heap = []
dict.__init__(self)
def smallest(self):
'''Find smallest item after removing deleted items from heap.'''
if len(self) == 0:
raise IndexError, "smallest of empty priorityDictionary"
heap = self.__heap
while heap[0][1] not in self or self[heap[0][1]] != heap[0][0]:
lastItem = heap.pop()
insertionPoint = 0
while 1:
smallChild = 2*insertionPoint+1
if smallChild+1 < len(heap) and \
heap[smallChild][0] > heap[smallChild+1][0]:
smallChild += 1
if smallChild >= len(heap) or lastItem <= heap[smallChild]:
heap[insertionPoint] = lastItem
break
heap[insertionPoint] = heap[smallChild]
insertionPoint = smallChild
return heap[0][1]
def __iter__(self):
'''Create destructive sorted iterator of priorityDictionary.'''
def iterfn():
while len(self) > 0:
x = self.smallest()
yield x
del self[x]
return iterfn()
def __setitem__(self,key,val):
'''Change value stored in dictionary and add corresponding
pair to heap. Rebuilds the heap if the number of deleted items
grows
too large, to avoid memory leakage.'''
dict.__setitem__(self,key,val)
heap = self.__heap
if len(heap) > 2 * len(self):
self.__heap = [(v,k) for k,v in self.iteritems()]
self.__heap.sort() # builtin sort likely faster than O(n)
heapify
else:
newPair = (val,key)
insertionPoint = len(heap)
heap.append(None)
while insertionPoint > 0 and val <
heap[(insertionPoint-1)//2][0]:
heap[insertionPoint] = heap[(insertionPoint-1)//2]
insertionPoint = (insertionPoint-1)//2
heap[insertionPoint] = newPair
def setdefault(self,key,val):
'''Reimplement setdefault to call our customized __setitem__.'''
if key not in self:
self[key] = val
return self[key]
def update(self, other):
for key in other.keys():
self[key] = other[key]
def Dijkstra(graph,start,end=None):
D = {} # dictionary of final distances
P = {} # dictionary of predecessors
Q = priorityDictionary() # estimated distances of non-final vertices
Q[start] = 0
for vertex in Q:
D[vertex] = Q[vertex]
if vertex == end: break
for edge in vertex.getOutgoing():
vwLength = D[vertex] + edge.getLength()
if edge in D:
if vwLength < D[edge]:
raise ValueError, "Dijkstra: found better path to
already-final vertex"
elif edge not in Q or vwLength < Q[edge]:
Q[edge] = vwLength
P[edge] = vertex
return (D,P)
def shortestPath(graph,start,end):
"""
Find a single shortest path from the given start vertex to the given
end vertex.
The input has the same conventions as Dijkstra().
The output is a list of the vertices in order along the shortest path.
"""
start = graph.getEdge(start)
end = graph.getEdge(end)
D,P = Dijkstra(graph,start,end)
Path = []
while 1:
Path.append(end)
if end == start: break
end = P[end]
Path.reverse()
return Path
def generate_routefile():
with open("dijk1.rou.xml", "w") as routes:
print >> routes, """<routes>
<vType id="vehicle1" accel="0.8" decel="4.5" sigma="0.5" length="5"
minGap="2.5" maxSpeed="16.67" guiShape="passenger"/>
<route id="1" edges="1 3 5 9" />"""
"</routes>"
def main():
traci.init(PORT)
route = shortestPath(graph, '1', '9')
#create the new route for vehicle
traci.route.add("1",route)
#assign the new route for vehicle with id vehicle1
traci.vehicle.add("vehicle1","1",-2,0,10.0)
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true", default=False,
help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
if __name__ == "__main__":
options = get_options()
# this script has been called from the command line. It will start sumo
as a
# server, then connect and run
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
generate_routefile()
# this is the normal way of using traci. sumo is started as a
# subprocess and then the python script connects and runs
sumoProcess = subprocess.Popen([sumoBinary, "-c", "dijkstra.sumo.cfg",
"--tripinfo-output", "tripinfo.xml", "--remote-port", str(PORT)],
stdout=sys.stdout, stderr=sys.stderr)
main()
sumoProcess.wait()
Please I don’t know how to fix this problem any advice or comments would
help me very much.
Your help and cooperation will be very appreciated.
my code and map.net.xml in the attached files.
Best regards,
Hayder
<?xml version="1.0" encoding="UTF-8"?>
<!-- generated on Mon Nov 9 15:44:31 2015 by SUMO netconvert Version 0.21.0
<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo-sim.org/xsd/netconvertConfiguration.xsd">
<input>
<node-files value="dijkstra.nod.xml"/>
<edge-files value="dijkstra.edg.xml"/>
</input>
<output>
<output-file value="Dijkstra1.net.xml"/>
</output>
</configuration>
-->
<net version="0.13" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://sumo-sim.org/xsd/net_file.xsd">
<location netOffset="0.00,862.00" convBoundary="0.00,0.00,856.00,1085.00" origBoundary="0.00,-862.00,856.00,223.00" projParameter="!"/>
<edge id=":2_0" function="internal">
<lane id=":2_0_0" index="0" speed="12.12" length="5.36" shape="138.08,997.75 139.31,998.67 140.44,998.88 141.47,998.38 142.41,997.16"/>
</edge>
<edge id=":2_1" function="internal">
<lane id=":2_1_0" index="0" speed="12.12" length="6.98" shape="138.08,997.75 143.60,1002.02"/>
</edge>
<edge id=":3_0" function="internal">
<lane id=":3_0_0" index="0" speed="12.12" length="6.83" shape="425.58,1082.03 427.01,1082.09 428.17,1081.39 429.07,1079.92 429.69,1077.69"/>
</edge>
<edge id=":3_1" function="internal">
<lane id=":3_1_0" index="0" speed="12.12" length="8.39" shape="425.58,1082.03 427.53,1082.29 429.45,1081.97 431.33,1081.05 433.18,1079.54"/>
</edge>
<edge id=":4_0" function="internal">
<lane id=":4_0_0" index="0" speed="12.12" length="9.30" shape="843.67,673.87 845.81,671.43 846.19,670.08 844.81,669.82 841.67,670.65"/>
</edge>
<edge id=":4_1" function="internal">
<lane id=":4_1_0" index="0" speed="12.12" length="14.58" shape="843.67,673.87 848.08,669.42 851.15,666.12 852.87,663.98 853.24,662.99"/>
</edge>
<edge id=":5_0" function="internal">
<lane id=":5_0_0" index="0" speed="12.12" length="3.73" shape="486.86,792.11 485.42,792.03 483.37,791.02"/>
</edge>
<edge id=":5_1" function="internal">
<lane id=":5_1_0" index="0" speed="12.12" length="4.05" shape="486.86,792.11 485.41,791.97 483.87,790.59 483.60,790.15"/>
</edge>
<edge id=":5_2" function="internal">
<lane id=":5_2_0" index="0" speed="12.12" length="9.29" shape="486.86,792.11 485.12,792.22 484.23,791.09 484.19,788.70 484.98,785.07"/>
</edge>
<edge id=":5_6" function="internal">
<lane id=":5_6_0" index="0" speed="12.12" length="7.66" shape="483.37,791.02 480.70,789.09 477.41,786.23"/>
</edge>
<edge id=":5_7" function="internal">
<lane id=":5_7_0" index="0" speed="12.12" length="6.79" shape="483.60,790.15 482.25,787.96 480.55,784.09"/>
</edge>
<edge id=":5_3" function="internal">
<lane id=":5_3_0" index="0" speed="12.12" length="11.29" shape="482.41,795.87 482.40,793.66 481.56,791.32 479.90,788.84 477.41,786.23"/>
</edge>
<edge id=":5_4" function="internal">
<lane id=":5_4_0" index="0" speed="12.12" length="12.13" shape="482.41,795.87 482.76,792.61 482.50,789.89 481.73,787.21 480.55,784.09"/>
</edge>
<edge id=":5_5" function="internal">
<lane id=":5_5_0" index="0" speed="12.12" length="11.10" shape="482.41,795.87 483.01,792.78 483.52,790.43 484.13,788.10 484.98,785.07"/>
</edge>
<edge id=":6_0" function="internal">
<lane id=":6_0_0" index="0" speed="12.12" length="11.65" shape="338.51,657.72 337.54,657.72 335.44,658.44 332.18,659.88 327.79,662.05"/>
</edge>
<edge id=":6_1" function="internal">
<lane id=":6_1_0" index="0" speed="12.12" length="4.66" shape="338.51,657.72 336.25,653.64"/>
</edge>
<edge id=":6_2" function="internal">
<lane id=":6_2_0" index="0" speed="12.12" length="7.83" shape="330.50,664.89 331.59,662.49 331.50,661.22 330.24,661.07 327.79,662.05"/>
</edge>
<edge id=":6_3" function="internal">
<lane id=":6_3_0" index="0" speed="12.12" length="12.91" shape="330.50,664.89 332.49,662.00 334.44,659.51 335.85,656.89 336.25,653.64"/>
</edge>
<edge id=":7_0" function="internal">
<lane id=":7_0_0" index="0" speed="12.12" length="5.83" shape="72.94,791.86 71.13,792.46 70.03,792.04 69.63,790.61 69.80,789.36"/>
</edge>
<edge id=":7_1" function="internal">
<lane id=":7_1_0" index="0" speed="12.12" length="8.17" shape="72.94,791.86 70.52,792.81 69.30,792.56 69.31,791.13 70.52,788.50"/>
</edge>
<edge id=":7_4" function="internal">
<lane id=":7_4_0" index="0" speed="12.12" length="1.20" shape="69.80,789.36 69.95,788.17"/>
</edge>
<edge id=":7_2" function="internal">
<lane id=":7_2_0" index="0" speed="12.12" length="5.04" shape="67.24,792.43 69.95,788.17"/>
</edge>
<edge id=":7_3" function="internal">
<lane id=":7_3_0" index="0" speed="12.12" length="5.12" shape="67.24,792.43 70.52,788.50"/>
</edge>
<edge id=":8_0" function="internal">
<lane id=":8_0_0" index="0" speed="12.12" length="10.88" shape="314.82,362.41 314.59,359.31 314.37,356.98 314.05,354.67 313.51,351.61"/>
</edge>
<edge id=":8_1" function="internal">
<lane id=":8_1_0" index="0" speed="12.12" length="9.25" shape="314.82,362.41 314.75,358.93 315.15,356.41 316.00,354.84 317.31,354.24"/>
</edge>
<edge id=":8_2" function="internal">
<lane id=":8_2_0" index="0" speed="12.12" length="10.66" shape="316.49,361.81 315.39,358.94 314.66,356.81 314.10,354.62 313.51,351.61"/>
</edge>
<edge id=":8_3" function="internal">
<lane id=":8_3_0" index="0" speed="12.12" length="7.12" shape="316.49,361.81 315.48,358.61 315.28,356.28 315.83,354.97"/>
</edge>
<edge id=":8_6" function="internal">
<lane id=":8_6_0" index="0" speed="12.12" length="1.69" shape="315.83,354.97 315.89,354.82 317.31,354.24"/>
</edge>
<edge id=":8_4" function="internal">
<lane id=":8_4_0" index="0" speed="12.12" length="10.62" shape="310.98,361.55 312.54,358.85 313.51,356.79 313.84,354.62 313.51,351.61"/>
</edge>
<edge id=":8_5" function="internal">
<lane id=":8_5_0" index="0" speed="12.12" length="9.88" shape="310.98,361.55 312.88,358.42 314.57,356.16 316.04,354.76 317.31,354.24"/>
</edge>
<edge id=":9_0" function="internal">
<lane id=":9_0_0" index="0" speed="12.12" length="12.63" shape="617.99,335.22 615.91,332.56 613.90,330.44 611.73,328.44 609.20,326.19"/>
</edge>
<edge id=":9_1" function="internal">
<lane id=":9_1_0" index="0" speed="12.12" length="6.51" shape="607.88,330.10 609.91,329.70 610.80,328.91 610.57,327.74 609.20,326.19"/>
</edge>
<edge id=":9_2" function="internal">
<lane id=":9_2_0" index="0" speed="12.12" length="11.34" shape="613.87,335.93 614.05,333.67 613.33,331.30 611.72,328.80 609.20,326.19"/>
</edge>
<edge id="1" from="1" to="2" priority="75">
<lane id="1_0" index="0" speed="12.12" length="192.18" shape="2.19,861.86 138.08,997.75"/>
</edge>
<edge id="10" from="5" to="8" priority="75">
<lane id="10_0" index="0" speed="12.12" length="453.03" shape="480.55,784.09 316.49,361.81"/>
</edge>
<edge id="11" from="5" to="9" priority="75">
<lane id="11_0" index="0" speed="12.12" length="467.27" shape="484.98,785.07 613.87,335.93"/>
</edge>
<edge id="12" from="6" to="7" priority="75">
<lane id="12_0" index="0" speed="12.12" length="286.01" shape="327.79,662.05 72.94,791.86"/>
</edge>
<edge id="13" from="6" to="8" priority="75">
<lane id="13_0" index="0" speed="12.12" length="292.03" shape="336.25,653.64 314.82,362.41"/>
</edge>
<edge id="14" from="7" to="8" priority="75">
<lane id="14_0" index="0" speed="12.12" length="490.01" shape="70.52,788.50 310.98,361.55"/>
</edge>
<edge id="15" from="7" to="10" priority="75">
<lane id="15_0" index="0" speed="12.12" length="798.86" shape="69.95,788.17 246.27,9.01"/>
</edge>
<edge id="16" from="8" to="9" priority="75">
<lane id="16_0" index="0" speed="12.12" length="291.57" shape="317.31,354.24 607.88,330.10"/>
</edge>
<edge id="17" from="8" to="10" priority="75">
<lane id="17_0" index="0" speed="12.12" length="348.36" shape="313.51,351.61 250.01,9.09"/>
</edge>
<edge id="18" from="9" to="10" priority="75">
<lane id="18_0" index="0" speed="12.12" length="479.54" shape="609.20,326.19 253.10,5.02"/>
</edge>
<edge id="2" from="1" to="7" priority="75">
<lane id="2_0" index="0" speed="12.12" length="91.99" shape="2.19,857.47 67.24,792.43"/>
</edge>
<edge id="3" from="2" to="3" priority="75">
<lane id="3_0" index="0" speed="12.12" length="293.11" shape="143.60,1002.02 425.58,1082.03"/>
</edge>
<edge id="4" from="2" to="6" priority="75">
<lane id="4_0" index="0" speed="12.12" length="381.82" shape="142.41,997.16 330.50,664.89"/>
</edge>
<edge id="5" from="3" to="5" priority="75">
<lane id="5_0" index="0" speed="12.12" length="286.71" shape="429.69,1077.69 482.41,795.87"/>
</edge>
<edge id="6" from="3" to="4" priority="75">
<lane id="6_0" index="0" speed="12.12" length="577.12" shape="433.18,1079.54 843.67,673.87"/>
</edge>
<edge id="7" from="4" to="5" priority="75">
<lane id="7_0" index="0" speed="12.12" length="375.02" shape="841.67,670.65 486.86,792.11"/>
</edge>
<edge id="8" from="4" to="9" priority="75">
<lane id="8_0" index="0" speed="12.12" length="403.46" shape="853.24,662.99 617.99,335.22"/>
</edge>
<edge id="9" from="5" to="6" priority="75">
<lane id="9_0" index="0" speed="12.12" length="189.24" shape="477.41,786.23 338.51,657.72"/>
</edge>
<junction id="1" type="unregulated" x="0.00" y="862.00" incLanes="" intLanes="" shape="1.06,862.99 3.32,860.73 3.32,858.61 1.06,856.34"/>
<junction id="10" type="unregulated" x="250.00" y="0.00" incLanes="17_0 18_0 15_0" intLanes="" shape="248.43,9.38 251.58,8.80 252.03,6.20 254.17,3.83 244.71,8.66 247.83,9.37"/>
<junction id="2" type="priority" x="141.00" y="1003.00" incLanes="1_0" intLanes=":2_0_0 :2_1_0" shape="143.17,1003.56 144.04,1000.48 143.80,997.95 141.02,996.38 139.22,996.62 136.95,998.88">
<request index="0" response="00" foes="00" cont="0"/>
<request index="1" response="00" foes="00" cont="0"/>
</junction>
<junction id="3" type="priority" x="430.00" y="1085.00" incLanes="3_0" intLanes=":3_0_0 :3_1_0" shape="434.30,1080.68 432.05,1078.40 431.26,1077.98 428.12,1077.39 426.02,1080.49 425.14,1083.57">
<request index="0" response="00" foes="00" cont="0"/>
<request index="1" response="00" foes="00" cont="0"/>
</junction>
<junction id="4" type="priority" x="856.00" y="664.00" incLanes="6_0" intLanes=":4_0_0 :4_1_0" shape="854.54,662.06 851.94,663.92 841.15,669.14 842.19,672.16 842.54,672.73 844.79,675.01">
<request index="0" response="00" foes="00" cont="0"/>
<request index="1" response="00" foes="00" cont="0"/>
</junction>
<junction id="5" type="priority" x="485.00" y="791.00" incLanes="7_0 5_0" intLanes=":5_6_0 :5_7_0 :5_2_0 :5_3_0 :5_4_0 :5_5_0" shape="487.38,793.62 486.34,790.59 486.52,785.51 483.45,784.63 482.04,783.51 479.06,784.67 478.50,785.05 476.32,787.40 480.84,795.57 483.98,796.16">
<request index="0" response="111000" foes="111000" cont="1"/>
<request index="1" response="110000" foes="110000" cont="1"/>
<request index="2" response="100000" foes="100000" cont="0"/>
<request index="3" response="000000" foes="000001" cont="0"/>
<request index="4" response="000000" foes="000011" cont="0"/>
<request index="5" response="000000" foes="000111" cont="0"/>
</junction>
<junction id="6" type="priority" x="338.00" y="655.00" incLanes="9_0 4_0" intLanes=":6_0_0 :6_1_0 :6_2_0 :6_3_0" shape="337.42,658.89 339.59,656.54 337.84,653.53 334.65,653.76 327.06,660.63 328.51,663.48 329.11,664.10 331.90,665.68">
<request index="0" response="1100" foes="1100" cont="0"/>
<request index="1" response="1000" foes="1000" cont="0"/>
<request index="2" response="0000" foes="0001" cont="0"/>
<request index="3" response="0000" foes="0011" cont="0"/>
</junction>
<junction id="7" type="priority" x="71.00" y="791.00" incLanes="12_0 2_0" intLanes=":7_4_0 :7_1_0 :7_2_0 :7_3_0" shape="73.66,793.29 72.21,790.44 71.91,789.28 69.12,787.71 66.11,791.29 68.37,793.56">
<request index="0" response="1100" foes="1100" cont="1"/>
<request index="1" response="1000" foes="1000" cont="0"/>
<request index="2" response="0000" foes="0001" cont="0"/>
<request index="3" response="0000" foes="0011" cont="0"/>
</junction>
<junction id="8" type="priority" x="316.00" y="356.00" incLanes="13_0 10_0 14_0" intLanes=":8_0_0 :8_1_0 :8_2_0 :8_6_0 :8_4_0 :8_5_0" shape="313.22,362.52 316.41,362.29 317.44,355.83 317.18,352.64 315.08,351.32 311.93,351.90 309.59,360.76 312.38,362.33">
<request index="0" response="110100" foes="110100" cont="0"/>
<request index="1" response="101100" foes="101100" cont="0"/>
<request index="2" response="110000" foes="110011" cont="0"/>
<request index="3" response="100000" foes="100010" cont="1"/>
<request index="4" response="000000" foes="000101" cont="0"/>
<request index="5" response="000000" foes="001111" cont="0"/>
</junction>
<junction id="9" type="priority" x="617.00" y="331.00" incLanes="8_0 16_0 11_0" intLanes=":9_0_0 :9_1_0 :9_2_0" shape="616.69,336.15 619.29,334.28 610.27,325.00 608.13,327.38 607.75,328.51 608.02,331.70 612.33,335.49 615.41,336.37">
<request index="0" response="010" foes="110" cont="0"/>
<request index="1" response="000" foes="101" cont="0"/>
<request index="2" response="011" foes="011" cont="0"/>
</junction>
<junction id=":5_6_0" type="internal" x="483.37" y="791.02" incLanes=":5_0_0 5_0" intLanes=":5_3_0 :5_4_0 :5_5_0"/>
<junction id=":5_7_0" type="internal" x="483.60" y="790.15" incLanes=":5_1_0 5_0" intLanes=":5_4_0 :5_5_0"/>
<junction id=":7_4_0" type="internal" x="69.80" y="789.36" incLanes=":7_0_0 2_0" intLanes=":7_2_0 :7_3_0"/>
<junction id=":8_6_0" type="internal" x="315.83" y="354.97" incLanes=":8_3_0 14_0" intLanes=":8_1_0 :8_5_0"/>
<connection from="1" to="4" fromLane="0" toLane="0" via=":2_0_0" dir="r" state="M"/>
<connection from="1" to="3" fromLane="0" toLane="0" via=":2_1_0" dir="s" state="M"/>
<connection from="10" to="17" fromLane="0" toLane="0" via=":8_2_0" dir="s" state="m"/>
<connection from="10" to="16" fromLane="0" toLane="0" via=":8_3_0" dir="l" state="m"/>
<connection from="11" to="18" fromLane="0" toLane="0" via=":9_2_0" dir="r" state="m"/>
<connection from="12" to="15" fromLane="0" toLane="0" via=":7_0_0" dir="L" state="m"/>
<connection from="12" to="14" fromLane="0" toLane="0" via=":7_1_0" dir="l" state="m"/>
<connection from="13" to="17" fromLane="0" toLane="0" via=":8_0_0" dir="s" state="m"/>
<connection from="13" to="16" fromLane="0" toLane="0" via=":8_1_0" dir="l" state="m"/>
<connection from="14" to="17" fromLane="0" toLane="0" via=":8_4_0" dir="s" state="M"/>
<connection from="14" to="16" fromLane="0" toLane="0" via=":8_5_0" dir="l" state="M"/>
<connection from="16" to="18" fromLane="0" toLane="0" via=":9_1_0" dir="r" state="M"/>
<connection from="2" to="15" fromLane="0" toLane="0" via=":7_2_0" dir="s" state="M"/>
<connection from="2" to="14" fromLane="0" toLane="0" via=":7_3_0" dir="s" state="M"/>
<connection from="3" to="5" fromLane="0" toLane="0" via=":3_0_0" dir="r" state="M"/>
<connection from="3" to="6" fromLane="0" toLane="0" via=":3_1_0" dir="R" state="M"/>
<connection from="4" to="12" fromLane="0" toLane="0" via=":6_2_0" dir="r" state="M"/>
<connection from="4" to="13" fromLane="0" toLane="0" via=":6_3_0" dir="s" state="M"/>
<connection from="5" to="9" fromLane="0" toLane="0" via=":5_3_0" dir="r" state="M"/>
<connection from="5" to="10" fromLane="0" toLane="0" via=":5_4_0" dir="s" state="M"/>
<connection from="5" to="11" fromLane="0" toLane="0" via=":5_5_0" dir="s" state="M"/>
<connection from="6" to="7" fromLane="0" toLane="0" via=":4_0_0" dir="r" state="M"/>
<connection from="6" to="8" fromLane="0" toLane="0" via=":4_1_0" dir="R" state="M"/>
<connection from="7" to="9" fromLane="0" toLane="0" via=":5_0_0" dir="L" state="m"/>
<connection from="7" to="10" fromLane="0" toLane="0" via=":5_1_0" dir="L" state="m"/>
<connection from="7" to="11" fromLane="0" toLane="0" via=":5_2_0" dir="l" state="m"/>
<connection from="8" to="18" fromLane="0" toLane="0" via=":9_0_0" dir="s" state="m"/>
<connection from="9" to="12" fromLane="0" toLane="0" via=":6_0_0" dir="r" state="m"/>
<connection from="9" to="13" fromLane="0" toLane="0" via=":6_1_0" dir="s" state="m"/>
<connection from=":2_0" to="4" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":2_1" to="3" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":3_0" to="5" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":3_1" to="6" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":4_0" to="7" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":4_1" to="8" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":5_0" to="9" fromLane="0" toLane="0" via=":5_6_0" dir="s" state="m"/>
<connection from=":5_6" to="9" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":5_1" to="10" fromLane="0" toLane="0" via=":5_7_0" dir="s" state="m"/>
<connection from=":5_7" to="10" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":5_2" to="11" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":5_3" to="9" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":5_4" to="10" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":5_5" to="11" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":6_0" to="12" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":6_1" to="13" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":6_2" to="12" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":6_3" to="13" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":7_0" to="15" fromLane="0" toLane="0" via=":7_4_0" dir="s" state="m"/>
<connection from=":7_4" to="15" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":7_1" to="14" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":7_2" to="15" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":7_3" to="14" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":8_0" to="17" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":8_1" to="16" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":8_2" to="17" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":8_3" to="16" fromLane="0" toLane="0" via=":8_6_0" dir="s" state="m"/>
<connection from=":8_6" to="16" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":8_4" to="17" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":8_5" to="16" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":9_0" to="18" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":9_1" to="18" fromLane="0" toLane="0" dir="s" state="M"/>
<connection from=":9_2" to="18" fromLane="0" toLane="0" dir="s" state="M"/>
</net>
import os, sys
import optparse
import subprocess
import random
from Queue import PriorityQueue
import xml.sax
from xml.sax import saxutils, parse, make_parser, handler
from copy import copy
from itertools import *
SUMO_HOME = "/home/hayder/sumo-0.22.0"
try:
sys.path.append(os.path.join(SUMO_HOME, "tools"))
# import the library
import sumolib
from sumolib import checkBinary
from sumolib.net import Net
from sumolib.net import NetReader
from sumolib.net import Lane
from sumolib.net import Edge
from sumolib.net import Node
from sumolib.net import Connection
from sumolib.net import Roundabout
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')")
graph = sumolib.net.readNet('Dijkstra1.net.xml')
vertex = graph.getNodes()
edge = graph.getEdges()
import traci
# the port used for communicating with your sumo instance
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]
class priorityDictionary(dict):
def __init__(self):
'''Initialize priorityDictionary by creating binary heap
of pairs (value,key). Note that changing or removing a dict entry will
not remove the old pair from the heap until it is found by smallest() or
until the heap is rebuilt.'''
self.__heap = []
dict.__init__(self)
def smallest(self):
'''Find smallest item after removing deleted items from heap.'''
if len(self) == 0:
raise IndexError, "smallest of empty priorityDictionary"
heap = self.__heap
while heap[0][1] not in self or self[heap[0][1]] != heap[0][0]:
lastItem = heap.pop()
insertionPoint = 0
while 1:
smallChild = 2*insertionPoint+1
if smallChild+1 < len(heap) and \
heap[smallChild][0] > heap[smallChild+1][0]:
smallChild += 1
if smallChild >= len(heap) or lastItem <= heap[smallChild]:
heap[insertionPoint] = lastItem
break
heap[insertionPoint] = heap[smallChild]
insertionPoint = smallChild
return heap[0][1]
def __iter__(self):
'''Create destructive sorted iterator of priorityDictionary.'''
def iterfn():
while len(self) > 0:
x = self.smallest()
yield x
del self[x]
return iterfn()
def __setitem__(self,key,val):
'''Change value stored in dictionary and add corresponding
pair to heap. Rebuilds the heap if the number of deleted items grows
too large, to avoid memory leakage.'''
dict.__setitem__(self,key,val)
heap = self.__heap
if len(heap) > 2 * len(self):
self.__heap = [(v,k) for k,v in self.iteritems()]
self.__heap.sort() # builtin sort likely faster than O(n) heapify
else:
newPair = (val,key)
insertionPoint = len(heap)
heap.append(None)
while insertionPoint > 0 and val < heap[(insertionPoint-1)//2][0]:
heap[insertionPoint] = heap[(insertionPoint-1)//2]
insertionPoint = (insertionPoint-1)//2
heap[insertionPoint] = newPair
def setdefault(self,key,val):
'''Reimplement setdefault to call our customized __setitem__.'''
if key not in self:
self[key] = val
return self[key]
def update(self, other):
for key in other.keys():
self[key] = other[key]
def Dijkstra(graph,start,end=None):
D = {} # dictionary of final distances
P = {} # dictionary of predecessors
Q = priorityDictionary() # estimated distances of non-final vertices
Q[start] = 0
for vertex in Q:
D[vertex] = Q[vertex]
if vertex == end: break
for edge in vertex.getOutgoing():
vwLength = D[vertex] + edge.getLength()
if edge in D:
if vwLength < D[edge]:
raise ValueError, "Dijkstra: found better path to already-final vertex"
elif edge not in Q or vwLength < Q[edge]:
Q[edge] = vwLength
P[edge] = vertex
return (D,P)
def shortestPath(graph,start,end):
"""
Find a single shortest path from the given start vertex to the given end vertex.
The input has the same conventions as Dijkstra().
The output is a list of the vertices in order along the shortest path.
"""
start = graph.getEdge(start)
end = graph.getEdge(end)
D,P = Dijkstra(graph,start,end)
Path = []
while 1:
Path.append(end)
if end == start: break
end = P[end]
Path.reverse()
return Path
def generate_routefile():
with open("dijk1.rou.xml", "w") as routes:
print >> routes, """<routes>
<vType id="vehicle1" accel="0.8" decel="4.5" sigma="0.5" length="5" minGap="2.5" maxSpeed="16.67" guiShape="passenger"/>
<route id="1" edges="1 3 5 9" />"""
"</routes>"
def main():
traci.init(PORT)
route = shortestPath(graph, '1', '9')
#create the new route for vehicle
traci.route.add("1",route)
#assign the new route for vehicle with id vehicle1
traci.vehicle.add("vehicle1","1",-2,0,10.0)
def get_options():
optParser = optparse.OptionParser()
optParser.add_option("--nogui", action="store_true", default=False, help="run the commandline version of sumo")
options, args = optParser.parse_args()
return options
if __name__ == "__main__":
options = get_options()
# this script has been called from the command line. It will start sumo as a
# server, then connect and run
if options.nogui:
sumoBinary = checkBinary('sumo')
else:
sumoBinary = checkBinary('sumo-gui')
generate_routefile()
# this is the normal way of using traci. sumo is started as a
# subprocess and then the python script connects and runs
sumoProcess = subprocess.Popen([sumoBinary, "-c", "dijkstra.sumo.cfg", "--tripinfo-output", "tripinfo.xml", "--remote-port", str(PORT)], stdout=sys.stdout, stderr=sys.stderr)
main()
sumoProcess.wait()
------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
_______________________________________________
sumo-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sumo-user