简体   繁体   中英

Path finding data structure for A* in python

I am using A* for pathfinding, and am creating a very limited graph to make traversal and pathfinding quick and easy.

How do I represent this as a data structure easily traversable from P0 to P1 by A* in python?

在此处输入图片说明

Everything is relative to points P0 and P1 which can be mirrored or 90 degree rotation to each other.

The points are at real coordinates and the other nodes are equal spaced and relative to our two points. The nodes have to remember their true coordinates so that once a path is chosen it can easily be drawn.

The weights on the edges are preweighted to give the resulting path an aesthetic look I prefer. The weights don't change so these can be pregenerated. However, there may be objects within the space that dynamically affect the weight of some edges (thus the need for pathfinding).

Pathfinding will happen every frame, so every 1/25th sec. So points for efficiency.

Here's the algorithm I'm trying to put together:

  1. Find the short and long side of the rectangle between the cells, work out the alignment.
  2. Calculate the coordinates of each node and edge.
  3. Apply pre-weight to the edges.
  4. Add additional weight for any nodes or edges that are blocked.
  5. Use A* algorithm to find the least-cost path which favors routes that minimize the distance.

And to remind you, a typical A* implementation has three user-serviceable parts I need to provide:

def move_cost(self, c1, c2, pred=None, start=None, goal=None):
    """ Compute the cost of movement from one coordinate to another."""

def estimate(self, c1, c2):
    """ Compute the cost of movement from one coordinate to
        another. Often euclidian distance."""

def successors(self, c):
    """ Compute the successors of coordinate 'c': all the
        coordinates that can be reached by one step from 'c'."""

How do I generate and traverse this data structure?

This is a large and complicated question and would benefit from being broken into smaller questions. Here are some thoughts to help you differentiate some sub problems that you've posed:

  • The traversal of the graph structure does not depend on geometry, only on the cost assigned to each edge. This cost can include distance if you'd like.
  • Since the path finding algorithm is geometry-agnostic (but may include distance in its cost function) the geometric layout of your nodes is a separate problem. You can think of the node coordinates as arbitrary attributes assigned to each node that may be used to calculate the cost (if you want cost to be distance-dependent).
  • The data structure for traversals would be a graph object with node and edge classes, and would allow you to assign and edit the weights assigned to each edge, or to add and remove edges whenever you want to create 'blocks'.
  • If you intended to vary the source and destination nodes used to calculate the paths, you should use a bidirectional graph.
  • You said "there may be objects within the space that dynamically affect the weight of some edges". The location of these objects and the functions do determine when and how much they affect the weights would be yet another separate task.

Here's the main things you need:

  1. The undirected graph structure (includes nodes, edges, attributes and the ability to access a cost function in order to solve for the least cost path)
  2. The cost function . This should take an edge as an argument, and if necessary, should read attributes from the nodes associated with that edge to calculate the cost.
  3. The interfering objects, I will call them ' attractors '.
  4. An 'effect' function that determines how the attractors edit the graph structure or edge weights. This might include a distance search that determines the nodes or edges that fall within a particular range of the attractor.
  5. Generating the initial coordinates and weights .

The undirected graph structure is very easy to implement using NetworkX . NetworkX allows you to attach arbitrary attributes to nodes and edges, is well-documented with many examples, and includes multiple least-cost pathfinding algorithms . NetworkX also allows you to designate a custom cost function for determining edge traversal costs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM