File size: 9,986 Bytes
910db9a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | # -*- coding: utf-8 -*-
# @author: Debroux Léonard <leonard.debroux@gmail.com>
# @author: Kevin Jadin <contact@kjadin.com>
# references : visualisation tool : http://gephi.org/
# http://networkx.lanl.gov/examples/drawing/index.html
import os
import networkx as nx
import matplotlib.pyplot as plt
from multicasttree import MulticastTree
from improve_methods import ImproveMethods
import logging as log
import random
import ksp
import haversine
from utils import Utils
from setup import Setup
# for saving/loading the shortest paths to/from a file
import cPickle as pickle
class NetworkGraph(nx.Graph):
""" NetworkGraph class """
def __init__(self, _filename, _weight_attribute, _shortest_paths_filename):
super(NetworkGraph, self).__init__(nx.read_gml(_filename))
self.filename = _filename
self.add_weight_attribute(_weight_attribute)
if os.path.isfile(_shortest_paths_filename):
""" shortest paths file exists """
log.info("loading shortest paths structures from file: %s" % _shortest_paths_filename)
self.load_shortest_paths(_shortest_paths_filename)
else:
""" shortest paths structures must be recomputed """
log.info("recomputing shortest paths structures")
sP, sPL, sPC = ksp.get_shortest_paths(self, Setup.get('k_shortest_paths'))
self.ShortestPaths = sP
self.ShortestPathsLength = sPL
self.ShortestPathsCount = sPC
log.info("saving shortest paths structures to file: %s" % _shortest_paths_filename)
self.save_shortest_paths(_shortest_paths_filename)
log.debug('ShortestPaths : %s' % self.ShortestPaths)
log.debug('ShortestPathsLength: %s' % self.ShortestPathsLength)
log.debug('ShortestPathsCount : %s' % self.ShortestPathsCount)
self.layout = nx.spring_layout(self, k=0.3, iterations=50)
def save_shortest_paths(self, filename):
""" Exports the shortest paths by exporting the memory to a file """
# pack the 3 data structures
struct = [self.ShortestPaths, self.ShortestPathsLength, self.ShortestPathsCount]
with file(filename, 'wb') as outfile:
pickle.dump(struct, outfile)
def load_shortest_paths(self, filename):
""" Importing the shortest paths. The file give should be a dump of the memory """
with file(filename, 'rb') as infile:
struct = pickle.load(infile)
sP, sPL, sPC = struct # unpack the data structures
self.ShortestPaths = sP
self.ShortestPathsLength = sPL
self.ShortestPathsCount = sPC
def add_weight_attribute(self, type = None):
""" Adds the weighs to the edges of the network graph based on the weight attribute
Can be either GEO, WEIGHT, BANDWIDTH or NONE
In the case of GEO, the weight are extrapolated based on the position of the nodes
In the case of WEIGHT and BANDWIDTH, the edges in the gml should have the corresponding attribute
If set to NONE, all edges have a weight of 1
"""
NG = self
Nodes = NG.nodes(data=True)
if type == "GEO":
log.debug("using %s attribute" % type)
# when geographic location attribute is present
longitude_attribute_str = "Longitude"
latitude_attribute_str = "Latitude"
# default value when the computation of the distance fails
# a failure occurs when the coordinates are wrong or not present
default_distance = 60
log.info("setting %s as default distance" % default_distance)
def map_geo(e):
n1, n2 = e
N1 = NG.node[n1]
N2 = NG.node[n2]
log.debug("N1: %s" % N1)
log.debug("N2: %s" % N2)
try:
origin = (N1[latitude_attribute_str], N1[longitude_attribute_str])
destination = (N2[latitude_attribute_str], N2[longitude_attribute_str])
# take the distance in km
dist = haversine.distance(origin, destination)
except Exception, e:
log.warning("no location information available for one node")
log.debug(str(e))
dist = default_distance
if dist < 1.0:
# a distance of 1 is the minimum value, the weight of a link in a network must be strictly positive
dist = 1
log.debug("dist: %s km" % dist)
return int(dist)
weights = {e:map_geo(e) for e in NG.edges()}
elif type == "BANDWIDTH":
# when bandwidth attribute is present
# edge attribute
log.debug("using %s attribute" % type)
raise Exception("TODO: bandwidth derivation")
pass
elif type == "WEIGHT":
# edge attribute
log.debug("using %s attribute" % type)
attribute_str = "weight"
weights = {(n1, n2):d[attribute_str] for (n1, n2, d) in NG.edges(data=True)}
else: # "NONE"
log.debug("no attribute present")
# when no attribute is present
# overwrite weights attribute
log.debug("overwriting weight attribute")
overwrite_value = 1
log.debug("overwriting edge weights to %d" % overwrite_value)
weights = {e:overwrite_value for e in NG.edges()}
nx.set_edge_attributes(NG, 'weight', weights)
def draw(self):
nx.draw(self, self.layout)
plt.show()
# clean plot
plt.clf()
def export(self, outfile):
nx.draw_graphviz(self)
nx.write_dot(self, outfile)
def getEdgePathWeight(self, path):
""" Compute the weight of a path expressed as [edge1, edge2, ..., edgeN],
where edges are couples of nodes
"""
totWeight = 0
for e in path:
n1, n2 = e
totWeight += self[n1][n2]['weight']
return totWeight
def getNodePathWeight(self, path, fullWeight = False):
""" Compute the weight of a path expressed as a list of nodes: [n1, n2, ..., nN] """
totWeight = 0
for i in range(len(path)-1):
n1 = path[i]
n2 = path[i+1]
totWeight += self[n1][n2]['weight']
selectionHeuristic = Setup.get('selection_heuristic')
if (selectionHeuristic == Setup.AVERAGED_MOST_EXPENSIVE_PATH) and (not fullWeight):
totWeight = totWeight/len(path)-1
return totWeight
def buildMCTree(self, root, events):
""" Creates the multicast tree based on the given events.
Depending on the client_ordering heuristic that is used, the list of event may be changed
Regularily checks that the tree is a valid multicast tree for the current clients
"""
log.debug('building multicast tree')
log.debug('set of events: %s' % events)
# should start with empty tree
T = MulticastTree(self, root)
# Variables for the heuristics
eventsList = list(events)
closestClient = None
# nodes will be added in the order from by the following list,
# which is defined according to the chosen client ordering method
chosenOrdering = list()
client_ordering = Setup.get('client_ordering')
pim_mode = Setup.get('pim_mode')
if client_ordering == Setup.CLOSEST_TREE:
# Chooses the client that is the closest to the tree and adds it.
# Will be useful to tests random client arrival versus known client set
# ---------------------------------------------------------------------
clients = Utils.compute_final_clients_set(eventsList)
while clients:
log.debug("eventsList: %s" % clients)
cost = float("inf")
for c in clients:
for t in T.nodes():
cTemp = self.ShortestPathsLength[t][c][0]
if cTemp < cost:
cost = cTemp
closestClient = c
chosenOrdering.append(closestClient)
clients.remove(closestClient)
chosenOrderingTuple = []
for c in chosenOrdering:
chosenOrderingTuple.append(('a', c))
chosenOrdering = chosenOrderingTuple[:]
improvePeriod, improveTime = Setup.get('improve_period'), Setup.get('improve_maxtime')
chosenOrdering = Utils.addImproveSteps(chosenOrdering, improvePeriod, improveTime)
elif client_ordering == Setup.RANDOM:
# first shuffle the list
chosenOrdering = eventsList[:]
random.shuffle(chosenOrdering)
log.info("shuffled list: %s" % chosenOrdering)
else: # add clients in the given order
chosenOrdering = eventsList
for (action, arg) in chosenOrdering:
discardTime = False # flag for reseting the event processing time when a node was already in the tree or hasn't been removed.
Utils.STATISTICS.startEvent(arg, T.number_of_nodes(), T.edges(), T.weight, len(T.C))
# The addition and removal are treated first
if action == 'a':
log.debug('tree nodes before adding the client: "%s"' % T.nodes())
log.debug('tree edges before adding the client: "%s"' % T.edges(data=True))
if arg in T.nodes():
discardTime = True
T.addClient(arg)
T.validate()
elif action == 'r':
T.removeClient(arg)
if arg in T.nodes():
discardTime = True
T.validate()
elif action == 't':
pass # nothing to do upon a tick event
elif action == 'i':
pass # treated later
else:
raise Exception("unrecognised action")
Utils.STATISTICS.endEvent(action, arg, T.number_of_nodes(), T.edges(), T.weight, len(T.C), discardTime)
# Improvement events are treated
if action == 'i':
# if pim mode is enabled or no time is dedicated to improving, the event is ignored
if (not pim_mode) and arg > 0:
Utils.STATISTICS.startImprove(T.edges(), T.weight)
T = ImproveMethods.improveTree(T, arg)
T.validate()
newWeight = T.weight
Utils.STATISTICS.endImprove(T.edges(), T.weight)
else:
log.debug("action (%s, %s) discarded because of PIM mode" % (action, arg))
T.validate()
if pim_mode:
# the following line is a test, can be disabled
log.info("ensuring if the tree follows PIM mode..")
T.validatePIMTree()
return T
|