File: shortestPath_SimPy.py

package info (click to toggle)
python-simpy 2.3.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,864 kB
  • sloc: python: 11,171; makefile: 143
file content (56 lines) | stat: -rw-r--r-- 2,346 bytes parent folder | download | duplicates (4)
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
from SimPy.Simulation import *
""" shortestPath_SimPy.py

    Finds the shortest path in a network.
    Author: Klaus Muller
"""

class node:
    def __init__(self):
        self.reached=0
  
class searcher(Process):
    def __init__(self,graph,path,length,from_node,to_node,distance,goal_node):
        Process.__init__(self)
        self.path=path[:]
        self.length=length
        self.from_node=from_node
        self.to_node=to_node
        self.distance=distance
        self.graph=graph
        self.goal_node=goal_node

    def run(self,to_node):
        if DEMO: print "Path so far: %s (length %d). Search from %s to %s" %(self.path,self.length,self.from_node,to_node)
        yield hold,self,self.distance
        if not nodes[to_node].reached:         
            self.path.append(to_node)
            self.length += self.distance
            nodes[to_node].reached = 1
            if to_node==self.goal_node:
                print "SHORTEST PATH",self.path,"Length:",self.length
                stopSimulation()
            else:                  
                for i in self.graph[to_node]:
                    s=searcher(graph=self.graph,path=self.path,length=self.length,from_node=i[0],
                               to_node=i[1],distance=i[2],goal_node=self.goal_node)
                    activate(s,s.run(i[1]))

print 'shortestPath_SimPy'
initialize()
nodes={}
DEMO=1
for i in ("Atown","Btown","Ccity","Dpueblo","Evillage","Fstadt"):
    nodes[i]=node()
""" Format graph definition: a_graph={node_id:[(from,to,distance),(from,to,distance)],node_id:[ . . . ])
"""
net={"Atown":(("Atown","Btown",3.5),("Atown","Ccity",1),("Atown","Atown",9),("Atown","Evillage",0.5)),
     "Btown":(("Btown","Ccity",5),),
     "Ccity":(("Ccity","Ccity",1),("Ccity","Fstadt",9),("Ccity","Dpueblo",3),("Ccity","Atown",3)),
     "Dpueblo":(("Dpueblo","Ccity",2),("Dpueblo","Fstadt",10)),
     "Evillage":(("Evillage","Btown",1),),
     "Fstadt":(("Fstadt","Ccity",3),)}                                                                                                        
if DEMO: print "Search for shortest path from %s to %s \nin graph %s" %("Atown","Fstadt",net)
startup=searcher(graph=net,path=[],length=0,from_node="Atown",to_node="Atown",distance=0,goal_node="Fstadt")
activate(startup,startup.run("Atown"))
simulate(until=10000)