47 #define USE_FSA_MEMORY 0 106 bool operator() (
const Node *x,
const Node *y )
const 119 m_FixedSizeAllocator( MaxNodes ),
121 m_State( SEARCH_STATE_NOT_INITIALISED ),
122 m_CurrentSolutionNode( NULL ),
123 m_AllocateNodeCount(0),
124 m_CancelRequest( false )
131 m_CancelRequest =
true;
137 m_CancelRequest =
false;
139 m_Start = AllocateNode();
140 m_Goal = AllocateNode();
142 assert((m_Start != NULL && m_Goal != NULL));
144 m_Start->m_UserState = Start;
145 m_Goal->m_UserState = Goal;
147 m_State = SEARCH_STATE_SEARCHING;
153 m_Start->h = m_Start->m_UserState.GoalDistanceEstimate( m_Goal->m_UserState );
154 m_Start->f = m_Start->g + m_Start->h;
159 m_OpenList.push_back( m_Start );
162 push_heap( m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f() );
172 assert( (m_State > SEARCH_STATE_NOT_INITIALISED) &&
173 (m_State < SEARCH_STATE_INVALID) );
176 if( (m_State == SEARCH_STATE_SUCCEEDED) ||
177 (m_State == SEARCH_STATE_FAILED)
186 if( m_OpenList.empty() || m_CancelRequest )
189 m_State = SEARCH_STATE_FAILED;
197 Node *n = m_OpenList.front();
198 pop_heap( m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f() );
199 m_OpenList.pop_back();
210 if(
false == n->
m_UserState.IsSameState( m_Start->m_UserState ) )
215 Node *nodeChild = m_Goal;
220 nodeParent->
child = nodeChild;
222 nodeChild = nodeParent;
223 nodeParent = nodeParent->
parent;
226 while( nodeChild != m_Start );
233 m_State = SEARCH_STATE_SUCCEEDED;
244 m_Successors.clear();
253 typename vector< Node * >::iterator successor;
256 for( successor = m_Successors.begin(); successor != m_Successors.end(); successor ++ )
258 FreeNode( (*successor) );
261 m_Successors.clear();
266 m_State = SEARCH_STATE_OUT_OF_MEMORY;
271 for(
typename vector< Node * >::iterator successor = m_Successors.begin(); successor != m_Successors.end(); successor ++ )
275 float newg = n->
g + n->
m_UserState.GetCost( (*successor)->m_UserState );
283 typename vector< Node * >::iterator openlist_result;
285 for( openlist_result = m_OpenList.begin(); openlist_result != m_OpenList.end(); openlist_result ++ )
287 if( (*openlist_result)->m_UserState.IsSameState( (*successor)->m_UserState ) )
293 if( openlist_result != m_OpenList.end() )
298 if( (*openlist_result)->g <= newg )
300 FreeNode( (*successor) );
307 typename vector< Node * >::iterator closedlist_result;
309 for( closedlist_result = m_ClosedList.begin(); closedlist_result != m_ClosedList.end(); closedlist_result ++ )
311 if( (*closedlist_result)->m_UserState.IsSameState( (*successor)->m_UserState ) )
317 if( closedlist_result != m_ClosedList.end() )
322 if( (*closedlist_result)->g <= newg )
325 FreeNode( (*successor) );
334 (*successor)->parent = n;
335 (*successor)->
g = newg;
336 (*successor)->h = (*successor)->m_UserState.GoalDistanceEstimate( m_Goal->m_UserState );
337 (*successor)->f = (*successor)->g + (*successor)->h;
341 if( closedlist_result != m_ClosedList.end() )
344 FreeNode( (*closedlist_result) );
345 m_ClosedList.erase( closedlist_result );
356 if( openlist_result != m_OpenList.end() )
359 FreeNode( (*openlist_result) );
360 m_OpenList.erase( openlist_result );
366 make_heap( m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f() );
371 m_OpenList.push_back( (*successor) );
374 push_heap( m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f() );
380 m_ClosedList.push_back( n );
392 Node *node = AllocateNode();
398 m_Successors.push_back( node );
423 }
while( n != m_Goal );
443 m_CurrentSolutionNode = m_Start;
446 return &m_Start->m_UserState;
457 if( m_CurrentSolutionNode )
459 if( m_CurrentSolutionNode->child )
462 Node *child = m_CurrentSolutionNode->
child;
464 m_CurrentSolutionNode = m_CurrentSolutionNode->
child;
476 m_CurrentSolutionNode = m_Goal;
479 return &m_Goal->m_UserState;
490 if( m_CurrentSolutionNode )
492 if( m_CurrentSolutionNode->parent )
497 m_CurrentSolutionNode = m_CurrentSolutionNode->
parent;
512 return GetOpenListStart( f,g,h );
517 iterDbgOpen = m_OpenList.begin();
518 if( iterDbgOpen != m_OpenList.end() )
520 f = (*iterDbgOpen)->f;
521 g = (*iterDbgOpen)->g;
522 h = (*iterDbgOpen)->h;
523 return &(*iterDbgOpen)->m_UserState;
532 return GetOpenListNext( f,g,h );
538 if( iterDbgOpen != m_OpenList.end() )
540 f = (*iterDbgOpen)->f;
541 g = (*iterDbgOpen)->g;
542 h = (*iterDbgOpen)->h;
543 return &(*iterDbgOpen)->m_UserState;
552 return GetClosedListStart( f,g,h );
557 iterDbgClosed = m_ClosedList.begin();
558 if( iterDbgClosed != m_ClosedList.end() )
560 f = (*iterDbgClosed)->f;
561 g = (*iterDbgClosed)->g;
562 h = (*iterDbgClosed)->h;
564 return &(*iterDbgClosed)->m_UserState;
573 return GetClosedListNext( f,g,h );
579 if( iterDbgClosed != m_ClosedList.end() )
581 f = (*iterDbgClosed)->f;
582 g = (*iterDbgClosed)->g;
583 h = (*iterDbgClosed)->h;
585 return &(*iterDbgClosed)->m_UserState;
598 assert(m_AllocateNodeCount == 0);
610 typename vector< Node * >::iterator iterOpen = m_OpenList.begin();
612 while( iterOpen != m_OpenList.end() )
614 Node *n = (*iterOpen);
623 typename vector< Node * >::iterator iterClosed;
625 for( iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end(); iterClosed ++ )
627 Node *n = (*iterClosed);
631 m_ClosedList.clear();
645 typename vector< Node * >::iterator iterOpen = m_OpenList.begin();
647 while( iterOpen != m_OpenList.end() )
649 Node *n = (*iterOpen);
664 typename vector< Node * >::iterator iterClosed;
666 for( iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end(); iterClosed ++ )
668 Node *n = (*iterClosed);
678 m_ClosedList.clear();
690 Node *address = m_FixedSizeAllocator.alloc();
696 m_AllocateNodeCount ++;
705 m_AllocateNodeCount --;
710 m_FixedSizeAllocator.free( node );
UserState * GetOpenListStart()
UserState * GetOpenListNext()
vector< Node * > m_OpenList
UserState * GetSolutionPrev()
AStarSearch(int MaxNodes=2000)
vector< Node * > m_Successors
UserState * GetClosedListStart()
UserState * GetSolutionEnd()
UserState * GetOpenListNext(float &f, float &g, float &h)
vector< Node * >::iterator iterDbgClosed
UserState * GetClosedListNext(float &f, float &g, float &h)
UserState * GetOpenListStart(float &f, float &g, float &h)
UserState * GetClosedListStart(float &f, float &g, float &h)
UserState * GetClosedListNext()
void SetStartAndGoalStates(UserState &Start, UserState &Goal)
void FreeNode(Node *node)
vector< Node * >::iterator iterDbgOpen
Node * m_CurrentSolutionNode
unsigned int SearchStep()
bool AddSuccessor(UserState &State)
vector< Node * > m_ClosedList
UserState * GetSolutionStart()
UserState * GetSolutionNext()