Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from plots import *
- from shortest_path import *
- import matplotlib.pyplot as mp
- import networkx as nx
- from manhattan_algorithm import Manhattan
- import random
- # Constants, the CAPACITY is not used in this lab
- N = 56
- d = 1
- CAPACITY = 1
- DELTA = 4
- # Numpy optionsind[0]
- np.random.seed(42)
- np.set_printoptions(formatter={'float': lambda x: "{0:0.3f}".format(x)})
- # trafficMatrix = np.loadtxt('matrice di traffico',dtype='%.4f',delimiter=',')
- # print('importata', trafficMatrix)
- class Graph():
- def __init__(self):
- # Create Adjacency matrix set to zero
- self.adjMatrix = self.createAdjMatrix()
- # Create and populate traffic matrix
- # self.traffMatrix = np.random.randint(10,size=(N,N))
- self.traffMatrix = self.createTraffMatrix(1)
- # self.traffMatrix = np.zeros((N, N))
- # for i in range(0, int(N / 2)):
- # for j in range(0, int(N / 2)):
- # self.traffMatrix[i, j] = 10 * random.random()
- # for i in range(int(N / 2), N):
- # for j in range(int(N / 2), N):
- # self.traffMatrix[i, j] = 10 * random.random()
- #
- # for i in range(0, int(N / 2)):
- # for j in range(int(N / 2), N):
- # self.traffMatrix[i, j] = random.random()
- # for i in range(int(N / 2), N):
- # for j in range(0, int(N / 2)):
- # self.traffMatrix[i, j] = random.random()
- self.delta = DELTA*np.ones(N)
- np.fill_diagonal(self.traffMatrix, 1)
- # Create list of nodes and dictionary of archs. Dictionaries are used to get direct access to a specific arch.
- # [e.g.] 'self.archs[(0,5)]' will return the arch object for the arch going from node 0 to node 5
- self.nodes = []
- self.archs = {}
- self.used_nodes = []
- self.manhattan = Manhattan(N, self.traffMatrix)
- self.list_node = []
- self.c = 0
- def createTraffMatrix(self, mode):
- # np.random.seed(42)
- self.traffMatrix = np.zeros((N, N))
- if mode == 0:
- # np.random.seed(42)
- for i in range(N):
- for j in range(N):
- if i != j:
- p = np.random.uniform(0, 1)
- if p < 0.1:
- self.traffMatrix[i, j] = np.random.uniform(5, 15)
- else:
- self.traffMatrix[i, j] = np.random.uniform(0, 1)
- elif mode == 1:
- # np.random.seed(42)
- for i in range(0, int(N / 2)):
- for j in range(0, int(N / 2)):
- self.traffMatrix[i, j] = 10 * random.random()
- for i in range(int(N / 2), N):
- for j in range(int(N / 2), N):
- self.traffMatrix[i, j] = 10 * random.random()
- for i in range(0, int(N / 2)):
- for j in range(int(N / 2), N):
- self.traffMatrix[i, j] = random.random()
- for i in range(int(N / 2), N):
- for j in range(0, int(N / 2)):
- self.traffMatrix[i, j] = random.random()
- elif mode == 2:
- # np.random.seed(42)
- self.traffMatrix = np.random.rand(N, N)
- np.fill_diagonal(self.traffMatrix, 0)
- return self.traffMatrix
- def algorithm(self):
- self.nods=self.manhattan.calculate_dim()
- [row, col] = self.nods.shape
- for i in range(0,row):
- for j in range(0,col):
- self.nods[i,j] = self.c
- self.c +=1
- # print(self.c)
- # print(self.nods)
- for i in range(0,N):
- self.nodes.append(Node(i))
- for i in range(0,row):
- for j in range(0, col):
- if j < col-1:
- self.src = int(self.nods[i,j])
- self.dst = int(self.nods[i,j+1])
- # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[i,j+1])))
- else:
- self.src = int(self.nods[i, j])
- self.dst = int(self.nods[i, 0])
- # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[i,0])))
- self.addArch(self.src,self.dst)
- self.addArch(self.dst,self.src)
- for j in range(0,col):
- for i in range(0,row):
- if i < row-1:
- self.src = int(self.nods[i,j])
- self.dst = int(self.nods[i+1,j])
- # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[i+1,j])))
- else:
- self.src = int(self.nods[i, j])
- self.dst = int(self.nods[0, j])
- # print(str(int(self.nods[i,j]))+'<->'+str(int(self.nods[0,j])))
- self.addArch(self.src, self.dst)
- self.addArch(self.dst, self.src)
- # print(self.traffMatrix)
- def addArch(self, src, dst):
- # Add an arch between nodes[src] and nodes[dst]
- a = Arch(src, dst)
- self.archs[(src, dst)] = a
- self.nodes[src].outArch.append(a)
- self.nodes[dst].inArch.append(a)
- self.nodes[src].nextHops.append(dst)
- self.adjMatrix[src, dst] = 1
- # print('arch added between: '+str(src)+' and '+str(dst))
- # self.delta[src] -= 1
- # self.delta[dst] -= 1
- def createAdjMatrix(self):
- mat = np.zeros((N,N))
- return mat
- def setWeights(self):
- # Set traffic values to each arch according to the topology and the traffic matrix
- for i in range(N):
- nodes = calcShortestPath(G, N, i)
- for j in range(N):
- src = -1
- dst = j
- while src != i or dst != None:
- node = nodes.get(dst)
- if node == None or node.prec == None:
- break
- prev = nodes.get(node.prec)
- src = prev.id
- if not dst == i:
- dst = node.id
- self.archs.get((src,dst)).traffic += self.traffMatrix[i,j]
- dst = src
- class Node():
- def __init__(self, id):
- self.id = id
- self.nextHops = []
- self.inArch = []
- self.outArch = []
- class Arch():
- def __init__(self, source, dest, capacity=CAPACITY, traffic = 0):
- self.capacity = capacity
- self.source = source
- self.dest = dest
- self.traffic = traffic
- if __name__ == '__main__':
- maf_flow = []
- # Initalise the graph parameters
- G = Graph()
- # Create nodes and arches in a ring topology
- G.algorithm()
- # Set traffic values to each arch according to the topology and the traffic matrix
- G.setWeights()
- # Plot adjacency and traffic matrix, traffic for all archs and the arch with maximum traffic
- # plotAdjacencyMatrix(G)
- # plotTrafficMatrix(G)
- # plotWeights(G)
- # plotGraph(G)
- max = plotMaxFlow(G)
- print(max)
- print('\nDone')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement