Exemplo n.º 1
0
def thread2(G, carData, roadData, returndict2):
    finalPath = []
    for carNum in range(int(len(carData) / 2), len(carData)):
        # items = algorithms.ksp_yen(G, '51', '3', 5)
        items = algorithms.ksp_yen(G, str(carData[carNum][1]),
                                   str(carData[carNum][2]), 3)

        for path in items:
            carRoute = path['path']

            length = len(carRoute)
            carRoute.reverse()
            print("2222222222222222222222222")
            # for i in range(len(carRoute)):
            #     f.write(carRoute[i])
            #     if i != len(carRoute) - 1:
            #         f.write(',')
            # f.write('\n')

            carRouteTmp = []
            for i in range(1, length):
                for j in range(len(roadData)):
                    if ((roadData[j][-3] == int(carRoute[length - i])
                         and roadData[j][-2] == int(carRoute[length - i - 1]))
                            or
                        (roadData[j][-2] == int(carRoute[length - i]) and
                         roadData[j][-3] == int(carRoute[length - i - 1]))):
                        carRouteTmp.append(roadData[j][0])
            finalPath.append(carRouteTmp)
    returndict2["result"] = finalPath
Exemplo n.º 2
0
def thread1(G, carData, roadData, returndict1):
    finalPath = []
    for carNum in range(0, int(len(carData) / 2)):
        # items = algorithms.ksp_yen(G, '51', '3', 5)
        items = algorithms.ksp_yen(G, str(carData[carNum][1]),
                                   str(carData[carNum][2]), 3)
        for path in items:
            # print(str(carNum) + "Cost:%s\t%s" % (path['cost'], "->".join(path['path'])))
            carRoute = path['path']
            length = len(carRoute)
            carRoute.reverse()
            print("1111111111111111111111111")

            carRouteTmp = []
            for i in range(1, length):
                for j in range(len(roadData)):
                    if ((roadData[j][-3] == int(carRoute[length - i])
                         and roadData[j][-2] == int(carRoute[length - i - 1]))
                            or
                        (roadData[j][-2] == int(carRoute[length - i]) and
                         roadData[j][-3] == int(carRoute[length - i - 1]))):
                        carRouteTmp.append(roadData[j][0])
            finalPath.append(carRouteTmp)

    returndict1["result"] = finalPath
Exemplo n.º 3
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
Exemplo n.º 4
0
def getDirections(start, end):

    # Load the graph
    G = DiGraph("shortestPath")

    # Get 3 shortest paths from the graph.
    items = algorithms.ksp_yen(G, start, end, 3)
    return items
Exemplo n.º 5
0
def getDirections(start, end):

    # Load the graph
    G = DiGraph("shortestPath")



    # Get 3 shortest paths from the graph.
    items = algorithms.ksp_yen(G, start, end, 3)
    return items
Exemplo n.º 6
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
Exemplo n.º 7
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
Exemplo n.º 8
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 adjust_route(topology, remove_file, routes, link_order):
    # get remove_links
    f = open(remove_file)
    links = []
    line = f.readline()
    while line:
        s = line.rstrip().split(' ')
        s = [int(i) for i in s]
        
        ls = s[0]
        ld = s[1]
        if ld < ls:
            tmp = ls
            ls = ld
            ld = tmp

        links.append((ls,ld))
        line = f.readline()
    f.close()

    # get final topology
    g = DiGraph()
    f = open(topology)
    line = f.readline()
    line = f.readline()
    while line:
        s = line.rstrip().split(' ')
        lls = int(s[0])
        lld = int(s[1])

        #g.add_nodes([int(s[0]), int(s[1])])
        g.add_edge(lls, lld, link_order[lls,lld][1] / link_order[lls,lld][0])
        g.add_edge(lld, lls, link_order[lls,lld][1] / link_order[lls,lld][0])

        line = f.readline()
    f.close()

    def sort_path(paths):
        for i in range(len(paths)):
            for j in range(i, len(paths)):
                if paths[i][1] < paths[j][1]:
                    tmp = paths[i]
                    paths[i] = paths[j]
                    paths[j] = tmp

    def calculate_cf(paths, link_order):
        for p in paths:
            cf = 100000
            for i in range(len(p[0]) - 1):
                tmp_cf = 0
                s = p[0][i]
                d = p[0][i+1]
                if s < d:
                    tmp_cf = link_order[s,d][0] / link_order[s,d][1]
                else:
                    tmp_cf = link_order[d,s][0] / link_order[d,s][1]
                if tmp_cf < cf:
                    cf = tmp_cf
            p[1] = cf
            sort_path(paths)

    for ls,ld in links:
        #print(ls,ld)
        for s,d in routes:
            route_paths = routes[s,d]

            remove_route = []
            remove_weight = 0
            for path, weight in route_paths:
                for i in range(len(path) - 1):
                    if (path[i] == ls and path[i+1] == ld) or (path[i] == ld and path[i+1] == ls):
                        remove_route.append([path, weight])
                        remove_weight += weight

                        # remove weight from all the link
                        for pi in range(len(path) - 1):
                            ps = path[pi]
                            pd = path[pi+1]
                            if pd < ps:
                                tmp = pd
                                pd = ps
                                ps = tmp

                            link_order[ps,pd][1] -= weight

                        break
            if remove_weight != 0:
                # Find k-shortest-paths between s,d
                tmp_paths = algorithms.ksp_yen(g, s, d, 20)
                yen_paths = [[yen_path['path'], 0, 0] for yen_path in tmp_paths]
                
                # make sure the proportion of every yen_path
                steps = 10.0
                gap = remove_weight / steps
                calculate_cf(yen_paths, link_order)
                for step in range(int(steps)):
                    for yi in range(len(yen_paths[0][0]) - 1):
                        ys = yen_paths[0][0][yi]
                        yd = yen_paths[0][0][yi + 1]
                        if yd < ys:
                            tmp = ys
                            ys = yd
                            yd = tmp
                        link_order[ys,yd][1] += gap
                    yen_paths[0][2] += 1 / steps
                    calculate_cf(yen_paths, link_order)

                # remain routes
                for rr in remove_route:
                    route_paths.remove(rr)

                # merge k-s-p into routes
                for yen_path, flows, proportion in yen_paths:
                    if proportion == 0:
                        continue

                    exist = False
                    for rp in route_paths:
                        if rp[0] == yen_path:
                            rp[1] += remove_weight * proportion
                            exist = True
                    if not exist:
                        route_paths.append([yen_path, remove_weight * proportion])

                routes[s,d] = route_paths
    
    return routes
