C Board  

Go Back   C Board > Cprogramming.com and AIHorizon.com's Artificial Intelligence Boards > General AI Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-12-2007, 01:25 PM   #1
Registered User
 
Join Date: Apr 2004
Posts: 24
A* pathfinding search function, need help

Hi,

I have been having trouble with a search function. It seems to find the optimal path correctly most of the time, however it runs into trouble and crashes when it needs to filter back through already visited nodes on a path search (at least i "think" thats why its crashes).

For example it handles well in this situation: (o is the starting point, x is the target)

Code:
         x        
                   
   ------         
         ------   
   ------         
                   
         o
But not in this situation:

Code:
      x          
      
  --------
  |      |
  |  o   |
  |      |      


.
If anyone could point out in the code why its failing on this I would be very thankful.

Code:
void GraphSearch::Search()
{
	IndexedPriorityQLow<double> pq(mFCosts, mGraph.NumNodes());

	pq.insert(mSource);

	//Moves target to a valid cell if its invalid
	if(mTerrain[mTarget] == obstacle)
		moveTarget();

	while(!pq.empty())
	{
		int edgeCount = 0;
		int NextClosestNode = pq.Pop();
 
		while(mTerrain[NextClosestNode] == obstacle)
		{
			NextClosestNode = pq.Pop();
		}

		mShortestPathTree[NextClosestNode] = mSearchFrontier[NextClosestNode];

		if(NextClosestNode == mTarget)
			return;

		EdgeIterator EdgeItr(mGraph.mEdges, NextClosestNode);

		CellEdge* pE = EdgeItr.begin();

		while(edgeCount < MAX_EDGES)
		{     
			double HCost = mGraph.GetNode(mTarget).Pos().distance(mGraph.GetNode(pE->To()).Pos());
			double GCost = mGCosts[NextClosestNode] + pE->Cost();
			
			if(mSearchFrontier[pE->To()] == NULL)
			{
				mFCosts[pE->To()] = GCost + HCost;
				mGCosts[pE->To()] = GCost;
				
				pq.insert(pE->To());
				mSearchFrontier[pE->To()] = pE;
			}
			else if((GCost < mGCosts[pE->To()]) && (mShortestPathTree[pE->To()]==NULL))
			{
				mFCosts[pE->To()] = GCost + HCost;
				mGCosts[pE->To()] = GCost;

				pq.ChangePriority(pE->To());

				mSearchFrontier[pE->To()] = pE;
			}

			edgeCount++;
			if(edgeCount < MAX_EDGES) 
				pE = EdgeItr.next();
		}
	}
}
McFury is offline   Reply With Quote
Old 11-12-2007, 02:50 PM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Code:
		while(mTerrain[NextClosestNode] == obstacle)
		{
			NextClosestNode = pq.Pop();
		}
Shouldn't you check if pq.empty() becomes true in this loop?

Do you have checks to see if pq is popping/pushing illegally [overflow and underflow]? Always good practice to make such code "safe".

I have no idea if this is really where the problem lies - it just looks a bit "fishy" to me.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Attempt to write function search() elsewhere C Programming 6 12-03-2008 08:18 AM
doubt in c parser coding akshara.sinha C Programming 4 12-23-2007 01:49 PM
Including lib in a lib bibiteinfo C++ Programming 0 02-07-2006 02:28 PM
Game Pointer Trouble? Drahcir C Programming 8 02-04-2006 02:53 AM
c++ linking problem for x11 kron Linux Programming 1 11-19-2004 10:18 AM


All times are GMT -6. The time now is 02:20 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22