Advertisement
Guest User

Untitled

a guest
Sep 18th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import math
  4.  
  5. class Edge:
  6. def __init__(self, targetNode, cost):
  7. self.node = targetNode
  8. self.cost = cost
  9.  
  10. class Node:
  11. def __init__(self, value, position):
  12. self.value = value
  13. self.position = position
  14. self.parent = 0
  15. self.isVisited = False
  16. self.gScore = 0
  17. self.edgeList = []
  18.  
  19. def Connect(self, targetNode, cost):
  20. newEdge = Edge(targetNode, cost)
  21. self.edgeList.append(newEdge)
  22.  
  23. class Graph:
  24. def __init__(self, nodes):
  25. self.nodeList = nodes
  26.  
  27. def ConnectNodes(self, startNode, endNode, cost):
  28. startNode.Connect(endNode, cost)
  29.  
  30. def DijkstraSearch(self, startNode, endNode):
  31. for i in self.nodeList:
  32. i.parent = 0
  33. i.gScore = math.inf
  34. i.isvisited = False
  35.  
  36. startNode.gScore = 0
  37.  
  38. queueList = self.nodeList
  39.  
  40. while len(queueList) != 0:
  41. currentVertex = 0
  42. minimum = 9999999
  43. for i in queueList:
  44. if i.gScore < minimum:
  45. currentVertex = i
  46. minimum = i.gScore
  47. currentVertex.isVisited = True
  48.  
  49. if currentVertex == endNode:
  50. break
  51.  
  52. for e in currentVertex.edgeList:
  53. if e.node.isVisited == False:
  54. newCost = currentVertex.gScore + e.cost
  55. if newCost < e.node.gScore:
  56. e.node.gScore = newCost
  57. e.node.parent = currentVertex
  58. parent = currentVertex
  59. queueList.remove(currentVertex)
  60. pathList = []
  61.  
  62. currentPathNode = endNode
  63. while currentPathNode != 0:
  64. pathList.append(currentPathNode)
  65. currentPathNode = currentPathNode.parent
  66.  
  67. return pathList
  68.  
  69. def main():
  70. print("pathfinding in python")
  71. # list of items
  72. a = [['A','B','C','D','E'],['F','G','H','I','J'],['K','L','M','N','O']]
  73.  
  74. # empty list
  75. lNodes = []
  76.  
  77. # iterate through the number of items in 'a' list
  78. for i in range(len(a)):
  79. # iterates the number of items in each sublist
  80. for j in range(len(a[i])):
  81. # create a node for the position
  82. lNodes.append(Node(a[i][j], [i, j]))
  83. # print the position of the new node
  84. print(i,j)
  85.  
  86. # create new graph with the nodes
  87. g = Graph(lNodes)
  88.  
  89. # iterates through the number of items in the nodeList
  90. for i in range(len(g.nodeList)):
  91. # iterates throught the number of items in the nodeList
  92. for j in range(len(g.nodeList)):
  93. # check its not the same nodes
  94. if g.nodeList[i] != g.nodeList[j]:
  95. # calc x dist
  96. distX = g.nodeList[j].position[0] - g.nodeList[i].position[0]
  97. #calc y disst
  98. distY = g.nodeList[j].position[1] - g.nodeList[i].position[1]
  99. # x ^ 2 + y ^ 2
  100. distance = distX**2 + distY**2
  101. # if its the next node
  102. if distance <= 1:
  103. # connect the nodes
  104. g.ConnectNodes(g.nodeList[i], g.nodeList[j], distance)
  105.  
  106. # print edge list
  107. for i in range(len(g.nodeList)):
  108. for j in g.nodeList[i].edgeList:
  109. print(j.node.value)
  110. print("\n")
  111.  
  112. startNode = g.nodeList[0]
  113. endNode = g.nodeList[11]
  114.  
  115. path = g.DijkstraSearch(startNode, endNode)
  116.  
  117. for i in path:
  118. print(i.value)
  119.  
  120.  
  121. if __name__ == "__main__":
  122. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement