]> rtime.felk.cvut.cz Git - hubacji1/iamcar2.git/blob - rrts/src/rrts.cc
Fix car dimension settings in test template
[hubacji1/iamcar2.git] / rrts / src / rrts.cc
1 /*
2  * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
3  *
4  * SPDX-License-Identifier: GPL-3.0-only
5  */
6
7 #include <algorithm>
8 #include <cassert>
9 #include "rrts.hh"
10
11 #ifndef USE_RRTS
12 #define USE_RRTS 0  // TODO improve, this solution isn't clear.
13 #endif
14
15 namespace rrts {
16
17 void
18 Ter::start()
19 {
20         this->_tstart = std::chrono::high_resolution_clock::now();
21 }
22
23 double
24 Ter::scnt() const
25 {
26         auto t = std::chrono::high_resolution_clock::now() - this->_tstart;
27         auto d = std::chrono::duration_cast<std::chrono::seconds>(t);
28         return d.count();
29 }
30
31 double
32 RRTNode::c() const
33 {
34         return this->_c;
35 }
36
37 void
38 RRTNode::c(double c)
39 {
40         assert(this->_p != nullptr);
41         this->_c = c;
42         this->_cc = this->_p->cc() + c;
43 }
44
45 double
46 RRTNode::cc() const
47 {
48         return this->_cc;
49 }
50
51 RRTNode*
52 RRTNode::p() const
53 {
54         return this->_p;
55 }
56
57 void
58 RRTNode::p(RRTNode& p)
59 {
60         if (this != &p) {
61                 this->_p = &p;
62         }
63 }
64
65 unsigned int
66 RRTNode::cusp() const
67 {
68         return this->_cusp;
69 }
70
71 void
72 RRTNode::cusp(RRTNode const& p)
73 {
74         this->_cusp = p.cusp();
75         if (this->sp() != p.sp() || this->sp() == 0.0) {
76                 this->_cusp++;
77         }
78 }
79
80 int
81 RRTNode::st(void)
82 {
83         return this->_segment_type;
84 }
85
86 void
87 RRTNode::st(int st)
88 {
89         this->_segment_type = st;
90 }
91
92 bool
93 RRTNode::operator==(RRTNode const& n)
94 {
95         return this == &n;
96 }
97
98 void
99 RRTS::recompute_cc_for_predecessors_and(RRTNode* g)
100 {
101         assert(this->_path.size() == 0);
102         while (g != nullptr) {
103                 this->_path.push_back(g);
104                 g = g->p();
105         }
106         std::reverse(this->_path.begin(), this->_path.end());
107         for (unsigned int i = 1; i < this->_path.size(); i++) {
108                 this->_path[i]->c(this->cost_build(
109                         *this->_path[i - 1],
110                         *this->_path[i]));
111         }
112         this->_path.clear();
113 }
114
115 void
116 RRTS::recompute_path_cc()
117 {
118         this->recompute_cc_for_predecessors_and(&this->_goal);
119 }
120
121 double
122 RRTS::min_gamma_eta() const
123 {
124         double ns = this->_nodes.size();
125         double gamma = pow(log(ns) / ns, 1.0 / 3.0);
126         return std::min(gamma, this->eta());
127 }
128
129 bool
130 RRTS::should_continue() const
131 {
132         return !this->should_finish();
133 }
134
135 void
136 RRTS::join_steered(RRTNode* f)
137 {
138         while (this->_steered.size() > 0) {
139                 this->store(this->_steered.front());
140                 RRTNode* t = &this->_nodes.back();
141                 t->p(*f);
142                 t->c(this->cost_build(*f, *t));
143                 t->cusp(*f);
144                 this->_steered.erase(this->_steered.begin());
145                 f = t;
146         }
147 }
148
149 bool
150 RRTS::connect()
151 {
152         RRTNode* f = this->_nn;
153         RRTNode* t = &this->_steered.front();
154 #if USE_RRTS
155         double cost = f->cc() + this->cost_build(*f, *t);
156         for (auto n: this->nv_) {
157                 double nc = n->cc() + this->cost_build(*n, *t);
158                 if (nc < cost) {
159                         f = n;
160                         cost = nc;
161                 }
162         }
163         // Check if it's possible to drive from *f to *t. If not, then fallback
164         // to *f = _nn. This could be also solved by additional steer from *f to
165         // *t instead of the following code.
166         this->set_bc_pose_to(*f);
167         if (!this->_bc.drivable(*t)) {
168                 f = this->_nn;
169         }
170 #endif
171         this->store(this->_steered.front());
172         t = &this->_nodes.back();
173         t->p(*f);
174         t->c(this->cost_build(*f, *t));
175         t->cusp(*f);
176         this->_steered.erase(this->_steered.begin());
177         return true;
178 }
179
180 void
181 RRTS::rewire()
182 {
183         RRTNode *f = &this->_nodes.back();
184         for (auto n: this->_nv) {
185                 double fc = f->cc() + this->cost_build(*f, *n);
186                 this->set_bc_pose_to(*f);
187                 bool drivable = this->_bc.drivable(*n);
188                 if (drivable && fc < n->cc()) {
189                         n->p(*f);
190                         n->c(this->cost_build(*f, *n));
191                 }
192         }
193 }
194
195 bool
196 RRTS::goal_drivable_from(RRTNode const& f)
197 {
198         this->set_bc_pose_to(f);
199         return this->_bc.drivable(this->_goal);
200 }
201
202 void
203 RRTS::store(RRTNode n)
204 {
205         this->_nodes.push_back(n);
206 }
207
208 double
209 RRTS::cost_build(RRTNode const& f, RRTNode const& t) const
210 {
211         return f.edist(t);
212 }
213
214 double
215 RRTS::cost_search(RRTNode const& f, RRTNode const& t) const
216 {
217         return this->cost_build(f, t);
218 }
219
220 void
221 RRTS::find_nn(RRTNode const& t)
222 {
223         this->_nn = &this->_nodes.front();
224         this->_cost = this->cost_search(*this->_nn, t);
225         for (auto& f: this->_nodes) {
226                 if (this->cost_search(f, t) < this->_cost) {
227                         this->_nn = &f;
228                         this->_cost = this->cost_search(f, t);
229                 }
230         }
231 }
232
233 void
234 RRTS::find_nv(RRTNode const& t)
235 {
236         this->_nv.clear();
237         this->_cost = this->min_gamma_eta();
238         for (auto& f: this->_nodes) {
239                 if (this->cost_search(f, t) < this->_cost) {
240                         this->_nv.push_back(&f);
241                 }
242         }
243 }
244
245 void
246 RRTS::compute_path()
247 {
248         this->_path.clear();
249         RRTNode *g = &this->_goal;
250         if (g->p() == nullptr) {
251                 return;
252         }
253         while (g != nullptr && this->_path.size() < 10000) {
254                 /* FIXME in ext13
255                  *
256                  * There shouldn't be this->_path.size() < 10000 condition.
257                  * However, the RRTS::compute_path() called from
258                  * RRTExt13::compute_path tends to re-allocate this->_path
259                  * infinitely. There's probably node->p() = &node somewhere...
260                  */
261                 this->_path.push_back(g);
262                 g = g->p();
263         }
264         std::reverse(this->_path.begin(), this->_path.end());
265 }
266
267 RRTS::RRTS() : _goal(0.0, 0.0, 0.0, 0.0), _gen(std::random_device{}())
268 {
269         this->_nodes.reserve(4000000);
270         this->_steered.reserve(1000);
271         this->_path.reserve(10000);
272         this->_nv.reserve(1000);
273         this->store(RRTNode()); // root
274 }
275
276 void
277 RRTS::set_bc_pose_to(Pose const& p)
278 {
279         this->_bc.set_pose_to(p);
280 }
281
282 void
283 RRTS::set_bc_to_become(std::string what)
284 {
285         this->_bc.become(what);
286 }
287
288 RRTGoal const&
289 RRTS::goal(void) const
290 {
291         return this->_goal;
292 }
293
294 void
295 RRTS::goal(double x, double y, double b, double e)
296 {
297         this->_goal = RRTGoal(x, y, b, e);
298 }
299
300 unsigned int
301 RRTS::icnt(void) const
302 {
303         return this->_icnt;
304 }
305
306 void
307 RRTS::icnt(unsigned int i)
308 {
309         this->_icnt = i;
310 }
311
312 unsigned int
313 RRTS::icnt_max(void) const
314 {
315         return this->_icnt_max;
316 }
317
318 void
319 RRTS::icnt_max(unsigned int i)
320 {
321         this->_icnt_max = i;
322 }
323
324 void
325 RRTS::tstart(void)
326 {
327         this->_ter.start();
328 }
329
330 double
331 RRTS::scnt() const
332 {
333         return this->_ter.scnt();
334 }
335
336 void
337 RRTS::set_init_pose_to(Pose const& p)
338 {
339         this->_nodes.front().x(p.x());
340         this->_nodes.front().y(p.y());
341         this->_nodes.front().h(p.h());
342 }
343
344 std::vector<Pose>
345 RRTS::path() const
346 {
347         std::vector<Pose> path;
348         for (auto n: this->_path) {
349                 path.push_back(Pose(n->x(), n->y(), n->h()));
350         }
351         return path;
352 }
353
354 double
355 RRTS::path_cost() const
356 {
357         return this->_goal.cc();
358 }
359
360 double
361 RRTS::last_path_cost(void) const
362 {
363         if (this->_logged_paths.size() == 0) {
364                 return 0.0;
365         }
366         assert(this->_logged_paths.back().size() > 0);
367         return this->_logged_paths.back().back().cc();
368 }
369
370 double
371 RRTS::eta() const
372 {
373         return this->_eta;
374 }
375
376 void
377 RRTS::eta(double e)
378 {
379         this->_eta = e;
380 }
381
382 Json::Value
383 RRTS::json(void) const
384 {
385         Json::Value jvo;
386         unsigned int i = 0, j = 0;
387         for (auto path: this->_logged_paths) {
388                 i = 0;
389                 for (auto n: path) {
390                         jvo["paths"][j][i][0] = n.x();
391                         jvo["paths"][j][i][1] = n.y();
392                         jvo["paths"][j][i][2] = n.h();
393                         jvo["paths"][j][i][3] = n.sp();
394                         jvo["paths"][j][i][4] = n.st();
395                         i++;
396                 }
397                 jvo["costs"][j] = path.back().cc();
398                 j++;
399         }
400         i = 0;
401         for (auto n: this->_path) {
402                 jvo["paths"][j][i][0] = n->x();
403                 jvo["paths"][j][i][1] = n->y();
404                 jvo["paths"][j][i][2] = n->h();
405                 jvo["paths"][j][i][3] = n->sp();
406                 jvo["paths"][j][i][4] = n->st();
407                 jvo["path"][i][0] = n->x();
408                 jvo["path"][i][1] = n->y();
409                 jvo["path"][i][2] = n->h();
410                 jvo["path"][i][3] = n->sp();
411                 jvo["path"][i][4] = n->st();
412                 i++;
413         }
414         jvo["costs"][j] = this->_path.back()->cc();
415         j++;
416         jvo["goal_cc"] = this->_goal.cc(); // TODO remove, use the following
417         jvo["cost"] = this->path_cost();
418         jvo["time"] = this->scnt();
419         return jvo;
420 }
421
422 void
423 RRTS::json(Json::Value jvi)
424 {
425         assert(jvi["init"] != Json::nullValue);
426         assert(jvi["goal"] != Json::nullValue);
427         this->set_init_pose_to(Pose(
428                 jvi["init"][0].asDouble(),
429                 jvi["init"][1].asDouble(),
430                 jvi["init"][2].asDouble()));
431         if (jvi["goal"].size() == 4) {
432                 this->goal(
433                         jvi["goal"][0].asDouble(),
434                         jvi["goal"][1].asDouble(),
435                         jvi["goal"][2].asDouble(),
436                         jvi["goal"][3].asDouble());
437         } else {
438                 this->goal(
439                         jvi["goal"][0].asDouble(),
440                         jvi["goal"][1].asDouble(),
441                         jvi["goal"][2].asDouble(),
442                         jvi["goal"][2].asDouble());
443         }
444 }
445
446 bool
447 RRTS::next()
448 {
449         if (this->icnt() == 0) {
450                 this->tstart();
451         }
452         auto rs = this->sample();
453 #if 1 // anytime RRTs
454 {
455         double d1 = this->cost_search(this->_nodes.front(), rs);
456         double d2 = this->cost_search(rs, this->_goal);
457         if (this->last_path_cost() != 0.0 && d1 + d2 > this->last_path_cost()) {
458                 auto& last_path = this->_logged_paths.back();
459                 rs = last_path[rand() % last_path.size()];
460         }
461 }
462 #endif
463         this->find_nn(rs);
464         this->steer(*this->_nn, rs);
465         if (this->collide_steered()) {
466                 return this->should_continue();
467         }
468 #if USE_RRTS
469         this->find_nv(this->_steered.front());
470 #endif
471         if (!this->connect()) {
472                 return this->should_continue();
473         }
474 #if USE_RRTS
475         this->rewire();
476 #endif
477         unsigned int ss = this->_steered.size();
478         this->join_steered(&this->_nodes.back());
479         RRTNode* just_added = &this->_nodes.back();
480         bool gf = false;
481         while (ss > 0 && just_added->p() != nullptr) {
482                 this->steer(*just_added, this->_goal);
483                 if (this->collide_steered()) {
484                         ss--;
485                         just_added = just_added->p();
486                         continue;
487                 }
488                 this->join_steered(just_added);
489                 bool gn = this->_goal.edist(this->_nodes.back()) < this->eta();
490                 bool gd = this->goal_drivable_from(this->_nodes.back());
491                 if (gn && gd) {
492                         double nc = this->cost_build(
493                                 this->_nodes.back(),
494                                 this->_goal);
495                         double ncc = this->_nodes.back().cc() + nc;
496                         if (this->_goal.p() == nullptr
497                                         || ncc < this->_goal.cc()) {
498                                 this->_goal.p(this->_nodes.back());
499                                 this->_goal.c(nc);
500                                 gf = true;
501                         }
502                 }
503                 ss--;
504                 just_added = just_added->p();
505         }
506         if (gf) {
507                 this->compute_path();
508         }
509         this->_time = this->scnt();
510         this->icnt(this->icnt() + 1);
511         return this->should_continue();
512 }
513
514 void
515 RRTS::reset()
516 {
517         if (this->path_cost() != 0.0
518                         && this->path_cost() < this->last_path_cost()) {
519                 this->_logged_paths.push_back(std::vector<RRTNode>());
520                 auto& last_path = this->_logged_paths.back();
521                 last_path.reserve(this->_path.size());
522                 RRTNode* p = nullptr;
523                 for (auto n: this->_path) {
524                         last_path.push_back(*n);
525                         if (p != nullptr) {
526                                 last_path.back().p(*p);
527                         }
528                         p = &last_path.back();
529                 }
530                 // Test that last path cost matches.
531                 auto last_path_cost = last_path.back().cc();
532                 for (unsigned int i = 1; i < last_path.size(); i++) {
533                         last_path[i].c(this->cost_build(
534                                 last_path[i - 1],
535                                 last_path[i]));
536                 }
537                 assert(last_path_cost == last_path.back().cc());
538         }
539         this->_goal = RRTGoal(
540                 this->_goal.x(),
541                 this->_goal.y(),
542                 this->_goal.b(),
543                 this->_goal.e());
544         this->_path.clear();
545         this->_steered.clear();
546         this->_nodes.erase(this->_nodes.begin() + 1, this->_nodes.end());
547         this->_nv.clear();
548         this->_nn = nullptr;
549         this->_bc = BicycleCar();
550 }
551
552 } // namespace rrts