10 this->tstart_ = std::chrono::high_resolution_clock::now();
16 using namespace std::chrono;
17 auto t = high_resolution_clock::now() - this->tstart_;
18 auto d = duration_cast<duration<double>>(t);
31 assert(this->p_ != nullptr);
33 this->cc_ = this->p_->cc() + c;
49 RRTNode::p(RRTNode& p)
57 RRTNode::operator==(RRTNode const& n)
63 RRTS::recompute_cc(RRTNode* g)
66 while (g != nullptr) {
67 this->path_.push_back(g);
70 std::reverse(this->path_.begin(), this->path_.end());
71 for (unsigned int i = 1; i < this->path_.size(); i++) {
72 this->path_[i]->c(this->cost_build(*this->path_[i - 1],
78 RRTS::recompute_path_cc()
80 this->recompute_cc(&this->goal_);
84 RRTS::min_gamma_eta() const
86 double ns = this->nodes_.size();
87 double gamma = pow(log(ns) / ns, 1.0 / 3.0);
88 return std::min(gamma, this->eta_);
92 RRTS::should_continue() const
94 return !this->should_finish();
98 RRTS::join_steered(RRTNode* f)
100 while (this->steered_.size() > 0) {
101 this->store(this->steered_.front());
102 RRTNode* t = &this->nodes_.back();
104 t->c(this->cost_build(*f, *t));
105 this->steered_.erase(this->steered_.begin());
119 RRTNode* f = this->nn_;
120 RRTNode* t = &this->steered_.front();
121 double cost = f->cc() + this->cost_build(*f, *t);
122 for (auto n: this->nv_) {
123 double nc = n->cc() + this->cost_build(*n, *t);
129 // Check if it's possible to drive from *f to *t. If not, then fallback
130 // to *f = nn_. This could be also solved by additional steer from *f to
131 // *t instead of the following code.
132 this->bc_.set_pose(*f);
133 if (!this->bc_.drivable(*t)) {
136 this->store(this->steered_.front());
137 t = &this->nodes_.back();
139 t->c(this->cost_build(*f, *t));
140 this->steered_.erase(this->steered_.begin());
147 RRTNode *f = &this->nodes_.back();
148 for (auto n: this->nv_) {
149 double fc = f->cc() + this->cost_build(*f, *n);
150 this->bc_.set_pose(*f);
151 bool drivable = this->bc_.drivable(*n);
152 if (drivable && fc < n->cc()) {
154 n->c(this->cost_build(*f, *n));
160 RRTS::goal_drivable_from(RRTNode const& f)
162 this->bc_.set_pose(f);
163 return this->bc_.drivable(this->goal_);
167 RRTS::store(RRTNode n)
169 this->nodes_.push_back(n);
173 RRTS::cost_build(RRTNode const& f, RRTNode const& t) const
179 RRTS::cost_search(RRTNode const& f, RRTNode const& t) const
181 return this->cost_build(f, t);
185 RRTS::find_nn(RRTNode const& t)
187 this->nn_ = &this->nodes_.front();
188 this->cost_ = this->cost_search(*this->nn_, t);
189 for (auto& f: this->nodes_) {
190 if (this->cost_search(f, t) < this->cost_) {
192 this->cost_ = this->cost_search(f, t);
198 RRTS::find_nv(RRTNode const& t)
201 this->cost_ = this->min_gamma_eta();
202 for (auto& f: this->nodes_) {
203 if (this->cost_search(f, t) < this->cost_) {
204 this->nv_.push_back(&f);
213 RRTNode *g = &this->goal_;
214 if (g->p() == nullptr) {
217 while (g != nullptr && this->path_.size() < 10000) {
220 * There shouldn't be this->path_.size() < 10000 condition.
221 * However, the RRTS::compute_path() called from
222 * RRTExt13::compute_path tends to re-allocate this->path_
223 * infinitely. There's probably node->p() = &node somewhere...
225 this->path_.push_back(g);
228 std::reverse(this->path_.begin(), this->path_.end());
231 RRTS::RRTS() : gen_(std::random_device{}()), goal_(0.0, 0.0, 0.0, 0.0)
233 this->nodes_.reserve(4000000);
234 this->steered_.reserve(1000);
235 this->path_.reserve(10000);
236 this->nv_.reserve(1000);
237 this->store(RRTNode()); // root
247 RRTS::icnt(unsigned int i)
255 return this->ter_.scnt();
263 for (auto n: this->path_) {
264 jvo["path"][i][0] = n->x();
265 jvo["path"][i][1] = n->y();
266 jvo["path"][i][2] = n->h();
269 jvo["goal_cc"] = this->goal_.cc();
270 jvo["time"] = this->time_;
275 RRTS::json(Json::Value jvi)
277 assert(jvi["init"] != Json::nullValue);
278 assert(jvi["goal"] != Json::nullValue);
279 assert(jvi["obst"] != Json::nullValue);
280 this->nodes_.front().x(jvi["init"][0].asDouble());
281 this->nodes_.front().y(jvi["init"][1].asDouble());
282 this->nodes_.front().h(jvi["init"][2].asDouble());
283 if (jvi["goal"].size() == 4) {
284 this->goal_ = RRTGoal(jvi["goal"][0].asDouble(),
285 jvi["goal"][1].asDouble(),
286 jvi["goal"][2].asDouble(),
287 jvi["goal"][3].asDouble());
289 this->goal_ = RRTGoal(jvi["goal"][0].asDouble(),
290 jvi["goal"][1].asDouble(),
291 jvi["goal"][2].asDouble(),
292 jvi["goal"][2].asDouble());
299 if (this->icnt_ == 0) {
303 auto rs = this->sample();
304 #if 1 // anytime RRTs
306 double d1 = this->cost_search(this->nodes_.front(), rs);
307 double d2 = this->cost_search(rs, this->goal_);
308 if (this->last_goal_cc_ != 0.0 && d1 + d2 > this->last_goal_cc_) {
309 rs = this->last_path_[rand() % this->last_path_.size()];
314 this->steer(this->nn(), rs);
315 if (this->collide_steered()) {
316 return this->should_continue();
318 this->find_nv(this->steered_.front());
319 if (!this->connect()) {
320 return this->should_continue();
323 unsigned int ss = this->steered_.size();
324 this->join_steered(&this->nodes_.back());
325 RRTNode* just_added = &this->nodes_.back();
327 while (ss > 0 && just_added->p() != nullptr) {
328 this->steer(*just_added, this->goal_);
329 if (this->collide_steered()) {
331 just_added = just_added->p();
334 this->join_steered(just_added);
335 bool gn = this->goal_.edist(this->nodes_.back()) < this->eta_;
336 bool gd = this->goal_drivable_from(this->nodes_.back());
338 double nc = this->cost_build(this->nodes_.back(),
340 double ncc = this->nodes_.back().cc() + nc;
341 if (this->goal_.p() == nullptr
342 || ncc < this->goal_.cc()) {
343 this->goal_.p(this->nodes_.back());
349 just_added = just_added->p();
352 this->compute_path();
354 this->time_ = this->ter_.scnt();
355 return this->should_continue();
361 if (this->goal_.cc() != 0.0 && this->goal_.cc() < this->last_goal_cc_) {
362 this->last_goal_cc_ = this->goal_.cc();
363 this->last_path_.clear();
364 for (auto n: this->path_) {
365 this->last_path_.push_back(*n);
368 this->goal_ = RRTGoal(this->goal_.x(), this->goal_.y(), this->goal_.b(),
371 this->steered_.clear();
372 this->nodes_.erase(this->nodes_.begin() + 1, this->nodes_.end());