32#pragma warning "SmoothThetaStar is not supported at the moment."
51#include <ompl/util/Console.h>
55#include "../base/Plannerglobal::settings.h"
59#define INF_COST 1000000
63#pragma warning(disable : 4786)
66template <
class UserState>
88 double mergeNodeThreshold{-1.};
102 Node() : parent(0), child(0), yaw(0.0f), g(0.0f), h(0.0f), f(0.0f) {}
104 UserState m_UserState;
110 class HeapCompare_f {
112 bool operator()(
const Node *x,
const Node *y)
const {
113 if (x->f > y->f)
return true;
115 if (x->f < y->f)
return false;
129 SmoothThetaStarSearch()
130 : m_AllocateNodeCount(0),
131 m_State(SEARCH_STATE_NOT_INITIALISED),
132 m_CurrentSolutionNode(NULL),
133 m_CancelRequest(false) {
135 m_connectGrandParent =
false;
139 SmoothThetaStarSearch(
bool euclideanCostChoice)
140 : m_AllocateNodeCount(0),
141 m_State(SEARCH_STATE_NOT_INITIALISED),
142 m_CurrentSolutionNode(NULL),
143 m_CancelRequest(false) {
144 m_euclideanCost = euclideanCostChoice;
146 m_connectGrandParent =
false;
149 SmoothThetaStarSearch(
int MaxNodes)
150 : m_AllocateNodeCount(0),
151 m_State(SEARCH_STATE_NOT_INITIALISED),
152 m_CurrentSolutionNode(NULL),
153 m_CancelRequest(false) {
155 m_connectGrandParent =
false;
159 void useAstar()
override { m_useAstar =
true; }
164 void CancelSearch()
override { m_CancelRequest =
true; }
168 m_CancelRequest =
false;
170 m_Start = AllocateNode();
171 m_Goal = AllocateNode();
173 assert((m_Start != NULL && m_Goal != NULL));
175 m_Start->m_UserState = Start;
176 m_Goal->m_UserState = Goal;
184 m_Start->h = m_Start->m_UserState.GoalDistanceEstimate(m_Goal->m_UserState);
185 m_Start->f = m_Start->g + m_Start->h;
186 m_Start->parent = m_Start;
190 m_OpenList.push_back(m_Start);
193 push_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
199 static double distance(
double x1,
double y1,
double x2,
double y2) {
200 return std::sqrt(std::pow(x1 - x2, 2.) + std::pow(y1 - y2, 2.));
206 assert((m_State > SEARCH_STATE_NOT_INITIALISED) &&
207 (m_State < SEARCH_STATE_INVALID));
211 if ((m_State == SEARCH_STATE_SUCCEEDED) ||
212 (m_State == SEARCH_STATE_FAILED)) {
219 if (m_OpenList.empty() || m_CancelRequest) {
228 if (mergeNodeThreshold > 0.) {
229 std::vector<Node *> new_Open;
230 for (
auto i = 0; i < m_OpenList.size(); ++i) {
231 bool markdel =
false;
232 auto a = m_OpenList[i]->m_UserState;
233 for (
auto j = i + 1; j < m_OpenList.size(); ++j) {
234 if (i == j)
continue;
235 auto b = m_OpenList[j]->m_UserState;
236 if (distance(a.x_r, a.y_r, b.x_r, b.y_r) < mergeNodeThreshold)
239 if (!markdel) new_Open.push_back(m_OpenList[i]);
241 if (new_Open.size() != m_OpenList.size())
242 OMPL_DEBUG(
"\tSmoothStar merging reduces nodes from %i to %i.",
243 (
int)m_OpenList.size(), (
int)new_Open.size());
244 m_OpenList = new_Open;
248 Node *n = m_OpenList.front();
249 pop_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
250 m_OpenList.pop_back();
253 if (n->m_UserState.IsGoal(m_Goal->m_UserState)) {
256 m_Goal->parent = n->parent;
259 OMPL_DEBUG(
"Setting Goal orientation..");
261 if (!(n->m_UserState.lineofsight(&(n->parent->m_UserState),
262 &((m_Goal)->m_UserState)))) {
263 (m_Goal)->m_UserState.setOrientation(&(n->parent->m_UserState));
266 OMPL_DEBUG(
"Setting Goal orientation done");
267 m_Goal->m_UserState.steer = n->m_UserState.steer;
269 m_Goal->m_UserState.costs = m_Goal->m_UserState.getLineCost();
274 if (!n->m_UserState.IsSameState(m_Start->m_UserState)) {
278 Node *nodeChild = m_Goal;
279 Node *nodeParent = m_Goal->parent;
282 nodeParent->child = nodeChild;
283 nodeChild = nodeParent;
284 nodeParent = nodeParent->parent;
287 nodeParent->m_UserState.PrintNodeInfo();
288 nodeChild->m_UserState.PrintNodeInfo();
291 if (nodeParent == nodeChild && nodeChild != m_Start) {
295 nodeParent->m_UserState.PrintNodeInfo();
296 nodeChild->m_UserState.PrintNodeInfo();
298 std::exit(EXIT_FAILURE);
300 }
while (nodeChild != m_Start &&
312 typename vector<Node *>::iterator closedlist_result;
313 typename vector<Node *>::iterator openlist_result;
315 m_ClosedList.push_back(n);
321 m_Successors.clear();
326 bool ret = n->m_UserState.GetSuccessors(
327 this, n->parent ? &n->parent->m_UserState : NULL);
330 while (!ret && !m_OpenList.empty()) {
331 n = m_OpenList.front();
332 if (n ==
nullptr)
break;
333 pop_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
334 m_OpenList.pop_back();
335 ret = n->m_UserState.GetSuccessors(
336 this, n->parent ? &n->parent->m_UserState : NULL);
340 typename vector<Node *>::iterator successor;
343 for (successor = m_Successors.begin(); successor != m_Successors.end();
345 FreeNode((*successor));
348 m_Successors.clear();
358 for (
typename vector<Node *>::iterator successor = m_Successors.begin();
359 successor != m_Successors.end(); successor++) {
361 for (closedlist_result = m_ClosedList.begin();
362 closedlist_result != m_ClosedList.end(); closedlist_result++) {
363 if ((*closedlist_result)
364 ->m_UserState.IsSameState((*successor)->m_UserState)) {
369 if (closedlist_result != m_ClosedList.end()) {
376 for (openlist_result = m_OpenList.begin();
377 openlist_result != m_OpenList.end(); openlist_result++) {
378 if ((*openlist_result)
379 ->m_UserState.IsSameState((*successor)->m_UserState)) {
384 if (openlist_result != m_OpenList.end()) {
390 (*successor)->parent = NULL;
397 double x = (*successor)->m_UserState.x_r;
398 double y = (*successor)->m_UserState.y_r;
403 (*successor)->m_UserState.x_r -= eta * dx / distance;
404 (*successor)->m_UserState.y_r += eta * dy / distance;
409 (*successor)->m_UserState.x_r,
410 (*successor)->m_UserState.y_r)) {
411 if (!m_connectGrandParent)
417 if (!m_connectGrandParent)
429 typename vector<Node *>::iterator closedlist_result;
430 typename vector<Node *>::iterator openlist_result;
434 if (n->parent != NULL &&
435 n->m_UserState.lineofsight(&(n->parent->m_UserState),
436 &((successor)->m_UserState)) &&
439 tcost = n->parent->g +
440 n->parent->m_UserState.GetCost((successor)->m_UserState);
442 tcost = n->parent->g +
443 n->parent->m_UserState.GetCostTrajFromParent(
444 (n->parent->m_UserState), (successor)->m_UserState);
448 if (tcost < (successor)->g) {
449 if (n->parent == n && n->parent != m_Start) {
451 n->parent->m_UserState.PrintNodeInfo();
452 n->m_UserState.PrintNodeInfo();
455 (successor)->m_UserState.PrintNodeInfo();
462 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->m_UserState);
464 std::cout <<
"Updating vertex..." << std::endl;
466 (successor)->parent = n->parent;
467 (successor)->yaw = (successor)->m_UserState.theta;
468 (successor)->g = (float)tcost;
469 (successor)->h = (
float)h_succ;
470 (successor)->f = (
float)(tcost + h_succ);
471 (successor)->m_UserState.setType(1);
474 for (openlist_result = m_OpenList.begin();
475 openlist_result != m_OpenList.end(); openlist_result++) {
476 if ((*openlist_result)
477 ->m_UserState.IsSameState((successor)->m_UserState)) {
483 if (openlist_result != m_OpenList.end()) {
485 FreeNode((*openlist_result));
486 m_OpenList.erase(openlist_result);
487 make_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
491 m_OpenList.push_back((successor));
494 push_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
501 newg = n->g + n->m_UserState.GetCost((successor)->m_UserState);
503 newg = n->g + n->m_UserState.GetCostTraj((successor)->m_UserState);
505 if (newg < (successor)->g) {
508 if (n->parent == n && n->parent != m_Start) {
510 n->parent->m_UserState.PrintNodeInfo();
511 n->m_UserState.PrintNodeInfo();
514 (successor)->m_UserState.PrintNodeInfo();
520 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->m_UserState);
521 (successor)->parent = n;
522 (successor)->g = newg;
523 (successor)->h = (
float)h_succ;
524 (successor)->f = (
float)(newg + h_succ);
526 (successor)->yaw = (successor)->m_UserState.theta;
527 (successor)->m_UserState.costs = (successor)->m_UserState.getLineCost();
528 (successor)->m_UserState.setType(1);
530 for (openlist_result = m_OpenList.begin();
531 openlist_result != m_OpenList.end(); openlist_result++) {
532 if ((*openlist_result)
533 ->m_UserState.IsSameState((successor)->m_UserState)) {
538 if (openlist_result != m_OpenList.end()) {
540 FreeNode((*openlist_result));
541 m_OpenList.erase(openlist_result);
542 make_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
546 m_OpenList.push_back((successor));
549 push_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
557 typename vector<Node *>::iterator closedlist_result;
558 typename vector<Node *>::iterator openlist_result;
565 if (n->parent->parent != NULL &&
566 n->m_UserState.lineofsight(&(n->parent->parent->m_UserState),
567 &((successor)->m_UserState)) &&
570 tcost = n->parent->parent->g + n->parent->parent->m_UserState.GetCost(
571 (successor)->m_UserState);
573 tcost = n->parent->parent->g +
574 n->parent->parent->m_UserState.GetCostTrajFromParent(
575 (n->parent->parent->m_UserState), (successor)->m_UserState);
577 if (tcost < (successor)->g) {
578 if (n->parent->parent == n && n->parent->parent != m_Start) {
592 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->m_UserState);
594 (successor)->parent = n->parent->parent;
595 (successor)->yaw = (successor)->m_UserState.theta;
596 (successor)->g = (float)tcost;
597 (successor)->h = (
float)h_succ;
598 (successor)->f = (
float)(tcost + h_succ);
599 (successor)->m_UserState.setType(1);
605 for (openlist_result = m_OpenList.begin();
606 openlist_result != m_OpenList.end(); openlist_result++) {
607 if ((*openlist_result)
608 ->m_UserState.IsSameState((successor)->m_UserState)) {
614 if (openlist_result != m_OpenList.end()) {
616 FreeNode((*openlist_result));
617 m_OpenList.erase(openlist_result);
618 make_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
622 m_OpenList.push_back((successor));
625 push_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
627 }
else if (n->parent != NULL &&
628 n->m_UserState.lineofsight(&(n->parent->m_UserState),
629 &((successor)->m_UserState)) &&
632 tcost = n->parent->g +
633 n->parent->m_UserState.GetCost((successor)->m_UserState);
635 tcost = n->parent->g +
636 n->parent->m_UserState.GetCostTrajFromParent(
637 (n->parent->m_UserState), (successor)->m_UserState);
641 if (tcost < (successor)->g) {
642 if (n->parent == n && n->parent != m_Start) {
655 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->m_UserState);
657 (successor)->parent = n->parent;
658 (successor)->yaw = (successor)->m_UserState.theta;
659 (successor)->g = (float)tcost;
660 (successor)->h = (
float)h_succ;
661 (successor)->f = (
float)(tcost + h_succ);
662 (successor)->m_UserState.setType(1);
669 for (openlist_result = m_OpenList.begin();
670 openlist_result != m_OpenList.end(); openlist_result++) {
671 if ((*openlist_result)
672 ->m_UserState.IsSameState((successor)->m_UserState)) {
678 if (openlist_result != m_OpenList.end()) {
680 FreeNode((*openlist_result));
681 m_OpenList.erase(openlist_result);
682 make_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
686 m_OpenList.push_back((successor));
689 push_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
697 newg = n->g + n->m_UserState.GetCost((successor)->m_UserState);
699 newg = n->g + n->m_UserState.GetCostTraj((successor)->m_UserState);
701 if (newg < (successor)->g) {
704 if (n->parent == n && n->parent != m_Start) {
706 n->parent->m_UserState.PrintNodeInfo();
707 n->m_UserState.PrintNodeInfo();
710 (successor)->m_UserState.PrintNodeInfo();
716 (successor)->m_UserState.GoalDistanceEstimate(m_Goal->m_UserState);
717 (successor)->parent = n;
718 (successor)->g = newg;
719 (successor)->h = (
float)h_succ;
720 (successor)->f = (
float)(newg + h_succ);
722 (successor)->yaw = (successor)->m_UserState.theta;
723 (successor)->m_UserState.costs = (successor)->m_UserState.getLineCost();
724 (successor)->m_UserState.setType(1);
726 for (openlist_result = m_OpenList.begin();
727 openlist_result != m_OpenList.end(); openlist_result++) {
728 if ((*openlist_result)
729 ->m_UserState.IsSameState((successor)->m_UserState)) {
734 if (openlist_result != m_OpenList.end()) {
736 FreeNode((*openlist_result));
737 m_OpenList.erase(openlist_result);
738 make_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
742 m_OpenList.push_back((successor));
745 push_heap(m_OpenList.begin(), m_OpenList.end(), HeapCompare_f());
755 Node *node = AllocateNode();
758 node->m_UserState =
State;
759 node->yaw =
State.theta;
760 m_Successors.push_back(node);
774 if (m_Start->child) {
779 }
while (n != m_Goal);
794 m_CurrentSolutionNode = m_Start;
796 return &m_Start->m_UserState;
804 if (m_CurrentSolutionNode) {
805 if (m_CurrentSolutionNode->child) {
806 Node *child = m_CurrentSolutionNode->child;
807 m_CurrentSolutionNode = m_CurrentSolutionNode->child;
808 return &child->m_UserState;
817 m_CurrentSolutionNode = m_Goal;
819 return &m_Goal->m_UserState;
827 if (m_CurrentSolutionNode) {
828 if (m_CurrentSolutionNode->parent) {
829 Node *parent = m_CurrentSolutionNode->parent;
830 m_CurrentSolutionNode = m_CurrentSolutionNode->parent;
831 return &parent->m_UserState;
841 if (m_Goal && m_State == SEARCH_STATE_SUCCEEDED) {
858 iterDbgOpen = m_OpenList.begin();
859 if (iterDbgOpen != m_OpenList.end()) {
860 f = (*iterDbgOpen)->f;
861 g = (*iterDbgOpen)->g;
862 h = (*iterDbgOpen)->h;
863 return &(*iterDbgOpen)->m_UserState;
876 if (iterDbgOpen != m_OpenList.end()) {
877 f = (*iterDbgOpen)->f;
878 g = (*iterDbgOpen)->g;
879 h = (*iterDbgOpen)->h;
880 return &(*iterDbgOpen)->m_UserState;
894 iterDbgClosed = m_ClosedList.begin();
895 if (iterDbgClosed != m_ClosedList.end()) {
896 f = (*iterDbgClosed)->f;
897 g = (*iterDbgClosed)->g;
898 h = (*iterDbgClosed)->h;
900 return &(*iterDbgClosed)->m_UserState;
913 if (iterDbgClosed != m_ClosedList.end()) {
914 f = (*iterDbgClosed)->f;
915 g = (*iterDbgClosed)->g;
916 h = (*iterDbgClosed)->h;
918 return &(*iterDbgClosed)->m_UserState;
929 if (m_AllocateNodeCount == 0) std::cout <<
"memory clean" << std::endl;
934 void FreeAllNodes() {
935 typename vector<Node *>::iterator iterOpen = m_OpenList.begin();
937 while (iterOpen != m_OpenList.end()) {
938 Node *n = (*iterOpen);
946 typename vector<Node *>::iterator iterClosed;
948 for (iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end();
950 Node *n = (*iterClosed);
954 m_ClosedList.clear();
961 void FreeUnusedNodes() {
963 typename vector<Node *>::iterator iterOpen = m_OpenList.begin();
965 while (iterOpen != m_OpenList.end()) {
966 Node *n = (*iterOpen);
978 typename vector<Node *>::iterator iterClosed;
980 for (iterClosed = m_ClosedList.begin(); iterClosed != m_ClosedList.end();
982 Node *n = (*iterClosed);
989 m_ClosedList.clear();
993 Node *AllocateNode() {
994 m_AllocateNodeCount++;
999 void FreeNode(Node *node) {
1000 m_AllocateNodeCount--;
1005 vector<Node *> openList()
const {
return m_OpenList; }
1009 vector<Node *> m_OpenList;
1012 vector<Node *> m_ClosedList;
1015 vector<Node *> m_Successors;
1018 unsigned int m_State;
1027 Node *m_CurrentSolutionNode;
1030 typename vector<Node *>::iterator iterDbgOpen;
1031 typename vector<Node *>::iterator iterDbgClosed;
1034 int m_AllocateNodeCount;
1036 bool m_CancelRequest;
ob::SE2StateSpace::StateType State
Definition: Primitives.h:12
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 * GetSolutionEnd()
Definition: stl_thetastar.h:740
bool m_connectGrandParent
Definition: stl_thetastar.h:71
virtual bool UpdateVertexGrandParent(Node *n, Node *successor)
Definition: stl_thetastar.h:490
@ 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_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 void SetStartAndGoalStates(UserState &Start, UserState &Goal)
Definition: stl_thetastar.h:152
virtual void EnsureMemoryFreed()
Definition: stl_thetastar.h:852
double max(const std::vector< double > &values)
Definition: PathStatistics.hpp:79
double std(const std::vector< double > &values)
Definition: PathStatistics.hpp:85
#define INF_COST
Definition: stl_thetastar.h:47
std::shared_ptr< Environment > environment
Environment used for planning.
Definition: PlannerSettings.h:73
static PlannerSettings::GlobalSettings settings
Definition: PlannerSettings.h:699