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(RRTNode *init, RRTNode *goal):
169 srand(static_cast<unsigned>(time(0)));
172 bool Karaman2011::next()
176 if (this->samples().size() == 0)
183 this->samples().push_back(rs);
184 RRTNode *nn = this->nn(rs);
186 std::vector<RRTNode *> nvs;
188 for (auto ns: this->steer(nn, rs)) {
194 this->nodes().size()),
196 this->nodes().push_back(ns);
199 if (!this->connect(pn, ns, nvs)) {
200 this->iy_[IYI(ns->y())].pop_back();
204 this->rewire(nvs, ns);
206 if (this->goal_found(pn, CO)) {
207 this->tlog(this->findt());
213 return this->goal_found();
216 bool Karaman2011::connect(
219 std::vector<RRTNode *> nvs)
221 RRTNode *op; // old parent
222 float od; // old direct cost
223 float oc; // old cumulative cost
224 bool connected = false;
225 pn->add_child(ns, this->cost(pn, ns));
226 if (this->collide(pn, ns)) {
227 pn->children().pop_back();
234 if (!connected || (nv->ccost() + this->cost(nv, ns) <
239 nv->add_child(ns, this->cost(nv, ns));
240 if (this->collide(nv, ns)) {
241 nv->children().pop_back();
248 } else if (connected) {
249 op->children().pop_back();
259 bool Karaman2011::rewire(std::vector<RRTNode *> nvs, RRTNode *ns)
261 RRTNode *op; // old parent
262 float od; // old direct cost
263 float oc; // old cumulative cost
265 if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
269 ns->add_child(nv, this->cost(ns, nv));
270 if (this->collide(ns, nv)) {
271 ns->children().pop_back();
283 T1::T1(RRTNode *init, RRTNode *goal):
286 srand(static_cast<unsigned>(time(0)));
292 if (this->samples().size() == 0)
296 this->samples().push_back(rs);
297 RRTNode *nn = this->nn(rs);
299 std::vector<RRTNode *> nvs;
301 RRTNode *op; // old parent
302 float od; // old direct cost
303 float oc; // old cumulative cost
304 std::vector<RRTNode *> steered = this->steer(nn, rs);
305 // RRT* for first node
306 RRTNode *ns = steered[0];
308 nvs = this->nv(ns, MIN(
309 GAMMA_RRTSTAR(this->nodes().size()),
311 this->nodes().push_back(ns);
314 pn->add_child(ns, this->cost(pn, ns));
315 if (this->collide(pn, ns)) {
316 pn->children().pop_back();
322 if (!connected || (nv->ccost() + this->cost(nv, ns) <
327 nv->add_child(ns, this->cost(nv, ns));
328 if (this->collide(nv, ns)) {
329 nv->children().pop_back();
333 } else if (connected) {
334 op->children().pop_back();
344 if (ns->ccost() + this->cost(ns, nv) < nv->ccost()) {
348 ns->add_child(nv, this->cost(ns, nv));
349 if (this->collide(ns, nv)) {
350 ns->children().pop_back();
360 if (this->goal_found(pn, CO)) {
361 this->tlog(this->findt());
365 for (i = 1; i < steered.size(); i++) {
367 this->nodes().push_back(ns);
369 pn->add_child(ns, this->cost(pn, ns));
370 if (this->collide(pn, ns)) {
371 pn->children().pop_back();
375 if (this->goal_found(pn, CO)) {
376 this->tlog(this->findt());
380 return this->goal_found();
387 if (this->samples().size() == 0)
394 this->samples().push_back(rs);
395 RRTNode *nn = this->nn(rs);
399 std::vector<RRTNode *> nvs;
400 std::vector<RRTNode *> newly_added;
403 for (auto ns: this->steer(nn, rs)) {
406 } else if (IS_NEAR(pn, ns)) {
409 if (sgn(ns->s()) == 0 || sgn(pn->s()) != sgn(ns->s()))
417 this->nodes().size()),
419 this->nodes().push_back(ns);
422 if (!this->connect(pn, ns, nvs)) {
423 this->iy_[IYI(ns->y())].pop_back();
427 this->rewire(nvs, ns);
429 newly_added.push_back(pn);
430 if (this->goal_found(pn, CO)) {
432 this->tlog(this->findt());
438 if (this->samples().size() <= 1)
439 return this->goal_found();
440 for (auto na: newly_added) {
444 for (auto ns: this->steer(na, this->goal())) {
447 } else if (IS_NEAR(pn, ns)) {
450 if (sgn(pn->s()) != sgn(ns->s()))
454 this->nodes().push_back(ns);
456 pn->add_child(ns, this->cost(pn, ns));
457 if (this->collide(pn, ns)) {
458 pn->children().pop_back();
460 this->iy_[IYI(ns->y())].pop_back();
465 if (this->goal_found(pn, CO)) {
467 this->tlog(this->findt());
474 return this->goal_found();
477 float T2::goal_cost()
479 std::vector<RRTNode *> nvs;
480 nvs = this->nv(this->goal(), 0.2);
482 if (std::abs(this->goal()->h() - nv->h()) >=
483 this->GOAL_FOUND_ANGLE)
485 if (nv->ccost() + this->cost(nv, this->goal()) >=
486 this->goal()->ccost())
488 RRTNode *op; // old parent
489 float oc; // old cumulative cost
490 float od; // old direct cost
491 op = this->goal()->parent();
492 oc = this->goal()->ccost();
493 od = this->goal()->dcost();
494 nv->add_child(this->goal(),
495 this->cost(nv, this->goal()));
496 if (this->collide(nv, this->goal())) {
497 nv->children().pop_back();
498 this->goal()->parent(op);
499 this->goal()->ccost(oc);
500 this->goal()->dcost(od);
502 op->rem_child(this->goal());
505 return this->goal()->ccost();
508 T3::T3(RRTNode *init, RRTNode *goal):
513 srand(static_cast<unsigned>(time(0)));
518 RRTNode *ron = nullptr;
519 RRTNode *gon = nullptr;
521 ret = this->p_root_.next();
522 ret &= this->p_goal_.next();
523 if (this->overlaptrees(&ron, &gon)) {
524 if (this->connecttrees(ron, gon))
525 this->goal_found(true);
526 this->tlog(this->findt());
532 bool T3::link_obstacles(
533 std::vector<CircleObstacle> *cobstacles,
534 std::vector<SegmentObstacle> *sobstacles)
537 ret = RRTBase::link_obstacles(cobstacles, sobstacles);
538 ret &= this->p_root_.link_obstacles(cobstacles, sobstacles);
539 ret &= this->p_goal_.link_obstacles(cobstacles, sobstacles);
543 bool T3::connecttrees(RRTNode *rn, RRTNode *gn)
545 while (gn != this->goal()) {
546 this->p_root_.nodes().push_back(new RRTNode(
551 this->p_root_.nodes().back(),
554 this->p_root_.nodes().back()));
555 rn = this->p_root_.nodes().back();
558 rn->add_child(this->goal(), this->p_root_.cost(rn, this->goal()));
562 bool T3::overlaptrees(RRTNode **ron, RRTNode **gon)
564 for (auto rn: this->p_root_.nodes()) {
565 if (rn->parent() == nullptr)
567 for (auto gn: this->p_goal_.nodes()) {
568 if (gn->parent() == nullptr)
570 if (IS_NEAR(rn, gn)) {
580 Klemm2015::Klemm2015(RRTNode *init, RRTNode *goal):
581 Karaman2011(init, goal),
585 srand(static_cast<unsigned>(time(0)));
586 this->root()->tree('R');
587 this->goal()->tree('G');
588 this->add_iy(this->goal());
591 bool Klemm2015::next()
593 RRTNode *xn = nullptr;
597 if (this->samples().size() == 0)
604 this->samples().push_back(rs);
605 //std::cerr << "next" << std::endl;
606 if (this->extendstar1(rs, &xn) != 2) {
608 // std::cerr << "- xn: " << xn->x() << ", " << xn->y();
609 // std::cerr << std::endl;
611 // std::cerr << "- xn: nullptr" << std::endl;
614 ret = this->connectstar(xn);
619 this->tlog(this->findt());
622 return this->goal_found();
625 int Klemm2015::extendstar1(RRTNode *rs, RRTNode **xn)
627 int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
628 char tree = this->root()->tree();
629 //std::cerr << "extend*1" << std::endl;
630 //std::cerr << "- tree is " << tree << std::endl;
631 //std::cerr << " - rs: " << rs->x() << ", " << rs->y() << std::endl;
633 // std::cerr << " - xn: " << (*xn)->x() << ", " << (*xn)->y();
634 // std::cerr << std::endl;
636 //for (int i = 0; i < IYSIZE; i++) {
637 // if (this->iy_[i].size() > 0) {
638 // RRTNode *tmpn = this->iy_[i].back();
639 // float tmpd = EDIST(tmpn, this->goal());
641 // std::cerr << i << ": " << tmpn->x();
642 // std::cerr << ", " << tmpn->y();
643 // std::cerr << ", " << tmpn->tree();
644 // std::cerr << " (" << tmpd << ")";
646 // if (tmpn == this->root())
647 // std::cerr << " root";
648 // if (tmpn == this->goal())
649 // std::cerr << " goal";
650 // std::cerr << std::endl;
653 RRTNode *nn = this->nn(rs);
654 //std::cerr << " - nn: " << nn->x() << ", " << nn->y() << std::endl;
655 std::vector<RRTNode *> nvs;
656 std::vector<RRTNode *> steered = this->steer(nn, rs);
657 RRTNode *ns = steered[1];
663 this->nodes().size()),
665 this->nodes().push_back(ns);
668 if (!this->connect(nn, ns, nvs)) {
669 this->iy_[IYI(ns->y())].pop_back();
673 this->rewire(nvs, ns);
675 for (auto n: steered) {
680 //std::cerr << " - xn: " << (*xn)->x() << ", " << (*xn)->y();
681 //std::cerr << std::endl;
685 int Klemm2015::extendstarC(RRTNode *rs)
687 int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
688 char tree = this->root()->tree();
689 //std::cerr << "extend*C" << std::endl;
690 //std::cerr << "- tree is " << tree << std::endl;
691 //std::cerr << " - rs: " << rs->x() << ", " << rs->y() << std::endl;
692 RRTNode *nn = this->nn(rs);
694 std::vector<RRTNode *> nvs;
696 for (auto ns: this->steer(nn, rs)) {
704 this->nodes().size()),
706 this->nodes().push_back(ns);
709 if (!this->connect(pn, ns, nvs)) {
710 this->iy_[IYI(ns->y())].pop_back();
715 this->rewire(nvs, ns);
717 if (IS_NEAR(pn, rs)) { // GOAL FOUND !
719 if (this->orig_root_ == this->root()) { // rs is in G tree
720 // add all rs parents to pn
722 } else { // rs is in R tree
727 while (tmp != this->goal()) {
728 this->nodes().push_back(new RRTNode(
732 this->nodes().back()->s(tmp->s());
733 this->nodes().back()->tree('R');
735 this->nodes().back(),
736 this->cost(pn, this->nodes().back()));
737 pn = this->nodes().back();
740 pn->add_child(tmp, this->cost(pn, tmp)); // add goal()
750 int Klemm2015::connectstar(RRTNode *x)
752 int ret = 0; // 0 - advanced, 1 - reached, 2 - trapped
753 //std::cerr << "connect* (start)" << std::endl;
754 ret = this->extendstarC(x);
755 //std::cerr << "connect* (end)" << std::endl;
759 void Klemm2015::swap()
763 this->root(this->goal());