AStarSearch< UserState > Class Template Reference

#include <AStarSearch.h>

Collaboration diagram for AStarSearch< UserState >:

Collaboration graph
[legend]
List of all members.

Public Types

 SS_NOT_STARTED
 SS_RUNNING
 SS_PATH_NOT_FOUND
 SS_PATH_FOUND
enum  SearchState { SS_NOT_STARTED, SS_RUNNING, SS_PATH_NOT_FOUND, SS_PATH_FOUND }

Public Member Functions

 AStarSearch (void)
 ~AStarSearch (void)
void _releaseSolution ()
void setStartState (UserState *startState, UserState *endState)
SearchState getSearchState ()
bool isRunnable ()
 Returns true if the search can be advanced at least one more step.
bool isSolved ()
 Returns true iff the search produced a valid result.
bool isUnsolvable ()
 Returns true iff the search cannot find a valid result.
bool advanceSearch ()
 Advances to the next best node.
const WaypointList< UserState > * getSolutionPath ()

Private Member Functions

AStarNode_findBestOpenNode ()
 Retrieves the node on the Open List with the smallest total cost to reach the goal node.
AStarNode_getFromClosed (AStarNode *node)
AStarNode_getFromOpen (AStarNode *node)
bool _isNodeClosed (AStarNode *node)
bool _isNodeOpen (AStarNode *node)
void _removeFromClosed (AStarNode *node)
void _removeFromOpen (AStarNode *node)
void _addToOpen (AStarNode *node)
void _addToClosed (AStarNode *node)
void _constructionWaypointList (AStarNode *endNode)
 Builds a WaypointList from UserStates.
void _releaseNodes ()

Private Attributes

SearchState mCurState
AStarNodemStartNode
UserState * mEndState
AStarNodemGoalNode
vector< AStarNode * > mOpenList
vector< AStarNode * > mClosedList
WaypointList< UserState > * mSolutionList

Classes

class  AStarNode
class  HeapCompare

Detailed Description

template<class UserState>
class AStarSearch< UserState >

Definition at line 51 of file AStarSearch.h.


Member Enumeration Documentation

template<class UserState>
enum AStarSearch::SearchState

Enumerator:
SS_NOT_STARTED 
SS_RUNNING 
SS_PATH_NOT_FOUND 
SS_PATH_FOUND 

Definition at line 192 of file AStarSearch.h.

00193         {
00194                 SS_NOT_STARTED,
00195                 SS_RUNNING,
00196                 SS_PATH_NOT_FOUND,
00197                 SS_PATH_FOUND
00198         };


Constructor & Destructor Documentation

template<class UserState>
AStarSearch< UserState >::AStarSearch ( void   )  [inline]

Definition at line 233 of file AStarSearch.h.

References AStarSearch< UserState >::mClosedList, AStarSearch< UserState >::mCurState, AStarSearch< UserState >::mGoalNode, AStarSearch< UserState >::mOpenList, AStarSearch< UserState >::mSolutionList, AStarSearch< UserState >::mStartNode, and AStarSearch< UserState >::SS_NOT_STARTED.

00234         {
00235                 mCurState = SS_NOT_STARTED;
00236 
00237                 mGoalNode = NULL;
00238                 mStartNode = NULL;
00239 
00240                 mSolutionList = NULL;
00241 
00242                 mOpenList.clear ();
00243                 mClosedList.clear ();
00244         }

template<class UserState>
AStarSearch< UserState >::~AStarSearch ( void   )  [inline]

Definition at line 247 of file AStarSearch.h.

References AStarSearch< UserState >::_releaseNodes(), and AStarSearch< UserState >::_releaseSolution().

00248         {
00249                 _releaseNodes ();
00250                 _releaseSolution ();
00251         }


Member Function Documentation

template<class UserState>
void AStarSearch< UserState >::_addToClosed ( AStarNode node  )  [inline, private]

Definition at line 599 of file AStarSearch.h.

References AStarSearch< UserState >::mClosedList.

Referenced by AStarSearch< UserState >::advanceSearch().

00600         {
00601                 mClosedList.push_back ( node );
00602         }

template<class UserState>
void AStarSearch< UserState >::_addToOpen ( AStarNode node  )  [inline, private]

Definition at line 590 of file AStarSearch.h.

References AStarSearch< UserState >::mOpenList.

Referenced by AStarSearch< UserState >::advanceSearch().

00591         {
00592                 mOpenList.push_back ( node );
00593 #ifdef _USE_HEAP_
00594                 push_heap ( mOpenList.begin(), mOpenList.end(), HeapCompare() );
00595 #endif
00596         }

template<class UserState>
void AStarSearch< UserState >::_constructionWaypointList ( AStarNode endNode  )  [inline, private]

Builds a WaypointList from UserStates.

The last UserState in the list will be the one associated with endNode. Each successor of endNode will have it's UserState added to the beginning of the list. Therefore, this method builds a list in reverse order from endNode to some startNode who's successor is NULL.

Remarks:
Make sure that there exists a node as an ancestor of endNode who's parent is NULL in order to stop the traversal.

Definition at line 618 of file AStarSearch.h.

References AStarSearch< UserState >::mSolutionList.

Referenced by AStarSearch< UserState >::advanceSearch().

00619         {
00620                 mSolutionList = new WaypointList<UserState>();
00621 
00622                 AStarNode* curNode = endNode;
00623                 while ( curNode != NULL )
00624                 {
00625                         mSolutionList->addWaypointToFront ( curNode->mUserState );
00626                         curNode = curNode->mSuccessor;
00627                 }
00628         }

template<class UserState>
AStarNode* AStarSearch< UserState >::_findBestOpenNode (  )  [inline, private]

Retrieves the node on the Open List with the smallest total cost to reach the goal node.

This is a combination of the entry cost as well as the estimated cost to goal.

Definition at line 443 of file AStarSearch.h.

References AStarSearch< UserState >::AStarNode::getEntryCost(), AStarSearch< UserState >::AStarNode::getEstimatedGoalCost(), AStarSearch< UserState >::AStarNode::getTotalCost(), and AStarSearch< UserState >::mOpenList.

Referenced by AStarSearch< UserState >::advanceSearch().

00444         {
00445                 if ( mOpenList.empty () )
00446                         throw exception ( "AStarSearch::_findBestOpenNode - empty open list" );
00447 
00448                 
00449 #ifdef _USE_HEAP_
00450                 AStarNode* bestNode = mOpenList.front();
00451                 pop_heap ( mOpenList.begin(), mOpenList.end(), HeapCompare() );
00452                 mOpenList.pop_back();
00453 #else
00454                 // Search the list backwards until we implement a priority heap
00455                 AStarNode* bestNode = mOpenList.front ();
00456                 float bestCost = bestNode->getTotalCost ();
00457 
00458                 vector<AStarNode*>::iterator bestNodeIter = mOpenList.begin();
00459                 vector<AStarNode*>::iterator iter = mOpenList.begin ();
00460 
00461                 while ( iter != mOpenList.end () )
00462                 {
00463                         if ( (*iter)->getTotalCost() < bestCost )
00464                         {
00465                                 bestNode = (*iter);
00466                                 bestCost = bestNode->getTotalCost();
00467                                 bestNodeIter = iter;
00468                         }
00469 
00470                         iter++;
00471                 }
00472 
00473                 if ( bestNodeIter != mOpenList.end() )
00474                 {
00475                         mOpenList.erase ( bestNodeIter );
00476                 }
00477 #endif
00478 
00479 #if _DEBUG
00480                 //cout << "Best Open Node: (" << bestNode->mUserState->mX << ", " << bestNode->mUserState->mY << ")" << endl;
00481                 cout << "\tEntry Cost: " << bestNode->getEntryCost () << endl;
00482                 cout << "\tGoal Cost: " << bestNode->getEstimatedGoalCost () << endl;
00483 #endif
00484 
00485                 return bestNode;
00486         }

template<class UserState>
AStarNode* AStarSearch< UserState >::_getFromClosed ( AStarNode node  )  [inline, private]

Definition at line 491 of file AStarSearch.h.

References AStarSearch< UserState >::mClosedList.

Referenced by AStarSearch< UserState >::advanceSearch().

00492         {
00493                 vector<AStarNode*>::iterator iter = mClosedList.begin ();
00494                 while ( iter != mClosedList.end () )
00495                 {
00496                         if ( *(*iter) == (*node) )
00497                                 return *iter;
00498 
00499                         iter++;
00500                 }
00501 
00502                 return NULL;
00503         }

template<class UserState>
AStarNode* AStarSearch< UserState >::_getFromOpen ( AStarNode node  )  [inline, private]

Definition at line 507 of file AStarSearch.h.

References AStarSearch< UserState >::mOpenList.

Referenced by AStarSearch< UserState >::advanceSearch().

00508         {
00509                 vector<AStarNode*>::iterator iter = mOpenList.begin ();
00510                 while ( iter != mOpenList.end () )
00511                 {
00512                         if ( *(*iter) == (*node) )
00513                                 return *iter;
00514 
00515                         iter++;
00516                 }
00517 
00518                 return NULL;
00519         }

template<class UserState>
bool AStarSearch< UserState >::_isNodeClosed ( AStarNode node  )  [inline, private]

Definition at line 523 of file AStarSearch.h.

References AStarSearch< UserState >::mClosedList.

00524         {
00525                 vector<AStarNode*>::iterator iter = mClosedList.begin ();
00526                 while ( iter != mClosedList.end () )
00527                 {
00528                         if ( *(*iter) == (*node) )
00529                                 return true;
00530 
00531                         iter++;
00532                 }
00533                 return false;
00534         }

template<class UserState>
bool AStarSearch< UserState >::_isNodeOpen ( AStarNode node  )  [inline, private]

Definition at line 537 of file AStarSearch.h.

References AStarSearch< UserState >::mClosedList.

00538         {
00539                 vector<AStarNode*>::iterator iter = mClosedList.begin ();
00540                 while ( iter != mClosedList.end () )
00541                 {
00542                         if ( *(*iter) == (*node) )
00543                                 return true;
00544 
00545                         iter++;
00546                 }
00547                 return false;
00548         }

template<class UserState>
void AStarSearch< UserState >::_releaseNodes (  )  [inline, private]

Definition at line 631 of file AStarSearch.h.

References AStarSearch< UserState >::mStartNode.

Referenced by AStarSearch< UserState >::advanceSearch(), AStarSearch< UserState >::setStartState(), and AStarSearch< UserState >::~AStarSearch().

00632         {
00633                 // All nodes in the search are part of a connected tree with
00634                 // mStartNode as the root
00635                 delete mStartNode;
00636                 mStartNode = NULL;
00637         }

template<class UserState>
void AStarSearch< UserState >::_releaseSolution (  )  [inline]

Definition at line 255 of file AStarSearch.h.

References AStarSearch< UserState >::mSolutionList.

Referenced by AStarSearch< UserState >::setStartState(), and AStarSearch< UserState >::~AStarSearch().

00256         {
00257                 delete mSolutionList;
00258                 mSolutionList = NULL;
00259         }

template<class UserState>
void AStarSearch< UserState >::_removeFromClosed ( AStarNode node  )  [inline, private]

Definition at line 551 of file AStarSearch.h.

References AStarSearch< UserState >::mClosedList.

Referenced by AStarSearch< UserState >::advanceSearch().

00552         {
00553                 vector<AStarNode*>::iterator iter = mClosedList.begin ();
00554                 while ( iter != mClosedList.end () )
00555                 {
00556                         if ( *(*iter) == (*node) )
00557                         {
00558                                 mClosedList.erase ( iter );
00559                                 return;
00560                         }
00561 
00562                         iter++;
00563                 }
00564 
00565                 throw exception ( "AStarSearch::_removeFromClosed - Node not found" );
00566         }

template<class UserState>
void AStarSearch< UserState >::_removeFromOpen ( AStarNode node  )  [inline, private]

Definition at line 569 of file AStarSearch.h.

References AStarSearch< UserState >::mOpenList.

Referenced by AStarSearch< UserState >::advanceSearch().

00570         {
00571                 vector<AStarNode*>::iterator iter = mOpenList.begin ();
00572                 while ( iter != mOpenList.end () )
00573                 {
00574                         if ( *(*iter) == (*node) )
00575                         {
00576                                 mOpenList.erase ( iter );
00577 #ifdef _USE_HEAP_
00578                                 make_heap ( mOpenList.begin(), mOpenList.end(), HeapCompare() );
00579 #endif
00580                                 return;
00581                         }
00582 
00583                         iter++;
00584                 }
00585 
00586                 throw exception ( "AStarSearch::_removeFromOpen - Node not found" );
00587         }

template<class UserState>
bool AStarSearch< UserState >::advanceSearch (  )  [inline]

Advances to the next best node.

Definition at line 326 of file AStarSearch.h.

References AStarSearch< UserState >::_addToClosed(), AStarSearch< UserState >::_addToOpen(), AStarSearch< UserState >::_constructionWaypointList(), AStarSearch< UserState >::_findBestOpenNode(), AStarSearch< UserState >::_getFromClosed(), AStarSearch< UserState >::_getFromOpen(), AStarSearch< UserState >::_releaseNodes(), AStarSearch< UserState >::_removeFromClosed(), AStarSearch< UserState >::_removeFromOpen(), AStarSearch< UserState >::AStarNode::getEntryCost(), AStarSearch< UserState >::AStarNode::getNeighbour(), AStarSearch< UserState >::mCurState, AStarSearch< UserState >::mEndState, AStarSearch< UserState >::mOpenList, AStarSearch< UserState >::AStarNode::mUserState, AStarSearch< UserState >::SS_NOT_STARTED, AStarSearch< UserState >::SS_PATH_FOUND, AStarSearch< UserState >::SS_PATH_NOT_FOUND, and AStarSearch< UserState >::SS_RUNNING.

Referenced by AStarSearchManager< ASR::Level::LevelNode >::_advanceSearch().

00327         {
00328                 if ( mCurState == SS_NOT_STARTED )
00329                         mCurState = SS_RUNNING;
00330 
00331                 if ( mCurState != SS_RUNNING )
00332                         throw exception ( "AStarSearch::singleStep - Search not in a runnable state" );
00333 
00334                 // We couldn't find a path
00335                 if ( mOpenList.empty () )
00336                 {
00337                         mCurState = SS_PATH_NOT_FOUND;
00338                 }
00339 
00340 
00341                 // Grab a node from the Open list
00342                 AStarNode* curNode = _findBestOpenNode ();
00343 
00344                 // We've reached the goal node so build our path
00345                 if ( curNode->mUserState->isNode ( *mEndState ) )
00346                 {
00347                         mCurState = SS_PATH_FOUND;
00348                         _constructionWaypointList ( curNode );
00349                         _releaseNodes ();
00350                         return true;
00351                 }
00352 
00353                 // Check all the neighbours for better matches and update any we find
00354                 for ( int i = 0; i < curNode->mUserState->getNumNeighbours(); i++ )
00355                 {
00356                         AStarNode* neighbour = curNode->getNeighbour ( i );
00357                         if ( neighbour == NULL )
00358                                 continue;
00359 
00360                         // This neighbour is inpassable
00361                         if ( fabs( neighbour->getTraversalCost () + 1.0f ) < 0.01f )
00362                                 continue;
00363 
00364                         // Determine if the node is on the closed list
00365                         AStarNode* closedNode = _getFromClosed ( neighbour );
00366                         if ( closedNode )
00367                         {
00368                                 // If a node was already on the closed list, then it has been
00369                                 // explored and has had it's neighbours explored. We may
00370                                 // have a better way "in to" the node so consider it again
00371                                 // with the new information
00372                                 float newEntryCost = curNode->getEntryCost() + neighbour->getTraversalCost();
00373                                 if ( newEntryCost < closedNode->getEntryCost() )
00374                                 {
00375                                         // Update the neighbour to use the current node as the entry point
00376                                         neighbour->setSuccessor ( curNode );
00377                                         neighbour->setEntryCost ( newEntryCost );
00378 
00379                                         // We need to re-examine this node
00380                                         _removeFromClosed ( closedNode );
00381                                         _addToOpen ( neighbour );
00382                                 }
00383                                 continue;
00384                         }
00385 
00386                         // The node was set to be explored but we have found a better way in
00387                         // so consider this way into the node
00388                         AStarNode* openNode = _getFromOpen ( neighbour );
00389                         if ( openNode )
00390                         {
00391                                 // If a node was already on the open list, it's estimated cost
00392                                 // to the goal has already been set
00393 
00394                                 float newEntryCost = curNode->getEntryCost() + neighbour->getTraversalCost();
00395                                 if ( newEntryCost < neighbour->getEntryCost() )
00396                                 {
00397                                         // Update the neighbour to use the current node as the entry point
00398                                         neighbour->setSuccessor ( curNode );
00399                                         neighbour->setEntryCost ( newEntryCost );
00400                                         
00401                                         _removeFromOpen ( openNode );
00402                                         _addToOpen ( neighbour );
00403                                 }
00404                                 continue;
00405                         }
00406 
00407                         // The neighbour state hasn't been considered before
00408                         // so set it up and add it to the open list for consideration
00409                         else
00410                         {
00411                                 neighbour->setSuccessor ( curNode );
00412                                 neighbour->setEntryCost ( curNode->getEntryCost() + neighbour->getTraversalCost () );
00413                                 neighbour->setEstimatedGoalCost( neighbour->mUserState->getEstimatedCostToState( *mEndState ));
00414                                 neighbour->createNeighbourNodes ();
00415                                 _addToOpen ( neighbour );
00416                         }
00417                 }
00418 
00419                 _addToClosed ( curNode );
00420 
00421                 return false;
00422         }

template<class UserState>
SearchState AStarSearch< UserState >::getSearchState (  )  [inline]

Definition at line 292 of file AStarSearch.h.

References AStarSearch< UserState >::mCurState.

00293         {
00294                 return mCurState;
00295         }

template<class UserState>
const WaypointList<UserState>* AStarSearch< UserState >::getSolutionPath (  )  [inline]

Definition at line 425 of file AStarSearch.h.

References AStarSearch< UserState >::mSolutionList.

00426         {
00427                 if ( !mSolutionList )
00428                         throw exception ( "AStarSearch::getSolutionPath - No solution exists" );
00429 
00430                 return mSolutionList;
00431         }

template<class UserState>
bool AStarSearch< UserState >::isRunnable (  )  [inline]

Returns true if the search can be advanced at least one more step.

Definition at line 300 of file AStarSearch.h.

References AStarSearch< UserState >::mCurState, AStarSearch< UserState >::SS_NOT_STARTED, and AStarSearch< UserState >::SS_RUNNING.

Referenced by AStarSearchManager< ASR::Level::LevelNode >::_advanceSearch().

00301         {
00302                 return (mCurState == SS_RUNNING || mCurState == SS_NOT_STARTED );
00303         }

template<class UserState>
bool AStarSearch< UserState >::isSolved (  )  [inline]

Returns true iff the search produced a valid result.

Definition at line 308 of file AStarSearch.h.

References AStarSearch< UserState >::mCurState, and AStarSearch< UserState >::SS_PATH_FOUND.

00309         {
00310                 return (mCurState == SS_PATH_FOUND );
00311         }

template<class UserState>
bool AStarSearch< UserState >::isUnsolvable (  )  [inline]

Returns true iff the search cannot find a valid result.

Definition at line 317 of file AStarSearch.h.

References AStarSearch< UserState >::mCurState, and AStarSearch< UserState >::SS_PATH_NOT_FOUND.

00318         {
00319                 return (mCurState == SS_PATH_NOT_FOUND );
00320         }

template<class UserState>
void AStarSearch< UserState >::setStartState ( UserState *  startState,
UserState *  endState 
) [inline]

Definition at line 265 of file AStarSearch.h.

References AStarSearch< UserState >::_releaseNodes(), AStarSearch< UserState >::_releaseSolution(), AStarSearch< UserState >::AStarNode::createNeighbourNodes(), AStarSearch< UserState >::mCurState, AStarSearch< UserState >::mEndState, AStarSearch< UserState >::mOpenList, AStarSearch< UserState >::mStartNode, AStarSearch< UserState >::AStarNode::mUserState, AStarSearch< UserState >::AStarNode::setEstimatedGoalCost(), and AStarSearch< UserState >::SS_PATH_NOT_FOUND.

Referenced by AStarSearchManager< ASR::Level::LevelNode >::createSearch().

00266         {
00267                 _releaseNodes ();
00268                 _releaseSolution ();
00269 
00270                 mStartNode = NULL;
00271                 mEndState = NULL;
00272 
00273                 if ( fabs(endState->getTraversalCost() + 1.0f) < 0.01f )
00274                 {
00275                         mCurState = SS_PATH_NOT_FOUND;
00276                         return;
00277                 }
00278 
00279                 mStartNode = new AStarNode ( startState );
00280                 mStartNode->createNeighbourNodes ();
00281                 mStartNode->setEstimatedGoalCost ( mStartNode->mUserState->getEstimatedCostToState ( *endState ) );
00282 
00283                 mEndState = endState;
00284 
00285                 mOpenList.push_back ( mStartNode );
00286         }


Member Data Documentation

template<class UserState>
vector<AStarNode*> AStarSearch< UserState >::mClosedList [private]

Definition at line 208 of file AStarSearch.h.

Referenced by AStarSearch< UserState >::_addToClosed(), AStarSearch< UserState >::_getFromClosed(), AStarSearch< UserState >::_isNodeClosed(), AStarSearch< UserState >::_isNodeOpen(), AStarSearch< UserState >::_removeFromClosed(), and AStarSearch< UserState >::AStarSearch().

template<class UserState>
SearchState AStarSearch< UserState >::mCurState [private]

Definition at line 201 of file AStarSearch.h.

Referenced by AStarSearch< UserState >::advanceSearch(), AStarSearch< UserState >::AStarSearch(), AStarSearch< UserState >::getSearchState(), AStarSearch< UserState >::isRunnable(), AStarSearch< UserState >::isSolved(), AStarSearch< UserState >::isUnsolvable(), and AStarSearch< UserState >::setStartState().

template<class UserState>
UserState* AStarSearch< UserState >::mEndState [private]

Definition at line 203 of file AStarSearch.h.

Referenced by AStarSearch< UserState >::advanceSearch(), and AStarSearch< UserState >::setStartState().

template<class UserState>
AStarNode* AStarSearch< UserState >::mGoalNode [private]

Definition at line 205 of file AStarSearch.h.

Referenced by AStarSearch< UserState >::AStarSearch().

template<class UserState>
vector<AStarNode*> AStarSearch< UserState >::mOpenList [private]

Definition at line 207 of file AStarSearch.h.

Referenced by AStarSearch< UserState >::_addToOpen(), AStarSearch< UserState >::_findBestOpenNode(), AStarSearch< UserState >::_getFromOpen(), AStarSearch< UserState >::_removeFromOpen(), AStarSearch< UserState >::advanceSearch(), AStarSearch< UserState >::AStarSearch(), and AStarSearch< UserState >::setStartState().

template<class UserState>
WaypointList<UserState>* AStarSearch< UserState >::mSolutionList [private]

Definition at line 210 of file AStarSearch.h.

Referenced by AStarSearch< UserState >::_constructionWaypointList(), AStarSearch< UserState >::_releaseSolution(), AStarSearch< UserState >::AStarSearch(), and AStarSearch< UserState >::getSolutionPath().

template<class UserState>
AStarNode* AStarSearch< UserState >::mStartNode [private]

Definition at line 202 of file AStarSearch.h.

Referenced by AStarSearch< UserState >::_releaseNodes(), AStarSearch< UserState >::AStarSearch(), and AStarSearch< UserState >::setStartState().


The documentation for this class was generated from the following file:
Generated on Sun Jun 25 19:23:43 2006 for Valors End by  doxygen 1.4.7