Exemplo n.º 1
0
    def create_function(self, bea, haddr):
        """
            Create function descriptor from VM IP and address of handler
            :param bea: Begin IP of function in VM disassembler trace
            :param haddr: Address of handler in binary

            :return Success status
        """
        print "[+] Analyzing function by address %s" % hex(bea)
        dg = None
        try:
            dg = DiGraph(bea, self.instruction_size)
            dg.parse(self.instructions)
        except Exception:
            print "[~] Unhandled exception in create graph routine"
            return False

        if dg.is_bad_cfg:
            print "[!] Function at address: %s invalid" % hex(bea).replace("0x", "").replace("L", "")
            return False

        f = Function(bea, dg.get_low_address(), dg, "sub_%s" % hex(bea).replace("0x", "").replace("L", ""), haddr)
        self.functions[bea] = f

        return True
Exemplo n.º 2
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.º 3
0
 def __init__(self):
     self.__g = DiGraph()  # vertex = activity
     self.__d = {}  # vertex : duration
     self.__ES = {}  # vertex : Earliest Start time
     self.__EF = {}  # vertex : Earliest Finish time
     self.__LS = {}  # vertex : Latest Start time
     self.__LF = {}  # vertex : Latest Finish time
     self.__total = 0  # total time of the project
Exemplo n.º 4
0
def scc(G):
    """Strongly Connected Components"""
    G_reversed = DiGraph()

    for u in G.adj:
        for v in G.adj[u]:
            G_reversed.add_edge(v, u, attr=G.adj[u][v])

    # topologically high to low
    sorted_vertices = topological_sort(G)[::-1]

    groups = _scc(G_reversed, sorted_vertices)
    return groups
Exemplo n.º 5
0
 def __loadFromFile(self):
     # may raise IOError if invalid filename
     f = open(self.__fName, "r")
     n, m = f.readline().strip().split()
     n = int(n)
     m = int(m)
     self.__graph = DiGraph(n)
     for i in range(m):
         x, y, cost = f.readline().strip().split()
         x = int(x)
         y = int(y)
         cost = int(cost)
         self.__graph.addEdge(x, y)
         self.__graph.setCost(x, y, cost)
Exemplo n.º 6
0
def main():
    # Load the graph
    #G = DiGraph("net5")
    #G = DiGraph("internet2")
    G = DiGraph("AS3356")
    
    # 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']))
    #      
    #items = algorithms.ksp_yen(G, "C", "C", 30)
    #for path in items:
    #    print "Cost:%s\t%s" % (path['cost'], "->".join(path['path']))

    getDisjointPaths(G, "1", "62", 10)
    getDisjointPaths(G, "1", "10", 10)
    getDisjointPaths(G, "1", "2", 10)
Exemplo n.º 7
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.º 8
0
def main(carData, roadData):
    G = DiGraph("net5")
    manager = Manager()
    return_dict1 = manager.dict()
    return_dict2 = manager.dict()
    return_dict3 = manager.dict()
    return_dict4 = manager.dict()

    start = datetime.now()
    p = Pool(4)
    p.apply_async(thread1, args=(G, carData, roadData, return_dict1))
    p.apply_async(thread2, args=(G, carData, roadData, return_dict2))
    p.apply_async(thread3, args=(G, carData, roadData, return_dict3))
    p.apply_async(thread4, args=(G, carData, roadData, return_dict4))
    p.close()
    p.join()

    # for carNum in range(int(len(carData) / 3), len(carData)):
    #     # items = algorithms.ksp_yen(G, '51', '3', 5)
    #     items = algorithms.ksp_yen(G, str(carData[carNum][1]), str(carData[carNum][2]), 2)
    #     finalPath = []
    #     for path in items:
    #         print("333333333333333333333")
    #         # print(str(carNum) + "Cost:%s\t%s" % (path['cost'], "->".join(path['path'])))

    end = datetime.now()
    print((end - start).seconds)

    subResult1 = return_dict1['result']
    subReuslt2 = return_dict2['result']
    subResult3 = return_dict3['result']
    subReuslt4 = return_dict4['result']

    allCarRoute = subResult1 + subReuslt2 + subResult3 + subReuslt4

    with open('./eightPath.txt', 'w') as f:
        for i in range(len(carData)):
            f.write(str(carData[i][0]))
            f.write('\n')

            for pathNum in range(3):
                for j in range(len(allCarRoute[0])):
                    f.write(str(allCarRoute[0][j]))
                    if j != len(allCarRoute[0]) - 1:
                        f.write(',')
                f.write('\n')
                allCarRoute.pop(0)

            # for j in range(len(allCarRoute[0])):
            #     f.write(str(allCarRoute[0][j]))
            #     if j != len(allCarRoute[0]) - 1:
            #         f.write(',')
            # f.write('\n')
            # allCarRoute.pop(0)

        f.close()

    return 0
Exemplo n.º 9
0
 def __init__(self, file):
     '''
     Constructor for controller
     Input: f (string) filename to read a DiGraph from
     '''
     f = open(file, "r")
     n, m = f.readline().strip().split()
     n = int(n)
     m = int(m)
     self.__dg = DiGraph(n)
     for i in range(m):
         x, y, cost = f.readline().strip().split()
         x = int(x)
         y = int(y)
         cost = int(cost)
         self.__dg.addEdge(x, y)
         #self.__dg.setCost(x, y, cost) #! do not
     f.close()
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("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.º 11
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.º 12
0
class Controller:
    def __init__(self, filename):
        self.__fName = filename
        self.__graph = None
        self.__loadFromFile()

    def __loadFromFile(self):
        # may raise IOError if invalid filename
        f = open(self.__fName, "r")
        n, m = f.readline().strip().split()
        n = int(n)
        m = int(m)
        self.__graph = DiGraph(n)
        for i in range(m):
            x, y, cost = f.readline().strip().split()
            x = int(x)
            y = int(y)
            cost = int(cost)
            self.__graph.addEdge(x, y)
            self.__graph.setCost(x, y, cost)

    def graph(self):
        return self.__graph
Exemplo n.º 13
0
def main(carData, roadData):


    start = datetime.now()

    with open('./answer.txt', 'w') as f:
        G = DiGraph("net5")

        # 5道,4道,3道,2道,1道
        channelCoefficient = [0.9, 0.8, 0.7, 0.6, 0.5]
        # 9, 8, 7, 6, 5, 4, 3, 2, 1
        speedCoefficient = [1.8, 1.7, 1.6, 1.5, 1.4, 1.3, 1.2, 1.1]

        finalPath = []

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


            items = algorithms_transport.ksp_yen(G, str(carData[carNum][1]), str(carData[carNum][2]), 2)

            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 = []
                carRouteTmp.append(carData[carNum][0])
                carRouteTmp.append(carData[carNum][-1])
                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)


                carRoute.reverse()

                for i in range(len(carRoute) - 1):

                    currentRoad = finalPath[carNum][i]
                    startPoint = carRoute[i]
                    endPoint = carRoute[i+1]

                    oldRoadLength = 0
                    speed = 0
                    channel = 0

                    for roadNum in range(len(roadData)):
                        if roadData[roadNum][0] == currentRoad:
                            oldRoadLength = roadData[roadNum][1]
                            speed = roadData[roadNum][2]
                            channel = roadData[roadNum][3]

                    newRoadLength = math.ceil(oldRoadLength * channelCoefficient[channel - 1] * speedCoefficient[speed - 1])



                    for p in G._data:
                        if p == startPoint:
                            pathDict[startPoint][endPoint] = newRoadLength

                print("debug")
                break


        f.write('#(carId,StartTime,RoadId...)')
        f.write('\n')

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

        f.close()

    end = datetime.now()
    print((end - start).seconds)
    return 0
from helpers import get_links
from graph import DiGraph

import os

# from pprint import pprint

filename = "graph.txt"

graph = DiGraph()

if os.path.isfile(filename):
    graph.read_from_filename(filename)

if graph.nodes:
    to_traverse = graph.get_to_traverse()
else:
    root_pagename = "Kevin_Bacon"
    to_traverse = {root_pagename}
count = len(graph.nodes)

# traverse whole graph
with open(filename, "a+", encoding="utf-8") as file:
    while to_traverse:
        pagename = to_traverse.pop()
        graph.add_node(pagename)
        count += 1

        print(f"Traversing {pagename}")
        print(count)
Exemplo n.º 15
0
    def __init__(
            self,
            n_children=2,
            n_depth=3,
            h_dims=128,
            node_tag_dims=128,
            edge_tag_dims=128,
            n_classes=10,
            steps=5,
            filters=[16, 32, 64, 128, 256],
            kernel_size=(3, 3),
            final_pool_size=(2, 2),
            glimpse_type='gaussian',
            glimpse_size=(15, 15),
    ):
        '''
        Basic idea:
        * We detect objects through an undirected graphical model.
        * The graphical model consists of a balanced tree of latent variables h
        * Each h is then connected to a bbox variable b and a class variable y
        * b of the root is fixed to cover the entire canvas
        * All other h, b and y are updated through message passing
        * The loss function should be either (not completed yet)
            * multiset loss, or
            * maximum bipartite matching (like Order Matters paper)
        '''
        NN.Module.__init__(self)
        self.n_children = n_children
        self.n_depth = n_depth
        self.h_dims = h_dims
        self.node_tag_dims = node_tag_dims
        self.edge_tag_dims = edge_tag_dims
        self.h_dims = h_dims
        self.n_classes = n_classes
        self.glimpse = create_glimpse(glimpse_type, glimpse_size)
        self.steps = steps

        self.cnn = build_cnn(
            filters=filters,
            kernel_size=kernel_size,
            final_pool_size=final_pool_size,
        )

        # Create graph of latent variables
        G = nx.balanced_tree(self.n_children, self.n_depth)
        nx.relabel_nodes(G, {i: 'h%d' % i
                             for i in range(len(G.nodes()))}, False)
        self.h_nodes_list = h_nodes_list = list(G.nodes)
        for h in h_nodes_list:
            G.node[h]['type'] = 'h'
        b_nodes_list = ['b%d' % i for i in range(len(h_nodes_list))]
        y_nodes_list = ['y%d' % i for i in range(len(h_nodes_list))]
        self.b_nodes_list = b_nodes_list
        self.y_nodes_list = y_nodes_list
        hy_edge_list = [(h, y) for h, y in zip(h_nodes_list, y_nodes_list)]
        hb_edge_list = [(h, b) for h, b in zip(h_nodes_list, b_nodes_list)]
        yh_edge_list = [(y, h) for y, h in zip(y_nodes_list, h_nodes_list)]
        bh_edge_list = [(b, h) for b, h in zip(b_nodes_list, h_nodes_list)]

        G.add_nodes_from(b_nodes_list, type='b')
        G.add_nodes_from(y_nodes_list, type='y')
        G.add_edges_from(hy_edge_list)
        G.add_edges_from(hb_edge_list)

        self.G = DiGraph(nx.DiGraph(G))
        hh_edge_list = [
            (u, v) for u, v in self.G.edges()
            if self.G.node[u]['type'] == self.G.node[v]['type'] == 'h'
        ]

        self.G.init_node_tag_with(node_tag_dims,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01))
        self.G.init_edge_tag_with(edge_tag_dims,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01),
                                  edges=hy_edge_list + hb_edge_list +
                                  bh_edge_list)
        self.G.init_edge_tag_with(h_dims * n_classes,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01),
                                  edges=yh_edge_list)

        # y -> h.  An attention over embeddings dynamically generated through edge tags
        self.G.register_message_func(self._y_to_h,
                                     edges=yh_edge_list,
                                     batched=True)

        # b -> h.  Projects b and edge tag to the same dimension, then concatenates and projects to h
        self.bh_1 = NN.Linear(self.glimpse.att_params, h_dims)
        self.bh_2 = NN.Linear(edge_tag_dims, h_dims)
        self.bh_all = NN.Linear(
            2 * h_dims + filters[-1] * NP.prod(final_pool_size), h_dims)
        self.G.register_message_func(self._b_to_h,
                                     edges=bh_edge_list,
                                     batched=True)

        # h -> h.  Just passes h itself
        self.G.register_message_func(self._h_to_h,
                                     edges=hh_edge_list,
                                     batched=True)

        # h -> b.  Concatenates h with edge tag and go through MLP.
        # Produces Δb
        self.hb = NN.Linear(h_dims + edge_tag_dims, self.glimpse.att_params)
        self.G.register_message_func(self._h_to_b,
                                     edges=hb_edge_list,
                                     batched=True)

        # h -> y.  Concatenates h with edge tag and go through MLP.
        # Produces Δy
        self.hy = NN.Linear(h_dims + edge_tag_dims, self.n_classes)
        self.G.register_message_func(self._h_to_y,
                                     edges=hy_edge_list,
                                     batched=True)

        # b update: just adds the original b by Δb
        self.G.register_update_func(self._update_b,
                                    nodes=b_nodes_list,
                                    batched=False)

        # y update: also adds y by Δy
        self.G.register_update_func(self._update_y,
                                    nodes=y_nodes_list,
                                    batched=False)

        # h update: simply adds h by the average messages and then passes it through ReLU
        self.G.register_update_func(self._update_h,
                                    nodes=h_nodes_list,
                                    batched=False)
Exemplo n.º 16
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
Exemplo n.º 17
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
Exemplo n.º 18
0
def main():
    G = DiGraph()
    undershorts = Node("undershorts")
    pants = Node("pants")
    belt = Node("belt")
    shirt = Node("shirt")
    tie = Node("tie")
    jacket = Node("jacket")
    socks = Node("socks")
    shoes = Node("shoes")
    watch = Node("watch")

    G.add_edge(shirt, belt)
    G.add_edge(shirt, tie)
    G.add_edge(tie, jacket)
    G.add_edge(belt, jacket)
    G.add_edge(pants, belt)
    G.add_edge(undershorts, shoes)
    G.add_edge(undershorts, pants)
    G.add_edge(pants, shoes)
    G.add_edge(socks, shoes)
    G.add_edge(watch, watch)

    vertices_sorted = topological_sort(G)
    # Can have more than 1 answers
    print(vertices_sorted)
Exemplo n.º 19
0
def test_bellman_ford():
    G = DiGraph()
    s = Node("s")
    t = Node("t")
    y = Node("y")
    x = Node("x")
    z = Node("z")

    G.add_edge(s, t, w=6)
    G.add_edge(s, y, w=7)
    G.add_edge(t, y, w=8)
    G.add_edge(t, x, w=5)
    G.add_edge(t, z, w=-4)
    G.add_edge(x, t, w=-2)
    G.add_edge(y, x, w=-3)
    G.add_edge(y, z, w=9)
    G.add_edge(z, x, w=7)
    G.add_edge(z, s, w=2)

    assert bellman_ford(G, s)
Exemplo n.º 20
0
                route_paths = new_global_routes_fixed[s, d]
                path_len = 0
                for path, weight in route_paths:
                    path_len += (len(path) - 1) * weight
                path_len_routes[(alpha, s, d)] = path_len

            #print path_len_routes

            for s, d in new_global_routes_base_fixed:
                route_paths = new_global_routes_base_fixed[s, d]
                path_len = 0
                for path, weight in route_paths:
                    path_len += (len(path) - 1) * weight
                path_len_routes_base[(alpha, s, d)] = path_len

            g = DiGraph()
            f = open(final_topology)
            line = f.readline()
            length_graph = int(line.rstrip().split(' ')[0])
            line = f.readline()
            while line:
                s = line.rstrip().split(' ')
                lls = int(s[0])
                lld = int(s[1])

                g.add_edge(lls, lld, 1)
                g.add_edge(lld, lls, 1)

                line = f.readline()
            f.close()
Exemplo n.º 21
0
def test_dag_shortest_path():
    G = DiGraph()
    r = Node("r")
    s = Node("s")
    t = Node("t")
    x = Node("x")
    y = Node("y")
    z = Node("z")

    G.add_edge(r, s, w=5)
    G.add_edge(r, t, w=3)
    G.add_edge(s, x, w=6)
    G.add_edge(s, t, w=2)
    G.add_edge(t, x, w=7)
    G.add_edge(t, y, w=4)
    G.add_edge(t, z, w=2)
    G.add_edge(x, y, w=-1)
    G.add_edge(x, z, w=1)
    G.add_edge(y, z, w=-2)

    dag_shortest_path(G, s)

    assert [u.d for u in G.node] == [float('inf'), 0, 2, 6, 5, 3]
Exemplo n.º 22
0
from graph import DiGraph
import os
import random

filename = "graph.txt"

graph = DiGraph()

if os.path.isfile(filename):
    graph.read_from_filename(filename)

print("Loading Complete.")

with open("temp.txt", "w+", encoding="utf-8") as f:
    f.write("\n".join(graph.nodes))
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.º 25
0
class Controller:
    def __init__(self, file):
        '''
        Constructor for controller
        Input: f (string) filename to read a DiGraph from
        '''
        f = open(file, "r")
        n, m = f.readline().strip().split()
        n = int(n)
        m = int(m)
        self.__dg = DiGraph(n)
        for i in range(m):
            x, y, cost = f.readline().strip().split()
            x = int(x)
            y = int(y)
            cost = int(cost)
            self.__dg.addEdge(x, y)
            #self.__dg.setCost(x, y, cost) #! do not
        f.close()

    def getGraph(self):
        return self.__dg

    def BFS(self, s):
        '''
        Breadth first search algorithm (BFS)
        Input:  s - start vertex
        Output: visited (set) - the set of accessible vertices from s
                prev (dictionary) - each key has as value the vertex
                    that is previous to the key in the BFS path
                dist (dictionary) - each key represent a vertex and has
                    as value the length of the shortest path from s to it
        '''
        visited = set()  # set
        q = Queue()  # queue
        prev = {}  # dictionary
        dist = {}  # dictionary

        prev[s] = None
        dist[s] = 0

        q.enqueue(s)
        while len(q) > 0:
            x = q.dequeue()  # get the next element from the queue
            for y in self.__dg.iterateOut(x):
                if y not in visited:
                    visited.add(y)
                    q.enqueue(y)
                    prev[y] = x
                    dist[y] = dist[x] + 1

        return visited, prev, dist

    def getPath(self, s, t):
        '''
        Finds a lowest length path between given vertices s and t
            by using a forward breadth-first search algorithm
        Input:  s - source vertex
                t - target vertex
        Output: a lowest length path between s and t (list of vertices)
        '''
        if s >= self.__dg.getV() or t >= self.__dg.getV():
            raise KeyError("Invalid vertices.")

        path = []

        #1. we perform a BFS starting at s (source vertex)
        visited, prev, dist = self.BFS(s)

        #2. we check if t (target vertex) is in visited
        #       i.e. there exists a (lowest length) path from s to t
        if t not in visited:
            return None  # there is no path from s to t

        #3. we compute the path
