Skip to main content

Alternative A* implementation, which provides the current and previous edge to the weight function.

Project description

networkx-astar-path

PyPI GitHub Workflow Status (master) Coveralls github branch PyPI - Python Version PyPI - License

Alternative A* implementation, which provides the current and previous edge to the weight function.

Requirements

  • Python 3.6.1 or newer

Installation

pip install networkx-astar-path

Usage

networkx's implementation of A* limit the arguments of the weight function to the current edge onlye. Some scenarious require that the cost for the current edge is dependent on the prevous edge. An example is the bearing between two edges. Surely, the bearing could be pre-calculate and stored on the node, but this is sometimes not possible.

The API of this implementation is the same, with the one exception that the weight-funciton signature has changed to

def weight(graph: nx.Graph, prev_edge: Optional[Tuple[Any, Any]], cur_edge: Tuple[Any, Any]) -> float:
    ...

If the weight-function is called for the first time, the value for prev_edge is None as we haven't visited any other edge yet.

Example

NOTE This example is not very practical, but shows how this function can be used.

Given the following graph

Graph

import networks as nx

graph = nx.DiGraph()

graph.add_edges_from([('S', 'A1')], weight=-2)
graph.add_edges_from([('A1', 'T')], weight=7)
graph.add_edges_from([('S','A2'), ('A2','B2'),('B2','C2'),('C2','T')], weight=1)

we are searching for the shortest path from S to T.

The weights have been chosen in a way that the path ['S', 'A2', 'B2', 'C2', 'T'] is shorter when we simply sum up the weights, but longer if the weight of the current edge is divided by the weight of the previous edge. The shortest path for the latter is ['S', 'A1', 'T'].

Let's find the shortest path by only looking at the weights of the current edge.

from networkx_astar_path import astar_path

path = astar_path(graph, source='S', target='T', weight="weight")
# ['S', 'A2', 'B2', 'C2', 'T']

Shortest path based on the current edge

We now define a "simple" weight function, which takes the previous edge into account:

If we already visited an edge, the weight is the weight of the current edge divided by the weight of the previous edge. Otherwise, the weight of the current edge is returned.

from networkx_astar_path import astar_path

def dependent_weight(graph: nx.Graph, prev_edge: Optional[Tuple[Any, Any]], cur_edge: Tuple[Any, Any]) -> float:
    if prev_edge is None:
        return graph.get_edge_data(*cur_edge)['weight']

    prev_weight = graph.get_edge_data(*prev_edge)['weight']
    cur_weight = graph.get_edge_data(*cur_edge)['weight']
    return cur_weight / prev_weight

path = astar_path(graph, source='S', target='T', weight=dependent_weight)
# ['S', 'A1', 'T']

Shortest path based on the previous edge

Development

This project uses poetry for packaging and managing all dependencies and pre-commit to run flake8, isort, mypy and black.

Clone this repository and run

poetry install
poetry run pre-commit install

to create a virtual enviroment containing all dependencies. Afterwards, You can run the test suite using

poetry run pytest

This repository follows the Conventional Commits style.

Cookiecutter template

This project was created using cruft and the cookiecutter-pyproject template. In order to update this repository to the latest template version run

cruft update

in the root of this repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

networkx-astar-path-1.0.1.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

networkx_astar_path-1.0.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file networkx-astar-path-1.0.1.tar.gz.

File metadata

  • Download URL: networkx-astar-path-1.0.1.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for networkx-astar-path-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2da0d9828f6cd9ef1f19b3f0ba4967a795f45ffc4826ca9fea5ee6730c9462cf
MD5 b7518b0b5a16464c5580ccee2fb91f2b
BLAKE2b-256 8e1e3914abf7cb94c193ee96d336a07e9be578d9eb0e4b3e216385df5937f498

See more details on using hashes here.

File details

Details for the file networkx_astar_path-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: networkx_astar_path-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for networkx_astar_path-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5d3e7ac87fc979d5f77954beafedc167ea8bff64bf397e4bab0900f6190d806
MD5 8b895b7a3e123ad861bccb6a3b0bb831
BLAKE2b-256 9792964574755101f6ab215f54bb6983222ad8839bf544719088b94c2c753472

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page