Repository navigation
Expand file tree
/
Copy pathNode.py
More file actions
77 lines (51 loc) · 1.94 KB
/
Copy pathNode.py
File metadata and controls
77 lines (51 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class Node:
def __init__(self,coordinate):
self.coordinate = coordinate
self.parent = None
self.gcost = -1
self.hcost = -1
self.fcost = -1
def return_coordinate(self):
return self.coordinate
def return_parent(self):
return self.parent
def return_gcost(self):
return self.gcost
def return_hcost(self):
return self.hcost
def return_fcost(self):
return self.fcost
def calculate_gcost(self,parentNode):
x_self = self.coordinate[0]
y_self = self.coordinate[1]
parentCoordinate = parentNode.return_coordinate()
x_parent = parentCoordinate[0]
y_parent = parentCoordinate[1]
return self.gcost + (x_self-x_parent)**2 + (y_self-y_parent)**2
def calculate_hcost(self,targetNode):
x_self = self.coordinate[0]
y_self = self.coordinate[1]
target = targetNode.return_coordinate()
x_target = target[0]
y_target = target[1]
return (x_self-x_target)**2 + (y_self-y_target)**2
def calculate_fcost(self,parentNode,target):
gcost = self.calculate_gcost(parentNode)
fcost = self.calculate_hcost(target)
return gcost + fcost
def update_gcost(self,parentNode):
self.gcost = self.calculate_gcost(parentNode)
def update_hcost(self,target):
self.hcost = self.calculate_hcost(target)
def update_fcost(self,parentNode,target):
self.update_gcost(parentNode)
self.update_hcost(target)
self.fcost = self.calculate_fcost(parentNode,target)
def set_parent(self,parent):
self.parent = parent
def set_gcost(self, gcost):
self.gcost = gcost
def set_hcost(self, hcost):
self.hcost = hcost
def set_fcost(self, fcost):
self.fcost = fcost