Skip to content

Commit a76910a

Browse files
committed
minor indentation and linting, also update to 1.0.8 for upgrading release.
1 parent a091843 commit a76910a

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.0.8
2+
## Performance
3+
- huge performance update by reducing lookup time for smallest entry in heapq. (see #53, thanks to @peterchenadded)
4+
15
# 1.0.7
26
## Bugfixes
37
- fixed calculation of graph width (see #52)

pathfinding/core/heap.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
from .grid import Grid
55
from .world import World
66

7+
78
class SimpleHeap:
8-
"""Simple wrapper around open_list that keeps track of order and removed nodes automatically."""
9+
"""Simple wrapper around open_list that keeps track of order and removed
10+
nodes automatically."""
911

1012
def __init__(self, node, grid):
1113
self.grid = grid
1214
self.open_list = [self._get_node_tuple(node, 0)]
1315
self.removed_node_tuples = set()
1416
self.heap_order = {}
1517
self.number_pushed = 0
16-
18+
1719
def _get_node_tuple(self, node, heap_order):
1820
if isinstance(self.grid, Graph):
1921
return (node.f, heap_order, node.node_id)
@@ -23,7 +25,7 @@ def _get_node_tuple(self, node, heap_order):
2325
return (node.f, heap_order, node.x, node.y, node.grid_id)
2426
else:
2527
assert False, "unsupported heap node node=%s" % node
26-
28+
2729
def _get_node_id(self, node):
2830
if isinstance(self.grid, Graph):
2931
return node.node_id
@@ -32,13 +34,13 @@ def _get_node_id(self, node):
3234
elif isinstance(self.grid, World):
3335
return (node.x, node.y, node.grid_id)
3436

35-
3637
def pop_node(self):
3738
"""
3839
Pops node off the heap. i.e. returns the one with the lowest f.
3940
4041
Notes:
41-
1. Checks if that values is in removed_node_tuples first, if not tries again.
42+
1. Checks if that values is in removed_node_tuples first, if not tries
43+
again.
4244
2. We use this approach to avoid invalidating the heap structure.
4345
"""
4446
node_tuple = heapq.heappop(self.open_list)
@@ -50,10 +52,11 @@ def pop_node(self):
5052
elif isinstance(self.grid, Grid):
5153
node = self.grid.node(node_tuple[2], node_tuple[3])
5254
elif isinstance(self.grid, World):
53-
node = self.grid.grids[node_tuple[4]].node(node_tuple[2], node_tuple[3])
55+
node = self.grid.grids[
56+
node_tuple[4]].node(node_tuple[2], node_tuple[3])
5457

5558
return node
56-
59+
5760
def push_node(self, node):
5861
"""
5962
Push node into heap.
@@ -67,12 +70,13 @@ def push_node(self, node):
6770
self.heap_order[node_id] = self.number_pushed
6871

6972
heapq.heappush(self.open_list, node_tuple)
70-
73+
7174
def remove_node(self, node, f):
7275
"""
7376
Remove the node from the heap.
7477
75-
This just stores it in a set and we just ignore the node if it does get popped from the heap.
78+
This just stores it in a set and we just ignore the node if it does
79+
get popped from the heap.
7680
7781
:param node: The node to remove.
7882
:param f: The old f value of the node.
@@ -81,7 +85,7 @@ def remove_node(self, node, f):
8185
heap_order = self.heap_order[node_id]
8286
node_tuple = self._get_node_tuple(node, heap_order)
8387
self.removed_node_tuples.add(node_tuple)
84-
88+
8589
def __len__(self):
8690
"""Returns the length of the open_list."""
87-
return len(self.open_list)
91+
return len(self.open_list)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
long_description=long_description,
1212
long_description_content_type="text/markdown",
1313
url="https://github.com/brean/python-pathfinding",
14-
version="1.0.7",
14+
version="1.0.8",
1515
license="MIT",
1616
author="Andreas Bresser",
1717
packages=find_packages(),

test/test_heap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from pathfinding.core.heap import SimpleHeap
22
from pathfinding.core.grid import Grid
33

4+
45
def test_heap():
56
grid = Grid(width=10, height=10)
67
start = grid.node(0, 0)

test/test_performance.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def _add_block(g: numpy.ndarray, x: int, y: int, padding: int):
99
for j in range(y - padding, y + padding):
1010
g[j][i] = 0
1111

12+
1213
def test_a_star():
1314
"""Test performance."""
1415
# Get a 500 x 500 grid
@@ -22,8 +23,8 @@ def test_a_star():
2223
end = finder_grid.node(400, 400)
2324

2425
finder = AStarFinder(diagonal_movement=DiagonalMovement.never)
25-
path, runs = finder.find_path(start, end, finder_grid)
26+
path, _ = finder.find_path(start, end, finder_grid)
2627

2728
assert path[0] == start
2829
assert path[-1] == end
29-
assert len(path) == 801
30+
assert len(path) == 801

0 commit comments

Comments
 (0)