• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atlanticwave-sdx / pce / 7880614954

13 Feb 2024 01:51AM UTC coverage: 80.0%. Remained the same
7880614954

push

github

web-flow
Merge pull request #174 from atlanticwave-sdx/add-iso3166_2_lvl4

Add iso3166_2_lvl4

308 of 405 branches covered (76.05%)

Branch coverage included in aggregate %.

896 of 1100 relevant lines covered (81.45%)

3.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.42
/src/sdx_pce/utils/random_topology_generator.py
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4✔
4
Created on Tue Mar  8 13:34:06 2022
5

6
@author: Yufeng Xin (yxin@renci.org)
7
"""
8
import random
4✔
9

10
import networkx as nx
4✔
11
import pylab as plt
4✔
12
from networkx.algorithms import approximation as approx
4✔
13
from networkx.generators.random_graphs import erdos_renyi_graph
4✔
14

15
from sdx_pce.utils.constants import Constants
4✔
16
from sdx_pce.utils.functions import GraphFunction
4✔
17

18

19
class RandomTopologyGenerator:
4✔
20
    def __init__(
4✔
21
        self,
22
        num_node,
23
        link_probability=0.2,
24
        l_bw=Constants.MIN_L_BW,
25
        u_bw=Constants.MAX_L_BW,
26
        l_lat=Constants.MIN_L_LAT,
27
        u_lat=Constants.MAX_L_LAT,
28
        seed=2022,
29
    ):
30
        """
31
        :param num_nodes: Total number of the random network's nodes
32
        :param link_probability: Link creation probability.
33
        """
34
        random.seed(seed)
4✔
35
        self.seed = seed
4✔
36

37
        self.num_node = num_node
4✔
38
        self.link_probability = link_probability
4✔
39

40
        self.low_bw = l_bw
4✔
41
        self.upper_bw = u_bw
4✔
42
        self.low_latency = l_lat
4✔
43
        self.upper_latency = u_lat
4✔
44

45
        self.graphFunction = GraphFunction()
4✔
46

47
    def bw_range(self, l_bw, u_bw):
4✔
48
        self.low_bw = l_bw
×
49
        self.upper_bw = u_bw
×
50

51
    def latency_range(self, l_lat, u_lat):
4✔
52
        self.low_latency = l_lat
×
53
        self.upper_latency = u_lat
×
54

55
    def set_graph(self, g):
4✔
56
        self.graph = g
×
57

58
    def get_graph(self):
4✔
59
        return self.graph
×
60

61
    def get_latency_list(self):
4✔
62
        return self.latency_list
×
63

64
    def generate_graph(self, plot=True, g=None) -> nx.Graph:
4✔
65
        """generate a random graph"""
66
        if g is None:
4✔
67
            while True:
2✔
68
                g = erdos_renyi_graph(self.num_node, self.link_probability, self.seed)
4✔
69
                if nx.is_connected(g):
4✔
70
                    connectivity = approx.node_connectivity(g)
4✔
71
                    if connectivity > 1:
4✔
72
                        print("Connectivity:" + str(connectivity))
4✔
73
                        print("Min edge cut:" + str(len(nx.minimum_edge_cut(g))))
4✔
74
                        break
4✔
75
                    else:
76
                        self.seed += 1
4✔
77
                else:
78
                    self.seed += 1
4✔
79

80
        self.graph = g
4!
81

82
        if plot:
4✔
83
            nx.draw(g, with_labels=True)
4✔
84
            plt.savefig("rg.png")
4✔
85
            plt.clf()
4✔
86

87
        self.link_property_assign()
4!
88

89
        self.graphFunction.set_graph(g)
4✔
90

91
        self.graphFunction.weight_assign()
4✔
92

93
        return self.graph
4✔
94

95
    def link_property_assign(self):
4✔
96
        """set the random bw and latency per link"""
97

98
        self.latency_list = []
4✔
99
        for u, v, w in self.graph.edges(data=True):
4✔
100
            w[Constants.BANDWIDTH] = random.randint(self.low_bw, self.upper_bw)
4✔
101
            w[Constants.ORIGINAL_BANDWIDTH] = w[Constants.BANDWIDTH]
4✔
102
            latency = random.randint(self.low_latency, self.upper_latency)
4✔
103
            w[Constants.LATENCY] = latency
4✔
104
            self.latency_list.append(latency)
4✔
105

106
        return self.latency_list
4✔
107

108
    def nodes_connected(self, g, u, v):
4✔
109
        """if u and v connected"""
110
        return u in g.neighbors(v)
×
111

112
    def get_connectivity(self):
4✔
113
        con = approx.node_connectivity(self.graph)
×
114
        return con
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc