I work on some program to move a robot through a random maze. To count path from place A to place B I use A* (Astart) algorithm. But, sometimes (in rare cases) A* is unnable to find path (but path do exists -- just A* can not find it). Is it normal that A* in some cases is unnable to find path or there is probably some error in my implementation of A*?
If one or more paths exist, A* should find the shortest one. I suspect a bug in your implementation, in how the connectivity graph was defined, or in a related detail.
You could compare your code to the wikipedia entry
In my algorithm heuristic is set to 0 -- each edge, cell or how you call it is equal.
It is my java implementation of A*. At the moment, unfortunately I can not see any error... but it must be somewhere there because in rare cases this algorithm falls in the sense that it can not find path, even if some exists.
The representation of maze is just 2D array. Even cells of array is for cells of maze. Odd cells of array is for walls or openings between cells of maze.
private static boolean computeShortestPath(int destX, int destY, boolean displayRoute) {
//ratX, ratY is beginning position of the robot //destX, destY is desired end position of the robot
int pointsTmp = points;
// copy maze int mazePathNumbers[][] = new int[N][N]; // LenMapa for (int x = 0; x < N; x++) for (int y = 0; y < N; y++) mazePathNumbers[x][y] = 0xffff;
class Pair { int x, y; Pair(int x, int y){this.x=x;this.y=y;}};
Pair positionOfNodes[] = new Pair[500]; // WezlyPoz positionOfNodes[0] = new Pair(ratX, ratY);
In this example, robot (^) wants to go to place marked by "@". (please copy maze shown below to notepad to have equal wide fonts) . - is cell , - is opening between cells # - wall x - destination position ^ - actual position of robot
PolyTech Forum website is not affiliated with any of the manufacturers or service providers discussed here.
All logos and trade names are the property of their respective owners.