2 This file is part of I am car.
4 I am car is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 I am car is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with I am car. If not, see <http://www.gnu.org/licenses/>.
26 #include "rrtplanner.h"
29 #define CATI(a, b) a ## b
30 #define CAT(a, b) CATI(a, b)
31 #define KUWATA2008_CCOST CAT(c, CO)
32 #define KUWATA2008_DCOST CO
34 LaValle1998::LaValle1998(RRTNode *init, RRTNode *goal):
37 srand(static_cast<unsigned>(time(0)));
40 bool LaValle1998::next()
44 if (this->samples().size() == 0)
51 this->samples().push_back(rs);
52 RRTNode *nn = this->nn(rs);
55 for (auto ns: this->steer(nn, rs)) {
59 this->nodes().push_back(ns);
61 pn->add_child(ns, this->cost(pn, ns));
62 if (this->collide(pn, ns)) {
63 pn->children().pop_back();
65 this->iy_[IYI(ns->y())].pop_back();
70 if (this->goal_found(pn, CO)) {
71 this->tlog(this->findt());
77 return this->goal_found();
80 Kuwata2008::Kuwata2008(RRTNode *init, RRTNode *goal):
83 srand(static_cast<unsigned>(time(0)));
86 bool Kuwata2008::next()
89 if (this->samples().size() == 0) {
94 this->samples().push_back(rs);
95 float heur = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
96 if (this->goal_found()) {
98 {}//this->cost = &KUWATA2008_CCOST;
100 {}//this->cost = &KUWATA2008_DCOST;
103 {}//this->cost = &KUWATA2008_CCOST;
105 {}//this->cost = &KUWATA2008_DCOST;
107 RRTNode *nn = this->nn(rs);
109 std::vector<RRTNode *> newly_added;
111 for (auto ns: this->steer(nn, rs)) {
115 this->nodes().push_back(ns);
117 pn->add_child(ns, KUWATA2008_DCOST(pn, ns));
118 if (this->collide(pn, ns)) {
119 pn->children().pop_back();
121 this->iy_[IYI(ns->y())].pop_back();
126 newly_added.push_back(pn);
127 if (this->goal_found(pn, &KUWATA2008_DCOST)) {
128 this->tlog(this->findt());
134 if (this->samples().size() <= 1)
135 return this->goal_found();
136 for (auto na: newly_added) {
139 for (auto ns: this->steer(na, this->goal())) {
143 this->nodes().push_back(ns);
145 pn->add_child(ns, KUWATA2008_DCOST(pn, ns));
146 if (this->collide(pn, ns)) {
147 pn->children().pop_back();
149 this->iy_[IYI(ns->y())].pop_back();
154 if (this->goal_found(pn,
155 &KUWATA2008_DCOST)) {
156 this->tlog(this->findt());
163 return this->goal_found();
166 Karaman2011::Karaman2011()
168 srand(static_cast<unsigned>(time(0)));
171 Karaman2011::Karaman2011(RRTNode *init, RRTNode *goal):
174 srand(static_cast<unsigned>(time(0)));
177 bool Karaman2011::next()
181 if (this->samples().size() == 0)
188 this->samples().push_back(rs);
189 RRTNode *nn = this->nn(rs);
191 std::vector<RRTNode *> nvs;
194 for (auto ns: this->steer(nn, rs)) {
197 } else if (IS_NEAR(nn, ns)) {
204 this->nodes().size()),
213 this->nodes().push_back(ns);
216 if (!this->connect(pn, ns, nvs)) {
217 this->iy_[IYI(ns->y())].pop_back();
221 this->rewire(nvs, ns);
223 if (this->goal_found(pn, CO)) {
224 this->tlog(this->findt());
230 return this->goal_found();
233 bool Karaman2011::connect(
236 std::vector<RRTNode *> nvs)
238 RRTNode *op; // old parent
239 float od; // old direct cost
240 float oc; // old cumulative cost
241 bool connected = false;
242 pn->add_child(ns, this->cost(pn, ns));
243 if (this->collide(pn, ns)) {
244 pn->children().pop_back();
251 if (!connected || (nv->ccost() + this->cost(nv, ns) <
256 nv->add_child(ns, this->cost(nv, ns));
257 if (this->collide(nv, ns)) {
258 nv->children().pop_back();
265 } else if (connected) {
266 op->children().pop_back();
276 bool Karaman2011::rewire(std::vector<RRTNode *> nvs, RRTNode *ns)
278 RRTNode *op; // old parent
279 float od; // old direct cost
280 float oc; // old cumulative cost
282 if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
286 ns->add_child(nv, this->cost(ns, nv));
287 if (this->collide(ns, nv)) {
288 ns->children().pop_back();
300 T1::T1(RRTNode *init, RRTNode *goal):
303 srand(static_cast<unsigned>(time(0)));
309 if (this->samples().size() == 0)
313 this->samples().push_back(rs);
314 RRTNode *nn = this->nn(rs);
316 std::vector<RRTNode *> nvs;
318 RRTNode *op; // old parent
319 float od; // old direct cost
320 float oc; // old cumulative cost
321 std::vector<RRTNode *> steered = this->steer(nn, rs);
322 // RRT* for first node
323 RRTNode *ns = steered[0];
325 nvs = this->nv(ns, MIN(
326 GAMMA_RRTSTAR(this->nodes().size()),
328 this->nodes().push_back(ns);
331 pn->add_child(ns, this->cost(pn, ns));
332 if (this->collide(pn, ns)) {
333 pn->children().pop_back();
339 if (!connected || (nv->ccost() + this->cost(nv, ns) <
344 nv->add_child(ns, this->cost(nv, ns));
345 if (this->collide(nv, ns)) {
346 nv->children().pop_back();
350 } else if (connected) {
351 op->children().pop_back();
361 if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
365 ns->add_child(nv, this->cost(ns, nv));
366 if (this->collide(ns, nv)) {
367 ns->children().pop_back();
377 if (this->goal_found(pn, CO)) {
378 this->tlog(this->findt());
382 for (i = 1; i < steered.size(); i++) {
384 this->nodes().push_back(ns);
386 pn->add_child(ns, this->cost(pn, ns));
387 if (this->collide(pn, ns)) {
388 pn->children().pop_back();
392 if (this->goal_found(pn, CO)) {
393 this->tlog(this->findt());
397 return this->goal_found();
404 if (this->samples().size() == 0)
411 this->samples().push_back(rs);
412 RRTNode *nn = this->nn(rs);
416 std::vector<RRTNode *> nvs;
417 std::vector<RRTNode *> newly_added;
420 for (auto ns: this->steer(nn, rs)) {
423 } else if (IS_NEAR(pn, ns)) {
426 if (sgn(ns->s()) == 0 || sgn(pn->s()) != sgn(ns->s()))
434 this->nodes().size()),
436 this->nodes().push_back(ns);
439 if (!this->connect(pn, ns, nvs)) {
440 this->iy_[IYI(ns->y())].pop_back();
441 this->nodes().pop_back();
447 this->rewire(nvs, ns);
449 newly_added.push_back(pn);
450 if (this->goal_found(pn, CO)) {
459 for (auto ns: this->steer(pn, rs, 0.01)) {
462 } else if (IS_NEAR(pn, ns)) {
465 if (sgn(ns->s()) == 0 || sgn(pn->s()) != sgn(ns->s()))
473 this->nodes().size()),
475 this->nodes().push_back(ns);
478 if (!this->connect(pn, ns, nvs)) {
479 this->iy_[IYI(ns->y())].pop_back();
480 this->nodes().pop_back();
486 this->rewire(nvs, ns);
488 newly_added.push_back(pn);
489 if (this->goal_found(pn, CO)) {
496 if (this->samples().size() <= 1)
497 return this->goal_found();
498 for (auto na: newly_added) {
502 for (auto ns: this->steer(na, this->goal())) {
505 } else if (IS_NEAR(pn, ns)) {
508 if (sgn(pn->s()) != sgn(ns->s()))
512 this->nodes().push_back(ns);
514 pn->add_child(ns, this->cost(pn, ns));
515 if (this->collide(pn, ns)) {
516 pn->children().pop_back();
518 this->iy_[IYI(ns->y())].pop_back();
519 this->nodes().pop_back();
526 if (this->goal_found(pn, CO)) {
535 for (auto ns: this->steer(pn, this->goal(), 0.01)) {
538 } else if (IS_NEAR(pn, ns)) {
541 if (sgn(pn->s()) != sgn(ns->s()))
545 this->nodes().push_back(ns);
547 pn->add_child(ns, this->cost(pn, ns));
548 if (this->collide(pn, ns)) {
549 pn->children().pop_back();
551 this->iy_[IYI(ns->y())].pop_back();
552 this->nodes().pop_back();
559 if (this->goal_found(pn, CO)) {
567 return this->goal_found();
570 float T2::goal_cost()
572 std::vector<RRTNode *> nvs;
573 nvs = this->nv(this->goal(), 0.2);
575 if (std::abs(this->goal()->h() - nv->h()) >=
576 this->GOAL_FOUND_ANGLE)
578 if (nv->ccost() + this->cost(nv, this->goal()) >=
579 this->goal()->ccost())
581 RRTNode *op; // old parent
582 float oc; // old cumulative cost
583 float od; // old direct cost
584 op = this->goal()->parent();
585 oc = this->goal()->ccost();
586 od = this->goal()->dcost();
587 nv->add_child(this->goal(),
588 this->cost(nv, this->goal()));
589 if (this->collide(nv, this->goal())) {
590 nv->children().pop_back();
591 this->goal()->parent(op);
592 this->goal()->ccost(oc);
593 this->goal()->dcost(od);
595 op->rem_child(this->goal());
598 return this->goal()->ccost();
603 for (auto n: this->p_root_.nodes())
604 if (n != this->p_root_.root() && n != this->p_root_.goal())
606 for (auto n: this->p_root_.dnodes())
607 if (n != this->p_root_.root() && n != this->p_root_.goal())
609 for (auto s: this->p_root_.samples())
610 if (s != this->p_root_.goal())
612 for (auto edges: this->p_root_.rlog())
616 for (auto n: this->p_goal_.nodes())
617 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
619 for (auto n: this->p_goal_.dnodes())
620 if (n != this->p_goal_.root() && n != this->p_goal_.goal())
622 for (auto s: this->p_goal_.samples())
623 if (s != this->p_goal_.goal())
625 for (auto edges: this->p_goal_.rlog())
629 for (auto n: this->nodes())
630 if (n != this->root())
632 for (auto n: this->dnodes())
633 if (n != this->root() && n != this->goal())
635 for (auto s: this->samples())
636 if (s != this->goal())
638 for (auto edges: this->rlog())
648 srand(static_cast<unsigned>(time(0)));
651 T3::T3(RRTNode *init, RRTNode *goal):
656 srand(static_cast<unsigned>(time(0)));
661 RRTNode *ron = nullptr;
662 RRTNode *gon = nullptr;
664 ret = this->p_root_.next();
665 ret |= this->p_goal_.next();
666 if (this->overlaptrees(&ron, &gon)) {
667 if (this->connecttrees(ron, gon))
668 this->goal_found(true);
669 this->tlog(this->findt());
675 bool T3::link_obstacles(
676 std::vector<CircleObstacle> *cobstacles,
677 std::vector<SegmentObstacle> *sobstacles)
680 ret = RRTBase::link_obstacles(cobstacles, sobstacles);
681 ret &= this->p_root_.link_obstacles(cobstacles, sobstacles);
682 ret &= this->p_goal_.link_obstacles(cobstacles, sobstacles);
686 bool T3::connecttrees(RRTNode *rn, RRTNode *gn)
688 while (gn != this->goal()) {
689 this->p_root_.nodes().push_back(new RRTNode(
694 this->p_root_.nodes().back(),
697 this->p_root_.nodes().back()));
698 rn = this->p_root_.nodes().back();
701 rn->add_child(this->goal(), this->p_root_.cost(rn, this->goal()));
705 bool T3::overlaptrees(RRTNode **ron, RRTNode **gon)
707 for (auto rn: this->p_root_.nodes()) {
708 if (rn->parent() == nullptr)
710 for (auto gn: this->p_goal_.nodes()) {
711 if (gn->parent() == nullptr)
713 if (IS_NEAR(rn, gn)) {
723 Klemm2015::Klemm2015(RRTNode *init, RRTNode *goal):
724 Karaman2011(init, goal),
728 srand(static_cast<unsigned>(time(0)));
729 this->root()->tree('R');
730 this->goal()->tree('G');
731 this->add_iy(this->goal());
734 bool Klemm2015::next()
736 RRTNode *xn = nullptr;
740 if (this->samples().size() == 0)
747 this->samples().push_back(rs);
748 //std::cerr << "next" << std::endl;
749 if (this->extendstar1(rs, &xn) != 2) {
751 // std::cerr << "- xn: " << xn->x() << ", " << xn->y();
752 // std::cerr << std::endl;
754 // std::cerr << "- xn: nullptr" << std::endl;
757 ret = this->connectstar(xn);
762 this->tlog(this->findt());
765 return this->goal_found();
768 int Klemm2015::extendstar1(RRTNode *rs, RRTNode **xn)
770 int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
771 char tree = this->root()->tree();
772 //std::cerr << "extend*1" << std::endl;
773 //std::cerr << "- tree is " << tree << std::endl;
774 //std::cerr << " - rs: " << rs->x() << ", " << rs->y() << std::endl;
776 // std::cerr << " - xn: " << (*xn)->x() << ", " << (*xn)->y();
777 // std::cerr << std::endl;
779 //for (int i = 0; i < IYSIZE; i++) {
780 // if (this->iy_[i].size() > 0) {
781 // RRTNode *tmpn = this->iy_[i].back();
782 // float tmpd = EDIST(tmpn, this->goal());
784 // std::cerr << i << ": " << tmpn->x();
785 // std::cerr << ", " << tmpn->y();
786 // std::cerr << ", " << tmpn->tree();
787 // std::cerr << " (" << tmpd << ")";
789 // if (tmpn == this->root())
790 // std::cerr << " root";
791 // if (tmpn == this->goal())
792 // std::cerr << " goal";
793 // std::cerr << std::endl;
796 RRTNode *nn = this->nn(rs);
797 //std::cerr << " - nn: " << nn->x() << ", " << nn->y() << std::endl;
798 std::vector<RRTNode *> nvs;
799 std::vector<RRTNode *> steered = this->steer(nn, rs);
800 RRTNode *ns = steered[1];
806 this->nodes().size()),
808 this->nodes().push_back(ns);
811 if (!this->connect(nn, ns, nvs)) {
812 this->iy_[IYI(ns->y())].pop_back();
816 this->rewire(nvs, ns);
818 for (auto n: steered) {
823 //std::cerr << " - xn: " << (*xn)->x() << ", " << (*xn)->y();
824 //std::cerr << std::endl;
828 int Klemm2015::extendstarC(RRTNode *rs)
830 int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
831 char tree = this->root()->tree();
832 //std::cerr << "extend*C" << std::endl;
833 //std::cerr << "- tree is " << tree << std::endl;
834 //std::cerr << " - rs: " << rs->x() << ", " << rs->y() << std::endl;
835 RRTNode *nn = this->nn(rs);
837 std::vector<RRTNode *> nvs;
839 for (auto ns: this->steer(nn, rs)) {
847 this->nodes().size()),
849 this->nodes().push_back(ns);
852 if (!this->connect(pn, ns, nvs)) {
853 this->iy_[IYI(ns->y())].pop_back();
858 this->rewire(nvs, ns);
860 if (IS_NEAR(pn, rs)) { // GOAL FOUND !
862 if (this->orig_root_ == this->root()) { // rs is in G tree
863 // add all rs parents to pn
865 } else { // rs is in R tree
870 while (tmp != this->goal()) {
871 this->nodes().push_back(new RRTNode(
875 this->nodes().back()->s(tmp->s());
876 this->nodes().back()->tree('R');
878 this->nodes().back(),
879 this->cost(pn, this->nodes().back()));
880 pn = this->nodes().back();
883 pn->add_child(tmp, this->cost(pn, tmp)); // add goal()
893 int Klemm2015::connectstar(RRTNode *x)
895 int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
896 //std::cerr << "connect* (start)" << std::endl;
897 ret = this->extendstarC(x);
898 //std::cerr << "connect* (end)" << std::endl;
902 void Klemm2015::swap()
906 this->root(this->goal());