Skip to content

Commit 177e5ed

Browse files
committed
Changed priority to cost
1 parent 464237e commit 177e5ed

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def a_star(graph, start, end):
4848
4949
"""
5050

51-
frontier = DijkstraHeap( Node(priority = 0, point = start, came_from = None) )
51+
frontier = DijkstraHeap( Node(cost = 0, point = start, came_from = None) )
5252

5353
while frontier:
5454

@@ -59,11 +59,11 @@ def a_star(graph, start, end):
5959

6060
for neighbor in graph.neighbors( current_node.point ):
6161

62-
new_cost = ( current_node.priority
62+
new_cost = ( current_node.cost
6363
+ graph.cost(current_node.point, neighbor)
6464
+ heuristic( neighbor, end) )
6565

66-
new_node = Node(priority = new_cost, point = neighbor, came_from=current_node.point)
66+
new_node = Node(cost = new_cost, point = neighbor, came_from=current_node.point)
6767

6868
frontier.insert(new_node)
6969

@@ -75,7 +75,7 @@ Lets go line by line:
7575
frontier = DijkstraHeap( Node(0, start, None) )
7676
```
7777

78-
This line creates a DijkstraHeap object and puts the starting point in it. We will see later how this can be implemented but the best part is that....This is not part of the algorithm! What is a DijkstraHeap then? This is a **priority queue** that has the following properties:
78+
This line creates a DijkstraHeap object and puts the starting point in it. We will see later how this can be implemented but the best part is that....This is not part of the algorithm! What is a DijkstraHeap then? This is a **cost queue** that has the following properties:
7979

8080
* If we try to insert an already visited element in the queue the DijkstraHeap will do nothing.
8181
* The DijkstraHeap always pop the element that has the lowest cost and NEVER pops an already visited element.
@@ -110,11 +110,11 @@ for neighbor in graph.neighbors( current_node.point ):
110110
We get each of the current point neighbors
111111

112112
```python
113-
new_cost = ( current_node.priority
113+
new_cost = ( current_node.cost
114114
+ graph.cost(current_node.point, neighbor)
115115
+ heuristic( neighbor, end) )
116116

117-
new_node = Node(priority = new_cost, point = neighbor, came_from=current_node.point)
117+
new_node = Node(cost = new_cost, point = neighbor, came_from=current_node.point)
118118

119119
frontier.insert(new_node)
120120

@@ -128,7 +128,7 @@ For each neighbor we calculate the new cost of reaching this neighbor from the c
128128

129129
Why this 3rd cost? Because we want to explore first the points that are near the end destination and expend less time in the points that are far from it. So if we artificially give the point a higher cost if the point is far from the destination it will be visited later.
130130

131-
When we have calculated this new cost we insert the point in the priority queue.
131+
When we have calculated this new cost we insert the point in the cost queue.
132132

133133
## But what about the MISTERIOUS DijkstraHeap?
134134

@@ -144,7 +144,7 @@ class DijkstraHeap(list):
144144
145145
This class will have three main elements:
146146
147-
- A heap that will act as a priority queue (self).
147+
- A heap that will act as a cost queue (self).
148148
- A visited dict that will act as a visited set and as a mapping of the form point:came_from
149149
- A costs dict that will act as a mapping of the form point:cost_so_far
150150
"""
@@ -178,6 +178,6 @@ class DijkstraHeap(list):
178178

179179
next_elem = heapq.heappop(self)
180180
self.visited[next_elem.point] = next_elem.came_from
181-
self.costs[next_elem.point] = next_elem.priority
181+
self.costs[next_elem.point] = next_elem.cost
182182
return next_elem
183183
```

__pycache__/a_star.cpython-35.pyc

-18 Bytes
Binary file not shown.

a_star.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class DijkstraHeap(list):
2424
2525
This class will have three main elements:
2626
27-
- A heap that will act as a priority queue (self).
27+
- A heap that will act as a cost queue (self).
2828
- A visited dict that will act as a visited set and as a mapping of the form point:came_from
2929
- A costs dict that will act as a mapping of the form point:cost_so_far
3030
"""
@@ -58,13 +58,13 @@ def pop(self):
5858

5959
next_elem = heapq.heappop(self)
6060
self.visited[next_elem.point] = next_elem.came_from
61-
self.costs[next_elem.point] = next_elem.priority
61+
self.costs[next_elem.point] = next_elem.cost
6262
return next_elem
6363

6464

6565

6666

67-
Node = collections.namedtuple("Node","priority point came_from")
67+
Node = collections.namedtuple("Node","cost point came_from")
6868

6969
def a_star(graph, start, end):
7070
"""
@@ -92,7 +92,7 @@ def a_star(graph, start, end):
9292

9393
for neighbor in graph.neighbors( current_node.point ):
9494

95-
new_cost = ( current_node.priority
95+
new_cost = ( current_node.cost
9696
+ graph.cost(current_node.point, neighbor)
9797
+ heuristic( neighbor, end) )
9898

0 commit comments

Comments
 (0)