Exemplo n.º 10
0
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
def adjust_route(topology, remove_file, routes, link_order):
    # get remove_links
    f = open(remove_file)
    links = []
    line = f.readline()
    while line:
        s = line.rstrip().split(' ')
        s = [int(i) for i in s]

        ls = s[0]
        ld = s[1]
        if ld < ls:
            tmp = ls
            ls = ld
            ld = tmp

        links.append((ls, ld))
        line = f.readline()
    f.close()

    # get final topology
    g = DiGraph()
    f = open(topology)
    line = f.readline()
    line = f.readline()
    while line:
        s = line.rstrip().split(' ')
        lls = int(s[0])
        lld = int(s[1])

        #g.add_nodes([int(s[0]), int(s[1])])
        g.add_edge(lls, lld, link_order[lls, lld][1] / link_order[lls, lld][0])
        g.add_edge(lld, lls, link_order[lls, lld][1] / link_order[lls, lld][0])

        line = f.readline()
    f.close()

    def sort_path(paths):
        for i in range(len(paths)):
            for j in range(i, len(paths)):
                if paths[i][1] < paths[j][1]:
                    tmp = paths[i]
                    paths[i] = paths[j]
                    paths[j] = tmp

    def calculate_cf(paths, link_order):
        for p in paths:
            cf = 100000
            for i in range(len(p[0]) - 1):
                tmp_cf = 0
                s = p[0][i]
                d = p[0][i + 1]
                if s < d:
                    tmp_cf = link_order[s, d][0] / link_order[s, d][1]
                else:
                    tmp_cf = link_order[d, s][0] / link_order[d, s][1]
                if tmp_cf < cf:
                    cf = tmp_cf
            p[1] = cf
            sort_path(paths)

    for ls, ld in links:
        #print(ls,ld)
        for s, d in routes:
            route_paths = routes[s, d]

            remove_route = []
            remove_weight = 0
            for path, weight in route_paths:
                for i in range(len(path) - 1):
                    if (path[i] == ls
                            and path[i + 1] == ld) or (path[i] == ld
                                                       and path[i + 1] == ls):
                        remove_route.append([path, weight])
                        remove_weight += weight

                        # remove weight from all the link
                        for pi in range(len(path) - 1):
                            ps = path[pi]
                            pd = path[pi + 1]
                            if pd < ps:
                                tmp = pd
                                pd = ps
                                ps = tmp

                            link_order[ps, pd][1] -= weight

                        break
            if remove_weight != 0:
                # Find k-shortest-paths between s,d
                tmp_paths = algorithms.ksp_yen(g, s, d, 20)
                yen_paths = [[yen_path['path'], 0, 0]
                             for yen_path in tmp_paths]

                # make sure the proportion of every yen_path
                steps = 10.0
                gap = remove_weight / steps
                calculate_cf(yen_paths, link_order)
                for step in range(int(steps)):
                    for yi in range(len(yen_paths[0][0]) - 1):
                        ys = yen_paths[0][0][yi]
                        yd = yen_paths[0][0][yi + 1]
                        if yd < ys:
                            tmp = ys
                            ys = yd
                            yd = tmp
                        link_order[ys, yd][1] += gap
                    yen_paths[0][2] += 1 / steps
                    calculate_cf(yen_paths, link_order)

                # remain routes
                for rr in remove_route:
                    route_paths.remove(rr)

                # merge k-s-p into routes
                for yen_path, flows, proportion in yen_paths:
                    if proportion == 0:
                        continue

                    exist = False
                    for rp in route_paths:
                        if rp[0] == yen_path:
                            rp[1] += remove_weight * proportion
                            exist = True
                    if not exist:
                        route_paths.append(
                            [yen_path, remove_weight * proportion])

                routes[s, d] = route_paths

    return routes
Exemplo n.º 13
0
def generate_small_graph(num_cols=5, num_rows=5, num_routes_per_od_pair=2):
  # we have n*m nodes, (((of which a 5*5 grid is for Caltec and a 5*5 grid
  # is for the streets (for now) --> imagine it as a 5 rows, 10 columns
  # grid, indexed like a matrix)))
  
  sensors = []

  # Generate directed road network
  G = nx.DiGraph()
  n = num_cols
  m = num_rows
  r = num_routes_per_od_pair

  for j in range(0, n):
      for k in range(0, m):
          G.add_node(k * n + j, pos = (j, -k))
      
  for j in range(0, n-1):
      for k in range(0, m-1):
          G.add_edge(k * n + j, k * n + j + 1, weight=1)
          sensors.append((k * n + j, k * n + j + 1))
          G.add_edge(k * n + j, (k+1) * n + j, weight=1)
          sensors.append((k * n + j, (k+1) * n + j))
  
 # Manually get the last row
  for j in range(0, n-1):
      G.add_edge((m-1) * n + j,(m-1) * n + j + 1, weight=6) 
      sensors.append(((m-1) * n + j, (m-1) * n + j + 1))
      # had to do this for this small node case to get at least 2 highways...

 # Manually set the last column
  for k in range(0, m-1):
      G.add_edge(k * n + n-1, (k+1) * n + n-1, weight=1)
      sensors.append((k * n + n-1, (k+1) * n + n-1));

  # Set bigger road weights
  for k in range(0, m-1):
    for j in range(0, n-1):
    # Highways
      if k % 4 == 0:
        G.edge[k*n + j][k*n + j+1]['weight'] = 6
        # sensors.append((k*n + j,k*n + j+1))

    # Big streets  
      if k % 4 == 2:
        G.edge[k*n + j][k*n + j+1]['weight'] = 3
        # Philipp: Hungry for sensors
        # if j % 3 == 0:
        # sensors.append((k*n + j,k*n + j+1))

    # Half big streets
      if j % 2 == 0:
        G.edge[k*n + j][(k+1)*n + j]['weight'] = 2
        # oh my gosh, I want more sensors
        # sensors.append((k*n + j,(k+1)*n + j))
          
        
  for (u, v, data) in G.edges(data=True):
      G.add_edge(v, u, weight = data['weight'])

  # Invert graph weights
  H = graph.DiGraph()
  
  for (u, v) in G.edges():
      H.add_edge(u, v, cost = 1./G.edge[u][v]['weight'])

  # Add opposite sensors
  sensors_opposite = [(v,u) for (u,v) in sensors]
  sensors.extend(sensors_opposite)
  
  # Find k shortest routes between all 2 nodes
  routes = []
  
  for pair in itertools.product(range(0, n*m), repeat=2):
      u = pair[0]
      v = pair[1]
      if u == v:
        continue
      # dijkstra would be routes.append(nx.shortest_path(G,source=v,target=w))
      k_shortest = algorithms.ksp_yen(H, u, v, max_k = r)
      paths = map(lambda x: x['path'], k_shortest)
      if len(paths) > 0 and not(len(paths[0]) == 0):
          routes.extend(paths)
          
  return G, routes, sensors
