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:
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:
Here's the main things you need:
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.