47#define INF_COST 1000000
51#pragma warning(disable : 4786)
57template <
class UserState>
98 if (x->
f > y->
f)
return true;
100 if (x->
f < y->
f)
return false;
115 : m_AllocateNodeCount(0),
117 m_CurrentSolutionNode(nullptr),
118 m_CancelRequest(false) {
125 : m_AllocateNodeCount(0),
127 m_CurrentSolutionNode(nullptr),
128 m_CancelRequest(false) {
135 : m_AllocateNodeCount(0),
137 m_CurrentSolutionNode(nullptr),
138 m_CancelRequest(false) {
153 m_CancelRequest =
false;
155 m_Start = AllocateNode();
156 m_Goal = AllocateNode();
158 assert((m_Start !=
nullptr && m_Goal !=
nullptr));
170 m_Start->
f = m_Start->
g + m_Start->
h;
171 m_Start->
parent = m_Start;
175 m_OpenList.push_back(m_Start);
178 push_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
200 if (m_OpenList.empty() || m_CancelRequest) {
210 Node *n = m_OpenList.front();
211 pop_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
212 m_OpenList.pop_back();
224 &((m_Goal)->m_UserState)))) {
240 Node *nodeChild = m_Goal;
244 nodeParent->
child = nodeChild;
245 nodeChild = nodeParent;
246 nodeParent = nodeParent->
parent;
253 if (nodeParent == nodeChild && nodeChild != m_Start) {
259 std::exit(EXIT_FAILURE);
261 }
while (nodeChild != m_Start &&
273 typename vector<Node *>::iterator closedlist_result;
274 typename vector<Node *>::iterator openlist_result;
276 m_ClosedList.push_back(n);
282 m_Successors.clear();
291 while (!ret && !m_OpenList.empty()) {
292 n = m_OpenList.front();
293 if (n ==
nullptr)
break;
294 pop_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
295 m_OpenList.pop_back();
301 typename vector<Node *>::iterator successor;
304 for (successor = m_Successors.begin(); successor != m_Successors.end();
306 FreeNode((*successor));
309 m_Successors.clear();
319 for (
typename vector<Node *>::iterator successor = m_Successors.begin();
320 successor != m_Successors.end(); successor++) {
322 for (closedlist_result = m_ClosedList.begin();
323 closedlist_result != m_ClosedList.end(); closedlist_result++) {
324 if ((*closedlist_result)
325 ->m_UserState.IsSameState((*successor)->m_UserState)) {
330 if (closedlist_result != m_ClosedList.end()) {
337 for (openlist_result = m_OpenList.begin();
338 openlist_result != m_OpenList.end(); openlist_result++) {
339 if ((*openlist_result)
340 ->m_UserState.IsSameState((*successor)->m_UserState)) {
345 if (openlist_result != m_OpenList.end()) {
351 (*successor)->parent =
nullptr;
365 typename vector<Node *>::iterator closedlist_result;
366 typename vector<Node *>::iterator openlist_result;
370 if (n->
parent !=
nullptr &&
372 &((successor)->m_UserState)) &&
384 if (tcost < (successor)->g) {
391 (successor)->m_UserState.PrintNodeInfo();
398 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->
m_UserState);
400 (successor)->parent = n->
parent;
401 (successor)->
yaw = (successor)->m_UserState.theta;
402 (successor)->g = (float)tcost;
403 (successor)->h = (
float)h_succ;
404 (successor)->f = (
float)(tcost + h_succ);
405 (successor)->m_UserState.setType(1);
408 for (openlist_result = m_OpenList.begin();
409 openlist_result != m_OpenList.end(); openlist_result++) {
410 if ((*openlist_result)
411 ->m_UserState.IsSameState((successor)->m_UserState)) {
417 if (openlist_result != m_OpenList.end()) {
419 FreeNode((*openlist_result));
420 m_OpenList.erase(openlist_result);
421 make_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
425 m_OpenList.push_back((successor));
428 push_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
435 newg = n->
g + n->
m_UserState.GetCost((successor)->m_UserState);
437 newg = n->
g + n->
m_UserState.GetCostTraj((successor)->m_UserState);
439 if (newg < (successor)->g) {
448 (successor)->m_UserState.PrintNodeInfo();
454 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->
m_UserState);
455 (successor)->parent = n;
456 (successor)->g = newg;
457 (successor)->h = (
float)h_succ;
458 (successor)->f = (
float)(newg + h_succ);
461 (successor)->m_UserState.costs = (successor)->m_UserState.getLineCost();
462 (successor)->m_UserState.setType(1);
464 for (openlist_result = m_OpenList.begin();
465 openlist_result != m_OpenList.end(); openlist_result++) {
466 if ((*openlist_result)
467 ->m_UserState.IsSameState((successor)->m_UserState)) {
472 if (openlist_result != m_OpenList.end()) {
474 FreeNode((*openlist_result));
475 m_OpenList.erase(openlist_result);
476 make_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
480 m_OpenList.push_back((successor));
483 push_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
491 typename vector<Node *>::iterator closedlist_result;
492 typename vector<Node *>::iterator openlist_result;
498 &((successor)->m_UserState)) &&
502 (successor)->m_UserState);
508 if (tcost < (successor)->g) {
516 (successor)->m_UserState.PrintNodeInfo();
523 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->
m_UserState);
526 (successor)->
yaw = (successor)->m_UserState.theta;
527 (successor)->g = (float)tcost;
528 (successor)->h = (
float)h_succ;
529 (successor)->f = (
float)(tcost + h_succ);
530 (successor)->m_UserState.setType(1);
533 for (openlist_result = m_OpenList.begin();
534 openlist_result != m_OpenList.end(); openlist_result++) {
535 if ((*openlist_result)
536 ->m_UserState.IsSameState((successor)->m_UserState)) {
542 if (openlist_result != m_OpenList.end()) {
544 FreeNode((*openlist_result));
545 m_OpenList.erase(openlist_result);
546 make_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
550 m_OpenList.push_back((successor));
553 push_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
555 }
else if (n->
parent !=
nullptr &&
557 &((successor)->m_UserState)) &&
569 if (tcost < (successor)->g) {
576 (successor)->m_UserState.PrintNodeInfo();
583 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->
m_UserState);
585 (successor)->parent = n->
parent;
586 (successor)->
yaw = (successor)->m_UserState.theta;
587 (successor)->g = (float)tcost;
588 (successor)->h = (
float)h_succ;
589 (successor)->f = (
float)(tcost + h_succ);
590 (successor)->m_UserState.setType(1);
593 for (openlist_result = m_OpenList.begin();
594 openlist_result != m_OpenList.end(); openlist_result++) {
595 if ((*openlist_result)
596 ->m_UserState.IsSameState((successor)->m_UserState)) {
602 if (openlist_result != m_OpenList.end()) {
604 FreeNode((*openlist_result));
605 m_OpenList.erase(openlist_result);
606 make_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
610 m_OpenList.push_back((successor));
613 push_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
621 newg = n->
g + n->
m_UserState.GetCost((successor)->m_UserState);
623 newg = n->
g + n->
m_UserState.GetCostTraj((successor)->m_UserState);
625 if (newg < (successor)->g) {
634 (successor)->m_UserState.PrintNodeInfo();
640 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->
m_UserState);
641 (successor)->parent = n;
642 (successor)->g = newg;
643 (successor)->h = (
float)h_succ;
644 (successor)->f = (
float)(newg + h_succ);
647 (successor)->m_UserState.costs = (successor)->m_UserState.getLineCost();
648 (successor)->m_UserState.setType(1);
650 for (openlist_result = m_OpenList.begin();
651 openlist_result != m_OpenList.end(); openlist_result++) {
652 if ((*openlist_result)
653 ->m_UserState.IsSameState((successor)->m_UserState)) {
658 if (openlist_result != m_OpenList.end()) {
660 FreeNode((*openlist_result));
661 m_OpenList.erase(openlist_result);
662 make_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
666 m_OpenList.push_back((successor));
669 push_heap(m_OpenList.begin(), m_OpenList.end(),
HeapCompare_f());
679 Node *node = AllocateNode();
684 m_Successors.push_back(node);
698 if (m_Start->
child) {
703 }
while (n != m_Goal);
718 m_CurrentSolutionNode = m_Start;
728 if (m_CurrentSolutionNode) {
729 if (m_CurrentSolutionNode->
child) {
730 Node *child = m_CurrentSolutionNode->
child;
731 m_CurrentSolutionNode = m_CurrentSolutionNode->
child;
741 m_CurrentSolutionNode = m_Goal;
751 if (m_CurrentSolutionNode) {
752 if (m_CurrentSolutionNode->
parent) {
754 m_CurrentSolutionNode = m_CurrentSolutionNode->
parent;
782 iterDbgOpen = m_OpenList.begin();
783 if (iterDbgOpen != m_OpenList.end()) {
784 f = (*iterDbgOpen)->f;
785 g = (*iterDbgOpen)->g;
786 h = (*iterDbgOpen)->h;
787 return &(*iterDbgOpen)->m_UserState;
800 if (iterDbgOpen != m_OpenList.end()) {
801 f = (*iterDbgOpen)->f;
802 g = (*iterDbgOpen)->g;
803 h = (*iterDbgOpen)->h;
804 return &(*iterDbgOpen)->m_UserState;
818 iterDbgClosed = m_ClosedList.begin();
819 if (iterDbgClosed != m_ClosedList.end()) {
820 f = (*iterDbgClosed)->f;
821 g = (*iterDbgClosed)->g;
822 h = (*iterDbgClosed)->h;
824 return &(*iterDbgClosed)->m_UserState;
837 if (iterDbgClosed != m_ClosedList.end()) {
838 f = (*iterDbgClosed)->f;
839 g = (*iterDbgClosed)->g;
840 h = (*iterDbgClosed)->h;
842 return &(*iterDbgClosed)->m_UserState;
859 void FreeAllNodes() {
860 typename vector<Node *>::iterator iterOpen = m_OpenList.begin();
862 while (iterOpen != m_OpenList.end()) {
863 Node *n = (*iterOpen);
871 typename vector<Node *>::iterator iterClosed;
873 for (iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end();
875 Node *n = (*iterClosed);
879 m_ClosedList.clear();
886 void FreeUnusedNodes() {
888 typename vector<Node *>::iterator iterOpen = m_OpenList.begin();
890 while (iterOpen != m_OpenList.end()) {
891 Node *n = (*iterOpen);
903 typename vector<Node *>::iterator iterClosed;
905 for (iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end();
907 Node *n = (*iterClosed);
914 m_ClosedList.clear();
918 Node *AllocateNode() {
919 m_AllocateNodeCount++;
924 void FreeNode(Node *node) {
925 m_AllocateNodeCount--;
931 vector<Node *> m_OpenList;
934 vector<Node *> m_ClosedList;
937 vector<Node *> m_Successors;
940 unsigned int m_State;
949 Node *m_CurrentSolutionNode;
952 typename vector<Node *>::iterator iterDbgOpen;
953 typename vector<Node *>::iterator iterDbgClosed;
956 int m_AllocateNodeCount;
958 bool m_CancelRequest;
ob::SE2StateSpace::StateType State
Definition: Primitives.h:12
Definition: stl_thetastar.h:95
bool operator()(const Node *x, const Node *y) const
Definition: stl_thetastar.h:97
Definition: stl_thetastar.h:76
float g
Definition: stl_thetastar.h:83
Node * parent
Definition: stl_thetastar.h:78
double yaw
Definition: stl_thetastar.h:82
Node * child
Definition: stl_thetastar.h:80
Node()
Definition: stl_thetastar.h:87
float f
Definition: stl_thetastar.h:85
float h
Definition: stl_thetastar.h:84
UserState m_UserState
Definition: stl_thetastar.h:89
Definition: stl_thetastar.h:58
virtual unsigned int SearchStep()
Definition: stl_thetastar.h:185
virtual void useAstar()
Definition: stl_thetastar.h:144
virtual UserState * GetSolutionNext()
Definition: stl_thetastar.h:727
virtual bool AddSuccessor(UserState &State)
Definition: stl_thetastar.h:678
virtual UserState * GetSolutionStart()
Definition: stl_thetastar.h:717
virtual UserState * GetClosedListStart(float &f, float &g, float &h)
Definition: stl_thetastar.h:817
virtual UserState * GetSolutionEnd()
Definition: stl_thetastar.h:740
virtual UserState * GetOpenListNext(float &f, float &g, float &h)
Definition: stl_thetastar.h:798
bool m_connectGrandParent
Definition: stl_thetastar.h:71
virtual bool UpdateVertexGrandParent(Node *n, Node *successor)
Definition: stl_thetastar.h:490
virtual UserState * GetClosedListNext(float &f, float &g, float &h)
Definition: stl_thetastar.h:835
ThetaStarSearch()
Definition: stl_thetastar.h:114
@ SEARCH_STATE_SEARCHING
Definition: stl_thetastar.h:62
@ SEARCH_STATE_SUCCEEDED
Definition: stl_thetastar.h:63
@ SEARCH_STATE_FAILED
Definition: stl_thetastar.h:64
@ SEARCH_STATE_INVALID
Definition: stl_thetastar.h:66
@ SEARCH_STATE_OUT_OF_MEMORY
Definition: stl_thetastar.h:65
@ SEARCH_STATE_NOT_INITIALISED
Definition: stl_thetastar.h:61
bool m_euclideanCost
Definition: stl_thetastar.h:69
virtual bool UpdateVertex(Node *n, Node *successor)
Definition: stl_thetastar.h:364
virtual UserState * GetOpenListStart()
Definition: stl_thetastar.h:776
virtual void FreeSolutionNodes()
Definition: stl_thetastar.h:695
virtual void CancelSearch()
Definition: stl_thetastar.h:149
virtual void use_connectGrandParent()
Definition: stl_thetastar.h:146
bool m_useAstar
Definition: stl_thetastar.h:70
virtual UserState * GetClosedListStart()
Definition: stl_thetastar.h:812
virtual int GetStepCount()
Definition: stl_thetastar.h:850
virtual float GetSolutionCost()
Definition: stl_thetastar.h:764
virtual UserState * GetClosedListNext()
Definition: stl_thetastar.h:830
Node * GetCurrentBestRawNode()
Definition: stl_thetastar.h:810
virtual UserState * GetSolutionPrev()
Definition: stl_thetastar.h:750
virtual UserState * GetOpenListNext()
Definition: stl_thetastar.h:793
virtual UserState * GetOpenListStart(float &f, float &g, float &h)
Definition: stl_thetastar.h:781
virtual void SetStartAndGoalStates(UserState &Start, UserState &Goal)
Definition: stl_thetastar.h:152
ThetaStarSearch(int MaxNodes)
Definition: stl_thetastar.h:134
virtual void EnsureMemoryFreed()
Definition: stl_thetastar.h:852
ThetaStarSearch(bool euclideanCostChoice)
Definition: stl_thetastar.h:124
Definition: stl_thetastar.h:54
double std(const std::vector< double > &values)
Definition: PathStatistics.hpp:85
#define INF_COST
Definition: stl_thetastar.h:47