#         path.append(v)
#         while v != s:           # while the current vertex is not the start vertex
#             for x in self.__dg.iterateIn(v):
#                 if x in dist and dist[x] == dist[v] - 1:
#                     v = x       # we search in the inbounds of v for a vertex whose path length from s
#                     break       # to it is just 1 unit below the path length from s to the current v
#             path.append(v)      # then, v becomes that vertex and we add it to the path list

        v = t  # we start at t
        path.append(t)
        while v != s:
            v = prev[v]
            path.append(v)
        #4. we reverse the order of elements in the list 'path'
        path.reverse()

        #5. we return the result
        return path
Exemplo n.º 26
0
                path_len = 0
                for path, weight in route_paths:
                    path_len += (len(path) - 1) * weight
                path_len_routes[(alpha, s, d)] = path_len


            #print path_len_routes

            for s,d in new_global_routes_base_fixed:
                route_paths = new_global_routes_base_fixed[s,d]
                path_len = 0
                for path, weight in route_paths:
                    path_len += (len(path) - 1) * weight
                path_len_routes_base[(alpha, s, d)] = path_len

            g = DiGraph()
            f = open(final_topology) 
            line = f.readline()
            length_graph = int(line.rstrip().split(' ')[0])
            line = f.readline()
            while line:
                s = line.rstrip().split(' ')
                lls = int(s[0])
                lld = int(s[1])

                g.add_edge(lls, lld, 1);
                g.add_edge(lld, lls, 1);

                line = f.readline()
            f.close()
Exemplo n.º 27
0
from GraphRendering.Render import run_system
from GraphRendering.Graphs import Graph
from GraphRendering.Physics import System

from graph import DiGraph
import os
import random

filename = "graph.txt"

graph = DiGraph()

if os.path.isfile(filename):
    graph.read_from_filename(filename)

print("Loading Complete.")

# start, end = random.sample(list(graph.nodes), 2)
start = "Kevin_Bacon"
end = "Computer_language"

assert (end in graph.nodes)

nodes = graph.shortest_path(start, end)

# for n in list(nodes):
#     nodes.extend(
#         random.sample([c for c in graph.nodes[n] if c in graph.nodes], 3)
#     )

nodes = set(nodes)  # make unique
Exemplo n.º 28
0
    def get_dfs_tree(self, property, node):
        if property not in self.properties:
            raise Exception(property + ' is not a SKOS property')
        di_graph = DiGraph()

        for s, p, o in self.vocab.triples(None, SKS[property], None):
            di_graph.add_edge(s, o)

        tree_edges = di_graph.dfs_tree(node)

        di_graph.clear()
        di_graph.add_edges_from(list(tree_edges))

        # label nodes with literals
        for n1, n2 in di_graph.edges():
            di_graph.nodes()[n1]['label'] = self.get_term_from_uri(n1)
            di_graph.nodes()[n2]['label'] = self.get_term_from_uri(n2)

        return di_graph
Exemplo n.º 29
0
from graph import DiGraph
import os
import random

filename = "graph.txt"

graph = DiGraph()

if os.path.isfile(filename):
    graph.read_from_filename(filename)

print("Loading Complete.")
node1 = random.choice(list(graph.nodes))
node2 = random.choice(list(graph.nodes))

print(graph.shortest_path(node1, node2))
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.º 31
0
def test_dijkstra():
    G = DiGraph()
    s = Node("s")
    t = Node("t")
    y = Node("y")
    x = Node("x")
    z = Node("z")

    G.add_edge(s, t, w=10)
    G.add_edge(s, y, w=5)
    G.add_edge(t, y, w=2)
    G.add_edge(y, t, w=3)
    G.add_edge(t, x, w=1)
    G.add_edge(y, x, w=9)
    G.add_edge(y, z, w=2)
    G.add_edge(z, s, w=7)
    G.add_edge(z, x, w=6)
    G.add_edge(x, z, w=4)

    dijkstra(G, s)

    assert [u.d for u in G.node] == [0, 8, 5, 9, 7]
Exemplo n.º 32
0
class TreeGlimpsedClassifier(NN.Module):
    def __init__(
            self,
            n_children=2,
            n_depth=3,
            h_dims=128,
            node_tag_dims=128,
            edge_tag_dims=128,
            n_classes=10,
            steps=5,
            filters=[16, 32, 64, 128, 256],
            kernel_size=(3, 3),
            final_pool_size=(2, 2),
            glimpse_type='gaussian',
            glimpse_size=(15, 15),
    ):
        '''
        Basic idea:
        * We detect objects through an undirected graphical model.
        * The graphical model consists of a balanced tree of latent variables h
        * Each h is then connected to a bbox variable b and a class variable y
        * b of the root is fixed to cover the entire canvas
        * All other h, b and y are updated through message passing
        * The loss function should be either (not completed yet)
            * multiset loss, or
            * maximum bipartite matching (like Order Matters paper)
        '''
        NN.Module.__init__(self)
        self.n_children = n_children
        self.n_depth = n_depth
        self.h_dims = h_dims
        self.node_tag_dims = node_tag_dims
        self.edge_tag_dims = edge_tag_dims
        self.h_dims = h_dims
        self.n_classes = n_classes
        self.glimpse = create_glimpse(glimpse_type, glimpse_size)
        self.steps = steps

        self.cnn = build_cnn(
            filters=filters,
            kernel_size=kernel_size,
            final_pool_size=final_pool_size,
        )

        # Create graph of latent variables
        G = nx.balanced_tree(self.n_children, self.n_depth)
        nx.relabel_nodes(G, {i: 'h%d' % i
                             for i in range(len(G.nodes()))}, False)
        self.h_nodes_list = h_nodes_list = list(G.nodes)
        for h in h_nodes_list:
            G.node[h]['type'] = 'h'
        b_nodes_list = ['b%d' % i for i in range(len(h_nodes_list))]
        y_nodes_list = ['y%d' % i for i in range(len(h_nodes_list))]
        self.b_nodes_list = b_nodes_list
        self.y_nodes_list = y_nodes_list
        hy_edge_list = [(h, y) for h, y in zip(h_nodes_list, y_nodes_list)]
        hb_edge_list = [(h, b) for h, b in zip(h_nodes_list, b_nodes_list)]
        yh_edge_list = [(y, h) for y, h in zip(y_nodes_list, h_nodes_list)]
        bh_edge_list = [(b, h) for b, h in zip(b_nodes_list, h_nodes_list)]

        G.add_nodes_from(b_nodes_list, type='b')
        G.add_nodes_from(y_nodes_list, type='y')
        G.add_edges_from(hy_edge_list)
        G.add_edges_from(hb_edge_list)

        self.G = DiGraph(nx.DiGraph(G))
        hh_edge_list = [
            (u, v) for u, v in self.G.edges()
            if self.G.node[u]['type'] == self.G.node[v]['type'] == 'h'
        ]

        self.G.init_node_tag_with(node_tag_dims,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01))
        self.G.init_edge_tag_with(edge_tag_dims,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01),
                                  edges=hy_edge_list + hb_edge_list +
                                  bh_edge_list)
        self.G.init_edge_tag_with(h_dims * n_classes,
                                  T.nn.init.uniform_,
                                  args=(-.01, .01),
                                  edges=yh_edge_list)

        # y -> h.  An attention over embeddings dynamically generated through edge tags
        self.G.register_message_func(self._y_to_h,
                                     edges=yh_edge_list,
                                     batched=True)

        # b -> h.  Projects b and edge tag to the same dimension, then concatenates and projects to h
        self.bh_1 = NN.Linear(self.glimpse.att_params, h_dims)
        self.bh_2 = NN.Linear(edge_tag_dims, h_dims)
        self.bh_all = NN.Linear(
            2 * h_dims + filters[-1] * NP.prod(final_pool_size), h_dims)
        self.G.register_message_func(self._b_to_h,
                                     edges=bh_edge_list,
                                     batched=True)

        # h -> h.  Just passes h itself
        self.G.register_message_func(self._h_to_h,
                                     edges=hh_edge_list,
                                     batched=True)

        # h -> b.  Concatenates h with edge tag and go through MLP.
        # Produces Δb
        self.hb = NN.Linear(h_dims + edge_tag_dims, self.glimpse.att_params)
        self.G.register_message_func(self._h_to_b,
                                     edges=hb_edge_list,
                                     batched=True)

        # h -> y.  Concatenates h with edge tag and go through MLP.
        # Produces Δy
        self.hy = NN.Linear(h_dims + edge_tag_dims, self.n_classes)
        self.G.register_message_func(self._h_to_y,
                                     edges=hy_edge_list,
                                     batched=True)

        # b update: just adds the original b by Δb
        self.G.register_update_func(self._update_b,
                                    nodes=b_nodes_list,
                                    batched=False)

        # y update: also adds y by Δy
        self.G.register_update_func(self._update_y,
                                    nodes=y_nodes_list,
                                    batched=False)

        # h update: simply adds h by the average messages and then passes it through ReLU
        self.G.register_update_func(self._update_h,
                                    nodes=h_nodes_list,
                                    batched=False)

    def _y_to_h(self, source, edge_tag):
        '''
        source: (n_yh_edges, batch_size, 10) logits
        edge_tag: (n_yh_edges, edge_tag_dims)
        '''
        n_yh_edges, batch_size, _ = source.shape

        w = edge_tag.reshape(n_yh_edges, 1, self.n_classes, self.h_dims)
        w = w.expand(n_yh_edges, batch_size, self.n_classes, self.h_dims)
        source = source[:, :, None, :]
        return (F.softmax(source) @ w).reshape(n_yh_edges, batch_size,
                                               self.h_dims)

    def _b_to_h(self, source, edge_tag):
        '''
        source: (n_bh_edges, batch_size, 6) bboxes
        edge_tag: (n_bh_edges, edge_tag_dims)
        '''
        n_bh_edges, batch_size, _ = source.shape
        # FIXME: really using self.x is a bad design here
        _, nchan, nrows, ncols = self.x.size()
        source, _ = self.glimpse.rescale(source, False)
        _source = source.reshape(-1, self.glimpse.att_params)

        m_b = T.relu(self.bh_1(_source))
        m_t = T.relu(self.bh_2(edge_tag))
        m_t = m_t[:, None, :].expand(n_bh_edges, batch_size, self.h_dims)
        m_t = m_t.reshape(-1, self.h_dims)

        # glimpse takes batch dimension first, glimpse dimension second.
        # here, the dimension of @source is n_bh_edges (# of glimpses), then
        # batch size, so we transpose them
        g = self.glimpse(self.x, source.transpose(0, 1)).transpose(0, 1)
        grows, gcols = g.size()[-2:]
        g = g.reshape(n_bh_edges * batch_size, nchan, grows, gcols)
        phi = self.cnn(g).reshape(n_bh_edges * batch_size, -1)

        # TODO: add an attribute (g) to h

        m = self.bh_all(T.cat([m_b, m_t, phi], 1))
        m = m.reshape(n_bh_edges, batch_size, self.h_dims)

        return m

    def _h_to_h(self, source, edge_tag):
        return source

    def _h_to_b(self, source, edge_tag):
        n_hb_edges, batch_size, _ = source.shape
        edge_tag = edge_tag[:, None]
        edge_tag = edge_tag.expand(n_hb_edges, batch_size, self.edge_tag_dims)
        I = T.cat([source, edge_tag], -1).reshape(n_hb_edges * batch_size, -1)
        db = self.hb(I)
        return db.reshape(n_hb_edges, batch_size, -1)

    def _h_to_y(self, source, edge_tag):
        n_hy_edges, batch_size, _ = source.shape
        edge_tag = edge_tag[:, None]
        edge_tag = edge_tag.expand(n_hy_edges, batch_size, self.edge_tag_dims)
        I = T.cat([source, edge_tag], -1).reshape(n_hy_edges * batch_size, -1)
        dy = self.hy(I)
        return dy.reshape(n_hy_edges, batch_size, -1)

    def _update_b(self, b, b_n):
        return b['state'] + b_n[0][2]['state']

    def _update_y(self, y, y_n):
        return y['state'] + y_n[0][2]['state']

    def _update_h(self, h, h_n):
        m = T.stack([e[2]['state'] for e in h_n]).mean(0)
        return T.relu(h['state'] + m)

    def forward(self, x, y=None):
        self.x = x
        batch_size = x.shape[0]

        self.G.zero_node_state((self.h_dims, ),
                               batch_size,
                               nodes=self.h_nodes_list)
        self.G.zero_node_state((self.n_classes, ),
                               batch_size,
                               nodes=self.y_nodes_list)
        self.G.zero_node_state((self.glimpse.att_params, ),
                               batch_size,
                               nodes=self.b_nodes_list)

        for t in range(self.steps):
            self.G.step()
            # We don't change b of the root
            self.G.node['b0']['state'].zero_()

        self.y_pre = T.stack([
            self.G.node['y%d' % i]['state']
            for i in range(self.n_nodes - 1, self.n_nodes - self.n_leaves -
                           1, -1)
        ], 1)
        self.v_B = T.stack(
            [
                self.glimpse.rescale(self.G.node['b%d' % i]['state'], False)[0]
                for i in range(self.n_nodes)
            ],
            1,
        )
        self.y_logprob = F.log_softmax(self.y_pre)
        return self.G.node['h0']['state']

    @property
    def n_nodes(self):
        return (self.n_children**self.n_depth - 1) // (self.n_children - 1)

    @property
    def n_leaves(self):
        return self.n_children**(self.n_depth - 1)
Exemplo n.º 33
0
###
# Create vertices for interstation connections
###

# interstation cost (mins)
isc = 5
interrows = []
for stopA in fullStops:
    for stopB in fullStops:
        a, b = spsl(stopA)
        c, d = spsl(stopB)
        if (a == c and not (b == d)):
            interrows.append((stopA, stopB, isc))

GMaster = DiGraph('awesome')
for stop in fullStops:
    GMaster.add_node(stop)

for row in rows:
    GMaster.add_edge(row[0], row[1], row[2])

for irow in interrows:
    GMaster.add_edge(irow[0], irow[1], irow[2])

print "Calculate Routes..."

# Holds all routes in memory to aid filtering
fromToDict = {}

for stopFrom in fullStops:
Exemplo n.º 34
0
  def build_flowgraph(self, blocks):
    g = DiGraph()

    self.block_offsets = {}
    self.block_nodes = {}

    # Add nodes
    for block in self.blocks:
      self.block_offsets[block.start_offset] = block
      block_node = g.make_add_node(block)
      self.block_nodes[block] = block_node

    # Compute a block's immediate predecessors and successors
    for block in self.blocks:
      for jump_offset in block.jump_offsets:
        assert jump_offset in self.block_offsets
        successor_block = self.block_offsets[jump_offset]
        successor_block.predecessors.add(block)
        block.successors.add(successor_block)
      if ( block.follow_offset
           and (not (jump_flags & block.flags or
                     (BB_NOFOLLOW in block.flags))) ):
        assert block.follow_offset in self.block_offsets
        successor_block = self.block_offsets[block.follow_offset]
        successor_block.predecessors.add(block)
        block.successors.add(successor_block)

    assert(len(self.blocks) > 0)
    self.entry_node = self.blocks[0]

    sorted_blocks = sorted(self.blocks, key=attrgetter('index'))
    for i, block in enumerate(sorted_blocks):

      # Is this this dead code? (Remove self loops in calculation)
      # Entry node, blocks[0] is never unreachable
      if not block.predecessors - set([block]) and block != blocks[0]:
        block.unreachable = True

      block = sorted_blocks[i]
      if block.follow_offset:
          if jump_flags & block.flags:
            kind = 'jump'
          elif BB_NOFOLLOW in block.flags:
            kind = 'no fallthrough'
          else:
            kind = 'fallthrough'
          g.make_add_edge(
              self.block_nodes[block],
              self.block_nodes[self.block_offsets[block.follow_offset]],
              kind)
      # Connect the current block to its jump targets
      for jump_index in block.jump_offsets:
          target_block = self.block_offsets[jump_index]
          if jump_index > block.start_offset:
            if BB_LOOP in block.flags:
              edge_type = 'forward_scope'
            else:
              edge_type = 'forward'
          else:
            edge_type = 'backward'

          if self.block_nodes[target_block] == self.block_nodes[block]:
            edge_type = 'self-loop'

          g.make_add_edge(
              self.block_nodes[block],
              self.block_nodes[target_block],
              edge_type)
          pass
      pass

    self.graph = g
    return
Exemplo n.º 35
0
from helpers import get_links
from graph import DiGraph

from pprint import pprint

root_pagename = "NBC"

graph = DiGraph()

# visited = set()
to_traverse = [root_pagename]

# traverse whole graph
while to_traverse:
    pagename = to_traverse.pop(0)
    graph.add_node(pagename)

    # visited.add(pagename)
    print(f"Traversing {pagename}")
    for link in get_links(pagename):
        graph.add_link(pagename, link)
        if link in graph.nodes: continue
        to_traverse.append(link)  # BFS

    # print("\n".join(to_traverse))
    # break
    if len(graph.nodes) >= 100:
        # pprint(graph.nodes)
        break

graph.write_to_file("graph.txt")