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

atlanticwave-sdx / pce / 3680947894

pending completion
3680947894

Pull #81

github

GitHub
Merge a6cd61918 into b40be3bdd
Pull Request #81: Updates coveralls.io configuration

845 of 1796 relevant lines covered (47.05%)

0.47 hits per line

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

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

6
@author: Yufeng Xin (yxin@renci.org)
7
"""
8
import copy
1✔
9
import json
1✔
10
import operator
1✔
11
import random
1✔
12
import re
1✔
13
import time
1✔
14

15
import networkx as nx
1✔
16
import numpy as np
1✔
17
import pylab as plt
1✔
18
from networkx.algorithms import approximation as approx
1✔
19
from networkx.generators.random_graphs import erdos_renyi_graph
1✔
20

21
import Utility.global_name as global_name
1✔
22
from Utility.functions import GraphFunction
1✔
23

24

25
class RandomTopologyGenerator:
1✔
26
    # inputs:
27
    #   N: Total number of the random network's nodes
28
    #   P: link creation probability
29
    def __init__(
1✔
30
        self,
31
        N,
32
        P=0.2,
33
        l_bw=global_name.Min_L_BW,
34
        u_bw=global_name.Max_L_BW,
35
        l_lat=global_name.Min_L_LAT,
36
        u_lat=global_name.Max_L_LAT,
37
        seed=2022,
38
    ):
39
        random.seed(seed)
1✔
40
        self.seed = seed
1✔
41

42
        self.num_node = N
1✔
43
        self.link_probability = P
1✔
44

45
        self.low_bw = l_bw
1✔
46
        self.upper_bw = u_bw
1✔
47
        self.low_latency = l_lat
1✔
48
        self.upper_latency = u_lat
1✔
49

50
        self.graphFunction = GraphFunction()
1✔
51

52
    def bw_range(self, l_bw, u_bw):
1✔
53
        self.low_bw = l_bw
×
54
        self.upper_bw = u_bw
×
55

56
    def latency_range(self, l_lat, u_lat):
1✔
57
        self.low_latency = l_lat
×
58
        self.upper_latency = u_lat
×
59

60
    def set_graph(self, g):
1✔
61
        self.graph = g
×
62

63
    def get_graph(self):
1✔
64
        return self.graph
×
65

66
    def get_latency_list(self):
1✔
67
        return self.latency_list
×
68

69
    def get_distance_list(self):
1✔
70
        return self.distance_list
×
71

72
    def generate_graph(self, plot=True, g=None):
1✔
73
        # generate a random graph
74
        if g is None:
1✔
75
            while True:
76
                g = erdos_renyi_graph(self.num_node, self.link_probability, self.seed)
1✔
77
                if nx.is_connected(g):
1✔
78
                    connectivity = approx.node_connectivity(g)
1✔
79
                    if connectivity > 1:
1✔
80
                        print("Connectivity:" + str(connectivity))
1✔
81
                        print("Min edge cut:" + str(len(nx.minimum_edge_cut(g))))
1✔
82
                        break
1✔
83
                    else:
84
                        self.seed += 1
1✔
85
                else:
86
                    self.seed += 1
1✔
87

88
        self.graph = g
1✔
89

90
        if plot:
1✔
91
            nx.draw(g, with_labels=True)
1✔
92
            plt.savefig("rg.png")
1✔
93
            plt.clf()
1✔
94

95
        self.link_property_assign()
1✔
96

97
        self.graphFunction.set_graph(g)
1✔
98

99
        self.graphFunction.weight_assign()
1✔
100

101
        return self.graph
1✔
102

103
    # set the random bw and latency per link
104
    def link_property_assign(self):  ## pass in the bw name
1✔
105
        self.latency_list = []
1✔
106
        for (u, v, w) in self.graph.edges(data=True):
1✔
107
            w[global_name.bandwidth] = random.randint(self.low_bw, self.upper_bw)
1✔
108
            w[global_name.original_bandwidth] = w[global_name.bandwidth]
1✔
109
            latency = random.randint(self.low_latency, self.upper_latency)
1✔
110
            w[global_name.latency] = latency
1✔
111
            self.latency_list.append(latency)
1✔
112

113
        return self.latency_list
1✔
114

115
    # set weight (cost) per link, assuming the objective is minizing a function of weight
116
    #   flag:
117
    #       1: bw: weight = alpha*(1.0/bw)
118
    #       2: latency: weight = latency
119
    #       2: random: weight = random cost
120
    #       3: cost: given from outside (static) definition
121
    #       default: hop: weight =1
122
    def weight_assign(self, flag=5, cost=None):
1✔
123
        random.seed(self.seed)
×
124
        distance_list = []
×
125

126
        if flag == 1:
×
127
            for (u, v, w) in self.graph.edges(data=True):
×
128
                w[global_name.weight] = global_name.alpha * (
×
129
                    1.0 / w[global_name.bandwidth]
130
                )
131
                distance_list.append(w[global_name.weight])
×
132
        elif flag == 2:
×
133
            for (u, v, w) in self.graph.edges(data=True):
×
134
                w[global_name.weight] = w[global_name.latency]
×
135
                distance_list.append(w[global_name.weight])
×
136
        elif flag == 3:
×
137
            for (u, v, w) in self.graph.edges(data=True):
×
138
                w[global_name.weight] = random.randint(1, 2**24)
×
139
                distance_list.append(w[global_name.weight])
×
140
        elif flag == 4:
×
141
            for (u, v, w) in self.graph.edges(data=True):
×
142
                w[global_name.weight] = cost[u, v]
×
143
                distance_list.append(w[global_name.weight])
×
144
        else:
145
            for (u, v, w) in self.graph.edges(data=True):
×
146
                w[global_name.weight] = 1.0
×
147
                distance_list.append(w[global_name.weight])
×
148
        self.distance_list = distance_list
×
149
        return distance_list
×
150

151
    # if u and v connected
152
    def nodes_connected(g, u, v):
1✔
153
        return u in g.neighbors(v)
×
154

155
    def get_connectivity(self):
1✔
156
        con = approx.node_connectivity(self.graph)
×
157
        return con
×
158

159

160
def dot_file(topology_file, te_file=None):
1✔
161
    graph = nx.Graph(nx.nx_pydot.read_dot(topology_file))
1✔
162
    # graph = nx.Graph(nx.nx_agraph.read_dot(topology_file))
163
    num_nodes = graph.number_of_nodes()
1✔
164
    mapping = dict(zip(graph, range(num_nodes)))
1✔
165
    graph = nx.relabel_nodes(graph, mapping)
1✔
166

167
    for (u, v, w) in graph.edges(data=True):
1✔
168
        if not "capacity" in w.keys():
1✔
169
            bandwidth = 1000.0
×
170
        else:
171
            capacity = w["capacity"].strip('"')
1✔
172
            bw = re.split(r"(\D+)", capacity)
1✔
173
            bandwidth = bw[0]
1✔
174
            if bw[1].startswith("G"):
1✔
175
                bandwidth = float(bw[0]) * 1000
1✔
176

177
        w[global_name.original_bandwidth] = float(bandwidth)
1✔
178
        w[global_name.bandwidth] = float(bandwidth)
1✔
179
        w[global_name.weight] = float(w["cost"])
1✔
180
        if not "latency" in w.keys():
1✔
181
            latency = 10
1✔
182
            w[global_name.latency] = latency
1✔
183

184
    connectivity = approx.node_connectivity(graph)
1✔
185
    print("Connectivity:" + str(connectivity))
1✔
186

187
    with open(te_file) as f:
1✔
188
        tm = json.load(f)
1✔
189
    o_tm = []
1✔
190
    for t in tm:
1✔
191
        tr = tuple(t)
1✔
192
        o_tm.append(tr)
1✔
193

194
    return graph, o_tm
1✔
195
    # connection = GetConnection('../test/data/test_connection.json')
196
    # g = GetNetworkToplogy(25,0.4)
197
    # print(lbnxgraphgenerator(25, 0.4,connection,g))
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