Skip to content

Commit

Permalink
Switch node walkable flag to use it as weight, so <0 is an obstacle a…
Browse files Browse the repository at this point in the history
…nd >=1 is a walkable field now
  • Loading branch information
brean committed Apr 13, 2018
1 parent 1008780 commit afdb9ca
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
16 changes: 9 additions & 7 deletions README.md
Expand Up @@ -15,15 +15,17 @@ A simple usage example to find a path using A*.
from pathfinding.finder.a_star import AStarFinder
```

1. Create a map using a 2D-list. 1, True or any value describes an obstacle. 0, False or None descibes a field that can be walked on. In this example we like the algorithm to create a path from the upper left to the bottom right. To make it not to easy for the algorithm we added an obstacle in the middle, so it can not use the direct way. Feel free to create a more complex map
1. Create a map using a 2D-list. Any value smaller or equal to 0 describes an obstacle. Any number bigger than 0 describes the weight of a field that can be walked on. The bigger the number the higher the cost to walk that field. In this example we like the algorithm to create a path from the upper left to the bottom right. To make it not to easy for the algorithm we added an obstacle in the middle, so it can not use the direct way. We ignore the weight for now, all fields have the same cost. Feel free to create a more complex map
```python
matrix = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
```

Note: you can use negative values to describe different types of obstacles. It does not make a difference for the path finding algorithm but it might be useful for your later map evaluation.

1. we create a new grid from this map representation. This will create Node instances for every element of our map. It will also set the size of the map. We assume that your map is a square, so the size height is defined by the length of the outer list and the width by the length of the first list inside it.

```python
Expand Down Expand Up @@ -67,9 +69,9 @@ from pathfinding.core.grid import Grid
from pathfinding.finder.a_star import AStarFinder

matrix = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]
grid = Grid(matrix=matrix)

Expand Down
15 changes: 11 additions & 4 deletions pathfinding.ipynb
Expand Up @@ -22,7 +22,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a map using a 2D-list. 1, True or any value describes an obstacle. 0, False or None descibes a field that can be walked on. In this example we like the algorithm to create a path from the upper left to the bottom right. To make it not to easy for the algorithm we added an obstacle in the middle, so it can not use the direct way. Feel free to create a more complex map"
"Create a map using a 2D-list. Any value smaller or equal to 0 describes an obstacle. Any number bigger than 0 describes the weight of a field that can be walked on. The bigger the number the higher the cost to walk that field. In this example we like the algorithm to create a path from the upper left to the bottom right. To make it not to easy for the algorithm we added an obstacle in the middle, so it can not use the direct way. We ignore the weight for now, all fields have the same cost. Feel free to create a more complex map"
]
},
{
Expand All @@ -32,12 +32,19 @@
"outputs": [],
"source": [
"matrix = [\n",
" [0, 0, 0],\n",
" [0, 1, 0],\n",
" [0, 0, 0]\n",
" [1, 1, 1],\n",
" [1, 0, 1],\n",
" [1, 1, 1]\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note: you can use negative values to describe different types of obstacles. It does not make a difference for the path finding algorithm but it might be useful for your later map evaluation."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
23 changes: 13 additions & 10 deletions pathfinding/core/grid.py
Expand Up @@ -8,29 +8,32 @@
from pathfinding.core.diagonal_movement import DiagonalMovement


def build_nodes(width, height, matrix=None):
def build_nodes(width, height, matrix=None, inverse=False):
"""
create nodes according to grid size. If a matrix is given it
will be used to determine what nodes are walkable
will be used to determine what nodes are walkable.
:rtype : list
"""
nodes = [None] * height
nodes = []
use_matrix = (isinstance(matrix, (tuple, list))) or \
(USE_NUMPY and isinstance(matrix, np.ndarray) and matrix.size > 0)

for y in range(height):
nodes[y] = [None] * width
nodes.append([])
for x in range(width):
# 0, '0', False, None will be walkable
# 1, '1', True will be walkable
# while others will be obstacles
walkable = not use_matrix or matrix[y][x] in [0, '0', False, None]
weight = int(matrix[y][x]) if use_matrix and walkable else 1
nodes[y][x] = Node(x=x, y=y, walkable=walkable, weight=weight)
# if inverse is False, otherwise
# it changes
weight = int(matrix[y][x]) if use_matrix else 1
walkable = weight <= 0 if inverse else weight >= 1

nodes[y].append(Node(x=x, y=y, walkable=walkable, weight=weight))
return nodes


class Grid(object):
def __init__(self, width=0, height=0, matrix=None):
def __init__(self, width=0, height=0, matrix=None, inverse=False):
"""
a grid represents the map (as 2d-list of nodes).
"""
Expand All @@ -42,7 +45,7 @@ def __init__(self, width=0, height=0, matrix=None):
self.height = len(matrix)
self.width = self.width = len(matrix[0]) if self.height > 0 else 0
if self.width > 0 and self.height > 0:
self.nodes = build_nodes(self.width, self.height, matrix)
self.nodes = build_nodes(self.width, self.height, matrix, inverse)
else:
self.nodes = [[]]

Expand Down
12 changes: 6 additions & 6 deletions test/csv_file.csv
@@ -1,6 +1,6 @@
0,0,0,0
0,1,1,0
0,0,1,1
1,0,0,0
1,0,1,0
0,0,0,0
1,1,1,1
1,0,0,1
1,1,0,0
0,1,1,1
0,1,0,1
1,1,1,1
6 changes: 3 additions & 3 deletions test/grid_test.py
Expand Up @@ -24,9 +24,9 @@
"""

SIMPLE_MATRIX = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
[1, 1, 1],
[1, 0, 1],
[1, 1, 1]
]

SIMPLE_WALKED = """
Expand Down
2 changes: 1 addition & 1 deletion test/path_test.py
Expand Up @@ -24,7 +24,7 @@


def grid_from_scenario(scenario):
grid = Grid(matrix=scenario['matrix'])
grid = Grid(matrix=scenario['matrix'], inverse=True)
start = grid.node(scenario['startX'], scenario['startY'])
end = grid.node(scenario['endX'], scenario['endY'])
return grid, start, end
Expand Down

0 comments on commit afdb9ca

Please sign in to comment.