Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.20 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # initializes 2d array with 1's in the correct spots
  5. def makeNetwork(nodes):
  6. network = []
  7. i = 0
  8. #sets up a loop to go through i, j indices
  9. while i < nodes:
  10. network.append([])
  11. j=0
  12. while j < nodes:
  13. # print(F'i = {i}, j = {j}')
  14. #below is the condition for if we need a 1 or a 0 in the spot
  15. if abs(i-j) == 1:
  16. network[i].append(1)
  17. else:
  18. network[i].append(0)
  19. j+=1
  20. i+=1
  21.  
  22. #these set the periodic bonds, i.e. if 5 nodes, needs to include 5-1 1-5 bonding
  23. network[0][nodes-1] = 1
  24. network[nodes-1][0] = 1
  25. #returns the 2d array we've created
  26. return network
  27.  
  28. #adds a noed, *does not correct array, always follow this by setNeighbors*
  29. def addNode(numberOfNodes, network):
  30. #unset the old periodic repititions
  31. network[0][len(network)-1] = 0
  32. network[len(network)-1][0] = 0
  33. i=0
  34.  
  35. #adds columns to existing rows.
  36. while i < len(network):
  37. j=0
  38. while j < numberOfNodes:
  39. network[i].append(0)
  40. j+=1
  41. i+=1
  42.  
  43. #adds additional rows filled with zero
  44. i=0
  45. while i < numberOfNodes:
  46. network.append([])
  47. i+=1
  48. i=1
  49. while i <= numberOfNodes:
  50. j=0
  51. while j < len(network):
  52. network[len(network)-i].append(0)
  53. j+=1
  54. i+=1
  55. return network
  56.  
  57. # This sets the correct 1's and 0's for nearest neighbor and for periodic conditions
  58. # it is very important to do this only BEFORE non-periodic connections have been made, else it will remove them.
  59. def setNeighbors(network):
  60. nodes = len(network)
  61. i=0
  62. while i < nodes:
  63. j=0
  64. while j < nodes:
  65. # print(F'i = {i}, j = {j}')
  66. #below is the condition for if we need a 1 or a 0 in the spot
  67. if abs(i-j) == 1:
  68. network[i][j]=1
  69. else:
  70. network[i][j]=0
  71. j+=1
  72. i+=1
  73. network[0][nodes-1] = 1
  74. network[nodes-1][0] = 1
  75. return network
  76.  
  77. # places 1's in the corresponding spots to indicate that there is a connection
  78. # between n1 and n2
  79. def addEdge(network, n1, n2):
  80. if n1 in getNodes(network) and n2 in getNodes(network):
  81. i1 = n1-1
  82. i2 = n2-1
  83.  
  84. network[i1][i2]=1
  85. network[i2][i1]=1
  86. else:
  87. print('Nodes to not exist in network')
  88.  
  89. # if we ever have to say 'remove node 3, and add node 7', or something, it would break everything
  90. # i.e. if there are 8 nodes, they can *only* be named 1, 2, ..., 8
  91. def getNodes(network):
  92. nodes = []
  93. for i in range(len(network)):
  94. nodes.append(i+1)
  95. return nodes
  96.  
  97. def getNeighbors(network, node):
  98. jn = node-1
  99. i = 0
  100. neighbors = []
  101. while i < len(network):
  102. if network[i][jn] == 1:
  103. neighbors.append(i+1)
  104. i+=1
  105. return neighbors
  106.  
  107.  
  108. def getCoords(network, r):
  109. nodes = len(network)
  110.  
  111. # Initialized with zero so that we can index as actual node number
  112. # might want to remove this later, if this is removed, we must remove [1:] from plotPoints
  113. xcoord = [0]
  114. ycoord = [0]
  115. # sets spacing in theta based on number of nodes
  116. div = (6.28)/nodes
  117. print(div)
  118.  
  119. i=0
  120.  
  121. while i < len(network):
  122. xcoord.append(r*np.cos(i*div))
  123. ycoord.append(r*np.sin(i*div))
  124.  
  125. # leaving this in case things go wrong again so we can see which points are giving problems.
  126. # print('x ', r*np.cos(i*div), '\ny ', r*np.sin(i*div), '\n i')
  127.  
  128. i+=1
  129. return xcoord, ycoord
  130.  
  131.  
  132. def plotPoints(network, r):
  133. #calls getCoords, takes the returned data, plots all points.
  134. datax, datay = getCoords(network, r)
  135. plt.plot(datax[1:], datay[1:], 'bo')
  136.  
  137.  
  138. def connectNeighbors(network):
  139. j=1
  140. xcoords, ycoords = getCoords(test, 10)
  141. while j <= len(network):
  142. neighbors = getNeighbors(test, j)
  143. for i, n in enumerate(neighbors):
  144. # indexing here is weird, x, ycoords have dummy value such that xcoords[1] gives true node
  145. # so neighbords[i] gives the number of the neighbor, so no -1 is needed
  146. x = [xcoords[j], xcoords[neighbors[i]]]
  147. y = [ycoords[j], ycoords[neighbors[i]]]
  148. #plt.plot(x, y, 'k-')
  149. j+=1
  150.  
  151. #adds num random connections to the network.
  152. def randomConnections(network, num):
  153. i = 0
  154. while i < num:
  155. addEdge(test, np.random.randint(1,len(test)+1), np.random.randint(1, len(test)+1))
  156. i+=1
  157.  
  158.  
  159. def findPathLengthsFromNode(net, node):
  160. nodes = getNodes(net)
  161. allDist = {}
  162. for i in nodes:
  163. distance = 0
  164. checked = []
  165. fifo = [i]
  166. nextStep = []
  167.  
  168. # While the queue is not empty.
  169. while fifo:
  170. # Sets the thing we are checking.
  171. check = fifo.pop(0)
  172. if check not in checked:
  173. checked.append(check)
  174. # print('check is', check)
  175. neighbors = getNeighbors(net, check)
  176.  
  177. # Breaks without adding distance if we're at the node, should only happen once.
  178. if node == i:
  179. break
  180. # If node is in neighbors, then we can just increment distance and break.
  181. if node in neighbors:
  182. distance +=1
  183. break
  184. # adds neighbors to the new list that we'll need to dequeue
  185. for j in neighbors:
  186. nextStep.append(j)
  187. #if fifo is fully dequeued, then we have to increment distance and switch over to the new queue
  188. if len(fifo) == 0:
  189. for k in nextStep:
  190. fifo.append(nextStep.pop(0))
  191. distance +=1
  192. allDist.update({i:distance})
  193. return allDist
  194.  
  195. def findAllPathLengths(net):
  196. #this will contain nothing but unrepeated pairwise distances
  197. distances = []
  198. #This dictionary will have node : dictionary of all nodes:distances from them.
  199. # i.e. {2: {1:2, 2:0, 3:1, ...}}
  200. # Access distances to say 4:2
  201. nodeLengthsDict={}
  202. for i in getNodes(net):
  203. nodeLengthsDict.update({i:findPathLengthsFromNode(net, i)})
  204. i = 1
  205. while i <= len(net):
  206. # this prevents repetitions, if we've done 11 12 13, ... we can safely start from 22 next time, 33, next etc.
  207. j=1
  208. while j < len(net):
  209. distances.append(nodeLengthsDict[i][j])
  210. j+=1
  211. i+=1
  212. return distances
  213.  
  214.  
  215.  
  216. test = makeNetwork(333)
  217. #print(test)
  218. #test = addNode(7, test)
  219. #print(test, '\n')
  220. #test = setNeighbors(test)
  221. #print(test, '\n')
  222. #print(getCoords)
  223. #print(getNeighbors(test, 3))
  224. #plotPoints(test, 10)
  225. randomConnections(test, 66)
  226. connectNeighbors(test)
  227.  
  228.  
  229. distances = findAllPathLengths(test)
  230. plt.hist(distances)
  231.  
  232.  
  233.  
  234. #add random edge with np.rand.randint(1, len(network)+1)
  235.  
  236. #print(test)
  237. #print(getNodes(test))
  238. #print(getNeighbors(test, 3))
  239.  
  240. # IMPORTANT: Node 1 starts at the right, theta = 0.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement