Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- # initializes 2d array with 1's in the correct spots
- def makeNetwork(nodes):
- network = []
- i = 0
- #sets up a loop to go through i, j indices
- while i < nodes:
- network.append([])
- j=0
- while j < nodes:
- # print(F'i = {i}, j = {j}')
- #below is the condition for if we need a 1 or a 0 in the spot
- if abs(i-j) == 1:
- network[i].append(1)
- else:
- network[i].append(0)
- j+=1
- i+=1
- #these set the periodic bonds, i.e. if 5 nodes, needs to include 5-1 1-5 bonding
- network[0][nodes-1] = 1
- network[nodes-1][0] = 1
- #returns the 2d array we've created
- return network
- #adds a noed, *does not correct array, always follow this by setNeighbors*
- def addNode(numberOfNodes, network):
- #unset the old periodic repititions
- network[0][len(network)-1] = 0
- network[len(network)-1][0] = 0
- i=0
- #adds columns to existing rows.
- while i < len(network):
- j=0
- while j < numberOfNodes:
- network[i].append(0)
- j+=1
- i+=1
- #adds additional rows filled with zero
- i=0
- while i < numberOfNodes:
- network.append([])
- i+=1
- i=1
- while i <= numberOfNodes:
- j=0
- while j < len(network):
- network[len(network)-i].append(0)
- j+=1
- i+=1
- return network
- # This sets the correct 1's and 0's for nearest neighbor and for periodic conditions
- # it is very important to do this only BEFORE non-periodic connections have been made, else it will remove them.
- def setNeighbors(network):
- nodes = len(network)
- i=0
- while i < nodes:
- j=0
- while j < nodes:
- # print(F'i = {i}, j = {j}')
- #below is the condition for if we need a 1 or a 0 in the spot
- if abs(i-j) == 1:
- network[i][j]=1
- else:
- network[i][j]=0
- j+=1
- i+=1
- network[0][nodes-1] = 1
- network[nodes-1][0] = 1
- return network
- # places 1's in the corresponding spots to indicate that there is a connection
- # between n1 and n2
- def addEdge(network, n1, n2):
- if n1 in getNodes(network) and n2 in getNodes(network):
- i1 = n1-1
- i2 = n2-1
- network[i1][i2]=1
- network[i2][i1]=1
- else:
- print('Nodes to not exist in network')
- # if we ever have to say 'remove node 3, and add node 7', or something, it would break everything
- # i.e. if there are 8 nodes, they can *only* be named 1, 2, ..., 8
- def getNodes(network):
- nodes = []
- for i in range(len(network)):
- nodes.append(i+1)
- return nodes
- def getNeighbors(network, node):
- jn = node-1
- i = 0
- neighbors = []
- while i < len(network):
- if network[i][jn] == 1:
- neighbors.append(i+1)
- i+=1
- return neighbors
- def getCoords(network, r):
- nodes = len(network)
- # Initialized with zero so that we can index as actual node number
- # might want to remove this later, if this is removed, we must remove [1:] from plotPoints
- xcoord = [0]
- ycoord = [0]
- # sets spacing in theta based on number of nodes
- div = (6.28)/nodes
- print(div)
- i=0
- while i < len(network):
- xcoord.append(r*np.cos(i*div))
- ycoord.append(r*np.sin(i*div))
- # leaving this in case things go wrong again so we can see which points are giving problems.
- # print('x ', r*np.cos(i*div), '\ny ', r*np.sin(i*div), '\n i')
- i+=1
- return xcoord, ycoord
- def plotPoints(network, r):
- #calls getCoords, takes the returned data, plots all points.
- datax, datay = getCoords(network, r)
- plt.plot(datax[1:], datay[1:], 'bo')
- def connectNeighbors(network):
- j=1
- xcoords, ycoords = getCoords(test, 10)
- while j <= len(network):
- neighbors = getNeighbors(test, j)
- for i, n in enumerate(neighbors):
- # indexing here is weird, x, ycoords have dummy value such that xcoords[1] gives true node
- # so neighbords[i] gives the number of the neighbor, so no -1 is needed
- x = [xcoords[j], xcoords[neighbors[i]]]
- y = [ycoords[j], ycoords[neighbors[i]]]
- #plt.plot(x, y, 'k-')
- j+=1
- #adds num random connections to the network.
- def randomConnections(network, num):
- i = 0
- while i < num:
- addEdge(test, np.random.randint(1,len(test)+1), np.random.randint(1, len(test)+1))
- i+=1
- def findPathLengthsFromNode(net, node):
- nodes = getNodes(net)
- allDist = {}
- for i in nodes:
- distance = 0
- checked = []
- fifo = [i]
- nextStep = []
- # While the queue is not empty.
- while fifo:
- # Sets the thing we are checking.
- check = fifo.pop(0)
- if check not in checked:
- checked.append(check)
- # print('check is', check)
- neighbors = getNeighbors(net, check)
- # Breaks without adding distance if we're at the node, should only happen once.
- if node == i:
- break
- # If node is in neighbors, then we can just increment distance and break.
- if node in neighbors:
- distance +=1
- break
- # adds neighbors to the new list that we'll need to dequeue
- for j in neighbors:
- nextStep.append(j)
- #if fifo is fully dequeued, then we have to increment distance and switch over to the new queue
- if len(fifo) == 0:
- for k in nextStep:
- fifo.append(nextStep.pop(0))
- distance +=1
- allDist.update({i:distance})
- return allDist
- def findAllPathLengths(net):
- #this will contain nothing but unrepeated pairwise distances
- distances = []
- #This dictionary will have node : dictionary of all nodes:distances from them.
- # i.e. {2: {1:2, 2:0, 3:1, ...}}
- # Access distances to say 4:2
- nodeLengthsDict={}
- for i in getNodes(net):
- nodeLengthsDict.update({i:findPathLengthsFromNode(net, i)})
- i = 1
- while i <= len(net):
- # this prevents repetitions, if we've done 11 12 13, ... we can safely start from 22 next time, 33, next etc.
- j=1
- while j < len(net):
- distances.append(nodeLengthsDict[i][j])
- j+=1
- i+=1
- return distances
- test = makeNetwork(333)
- #print(test)
- #test = addNode(7, test)
- #print(test, '\n')
- #test = setNeighbors(test)
- #print(test, '\n')
- #print(getCoords)
- #print(getNeighbors(test, 3))
- #plotPoints(test, 10)
- randomConnections(test, 66)
- connectNeighbors(test)
- distances = findAllPathLengths(test)
- plt.hist(distances)
- #add random edge with np.rand.randint(1, len(network)+1)
- #print(test)
- #print(getNodes(test))
- #print(getNeighbors(test, 3))
- # IMPORTANT: Node 1 starts at the right, theta = 0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement