Esempio n. 1
0
def returnsingle():
    source = request.GET.get('source')
    sink = request.GET.get('sink')
    rank = request.GET.get('rank')
	
    # Load the graph
    G = DiGraph("net5")    
    # Get the painting object and set its properties.
    paint = G.painter()
    paint.set_source_sink(source, sink)    
    # Generate the graph using the painter we configured.
    G.export(False, paint)
    i=int(rank)
    iRank = i
    # Get shortest paths from the graph.
    items = algorithms.ksp_yen(G, source, sink, i)
    for path in items:
        sCost = path['cost']
        sPath = ", ".join(path['path'])               
        response.content_type='application/json'
        response.headers['Access-Control-Allow-Origin']='*'
        bus_Stop, bus_No, stop_Name, route_Id =  result.busChanger(source,sink,iRank)
        if i==1:
            return { "BusStop": bus_Stop, "BusNo": bus_No, "StopName": stop_Name, "RouteNo ": route_Id}
        else:
            i=i-1
            continue
Esempio n. 2
0
def main():
    # Load the graph
    G = DiGraph("net5")
    
    # Get the painting object and set its properties.
    paint = G.painter()
    paint.set_source_sink("C", "H")
    paint.set_rank_same(['C', 'D', 'F'])
    paint.set_rank_same(['E', 'G', 'H'])
    
    # Generate the graph using the painter we configured.
    G.export(False, paint)
    
    # Get 30 shortest paths from the graph.
    items = algorithms.ksp_yen(G, "C", "H", 30)
    for path in items:
        print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))
          
    return 0
Esempio n. 3
0
def main():
    # Load the graph
    G = DiGraph("net5")

    # Get the painting object and set its properties.
    paint = G.painter()
    paint.set_source_sink("C", "H")
    paint.set_rank_same(['C', 'D', 'F'])
    paint.set_rank_same(['E', 'G', 'H'])

    # Generate the graph using the painter we configured.
    G.export(False, paint)

    # Get 30 shortest paths from the graph.
    items = algorithms.ksp_yen(G, "C", "H", 30)
    for path in items:
        print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))

    return 0
Esempio n. 4
0
from graph import DiGraph
import algorithms

source = "s175"
sink = "s555"
rank = "13"

# Load the graph
G = DiGraph("net5")    
# Get the painting object and set its properties.
paint = G.painter()
paint.set_source_sink(source, sink)    
# Generate the graph using the painter we configured.
G.export(False, paint)
i=int(rank)
iRank = i
# Get shortest paths from the graph.
items = algorithms.ksp_yen(G, source, sink, i)
print(items)
#for path in items:
#   sCost = path['cost']
#    sPath = ", ".join(path['path'])               
#    response.content_type='application/json'
#    if i==1:
#        return { "Cost": sCost, "Path": sPath, "BusChange": result.busChanger(source,sink,iRank) }
#    else:
#        i=i-1
#        continue
def main():
    # Load the graph
    G = DiGraph("net5")

    # Get the painting object and set its properties.
    paint = G.painter()
    #paint.set_source_sink('ada', 'bob')
    paint.set_source_sink('s5', 's7')

    # load the graph dictionary
    das_ergebnis = joblib.load(
        "/Users/aditimiglani/Downloads/das_ergebnis.pkl")

    list_of_edges = das_ergebnis['edges']
    list_of_nodes = das_ergebnis['nodes']
    for edge in list_of_edges:
        source = str(edge['source'])
        target = str(edge['target'])
        load = float(edge['load'])
        G.add_edge(source, target, load)
        G.add_edge(target, source, load)

    #G.add_edge('ada', 'sue', 3)
    #G.add_edge('ada', 'john', 2)
    #G.add_edge('sue', 'bob', 1)
    #G.add_edge('a', 'b', 1)
    #G.add_edge('sue', 'john', 1)
    #G.add_edge('john', 'bob', 1)

    # Generate the graph using the painter we configured.
    G.export(False, paint)

    # Get 5 shortest paths from the graph.
    items = algorithms.ksp_yen(G, 's5', 's7', 5)
    all_paths = []
    #items = algorithms.ksp_yen(G, 'ada', 'bob', 5)
    for path in items:
        print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))
        all_paths.append(path['path'])

    print all_paths
    sub_das_ergebnis = {}
    sub_das_ergebnis['nodes'] = []
    sub_das_ergebnis['edges'] = []
    for sub_dict in list_of_nodes:
        for path in all_paths:
            for i in range(len(path)):
                if path[i] == sub_dict['id']:
                    if sub_dict not in sub_das_ergebnis['nodes']:
                        if i == 0 or i == len(path) - 1:
                            sub_dict['root'] = True
                            sub_das_ergebnis['nodes'].append(sub_dict)
                        else:
                            sub_dict['root'] = False
                            sub_das_ergebnis['nodes'].append(sub_dict)
    for sub_dict in list_of_edges:
        for path in all_paths:
            for i in range(len(path)):
                if i < len(path) - 1:
                    if (path[i] == sub_dict['source']
                            or path[i] == sub_dict['target']) and (
                                path[i + 1] == sub_dict['source']
                                or path[i + 1] == sub_dict['target']):
                        if sub_dict not in sub_das_ergebnis['edges']:
                            sub_das_ergebnis['edges'].append(sub_dict)

    joblib.dump(sub_das_ergebnis, 'sub_das_ergebnis.pkl')
    print sub_das_ergebnis
    print 'loading'
    loaded = joblib.load('sub_das_ergebnis.pkl')
    print loaded

    return 0
