Skip to content

Instantly share code, notes, and snippets.

@abyrd
Created August 11, 2009 23:50
Show Gist options
  • Save abyrd/166193 to your computer and use it in GitHub Desktop.
Save abyrd/166193 to your computer and use it in GitHub Desktop.
Checking basic Graphserver functions
# Incarnate the graph database created earlier
# with the graph compiler tools
from graphserver.core import Graph, State, Link
from graphserver.graphdb import GraphDatabase
gdb = GraphDatabase('trimet_13sep2009.linked.gsdb')
g = gdb.incarnate()
# Board-alight graphs contain many vertices which do not
# correspond to stations – print a list of only the stations
# Is there a better way to do this?
# [v.label for v in g.vertices if v.label[0:3] == 'sta']
# Store a particular moment as seconds since the epoch
t0 = 1253730000
# Check it in human-readable form.
# Wed Sep 23 20:20:00 2009 (European time)
import time
time.ctime(t0)
# Tri-Met stations:
beaverton = 'sta-9979' # Beaverton Transit Center Northbound
psquare = 'sta-8334' # Pioneer Square MAX Eastbound
lombard = 'sta-3451' # North Lombard and Mississippi (east of MAX Yellow line)
airport = 'sta-10579' # Portland International Airport
belmont = 'sta-430' # SE Belmont and 42nd Westbound
denney = 'sta-9749' # an office park in Beaverton
# Give similar results to Tri-Met
# orig = belmont
# dest = beaverton
# orig = psquare
# dest = denney
# orig = lombard
# dest = denney
# Gives a better/faster solution than Tri-Met...
# Because it uses the green MAX line
# which is not yet visible on their site.
orig = psquare
dest = 'sta-1561'
# Build a shortest path tree for this particular pair
# and display the results
spt = g.shortest_path_tree( orig, dest, State(1, t0))
vertices, edges = spt.path( dest )
for i in range(len(vertices)) :
v = vertices[i]
vp = v.payload
print "%s %3i %10s %15s" % (time.ctime(vp.time), vp.weight / 60, vp.trip_id, v.label),
try:
e = edges[i]
ep = e.payload
print type(ep).__name__
except:
print "ARRIVAL"
print "\nwalked %i meters, %i vehicles." % (vp.dist_walked, vp.num_transfers)
# Get a full path tree from pioneer square
# and print the times for the shortest path to every other stop
spt = g.shortest_path_tree( psquare, None, State(1, t0))
for r in [(v.label, (v.payload.time - t0) / 60, v.payload.dist_walked, v.payload.num_transfers) for v in spt.vertices if v.label[0:3] == 'sta'] :
print "%10s in %3i min, walked %4i meters, %1i vehicles" % r
# Improvised stress and speed test.
# Find a full shortest path tree from every station
# and time the calculations
ti = time.time()
for vl in vlabels :
tn = time.time()
spt = g.shortest_path_tree(vl, None, State(1, t0))
# About 1.8 seconds per tree for PDX linked transit graph
print vl, time.time() - tn
# Clean up and avoid massive memory leak
spt.destroy()
# Gives about 2 seconds per full path tree
print (time.time() - ti) / len(vlabels)
# Clean up
g.destroy()
from graphserver.core import *
from graphserver.graphdb import GraphDatabase
import time
t0 = 1253730000
time.ctime(t0)
gdb = GraphDatabase('bart.linked.gsdb')
mmon()
g = gdb.incarnate()
mmoff()
orig = 'sta-12TH'
dest = 'sta-SANL'
spt = g.shortest_path_tree( orig, dest, State(1, t0))
vertices, edges = spt.path( dest )
for i in range(len(vertices)) :
v = vertices[i]
vp = v.payload
print "%s %3i %10s %15s" % (time.ctime(vp.time), vp.weight / 60, vp.trip_id, v.label),
try:
e = edges[i]
ep = e.payload
print type(ep).__name__
except:
print "ARRIVAL"
spt.destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment