Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import pandas as pd
- class Edge:
- def __init__(self, targetNode, Cost):
- self.node = targetNode
- self.Cost = Cost
- class Node:
- def __init__(self, value):
- self.value = value
- self.parent = 0
- self.isVisited = False
- self.gScore = 0
- self.edgeList = []
- def Connect(self, targetNode, cost):
- newEdge = Edge(targetNode, cost)
- self.edgeList.append(newEdge)
- class Graph:
- def __init__(self, nodes):
- self.nodeList = nodes
- def ConnectNodes(self, startNode, endNode, cost):
- startNode.Connect(endNode, cost)
- def DijkstraSearch(self, startNode, endNode):
- for i in nodeList:
- for j in i:
- i.parent = 0
- i.gScore = 999999
- i.isvisited = False
- def main():
- print("pathfinding in python")
- # list of items
- a = [['A','B','C','D','E'],['F','G','H','I','J'],['K','L','M','N','O']]
- # empty list
- lNodes = []
- # iterate through the number of items in 'a' list
- for i in range(len(a)):
- # iterates the number of items in each sublist
- for j in range(len(a[i])):
- # create a node for the position
- lNodes.append(Node([i,j]))
- # print the position of the new node
- print(i,j)
- # create new graph with the nodes
- g = Graph(lNodes)
- # iterates through the number of items in the nodeList
- for i in range(len(g.nodeList)):
- # iterates throught the number of items in the nodeList
- for j in range(len(g.nodeList)):
- # check its not the same nodes
- if g.nodeList[i] != g.nodeList[j]:
- # calc x dist
- distX = g.nodeList[j].value[0] - g.nodeList[i].value[0]
- calc y disst
- distY = g.nodeList[j].value[1] - g.nodeList[i].value[1]
- # x ^ 2 + y ^ 2
- distance = distX**2 + distY**2
- # if its the next node
- if distance <= 1:
- # connect the nodes
- g.ConnectNodes(g.nodeList[i], g.nodeList[j], distance)
- # print edge list
- for i in range(len(g.nodeList)):
- for j in g.nodeList[i].edgeList:
- print(j.node.value)
- print("\n")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement