def directed_dfs(digraph, start, end, max_total_dist, max_dist_outdoors):
    """
    Finds the shortest path from start to end using a directed depth-first
    search. The total distance traveled on the path must not
    exceed max_total_dist, and the distance spent outdoors on this path must
    not exceed max_dist_outdoors.

    Parameters:
        digraph: Digraph instance
            The graph on which to carry out the search
        start: string
            Building number at which to start
        end: string
            Building number at which to end
        max_total_dist: int
            Maximum total distance on a path
        max_dist_outdoors: int
            Maximum distance spent outdoors on a path

    Returns:
        The shortest-path from start to end, represented by
        a list of building numbers (in strings), [n_1, n_2, ..., n_k],
        where there exists an edge from n_i to n_(i+1) in digraph,
        for all 1 <= i < k

        If there exists no path that satisfies max_total_dist and
        max_dist_outdoors constraints, then raises a ValueError.
    """
    start = Node(start)
    end = Node(end)
    best_path = get_best_path(digraph, start, end, [], max_dist_outdoors,
                              max_total_dist, 1E9, None)
    if best_path == None:
        raise ValueError('No valid path')
    return [node.get_name() for node in best_path]
示例#2
0
def expand_node(node: Node, algorithm: Algorithm):
    """Expand node and get the list with all the adjacent nodes

    Args:
        node (Node): Current node
        algorithm (Algorithm): Choosen Algorithm

    Returns:
        list: List with the adjacent nodes
    """
    expansion = []
    moves = node.gamestate.expand()

    for move in moves:
        new_gamestate = node.gamestate.clone()
        from_i, to_i = move
        new_gamestate.move_ball(from_i, to_i)
        new_node = Node(new_gamestate, 0, node.dist + 1)

        if algorithm == Algorithm.GREEDY or algorithm == Algorithm.A_STAR:
            new_node.set_cost(new_node.number_of_wrong_heuristics())

        expansion.append(new_node)

    for children in expansion:
        children.parent = node

    return expansion
示例#3
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    d = Digraph()
    with open(map_filename) as f:
        for line in f:
            data = line.split(' ')
            assert (len(data) == 4)

            src = Node(data[0])
            dest = Node(data[1])
            edge = WeightedEdge(src, dest, data[2], data[3])

            if not d.has_node(src):
                d.add_node(src)
            if not d.has_node(dest):
                d.add_node(dest)
            d.add_edge(edge)
        f.close()
    return d
示例#4
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    digraph = Digraph()
    print("Loading map from file...")
    with open(map_filename, 'r') as file:
        for line in file:
            parts = line.split()
            src = Node(parts[0])
            dest = Node(parts[1])
            edge = WeightedEdge(src, dest, int(parts[2]), int(parts[3]))
            edge = WeightedEdge(src, dest, int(parts[2]), int(parts[3]))
            if not digraph.has_node(src):
                digraph.add_node(src)
            if not digraph.has_node(dest):
                digraph.add_node(dest)
            digraph.add_edge(edge)

    return digraph
示例#5
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("Loading map from file...")
    f=open(map_filename,'r')
    g=Digraph()
    for line in f:
        src, dest, total_distance, outdoor_distance = line.split()
        src_node=Node(src)
        dest_node=Node(dest)
        src_dest=WeightedEdge(src_node, dest_node, total_distance, outdoor_distance)
        if not g.has_node(src_node):
            g.add_node(src_node)
        if not g.has_node(dest_node):
            g.add_node(dest_node)
        g.add_edge(src_dest)
    f.close()
    return g
示例#6
0
 def jaccards_coefficient(self, source, target):
     nodes1 = self.database.one_to_many_nodes(source)
     nodes2 = self.database.one_to_many_nodes(target)
     n1 = set([Node(n).id for n in nodes1])
     n2 = set([Node(n).id for n in nodes2])
     score = len(n1.intersection(n2)) / len(n1.union(n2))
     return score
示例#7
0
文件: ps2.py 项目: guoguozy/Python
def get_best_path(digraph, start, end, path, max_dist_outdoors, best_dist,
                  best_path):

    if (not digraph.has_node(Node(start))) or (not digraph.has_node(
            Node(end))):
        raise ValueError  # 起点终点存在不在图中的情况
    elif start == end:  # 找到比现有path更佳的path则更改best_path
        best_dist[0] = path[1]
        best_path.clear()
        for j in range(len(path[0])):
            best_path.append(path[0][j])
        best_path.append(start)
    else:
        for i in digraph.get_edges_for_node(Node(start)):
            if str(i.get_destination()) not in path[0]:  # 排除构成环的情况
                tmp_path = copy.deepcopy(path)  # 深拷贝path
                tmp_path[0].append(start)  # 将之加入path
                tmp_path[1] = path[1] + int(i.get_total_distance())
                tmp_path[2] = path[2] + int(i.get_outdoor_distance())
                if tmp_path[2] <= max_dist_outdoors and tmp_path[
                        1] <= best_dist[0]:
                    get_best_path(digraph, str(i.get_destination()), end,
                                  tmp_path, max_dist_outdoors, best_dist,
                                  best_path)
    return (best_path, best_dist[0])
示例#8
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    g = Digraph()
    with open(map_filename) as f:
        data = f.read().strip()
    dataList = data.split('\n')
    for mapEdge in dataList:
        edgeList = mapEdge.split(' ')  # from to TD, DO
        fromN = Node(edgeList[0])
        toN = Node(edgeList[1])
        if not g.has_node(fromN):
            g.add_node(fromN)
        if not g.has_node(toN):
            g.add_node(toN)
        g.add_edge(WeightedEdge(fromN, toN, edgeList[2], edgeList[3]))
    return g
示例#9
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("Loading map from file...")
    openfile = open(map_filename, 'r')
    gph = Digraph()
    for line in openfile:
        NewLine = line.strip('\n').split(" ")
        a, b, c, d = NewLine
        a, b = Node(a), Node(b)
        c, d = int(c), int(d)
        if a not in gph.nodes:
            gph.add_node(a)
        if b not in gph.nodes:
            gph.add_node(b)
        DirEdge = WeightedEdge(a, b, c, d)
        gph.add_edge(DirEdge)
    openfile.close()
    return gph
示例#10
0
def build_graph(w, words, embedding):
    dic_nodes = {}
    edge_names = []

    # T is a node with special distribution
    # TODO : CHeck TUTTE 1984
    dic_nodes["T"] = Node(name="T", node_type="F", dist="MTT")

    for i, wordi in enumerate(words):
        for j, wordj in enumerate(words):
            # TODO: Rajouter les siblings
            if i == j:
                continue
            name_V = f"V_{wordi}_{wordj}"
            node_V = Node(name_V, "V", nb_states=2)
            dic_nodes[name_V] = node_V
            dist = np.array([phi_edge(i, j, w, embedding, y) for y in [0, 1]])
            name_F = f"F_{wordi}_{wordj}"
            node_F = Node(name_F, "F", dist=dist, dist_index=[name_V])
            dic_nodes[name_F] = node_F
            edge_names.append((name_V, name_F))
            edge_names.append((name_V, 'T'))

    graph = Graph(dic_nodes, edge_names)
    return graph
示例#11
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    g = Digraph()
    with open(map_filename, "r") as file:
        for line in file:
            (src, dst, tot_dist, outdoor_dist) = line.split(' ')
            tot_dist = int(tot_dist)
            outdoor_dist = int(outdoor_dist)
            if not g.has_node(Node(src)):
                g.add_node(Node(src))
            if not g.has_node(Node(dst)):
                g.add_node(Node(dst))
            g.add_edge(WeightedEdge(src, dst, tot_dist, outdoor_dist))
    return g
示例#12
0
def get_neighbour_nodes(node: graph.Node):
    neighbour_nodes = list()
    node_rep = node.data

    neighbour_reps = get_neighbour_nodes_reps(node_rep)

    for nr in neighbour_reps:
        string_rep = "".join(nr)
        if string_rep in shared.GRAPH_NODES:
            found_node = shared.GRAPH_NODES[string_rep]

            edges = [ndp.node for ndp in node.edges]
            if found_node not in edges:
                node.add_edge(found_node, 1)
                found_node.add_edge(node, 1)
            neighbour_nodes.append(found_node)

        else:

            new_node = build_node(nr)
            node.add_edge(new_node, 1)
            new_node.add_edge(node, 1)
            # print(node, new_node)

            neighbour_nodes.append(new_node)

    return neighbour_nodes
示例#13
0
def add_nodes():
    """Tests Graph.add_nodes method should add a node from the graph"""
    h = Node('h')
    i = Node('i')
    GRAPH2.add_nodes(h, i)

    return _get_node_list(GRAPH2)
示例#14
0
def test_edge_init():
    e = Edge(Node(1), Node(2))
    assert e is not None
    assert e.n1 is not None
    assert e.n1.value == 1
    assert e.n2 is not None
    assert e.n2.value == 2
示例#15
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    D = Digraph()
    with open(map_filename, 'r') as f:
        for count, line in enumerate(f):
            src, dest, total_dist, out_dist = line.split(' ')
            source = Node(src)
            destination = Node(dest)
            we = WeightedEdge(source, destination, total_dist, out_dist)
            D = _add_node(D, source, destination)
            D.add_edge(we)
    return D
示例#16
0
    def _construct_graph(
            customers: Mapping[int, CustomerDemand], review_demands: Dict[int,
                                                                          int],
            num_of_products: int) -> Tuple[FlowGraph, Node, Node, dict]:
        source = Node("source")
        sink = Node("sink")
        graph = FlowGraph()

        product_nodes = {}
        for product in range(1, num_of_products + 1):
            product_node = Node("P" + str(product))
            product_nodes[product] = product_node
            graph.create_edge(product_node, sink, review_demands[product], inf)

        assignment_edges = {}
        for customer in customers.values():
            customer_node = Node("C" + str(customer.i))
            graph.create_edge(source, customer_node, customer.low,
                              customer.upper)
            for product in customer.products:
                edge = graph.create_edge(customer_node, product_nodes[product],
                                         0, 1)
                assignment_edges[(customer.i, product)] = edge

        return graph, source, sink, assignment_edges
示例#17
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    # TODO
    # print("Loading map from file...")
    file = open(map_filename, 'r')  # open the file
    g = Digraph()
    for line in file:  # read each line of the file
        line = line.strip('\n')  # remove the \n character
        line = line.split(' ')
        for i in range(0, 2):
            nod = Node(line[i])
            if not g.has_node(nod):
                g.add_node(nod)
        wei_edge = WeightedEdge(Node(line[0]), Node(line[1]), int(line[2]),
                                int(line[3]))
        g.add_edge(wei_edge)
    file.close()
    return g
示例#18
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    # MY_CODE
    map_graph = Digraph()
    with open(map_filename, 'r') as f:
        for line in f:
            src, dest, total_dist, outdoor_dist = line.split(' ')
            src = Node(src)
            dest = Node(dest)
            if not map_graph.has_node(src):
                map_graph.add_node(src)
            if not map_graph.has_node(dest):
                map_graph.add_node(dest)
            edge = WeightedEdge(src, dest, int(total_dist), int(outdoor_dist))
            map_graph.add_edge(edge)
    return map_graph
示例#19
0
def directed_dfs(digraph, start, end, max_total_dist, max_dist_outdoors):
    """
    Finds the shortest path from start to end using a directed depth-first
    search. The total distance traveled on the path must not
    exceed max_total_dist, and the distance spent outdoors on this path must
    not exceed max_dist_outdoors.

    Parameters:
        digraph: Digraph instance
            The graph on which to carry out the search
        start: string
            Building number at which to start
        end: string
            Building number at which to end
        max_total_dist: int
            Maximum total distance on a path
        max_dist_outdoors: int
            Maximum distance spent outdoors on a path

    Returns:
        The shortest-path from start to end, represented by
        a list of building numbers (in strings), [n_1, n_2, ..., n_k],
        where there exists an edge from n_i to n_(i+1) in digraph,
        for all 1 <= i < k

        If there exists no path that satisfies max_total_dist and
        max_dist_outdoors constraints, then raises a ValueError.
    """
    # MY_CODE
    ret = get_best_path(digraph, Node(start), Node(end), [[start], 0, 0],
                        max_dist_outdoors, max_total_dist, None)
    if ret[0] is None:
        raise ValueError("Problem shouldn't be impossible!")
    else:
        return ret[0]
示例#20
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    #    print("Loading map from file...")
    inFile = open(map_filename, 'r')
    graph = Digraph()
    for line in inFile:
        linedata = line.split(' ')
        scr = Node(linedata[0])
        des = Node(linedata[1])
        graph.nodes.add(scr)
        graph.nodes.add(des)
        if not scr in graph.edges:
            graph.add_node(scr)
        if not des in graph.edges:
            graph.add_node(des)
        edge = WeightedEdge(scr, des, int(linedata[2]), int(linedata[3]))
        graph.add_edge(edge)
    return graph
示例#21
0
    def buildGraph(mapEntries):
        g = Digraph()
        nodelist = []

        #createing nodelist and adding nodes
        for eachEntry in mapEntries:
            eachEntrySource = eachEntry[0]
            eachEntryDest = eachEntry[1]
            if eachEntrySource not in nodelist:  #making sure the node is unique
                nodelist.append(eachEntrySource)
                g.add_node(Node(eachEntrySource))
            if eachEntryDest not in nodelist:
                nodelist.append(eachEntryDest)
                g.add_node(Node(eachEntryDest))

        #creating edges
        for eachEntry in mapEntries:
            src = Node(eachEntry[0])  #eachEntrySource Node
            dest = Node(eachEntry[1])  #"eachEntryDest"
            tD = eachEntry[2]  #eachEntryTotalDistance
            oD = eachEntry[3]  #eachEntryOutdoorDistance
            g.add_edge(WeightedEdge(src, dest, tD,
                                    oD))  #Adding the weighted edge kind

        return g
示例#22
0
def test_shortest_path_dijkstra_2():
    g = Graph()
    a = Node('A')
    b = Node('B')
    g.node_list.append(a)
    g.node_list.append(b)
    assert g.shortest_path_dijkstra(a, b) is None
示例#23
0
    def draw_node(self, node: Node, painter):
        """Draw the specified node."""
        painter.setBrush(
            QBrush(
                self.selected_color
                if node is self.selected_node else self.regular_node_color,
                Qt.SolidPattern,
            ))

        node_position = node.get_position()
        node_radius = Vector(node.get_radius()).repeat(2)

        painter.drawEllipse(QPointF(*node_position), *node_radius)

        if self.labels_checkbox.isChecked():
            label = node.get_label()

            # scale font down, depending on the length of the label of the node
            painter.setFont(
                QFont(self.font_family, self.font_size / len(label)))

            # draw the node label within the node dimensions
            painter.drawText(
                QRectF(*(node_position - node_radius), *(2 * node_radius)),
                Qt.AlignCenter,
                label,
            )
示例#24
0
 def common_neighbors(self, source, target):
     nodes1 = self.database.one_to_many_nodes(source)
     nodes2 = self.database.one_to_many_nodes(target)
     n1 = set([Node(n).id for n in nodes1])
     n2 = set([Node(n).id for n in nodes2])
     score = len(n1.intersection(n2))
     return score
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    print("Loading map from file...")
    campus_map = Digraph()
    with open(map_filename, 'r') as file_handle:
        for line in file_handle:  # 32 36 70 0
            info = line.strip().split(
            )  # info = ["32","36","70","0"] strip返回一个去掉前后端的line的副本
            from_node = Node(info[0])
            to_node = Node(info[1])
            if not campus_map.has_node(from_node):
                campus_map.add_node(from_node)
            if not campus_map.has_node(to_node):
                campus_map.add_node(to_node)
            cur_edge = WeightedEdge(from_node, to_node, int(info[2]),
                                    int(info[3]))
            # 假设cur_edge不存在重复的情况,也就是说mit_map.txt没有提供重复的数据
            campus_map.add_edge(cur_edge)
    return campus_map
示例#26
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """
    digraph = Digraph()
    count = 0
    with open(map_filename, 'r') as file:
        for line in file:
            line_list = line.split(' ')
            count += 1
            src = Node(line_list[0])
            dest = Node(line_list[1])
            if not digraph.has_node(src): digraph.add_node(src)
            if not digraph.has_node(dest): digraph.add_node(dest)
            weighted_edge = WeightedEdge(src, dest, line_list[2], line_list[3])
            digraph.add_edge(weighted_edge)
    return digraph
示例#27
0
def load_map(map_filename):
    """
    Parses the map file and constructs a directed graph

    Parameters:
        map_filename : name of the map file

    Assumes:
        Each entry in the map file consists of the following four positive
        integers, separated by a blank space:
            From To TotalDistance DistanceOutdoors
        e.g.
            32 76 54 23
        This entry would become an edge from 32 to 76.

    Returns:
        a Digraph representing the map
    """

    print("Loading map from file...")
    graph = Digraph()
    f = open(map_filename, 'r')
    for line in f:
        line = line.split()
        src = Node(line[0])
        dst = Node(line[1])
        for node in [src, dst]:
            if not graph.has_node(node):
                graph.add_node(node)
        edge = WeightedEdge(src, dst, int(line[2]), int(line[3]))
        graph.add_edge(edge)
    f.close()
    return graph
示例#28
0
 def preferential_attachment(self, source, target):
     nodes1 = self.database.one_to_many_nodes(source)
     nodes2 = self.database.one_to_many_nodes(target)
     n1 = set([Node(n).id for n in nodes1])
     n2 = set([Node(n).id for n in nodes2])
     score = len(n1) * len(n2)
     return score
示例#29
0
    def test_get_neighbor_simpler_complexity_none0(self):
        '''
        Possible simpler neighors for 1,2:
        1, 0; 1, 2; 0, 2

        Run 100 times to reduce probability of randomly passing every time.
        '''
        for _ in range(1000):
            num_models = 3
            models = []
            for i in range(num_models):
                models.append(Model(None))
                models[i].complexity_index = i

            graph = Graph(models)

            node0 = Node(models[1], models[0])
            graph.mark_node_visited(node0)
            node1 = Node(models[1], models[2])
            graph.mark_node_visited(node1)
            node = Node(models[0], models[2])
            graph.mark_node_visited(node)
            neighbor_node_simpler_complexity = graph.get_neighbor_simpler_complexity(node)

            self.assertIsNone(neighbor_node_simpler_complexity)
示例#30
0
 def test_load_map(self):
     g = load_map('test_load_map.txt')
     self.assertEqual(g.has_node(Node("1")), True)
     self.assertEqual(g.has_node(Node("2")), True)
     self.assertEqual(g.has_node(Node("3")), True)
     self.assertEqual(g.has_node(Node("4")), True)
     self.assertEqual(g.has_node(Node("5")), False)
示例#31
0
 def test_ElementList(self):
     jack = Node(0, {"age": 21}, name="jack")
     jill = Node(1, {"age": 21}, name="jill")
     jon = Node(2, {"age": 22}, name="jon")
     e0 = Edge(0, jack, "loves", jill, intensity=100, ferocity=100)
     e1 = Edge(1, jack, "loves", jon)
     e2 = Edge(2, jon, "loves", jack)
     e3 = Edge(3, jill, "bangs", jack)
     e4 = Edge(4, jill, "hates", jon)
     self.assertTrue(set([e0]) == set(jack.edges().filter_by_property({"ferocity": 100}, intensity=100)))
示例#32
0
文件: task.py 项目: jtamiace/ka-lite
 def __init__(self, id, fun, apply_single=True, min_count=None, max_chunksize=0, writer=None):
     Node.__init__(self, id)
     self._read = None  # to be set by subclasss
     self._out_writer = writer
     self._exc = None
     self._done = False
     self._num_writers = 0
     self._wlock = threading.Lock()
     self.fun = fun
     self.min_count = None
     self.max_chunksize = 0  # not set
     self.apply_single = apply_single
示例#33
0
class TestEvent(unittest.TestCase):

    def setUp(self):
        self.node = Node(Event(3))
        self.attrs = self.node._wraps._attrs.iterkeys()

    def test_initializer(self):
        node = Node(Event(3, title=u"title", summary=u"summary"))
        self.assertEquals(node.title, u"title")
        self.assertEquals(node._id, 3)
        self.assertEquals(node.summary, u"summary")
        self.assertTrue(node.speaker is None)
        self.assertRaises(AttributeError, Event, 3, t=u"title")

    def test_ready(self):
        """Check that Event._is_ready returns True,
        iff all fields are set.
        """
        for attr in self.attrs:
            self.assertFalse(self.node._is_ready())
            setattr(self.node._wraps, attr, "val")
        self.assertTrue(self.node._is_ready())

    def test_wrong_attribute(self):
        with self.assertRaises(AttributeError):
            setattr(self.node.wrong_attr, False)

    def test_start_time(self):
        time = "10:30"
        self.node._wraps.time_start = time
        self.assertEquals(self.node.starts(), time)

    def test_start_time(self):
        time = "10:30"
        self.node._wraps.time_end = time
        self.assertEquals(self.node.ends(), time)

    def test_add_conflicts(self):
        e = Node(Event(5))
        self.node.add_conflict_with_node(e)
        self.assertTrue(e.get_id() in self.node.conflicts_with)

    def test_save(self):
        self.assertRaises(NotReadyError, self.node.save)
        for attr in self.attrs:
            setattr(self.node._wraps, attr, "val")
        self.node.save()
示例#34
0
class TestNode(unittest.TestCase):
    def setUp(self):
        self.node = Node()

    def test_is_last_false(self):
        next_node = Node()
        self.node.connects.update({50: next_node})
        result = self.node.is_last()
        self.assertFalse(result)

    def test_is_last_true(self):
        next_node = Node()
        next_node.mark = 50
        self.node.connects.update({50: next_node})
        result = self.node.is_last()
        self.assertTrue(result)

    def test_is_last_empty(self):
        result = self.node.is_last()
        self.assertTrue(result)

    def test_add_connect_error(self):
        value = 50
        method = self.node.add_connect
        self.assertRaises(TypeError, method, args=(value, None))

    def test_add_connect_ok(self):
        value = 50
        self.node.add_connect(value, Node())
        self.assertTrue(self.node.connects[value])
        self.assertTrue(isinstance(self.node.connects[value], Node))

    def test_str_ok(self):
        self.node.name = 5
        string = str(self.node)
        self.assertEqual(string, '[5]')
示例#35
0
 def test_Node_edges(self):
     jack = Node(0, {"age": 21}, name="jack")
     jill = Node(1, {"age": 21}, name="jill")
     jon = Node(2, {"age": 22}, name="jon")
     e0 = Edge(0, jack, "loves", jill, intensity=100, ferocity=100)
     e1 = Edge(1, jack, "loves", jon)
     e2 = Edge(2, jon, "loves", jack)
     e3 = Edge(3, jill, "bangs", jack)
     e4 = Edge(4, jill, "hates", jon)
     eall_set = set((e0, e1, e2, e3))
     eloves_set = set((e0, e1, e2))
     self.assertTrue(eall_set == set(jack.edges()))
     self.assertTrue(e4 not in jack.edges())
     self.assertTrue(eloves_set == set(jack.edges("loves")))
     self.assertTrue(set([e2]) == set(jack.edges("loves", "incoming")))
     self.assertTrue(set([e0]) == set(jack.edges("loves", intensity=100)))
     self.assertTrue(set([]) == set(jack.edges("loves", properties={"ferocity": 99}, intensity=100)))
示例#36
0
 def setUp(self):
     self.node = Node(Event(3))
     self.attrs = self.node._wraps._attrs.iterkeys()
示例#37
0
 def test_is_last_true(self):
     next_node = Node()
     next_node.mark = 50
     self.node.connects.update({50: next_node})
     result = self.node.is_last()
     self.assertTrue(result)
    def __init__(self,x,y):
        Node.__init__(self,x,y)
	self.id = None
    def __init__(self,x,y):
        Node.__init__(self,x,y)
	self.id = None
	self.visited = False
示例#40
0
def build_graph():
    numbers = "0123456789"
    ascii_upper = string.ascii_uppercase
    ascii_upper_and_numbers = ascii_upper + numbers

    node1 = Node(1)
    node2 = Node(2)
    node3 = Node(3)
    node4 = Node(4)
    node5 = Node(5)
    node6 = Node(6)
    node7 = Node(7)
    node8 = Node(8)
    node9 = Node(9)

    node1_edges = build_multiple_edges(node2, ascii_upper)
    node1.edges = node1_edges

    node2_edges = build_multiple_edges(node3, ascii_upper)
    node2.edges = node2_edges

    node3_edges = build_multiple_edges(node5, ascii_upper_and_numbers) + [Edge(node4, ' ')]
    node3.edges = node3_edges

    node4_edges = build_multiple_edges(node5, ascii_upper_and_numbers)
    node4.edges = node4_edges

    node5_edges = build_multiple_edges(node6, ascii_upper_and_numbers)
    node5.edges = node5_edges

    node6_edges = build_multiple_edges(node7, ascii_upper_and_numbers)
    node6.edges = node6_edges

    node7_edges = build_multiple_edges(node8, ascii_upper_and_numbers)
    node7.edges = node7_edges

    node8_edges = build_multiple_edges(node9, ascii_upper_and_numbers)
    node8.edges = node8_edges

    graph = Graph()
    graph.nodes = [node1, node2, node3, node4, node5, node6, node7, node8]

    graph.start_node = node1
    graph.end_node = node9
    graph.current_node = graph.start_node

    return graph
示例#41
0
 def test_Node(self):
     n = Node(0)
     n.name = "jack"
     self.assertEqual(n.properties["name"], "jack")
     n.gender = "sexy"
     self.assertEqual(n.properties["gender"], "sexy")
示例#42
0
 def addRootNode(self, graph):
   graph.add_complex_node(Node.rootNode())
   return graph
示例#43
0
 def setUp(self):
     self.node = Node()
示例#44
0
  def computeGraph(self, counterFilter, tfidfFilter):
    logger.info('getGraph')
    
    # First list all Entities and convert them into Nodes
    allNodes = []
    for doc in self.docList.docs:
      for entity in doc.entities:
        n = Node(entity.name, entity.type)
        n.frequency = entity.tfidf
        allNodes.append(n)
        
        if not entity.type in self.types:
          self.types.append(entity.type)
				
    # Merge same Nodes together and aggregate the TFIDF
    print "allNodes len: " + str(len(allNodes))
    nodesDict = {}
    for node in allNodes:
        key = node.name + node.type
        if key not in nodesDict:
          nodesDict[key] = node
        else: #Aggregate
          nodesDict[key].counter += 1
          nodesDict[key].frequency += node.frequency
    print "Nbr of unique Nodes: " + str(len(nodesDict))
    
    #Compute the average TFIDF
    frequencies = []
    counters = []
    for key, node in nodesDict.iteritems():
      node.frequency = int(100000*(node.frequency/node.counter))
      nodesDict[key] = node
      frequencies.append(node.frequency)
      counters.append(node.counter)

    # Take only the best nodes
    frequencies.sort() # sorted by ascending order
    counters.sort() # sorted by ascending order
    numberOfNode = 40
    print "len nodesDict " + str(len(nodesDict))
    print "min freq" + str(frequencies[-numberOfNode])
    print "min counter" + str(counters[-numberOfNode])
    for key, node in nodesDict.items():
      '''if node.frequency < frequencies[-numberOfNode]:
        del nodesDict[key]
      if node.counter < counters[-15]:
        del nodesDict[key]'''
      if node.counter < counterFilter or node.frequency < frequencies[-min(tfidfFilter,len(frequencies)-1)]:
        del nodesDict[key]
    print "len nodesDict filtered" + str(len(nodesDict))
    
    # Set node's id
    i = 0
    IdToPosition = {}
    for key, node in nodesDict.iteritems():
      nodesDict[key].id = i # TODO can be optimized and node just before in 'uniqueNodes.append(node)'
      nodesDict[key].id = abs(hash(key)) % (10 ** 8)
      IdToPosition[ nodesDict[key].id ] = i
      i += 1
      
    #Todo Update rank and frequency of Nodes ...
    
    #Create links with Weight
    linksDict = {}
    for doc in self.docList.docs:
      for i in range(len(doc.entities)):
        key1 = doc.entities[i].name + doc.entities[i].type
        if not key1 in nodesDict: # Entity not selected
          continue
        for j in range(i+1, len(doc.entities)):
          key2 = doc.entities[j].name + doc.entities[j].type
          if not key2 in nodesDict: # Entity not selected
            continue
          if nodesDict[key1] < nodesDict[key2]:
            if (nodesDict[key1].id,nodesDict[key2].id) in linksDict:
              linksDict[(nodesDict[key1].id,nodesDict[key2].id)][0] += 1
              linksDict[(nodesDict[key1].id,nodesDict[key2].id)][1].append(doc.id)
            else:
              linksDict[(nodesDict[key1].id,nodesDict[key2].id)] = [1,[doc.id]]
          else:
            if (nodesDict[key2],nodesDict[key1]) in linksDict:
              linksDict[(nodesDict[key2].id,nodesDict[key1].id)][0] += 1
              linksDict[(nodesDict[key2].id,nodesDict[key1].id)][0].append(doc.id)
            else:
              linksDict[(nodesDict[key2].id,nodesDict[key1].id)] = [1,[doc.id]]
          pass

    print "linksDict len: " + str(len(linksDict))
        
    adjacency = np.zeros((len(nodesDict), len(nodesDict)))# Adjacency matrix
    links = []
    for k, link in linksDict.viewitems():
      if not k[0] == k[1]:
        links.append(Link(k[0],k[1],link[0],link[1]))
        adjacency[IdToPosition[k[0]]][IdToPosition[k[1]]] = link[0]
        adjacency[IdToPosition[k[1]]][IdToPosition[k[0]]] = link[0]
    
    print "links len: " + str(len(links))
    
    pr = pageRank(adjacency, .85, .000001)
    for key, node in nodesDict.iteritems():
      nodesDict[key].rank = pr[IdToPosition[nodesDict[key].id]]  
    
    # Keep only link with weight >= 2
    '''entitiesToKeep = []
    for link in links:
      if link.weight <= 1:
        links = [x for x in links if x != link]
        #links.remove(link)
      else:
        entitiesToKeep.append(link.source)
        entitiesToKeep.append(link.target)
        
    # Keep only entity connected
    for key, node in nodesDict.items():
      if not node.id in entitiesToKeep:
        del nodesDict[key]'''
        
    self.nodes = nodesDict.values()
    self.links = links
# ismailakbudak.com

import os,sys,inspect
currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0, parentdir) 

from graph import Node

def log(message):
    print("TEST:: %s"%(message))

def log_neighbours(node):
    log("%s neighbours : %s "%(node, str(node.neighbours)) ) 

n1=Node(1,1)
n2=Node(2,2)
n3=Node(3,3)
n4=Node(4,4)
n5=Node(5,5)
n6=Node(6,6)

n1.addNeighbour(n2)
n1.addNeighbour(n3)
n1.addNeighbour(n3) 
n1.addNeighbour(n5)
n1.addNeighbour(n6)

n3.addNeighbour(n2)
n4.addNeighbour(n2)
示例#46
0
 def test_add_conflicts(self):
     e = Node(Event(5))
     self.node.add_conflict_with_node(e)
     self.assertTrue(e.get_id() in self.node.conflicts_with)