def GenerateGraph(person1,person2):
	person1_found = 'N'
	person2_found = 'N'

	#u'phillip.k': u's2625'
	print person1
	print person2
	try:
		node1 =person_node_mapping[person1.lower()]
		person1_found = 'Y'
	except:
		print "Person 1 Not found"
		node1 =u's78' #Janel Guerrero

	try:
		node2 =person_node_mapping[person2.lower()]
		person2_found = 'Y'
	except:
		print "Person 2 Not found"
		node2 = u's30018'

	
	G = DiGraph("net5")
	
	# Get the painting object and set its properties.
	paint = G.painter()
	#paint.set_source_sink('ada', 'bob')
	paint.set_source_sink(node1, node2)
	
	# load the graph dictionary

	list_of_edges = das_ergebnis['edges']
	list_of_nodes = das_ergebnis['nodes']
	for edge in list_of_edges:
	    source = str(edge['source'])
	    target = str(edge['target'])
	    load = float(edge['load'])
	    G.add_edge(source, target, load)
	    G.add_edge(target, source, load)

	#G.add_edge('ada', 'sue', 3)
	#G.add_edge('ada', 'john', 2)
	#G.add_edge('sue', 'bob', 1)
	#G.add_edge('a', 'b', 1)
	#G.add_edge('sue', 'john', 1)
	#G.add_edge('john', 'bob', 1)

	# Generate the graph using the painter we configured.
	#G.export(False, paint)
	
	# Get 5 shortest paths from the graph.
	items = algorithms.ksp_yen(G, node1, node2, 5)
	all_paths = []
	#items = algorithms.ksp_yen(G, 'ada', 'bob', 5)
	for path in items:
	    print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))
	    all_paths.append(path['path'])

	print all_paths
	sub_das_ergebnis = {}
	sub_das_ergebnis['nodes'] = []
	sub_das_ergebnis['edges'] = []
	for sub_dict in list_of_nodes:
	    for path in all_paths:
	        for i in range(len(path)):
	            if path[i] == sub_dict['id']:
	                if sub_dict not in sub_das_ergebnis['nodes']:
	                    if i == 0 or i == len(path) - 1:
	                        sub_dict['root'] = True
	                        sub_das_ergebnis['nodes'].append(sub_dict)
	                    else:
	                        sub_dict['root'] = False
	                        sub_das_ergebnis['nodes'].append(sub_dict)
	for sub_dict in list_of_edges:
	    for path in all_paths:
	        for i in range(len(path)):
	            if i < len(path)-1:
	                if (path[i] == sub_dict['source'] or path[i] == sub_dict['target']) and (path[i+1] == sub_dict['source'] or path[i+1] == sub_dict['target']):
	                    if sub_dict not in sub_das_ergebnis['edges']:
	                        sub_das_ergebnis['edges'].append(sub_dict)
				


	#joblib.dump(sub_das_ergebnis, 'sub_das_ergebnis.pkl')   
	nodes =sub_das_ergebnis['nodes']
	edges =sub_das_ergebnis['edges']


	node_list = []

	count =0
	for node in nodes:
		if node['caption'] in enron_table:
			word = enron_table[node['caption']][1]
			org = enron_table[node['caption']][0]
			node_list.append([word,org,node['id']])
			count+=1
		elif node['caption'] in other_org_table:
			word = other_org_table[node['caption']][1]
			org = other_org_table[node['caption']][0]
			node_list.append([word,org,node['id']])
			count+=1
		else:
			continue

	edge_list = []
	connection_exist =[]
	for edge in edges:
		source = edge['source']
		target = edge['target']
		value = edge['load']
		value = value/4
		value1 = int(value*10)
		value2 = int(value*200)
		connection_exist.append(edge['source'])
		connection_exist.append(edge['target'])

		edge_list.append([edge['source'],edge['target'],value1,value2])

	F = open("WorldCup2014.js","w")
	F.write("var nodes = [") 

	nodes =[]
	for node in node_list:
		num = 1
		if node[0] ==person1:
			num = 2
		if node[0] ==person2:
			num = 3

		string = "{id: " + str(node[2][1:]) + ", label: '" + str(node[0].title()) + "', value: " + str(20) +", group:  " + str(num)+"},"
		nodes.append(string)
		#print string
		F.write(string)

	F.write("];")
	F.write("var edges = [")
	edges =[]

	for edge in edge_list:
		string = "{from: %s, to: %s, value: %s, title: 'Relationship score: %s'}," %(edge[0][1:],edge[1][1:],edge[2],edge[3])
		edges.append(string)
		F.write(string)

	F.write("];")
	F.close()



	return nodes,edges