Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. import numpy as np
  2. from plots import *
  3. from shortest_path import *
  4. import matplotlib.pyplot as mp
  5. import networkx as nx
  6. from manhattan_algorithm import Manhattan
  7. import random
  8.  
  9.  
  10. # Constants, the CAPACITY is not used in this lab
  11. N = 56
  12. d = 1
  13. CAPACITY = 1
  14. DELTA = 4
  15.  
  16. # Numpy optionsind[0]
  17. np.random.seed(42)
  18. np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
  19. # trafficMatrix = np.loadtxt('matrice di traffico',dtype='%.4f',delimiter=',')
  20. # print('importata', trafficMatrix)
  21.  
  22.  
  23. class Graph():
  24.  
  25. def __init__(self):
  26. # Create Adjacency matrix set to zero
  27. self.adjMatrix = self.createAdjMatrix()
  28. # Create and populate traffic matrix
  29. # self.traffMatrix = np.random.randint(10,size=(N,N))
  30. self.traffMatrix = self.createTraffMatrix(1)
  31. # self.traffMatrix = np.zeros((N, N))
  32. # for i in range(0, int(N / 2)):
  33. # for j in range(0, int(N / 2)):
  34. # self.traffMatrix[i, j] = 10 * random.random()
  35. # for i in range(int(N / 2), N):
  36. # for j in range(int(N / 2), N):
  37. # self.traffMatrix[i, j] = 10 * random.random()
  38. #
  39. # for i in range(0, int(N / 2)):
  40. # for j in range(int(N / 2), N):
  41. # self.traffMatrix[i, j] = random.random()
  42. # for i in range(int(N / 2), N):
  43. # for j in range(0, int(N / 2)):
  44. # self.traffMatrix[i, j] = random.random()
  45. self.delta = DELTA*np.ones(N)
  46. np.fill_diagonal(self.traffMatrix, 1)
  47. # Create list of nodes and dictionary of archs. Dictionaries are used to get direct access to a specific arch.
  48. # [e.g.] 'self.archs[(0,5)]' will return the arch object for the arch going from node 0 to node 5
  49. self.nodes = []
  50. self.archs = {}
  51. self.used_nodes = []
  52. self.manhattan = Manhattan(N, self.traffMatrix)
  53. self.list_node = []
  54. self.c = 0
  55.  
  56. def createTraffMatrix(self, mode):
  57. # np.random.seed(42)
  58. self.traffMatrix = np.zeros((N, N))
  59. if mode == 0:
  60. # np.random.seed(42)
  61. for i in range(N):
  62. for j in range(N):
  63. if i != j:
  64. p = np.random.uniform(0, 1)
  65. if p < 0.1:
  66. self.traffMatrix[i, j] = np.random.uniform(5, 15)
  67. else:
  68. self.traffMatrix[i, j] = np.random.uniform(0, 1)
  69. elif mode == 1:
  70. # np.random.seed(42)
  71. for i in range(0, int(N / 2)):
  72. for j in range(0, int(N / 2)):
  73. self.traffMatrix[i, j] = 10 * random.random()
  74. for i in range(int(N / 2), N):
  75. for j in range(int(N / 2), N):
  76. self.traffMatrix[i, j] = 10 * random.random()
  77.  
  78. for i in range(0, int(N / 2)):
  79. for j in range(int(N / 2), N):
  80. self.traffMatrix[i, j] = random.random()
  81. for i in range(int(N / 2), N):
  82. for j in range(0, int(N / 2)):
  83. self.traffMatrix[i, j] = random.random()
  84.  
  85. elif mode == 2:
  86. # np.random.seed(42)
  87. self.traffMatrix = np.random.rand(N, N)
  88. np.fill_diagonal(self.traffMatrix, 0)
  89. return self.traffMatrix
  90.  
  91.  
  92. def algorithm(self):
  93. self.nods=self.manhattan.calculate_dim()
  94. [row, col] = self.nods.shape
  95. for i in range(0,row):
  96. for j in range(0,col):
  97. self.nods[i,j] = self.c
  98. self.c +=1
  99. # print(self.c)
  100. # print(self.nods)
  101.  
  102.  
  103. for i in range(0,N):
  104. self.nodes.append(Node(i))
  105. for i in range(0,row):
  106. for j in range(0, col):
  107. if j < col-1:
  108. self.src = int(self.nods[i,j])
  109. self.dst = int(self.nods[i,j+1])
  110. # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[i,j+1])))
  111. else:
  112. self.src = int(self.nods[i, j])
  113. self.dst = int(self.nods[i, 0])
  114. # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[i,0])))
  115.  
  116. self.addArch(self.src,self.dst)
  117. self.addArch(self.dst,self.src)
  118.  
  119. for j in range(0,col):
  120. for i in range(0,row):
  121. if i < row-1:
  122. self.src = int(self.nods[i,j])
  123. self.dst = int(self.nods[i+1,j])
  124. # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[i+1,j])))
  125.  
  126.  
  127. else:
  128. self.src = int(self.nods[i, j])
  129. self.dst = int(self.nods[0, j])
  130. # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[0,j])))
  131.  
  132. self.addArch(self.src, self.dst)
  133. self.addArch(self.dst, self.src)
  134. # print(self.traffMatrix)
  135.  
  136.  
  137.  
  138. def addArch(self, src, dst):
  139. # Add an arch between nodes[src] and nodes[dst]
  140. a = Arch(src, dst)
  141. self.archs[(src, dst)] = a
  142. self.nodes[src].outArch.append(a)
  143. self.nodes[dst].inArch.append(a)
  144. self.nodes[src].nextHops.append(dst)
  145. self.adjMatrix[src, dst] = 1
  146. # print('arch added between: '+str(src)+' and '+str(dst))
  147. # self.delta[src] -= 1
  148. # self.delta[dst] -= 1
  149.  
  150.  
  151.  
  152. def createAdjMatrix(self):
  153. mat = np.zeros((N,N))
  154. return mat
  155.  
  156. def setWeights(self):
  157. # Set traffic values to each arch according to the topology and the traffic matrix
  158. for i in range(N):
  159. nodes = calcShortestPath(G, N, i)
  160. for j in range(N):
  161. src = -1
  162. dst = j
  163. while src != i or dst != None:
  164. node = nodes.get(dst)
  165. if node == None or node.prec == None:
  166. break
  167. prev = nodes.get(node.prec)
  168. src = prev.id
  169. if not dst == i:
  170. dst = node.id
  171. self.archs.get((src,dst)).traffic += self.traffMatrix[i,j]
  172. dst = src
  173.  
  174.  
  175. class Node():
  176.  
  177. def __init__(self, id):
  178. self.id = id
  179. self.nextHops = []
  180. self.inArch = []
  181. self.outArch = []
  182.  
  183.  
  184.  
  185. class Arch():
  186.  
  187. def __init__(self, source, dest, capacity=CAPACITY, traffic = 0):
  188. self.capacity = capacity
  189. self.source = source
  190. self.dest = dest
  191. self.traffic = traffic
  192.  
  193.  
  194. if __name__ == '__main__':
  195.  
  196. maf_flow = []
  197. # Initalise the graph parameters
  198. G = Graph()
  199. # Create nodes and arches in a ring topology
  200. G.algorithm()
  201. # Set traffic values to each arch according to the topology and the traffic matrix
  202. G.setWeights()
  203.  
  204. # Plot adjacency and traffic matrix, traffic for all archs and the arch with maximum traffic
  205. # plotAdjacencyMatrix(G)
  206. # plotTrafficMatrix(G)
  207. # plotWeights(G)
  208. # plotGraph(G)
  209. max = plotMaxFlow(G)
  210. print(max)
  211. print('\nDone')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement