Monday, October 3, 2011

Google App Engine For Small Business Supply Chains

We all know technology is terrific enabler for small business supply chains. We can now use google maps to track and trace your salesperson and transportation fleet; share transaction information through googledocs, talk for free on GoogleTalk. Receive related advertisement when you use Gmail, etc...














How about creating your own transportation routing system, customized to your operational needs using Google App Engine?

When you run the code, it will prompt you to enter the name of nodes, press enter and keep on entering name of nodes. When done, you type in “ok”, then the nodes will pair up, you need to key in the distances here. once the distances are keyed, it will dine the shortest distance.

Isn't it an elegan solution? :)

import sys
print 'Content-Type: text/html'
print ''
print '<pre>'
class node:
    label = ''
    # adjacency list of the node
    neighbors = [] # list of nodes
    distances = [] # distances to neighbors
    # for Dijkstra
    prevNode = None
    totalDistance = 0
    # constructor
    def __init__(self, label):
        self.label = label
        self.neighbors = []
        self.distances = []
        self.prevNode = None
        self.totalDistance = 0
# find the shortest paths to all nodes recursively
def dijkstra(node):
    # visit all neighbors and update them if necessary
    while len(node.neighbors) > 0:
        n = node.neighbors.pop(0)
        d = node.distances.pop(0)
        if n.prevNode == None or n.totalDistance > d + node.totalDistance:
            n.prevNode = node
            n.totalDistance = d + node.totalDistance
        dijkstra(n)
# get the shortest path to the given node
def route(endNode):
    node = endNode
    labels = [endNode.label]
    # distances = []
    while node.label != node.prevNode.label:
        # distances.append(node.totalDistance - node.prevNode.totalDistance)
        node = node.prevNode
        labels.append(node.label)
    labels.reverse()
    return labels
    # distances.reverse()
    # return (labels, distances)
       
# create a graph - need user input on places it needs to go to
#1st step to add in node name and coordinates first, then display node name and coordinates when i type ok. then calculate the distances relative to each other, then pair and add in distances.
##a = node('a')
##b = node('b')
##c = node('c')
##d = node('d')
##e = node('e')
##f = node('f')

graph = []
while(True):
    x = raw_input("name of node : ")
    if x=='ok':
        break
    x=node(x)
    graph.append(x)
print "graphs are", graph
prelimedges=[]
for n in graph:
    print n.label
   
    prelimedges.append((n))
print "prelimedges are ", prelimedges
print "first node is ",prelimedges[len(prelimedges)-1]
print "last node is ",prelimedges[0]

edges=[]
##edges.append((prelimedges[0],prelimedges[1]))
##
##print edges
##print len(prelimedges)
start=0
while start<len(prelimedges):
    print start
    count=0
    while count< len(prelimedges):
        print "count is",count
        print "start is",start
        if start==count:
            count=count+1
            print "count now becomes", count
            if count==len(prelimedges):
                break
       
        distance=raw_input("distance between them: ")
        intdistance=int(distance)
        edges.append(((prelimedges[start]),(prelimedges[count]),intdistance))
        count=count+1
        print "after appending to edges, count is", count
        print edges
    start=start+1
print "at the end, the edges are", edges


# create bidirectional edges of the graph
##edges = []
##edges.append((a, b, 14))
##edges.append((a, c, 9))
###edges.append((a, d, 7))
##edges.append((b, c, 2))
#edges.append((b, f, 9))
#edges.append((c, d, 10))
#edges.append((c, e, 11))
#edges.append((d, e, 15))
#edges.append((f, e, 6))
#edges.append((e,g,10))
#print edges
# create adjaceny list of neighbors for each node
for edge in edges:
    edge[0].neighbors.append(edge[1])
    edge[0].distances.append(edge[2])
    edge[1].neighbors.append(edge[0])
    edge[1].distances.append(edge[2])
# print the graph
print 'The graph:'
print
for n in graph:
    print 'Node: ', n.label
    print 'Neighbors:'
    for i in range(len(n.neighbors)):
        print n.neighbors[i].label, n.distances[i]
    print
# find the shortest paths to all neighbors starting w/ the given node
#startNode = a
startNode = graph[0]
print 'Route start node:', startNode.label
startNode.prevNode = startNode
dijkstra(startNode)
##print
##print 'The graph after Dijkstra:'
##print
##for n in graph:
##    print 'Node:', n.label
##    print 'totalDistance:', n.totalDistance
##    print 'prevNode:', n.prevNode.label
##    print
# print the shortest path to the given node
#endNode = c
endNode = graph[len(graph)-1]
print 'Route end node:', endNode.label
print 'Route:', route(endNode)
print 'Total distance:', endNode.totalDistance

No comments:

Post a Comment