Exemplo n.º 14
0
def main(carData, roadData):
    # 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_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, "E", "H", 10)

    start = datetime.now()

    with open('./answer.txt', 'w') as f:

        for carNum in range(len(carData)):
            # items = algorithms.ksp_yen(G, '51', '3', 5)

            f.write(str(carData[carNum][0]))
            f.write('\n')

            items = algorithms.ksp_yen(G, str(carData[carNum][1]),
                                       str(carData[carNum][2]), 2)
            finalPath = []
            for path in items:
                print(
                    str(carNum) + "Cost:%s\t%s" %
                    (path['cost'], "->".join(path['path'])))
                carRoute = path['path']

                length = len(carRoute)
                carRoute.reverse()

                # for i in range(len(carRoute)):
                #     f.write(carRoute[i])
                #     if i != len(carRoute) - 1:
                #         f.write(',')
                # f.write('\n')

                carRouteTmp = []
                for i in range(1, length):
                    for j in range(len(roadData)):
                        if ((roadData[j][-3] == int(carRoute[length - i]) and
                             roadData[j][-2] == int(carRoute[length - i - 1]))
                                or
                            (roadData[j][-2] == int(carRoute[length - i])
                             and roadData[j][-3] == int(
                                 carRoute[length - i - 1]))):
                            carRouteTmp.append(roadData[j][0])
                finalPath.append(carRouteTmp)

            for i in range(len(finalPath)):
                for j in range(len(finalPath[i])):
                    f.write(str(finalPath[i][j]))
                    if j != len(finalPath[i]) - 1:
                        f.write(',')
                f.write('\n')

        f.close()

    end = datetime.now()
    print((end - start).seconds)
    return 0
Exemplo n.º 15
0
fd.close()
for i in range(0, len(host_node)):
    host_node[i] = host_node[i].encode(CODEC)

switch_path = "switches.json"
fd = open(switch_path, 'r')
switch_node = json.loads(fd.read())
fd.close()
for i in range(0, len(switch_node)):
    switch_node[i] = switch_node[i].encode(CODEC)

mynet = "200h100r.json"
g = DiGraph(mynet)
for i in range(0, 100):
    terminal.append(host_node[i])
max_count = 0
avg_count = 0
for sw in range(0, 100):
    temp = 0
    for h in range(0, 200):
        path = ksp_yen(g, switch_node[sw], host_node[h], 2)
        if len(path) > 1:
            if path[0].get('cost') == path[1].get('cost'):
                temp = temp + 1

    if temp > max_count:
        max_count = temp
    print max_count
    avg_count = avg_count + temp
print max_count, avg_count / float(100)