-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimple_planner.py
50 lines (40 loc) · 1.38 KB
/
simple_planner.py
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
import json
import networkx as nx
from itertools import izip
from celery import Celery
celery = Celery('tut1',broker='redis://',backend='redis://')
graph = nx.MultiGraph()
data_str = open('routes.json').read()
data = json.loads(data_str)
for item in data:
route_name = item['name']
stops = item['stops']
stop_names = [stop['stop']['name'] for stop in stops]
if stop_names!=[]:
graph.add_star(stop_names,route=route_name)
@celery.task
def get_routes(start,end):
paths = nx.all_shortest_paths(graph,start,end)
return [path for path in paths]
@celery.task
def get_full_routes(start,end):
paths = nx.all_shortest_paths(graph,start,end)
routes=[]
for path in paths:
for k,v in zip(path,path[1:]):
edges = graph[k][v]
route=[]
for edge in edges.values():
route.append((k,v,edge['route']))
routes.append(route)
return routes
#print nx.shortest_path(graph,'Dilsukhnagar Bus station','Patancheru Bus Stop')
#print nx.shortest_path(graph,'Patancheru Bus Stop','Hayath Nagar Bus Stop')
#for path in nx.all_shortest_paths(graph,'Dilsukhnagar Bus station','Patancheru Bus Stop'):
# print path
#all_paths = nx.all_pairs_shortest_path(graph)
#path = all_paths['Nagole Bus Stop']['Ziaguda']
#for k,v in zip(path,path[1:]):
# edges = graph[k][v]
# for edge in edges.values():
# print k,v,edge['route']