Guest User

Untitled

a guest
Sep 18th, 2019
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import numpy as np
  2. import pandas as pd
  3.  
  4. class Edge:
  5. def __init__(self, targetNode, Cost):
  6. self.node = targetNode
  7. self.Cost = Cost
  8.  
  9. class Node:
  10. def __init__(self, value):
  11. self.value = value
  12. self.parent = 0
  13. self.isVisited = False
  14. self.gScore = 0
  15. self.edgeList = []
  16.  
  17. def Connect(self, targetNode, cost):
  18. newEdge = Edge(targetNode, cost)
  19. self.edgeList.append(newEdge)
  20.  
  21. class Graph:
  22. def __init__(self, nodes):
  23. self.nodeList = nodes
  24.  
  25. def ConnectNodes(self, startNode, endNode, cost):
  26. startNode.Connect(endNode, cost)
  27.  
  28. def DijkstraSearch(self, startNode, endNode):
  29. for i in nodeList:
  30. for j in i:
  31. i.parent = 0
  32. i.gScore = 999999
  33. i.isvisited = False
  34.  
  35.  
  36. def main():
  37. print("pathfinding in python")
  38. # list of items
  39. a = [['A','B','C','D','E'],['F','G','H','I','J'],['K','L','M','N','O']]
  40.  
  41. # empty list
  42. lNodes = []
  43.  
  44. # iterate through the number of items in 'a' list
  45. for i in range(len(a)):
  46. # iterates the number of items in each sublist
  47. for j in range(len(a[i])):
  48. # create a node for the position
  49. lNodes.append(Node([i,j]))
  50. # print the position of the new node
  51. print(i,j)
  52.  
  53. # create new graph with the nodes
  54. g = Graph(lNodes)
  55.  
  56. # iterates through the number of items in the nodeList
  57. for i in range(len(g.nodeList)):
  58. # iterates throught the number of items in the nodeList
  59. for j in range(len(g.nodeList)):
  60. # check its not the same nodes
  61. if g.nodeList[i] != g.nodeList[j]:
  62. # calc x dist
  63. distX = g.nodeList[j].value[0] - g.nodeList[i].value[0]
  64. calc y disst
  65. distY = g.nodeList[j].value[1] - g.nodeList[i].value[1]
  66. # x ^ 2 + y ^ 2
  67. distance = distX**2 + distY**2
  68. # if its the next node
  69. if distance <= 1:
  70. # connect the nodes
  71. g.ConnectNodes(g.nodeList[i], g.nodeList[j], distance)
  72.  
  73. # print edge list
  74. for i in range(len(g.nodeList)):
  75. for j in g.nodeList[i].edgeList:
  76. print(j.node.value)
  77. print("\n")
  78.  
  79. if __name__ == "__main__":
  80. main()
RAW Paste Data