当前位置: 首页>>代码示例>>Python>>正文


Python Topology.name方法代码示例

本文整理汇总了Python中fnss.topologies.topology.Topology.name方法的典型用法代码示例。如果您正苦于以下问题:Python Topology.name方法的具体用法?Python Topology.name怎么用?Python Topology.name使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fnss.topologies.topology.Topology的用法示例。


在下文中一共展示了Topology.name方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: star_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def star_topology(n):
    """
    Return a star (a.k.a hub-and-spoke) topology of :math:`n+1` nodes

    The root (hub) node has id 0 while all leaf (spoke) nodes have id
    :math:`(1, n+1)`.

    Each node has the attribute type which can either be *root* (for node 0) or
    *leaf* for all other nodes

    Parameters
    ----------
    n : int
        The number of leaf nodes

    Returns
    -------
    topology : A Topology object
    """
    if not isinstance(n, int):
        raise TypeError('n argument must be of int type')
    if n < 1:
        raise ValueError('n argument must be a positive integer')
    G = Topology(nx.star_graph(n))
    G.name = "star_topology(%d)" % (n)
    G.graph['type'] = 'star'
    G.node[0]['type'] = 'root'
    for v in range(1, n + 1):
        G.node[v]['type'] = 'leaf'
    return G
开发者ID:fnss,项目名称:fnss,代码行数:32,代码来源:simplemodels.py

示例2: k_ary_tree_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def k_ary_tree_topology(k, h):
    """
    Return a balanced k-ary tree topology of with depth h
    
    Each node has two attributes:
     * type: which can either be *root*, *intermediate* or *leaf*
     * depth:math:`(0, h)` the height of the node in the tree, where 0 is the
       root and h are leaves.
    
    Parameters
    ----------
    k : int
        The branching factor of the tree
    h : int 
        The height or depth of the tree

    Returns
    -------
    topology : A Topology object
    """
    if not isinstance(k, int) or not isinstance(h, int):
        raise TypeError('k and h arguments must be of int type')
    if k <= 1:
        raise ValueError("Invalid k parameter. It should be > 1")
    if h < 1:
        raise ValueError("Invalid h parameter. It should be >=1")
    G = Topology(nx.balanced_tree(k, h))
    G.name = "k_ary_tree_topology(%d,%d)" % (k, h)
    G.graph['type'] = 'tree'
    G.graph['k'] = k
    G.graph['h'] = h
    G.node[0]['type'] = 'root'
    G.node[0]['depth'] = 0
    # Iterate through the tree to assign labels to nodes
    v = 1
    for depth in range(1, h + 1):
        for _ in range(k**depth):
            G.node[v]['depth'] = depth
            if depth == h:
                G.node[v]['type'] = 'leaf'
            else:
                G.node[v]['type'] = 'intermediate'
            v += 1                   
    return G
开发者ID:ccascone,项目名称:fnss,代码行数:46,代码来源:simplemodels.py

示例3: full_mesh_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def full_mesh_topology(n):
    """
    Return a fully connected mesh topology of n nodes

    Parameters
    ----------
    n : int
        The number of nodes

    Returns
    -------
    topology : A Topology object
    """
    if not isinstance(n, int):
        raise TypeError('n argument must be of int type')
    if n < 1:
        raise ValueError('n argument must be a positive integer')
    G = Topology(nx.complete_graph(n))
    G.name = "full_mesh_topology(%d)" % (n)
    G.graph['type'] = 'full_mesh'
    return G
开发者ID:fnss,项目名称:fnss,代码行数:23,代码来源:simplemodels.py

示例4: line_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def line_topology(n):
    """
    Return a line topology of n nodes

    Parameters
    ----------
    n : int
        The number of nodes

    Returns
    -------
    topology : A Topology object

    """
    if not isinstance(n, int):
        raise TypeError('n argument must be of int type')
    if n < 1:
        raise ValueError('n argument must be a positive integer')
    G = Topology(nx.path_graph(n))
    G.name = "line_topology(%d)" % (n)
    G.graph['type'] = 'line'
    return G
开发者ID:fnss,项目名称:fnss,代码行数:24,代码来源:simplemodels.py

示例5: erdos_renyi_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def erdos_renyi_topology(n, p, seed=None, fast=False):
    r"""Return a random graph :math:`G_{n,p}` (Erdos-Renyi graph, binomial
    graph).

    Chooses each of the possible edges with probability p.

    Parameters
    ----------
    n : int
        The number of nodes.
    p : float
        Probability for edge creation.
    seed : int, optional
        Seed for random number generator (default=None).
    fast : boolean, optional
        Uses the algorithm proposed by [3]_, which is faster for small p

    References
    ----------
    .. [1] P. Erdos and A. Renyi, On Random Graphs, Publ. Math. 6, 290 (1959).
    .. [2] E. N. Gilbert, Random Graphs, Ann. Math. Stat., 30, 1141 (1959).
    .. [3] Vladimir Batagelj and Ulrik Brandes,
       "Efficient generation of large random networks",
       Phys. Rev. E, 71, 036113, 2005.
    """
    # validate input parameters
    if not isinstance(n, int) or n < 0:
        raise ValueError('n must be a positive integer')
    if p > 1 or p < 0:
        raise ValueError('p must be a value in (0,1)')
    if fast:
        G = Topology(nx.fast_gnp_random_graph(n, p, seed=seed))
    else:
        G = Topology(nx.gnp_random_graph(n, p, seed=seed))
    G.name = "erdos_renyi_topology(%s, %s)" % (n, p)
    G.graph['type'] = 'er'
    return G
开发者ID:fnss,项目名称:fnss,代码行数:39,代码来源:randmodels.py

示例6: waxman_1_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def waxman_1_topology(n, alpha=0.4, beta=0.1, L=1.0,
                      distance_unit='Km', seed=None):
    r"""
    Return a Waxman-1 random topology.

    The Waxman-1 random topology models assigns link between nodes with
    probability

    .. math::
            p = \alpha*exp(-d/(\beta*L)).

    where the distance *d* is chosen randomly in *[0,L]*.

    Parameters
    ----------
    n : int
        Number of nodes
    alpha : float
        Model parameter chosen in *(0,1]* (higher alpha increases link density)
    beta : float
        Model parameter chosen in *(0,1]* (higher beta increases difference
        between density of short and long links)
    L : float
        Maximum distance between nodes.
    seed : int, optional
        Seed for random number generator (default=None).

    Returns
    -------
    G : Topology

    Notes
    -----
    Each node of G has the attributes *latitude* and *longitude*. These
    attributes are not expressed in degrees but in *distance_unit*.

    Each edge of G has the attribute *length*, which is also expressed in
    *distance_unit*.

    References
    ----------
    .. [1]  B. M. Waxman, Routing of multipoint connections.
       IEEE J. Select. Areas Commun. 6(9),(1988) 1617-1622.
    """
    # validate input parameters
    if not isinstance(n, int) or n <= 0:
        raise ValueError('n must be a positive integer')
    if alpha > 1 or alpha <= 0 or beta > 1 or beta <= 0:
        raise ValueError('alpha and beta must be float values in (0,1]')
    if L <= 0:
        raise ValueError('L must be a positive number')
    if seed is not None:
        random.seed(seed)

    G = Topology(type='waxman_1', distance_unit=distance_unit)

    G.name = "waxman_1_topology(%s, %s, %s, %s)" % (n, alpha, beta, L)
    G.add_nodes_from(range(n))
    nodes = list(G.nodes())
    while nodes:
        u = nodes.pop()
        for v in nodes:
            d = L * random.random()
            if random.random() < alpha * math.exp(-d / (beta * L)):
                G.add_edge(u, v, length=d)
    return G
开发者ID:fnss,项目名称:fnss,代码行数:68,代码来源:randmodels.py

示例7: glp_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def glp_topology(n, m, m0, p, beta, seed=None):
    r"""
    Return a random topology using the Generalized Linear Preference (GLP)
    preferential attachment model.

    It differs from the extended Barabasi-Albert model in that there is link
    rewiring and a beta parameter is introduced to fine-tune preferential
    attachment.

    More precisely, the GLP topology is built as follows. First, a
    line topology with *m0* nodes is created. Then, at each step:
    with probability *p*, add *m* new links between existing nodes, selected
    with probability:

    .. math::
        \Pi(i) = \frac{deg(i) - \beta 1}{\sum_{v \in V} (deg(v) - \beta)}

    with probability :math:`1-p`, add a new node and attach it to m nodes of
    the existing topology selected with probability :math:`\Pi(i)`

    Repeat the previous step until the topology comprises n nodes in total.

    Parameters
    ----------
    n : int
        Number of nodes
    m : int
        Number of edges to attach from a new node to existing nodes
    m0 : int
        Number of edges initially attached to the network
    p : float
        The probability that new links are added
    beta : float
        Parameter to fine-tune preferntial attachment: beta < 1
    seed : int, optional
        Seed for random number generator (default=None).

    Returns
    -------
    G : Topology

    References
    ----------
    .. [1] T. Bu and D. Towsey "On distinguishing between Internet power law
       topology generators", Proceeding od the 21st IEEE INFOCOM conference.
       IEEE, volume 2, pages 638-647, 2002.
    """
    def calc_pi(G, beta):
        """Calculate GLP Pi function for all nodes of the graph"""
        # validate input parameter
        if beta >= 1:
            raise ValueError('beta must be < 1')
        degree = dict(G.degree())
        den = float(sum(degree.values()) - (G.number_of_nodes() * beta))
        return {node: (degree[node] - beta) / den for node in G.nodes()}

    def add_m_links(G, pi):
        """Add m links between existing nodes to the graph"""
        n_nodes = G.number_of_nodes()
        n_edges = G.number_of_edges()
        max_n_edges = (n_nodes * (n_nodes - 1)) / 2
        if n_edges + m > max_n_edges:  # cannot add m links
            add_node(G, pi)  # add a new node instead
            # return in any case because before doing another operation
            # (add node or links) we need to recalculate pi
            return
        new_links = 0
        while new_links < m:
            u = random_from_pdf(pi)
            v = random_from_pdf(pi)
            if u != v and not G.has_edge(u, v):
                G.add_edge(u, v)
                new_links += 1

    def add_node(G, pi):
        """Add one node to the graph and connect it to m existing nodes"""
        new_node = G.number_of_nodes()
        G.add_node(new_node)
        new_links = 0
        while new_links < m:
            existing_node = random_from_pdf(pi)
            if not G.has_edge(new_node, existing_node):
                G.add_edge(new_node, existing_node)
                new_links += 1

    # validate input parameters
    if n < 1 or m < 1 or m0 < 1:
        raise ValueError('n, m and m0 must be a positive integers')
    if beta >= 1:
        raise ValueError('beta must be < 1')
    if m >= m0:
        raise ValueError('m must be <= m0')
    if p > 1 or p < 0:
        raise ValueError('p must be included between 0 and 1')
    if seed is not None:
        random.seed(seed)
    # step 1: create a graph of m0 nodes connected by n-1 edges
    G = Topology(nx.path_graph(m0))
    G.graph['type'] = 'glp'
    G.name = "glp_topology(%d, %d, %d, %f, %f)" % (n, m, m0, p, beta)
#.........这里部分代码省略.........
开发者ID:fnss,项目名称:fnss,代码行数:103,代码来源:randmodels.py

示例8: extended_barabasi_albert_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def extended_barabasi_albert_topology(n, m, m0, p, q, seed=None):
    r"""
    Return a random topology using the extended Barabasi-Albert preferential
    attachment model.

    Differently from the original Barabasi-Albert model, this model takes into
    account the presence of local events, such as the addition of new links or
    the rewiring of existing links.

    More precisely, the Barabasi-Albert topology is built as follows. First, a
    topology with *m0* isolated nodes is created. Then, at each step:
    with probability *p* add *m* new links between existing nodes, selected
    with probability:

    .. math::
        \Pi(i) = \frac{deg(i) + 1}{\sum_{v \in V} (deg(v) + 1)}

    with probability *q* rewire *m* links. Each link to be rewired is selected as
    follows: a node i is randomly selected and a link is randomly removed from
    it. The node i is then connected to a new node randomly selected with
    probability :math:`\Pi(i)`,
    with probability :math:`1-p-q` add a new node and attach it to m nodes of
    the existing topology selected with probability :math:`\Pi(i)`

    Repeat the previous step until the topology comprises n nodes in total.

    Parameters
    ----------
    n : int
        Number of nodes
    m : int
        Number of edges to attach from a new node to existing nodes
    m0 : int
        Number of edges initially attached to the network
    p : float
        The probability that new links are added
    q : float
        The probability that existing links are rewired
    seed : int, optional
        Seed for random number generator (default=None).

    Returns
    -------
    G : Topology

    References
    ----------
    .. [1] A. L. Barabasi and R. Albert "Topology of evolving networks: local
       events and universality", Physical Review Letters 85(24), 2000.
    """
    def calc_pi(G):
        """Calculate extended-BA Pi function for all nodes of the graph"""
        degree = dict(G.degree())
        den = float(sum(degree.values()) + G.number_of_nodes())
        return {node: (degree[node] + 1) / den for node in G.nodes()}

    # input parameters
    if n < 1 or m < 1 or m0 < 1:
        raise ValueError('n, m and m0 must be a positive integer')
    if m >= m0:
        raise ValueError('m must be <= m0')
    if n < m0:
        raise ValueError('n must be > m0')
    if p > 1 or p < 0:
        raise ValueError('p must be included between 0 and 1')
    if q > 1 or q < 0:
        raise ValueError('q must be included between 0 and 1')
    if p + q > 1:
        raise ValueError('p + q must be <= 1')
    if seed is not None:
        random.seed(seed)
    G = Topology(type='extended_ba')
    G.name = "ext_ba_topology(%d, %d, %d, %f, %f)" % (n, m, m0, p, q)
    # Step 1: Add m0 isolated nodes
    G.add_nodes_from(range(m0))

    while G.number_of_nodes() < n:
        pi = calc_pi(G)
        r = random.random()

        if r <= p:
            # add m new links with probability p
            n_nodes = G.number_of_nodes()
            n_edges = G.number_of_edges()
            max_n_edges = (n_nodes * (n_nodes - 1)) / 2
            if n_edges + m > max_n_edges:  # cannot add m links
                continue  # rewire or add nodes
            new_links = 0
            while new_links < m:
                u = random_from_pdf(pi)
                v = random_from_pdf(pi)
                if u is not v and not G.has_edge(u, v):
                    G.add_edge(u, v)
                    new_links += 1

        elif r > p and r <= p + q:
            # rewire m links with probability q
            rewired_links = 0
            while rewired_links < m:
                i = random.choice(list(G.nodes()))  # pick up node randomly (uniform)
#.........这里部分代码省略.........
开发者ID:fnss,项目名称:fnss,代码行数:103,代码来源:randmodels.py

示例9: barabasi_albert_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def barabasi_albert_topology(n, m, m0, seed=None):
    r"""
    Return a random topology using Barabasi-Albert preferential attachment
    model.

    A topology of n nodes is grown by attaching new nodes each with m links
    that are preferentially attached to existing nodes with high degree.

    More precisely, the Barabasi-Albert topology is built as follows. First, a
    line topology with m0 nodes is created. Then at each step, one node is
    added and connected to m existing nodes. These nodes are selected randomly
    with probability

    .. math::
            \Pi(i) = \frac{deg(i)}{sum_{v \in V} deg V}.

    Where i is the selected node and V is the set of nodes of the graph.

    Parameters
    ----------
    n : int
        Number of nodes
    m : int
        Number of edges to attach from a new node to existing nodes
    m0 : int
        Number of nodes initially attached to the network
    seed : int, optional
        Seed for random number generator (default=None).

    Returns
    -------
    G : Topology

    Notes
    -----
    The initialization is a graph with with m nodes connected by :math:`m -1`
    edges.
    It does not use the Barabasi-Albert method provided by NetworkX because it
    does not allow to specify *m0* parameter.
    There are no disconnected subgraphs in the topology.

    References
    ----------
    .. [1] A. L. Barabasi and R. Albert "Emergence of scaling in
       random networks", Science 286, pp 509-512, 1999.
    """
    def calc_pi(G):
        """Calculate BA Pi function for all nodes of the graph"""
        degree = dict(G.degree())
        den = float(sum(degree.values()))
        return {node: degree[node] / den for node in G.nodes()}

    # input parameters
    if n < 1 or m < 1 or m0 < 1:
        raise ValueError('n, m and m0 must be positive integers')
    if m >= m0:
        raise ValueError('m must be <= m0')
    if n < m0:
        raise ValueError('n must be > m0')
    if seed is not None:
        random.seed(seed)
    # Step 1: Add m0 nodes. These nodes are interconnected together
    # because otherwise they will end up isolated at the end
    G = Topology(nx.path_graph(m0))
    G.name = "ba_topology(%d,%d,%d)" % (n, m, m0)
    G.graph['type'] = 'ba'

    # Step 2: Add one node and connect it with m links
    while G.number_of_nodes() < n:
        pi = calc_pi(G)
        u = G.number_of_nodes()
        G.add_node(u)
        new_links = 0
        while new_links < m:
            v = random_from_pdf(pi)
            if not G.has_edge(u, v):
                G.add_edge(u, v)
                new_links += 1
    return G
开发者ID:fnss,项目名称:fnss,代码行数:81,代码来源:randmodels.py

示例10: waxman_2_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def waxman_2_topology(n, alpha=0.4, beta=0.1, domain=(0, 0, 1, 1),
                      distance_unit='Km', seed=None):
    r"""Return a Waxman-2 random topology.

    The Waxman-2 random topology models place n nodes uniformly at random
    in a rectangular domain. Two nodes u, v are connected with a link
    with probability

    .. math::
            p = \alpha*exp(-d/(\beta*L)).

    where the distance *d* is the Euclidean distance between the nodes u and v.
    and *L* is the maximum distance between all nodes in the graph.


    Parameters
    ----------
    n : int
        Number of nodes
    alpha : float
        Model parameter chosen in *(0,1]* (higher alpha increases link density)
    beta : float
        Model parameter chosen in *(0,1]* (higher beta increases difference
        between density of short and long links)
    domain : tuple of numbers, optional
         Domain size (xmin, ymin, xmax, ymax)
    seed : int, optional
        Seed for random number generator (default=None).

    Returns
    -------
    G : Topology

    Notes
    -----
    Each edge of G has the attribute *length*

    References
    ----------
    .. [1]  B. M. Waxman, Routing of multipoint connections.
       IEEE J. Select. Areas Commun. 6(9),(1988) 1617-1622.
    """
    # validate input parameters
    if not isinstance(n, int) or n <= 0:
        raise ValueError('n must be a positive integer')
    if alpha > 1 or alpha <= 0 or beta > 1 or beta <= 0:
        raise ValueError('alpha and beta must be float values in (0,1]')
    if not isinstance(domain, tuple) or len(domain) != 4:
        raise ValueError('domain must be a tuple of 4 number')
    (xmin, ymin, xmax, ymax) = domain
    if xmin > xmax:
        raise ValueError('In domain, xmin cannot be greater than xmax')
    if  ymin > ymax:
        raise ValueError('In domain, ymin cannot be greater than ymax')
    if seed is not None:
        random.seed(seed)

    G = Topology(type='waxman_2', distance_unit=distance_unit)
    G.name = "waxman_2_topology(%s, %s, %s)" % (n, alpha, beta)
    G.add_nodes_from(range(n))


    for v in G.nodes():
        G.node[v]['latitude'] = (ymin + (ymax - ymin)) * random.random()
        G.node[v]['longitude'] = (xmin + (xmax - xmin)) * random.random()

    l = {}
    nodes = list(G.nodes())
    while nodes:
        u = nodes.pop()
        for v in nodes:
            x_u = G.node[u]['longitude']
            x_v = G.node[v]['longitude']
            y_u = G.node[u]['latitude']
            y_v = G.node[v]['latitude']
            l[(u, v)] = math.sqrt((x_u - x_v) ** 2 + (y_u - y_v) ** 2)
    L = max(l.values())
    for (u, v), d in l.items():
        if random.random() < alpha * math.exp(-d / (beta * L)):
            G.add_edge(u, v, length=d)

    return G
开发者ID:fnss,项目名称:fnss,代码行数:84,代码来源:randmodels.py

示例11: dumbbell_topology

# 需要导入模块: from fnss.topologies.topology import Topology [as 别名]
# 或者: from fnss.topologies.topology.Topology import name [as 别名]
def dumbbell_topology(m1, m2):
    """
    Return a dumbbell topology consisting of two star topologies
    connected by a path.

    More precisely, two star graphs :math:`K_{m1}` form the left and right
    bells, and are connected by a path :math:`P_{m2}`.

    The :math:`2*m1+m2`  nodes are numbered as follows.
     * :math:`0,...,m1-1` for the left barbell,
     * :math:`m1,...,m1+m2-1` for the path,
     * :math:`m1+m2,...,2*m1+m2-1` for the right barbell.

    The 3 subgraphs are joined via the edges :math:`(m1-1,m1)` and
    :math:`(m1+m2-1,m1+m2)`. If m2 = 0, this is merely two star topologies
    joined together.

    Please notice that this dumbbell topology is different from the barbell
    graph generated by networkx's barbell_graph function. That barbell graph
    consists of two complete graphs connected by a path. This consists of two
    stars whose roots are connected by a path. This dumbbell topology is
    particularly useful for simulating transport layer protocols.

    All nodes and edges of this topology have an attribute *type* which can be
    either *right bell*, *core* or *left_bell*

    Parameters
    ----------
    m1 : int
        The number of nodes in each bell
    m2 : int
        The number of nodes in the path

    Returns
    -------
    topology : A Topology object
    """
    if not isinstance(m1, int) or not isinstance(m2, int):
        raise TypeError('m1 and m2 arguments must be of int type')
    if m1 < 2:
        raise ValueError("Invalid graph description, m1 should be >= 2")
    if m2 < 1:
        raise ValueError("Invalid graph description, m2 should be >= 1")

    G = Topology(type='dumbbell')
    G.name = "dumbbell_topology(%d,%d)" % (m1, m2)

    # left bell
    G.add_node(m1)
    for v in range(m1):
        G.add_node(v, type='left_bell')
        G.add_edge(v, m1, type='left_bell')

    # right bell
    for v in range(m1):
        G.add_node(v + m1 + m2, type='right_bell')
        G.add_edge(v + m1 + m2, m1 + m2 - 1, type='right_bell')

    # connecting path
    for v in range(m1, m1 + m2 - 1):
        G.node[v]['type'] = 'core'
        G.add_edge(v, v + 1, type='core')
    G.node[m1 + m2 - 1]['type'] = 'core'

    return G
开发者ID:fnss,项目名称:fnss,代码行数:67,代码来源:simplemodels.py


注:本文中的fnss.topologies.topology.Topology.name方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。