-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
gcs_trajectory_optimization_test.cc
722 lines (606 loc) · 36.6 KB
/
gcs_trajectory_optimization_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
#include "drake/planning/trajectory_optimization/gcs_trajectory_optimization.h"
#include <gtest/gtest.h>
#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/common/test_utilities/expect_no_throw.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/geometry/optimization/graph_of_convex_sets.h"
#include "drake/geometry/optimization/hpolyhedron.h"
#include "drake/geometry/optimization/point.h"
#include "drake/geometry/optimization/vpolytope.h"
#include "drake/solvers/gurobi_solver.h"
#include "drake/solvers/mosek_solver.h"
namespace drake {
namespace planning {
namespace trajectory_optimization {
namespace {
using Eigen::Vector2d;
using Eigen::Vector3d;
using geometry::optimization::ConvexSets;
using geometry::optimization::GraphOfConvexSetsOptions;
using geometry::optimization::HPolyhedron;
using geometry::optimization::MakeConvexSets;
using geometry::optimization::Point;
using geometry::optimization::VPolytope;
bool GurobiOrMosekSolverAvailable() {
return (solvers::MosekSolver::is_available() &&
solvers::MosekSolver::is_enabled()) ||
(solvers::GurobiSolver::is_available() &&
solvers::GurobiSolver::is_enabled());
}
GTEST_TEST(GcsTrajectoryOptimizationTest, Basic) {
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
// Add a single region (the unit box), and plan a line segment inside that
// box.
Vector2d start(-0.5, -0.5), goal(0.5, 0.5);
auto& regions =
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeUnitBox(kDimension)), 1);
auto& source = gcs.AddRegions(MakeConvexSets(Point(start)), 0);
auto& target = gcs.AddRegions(MakeConvexSets(Point(goal)), 0);
gcs.AddEdges(source, regions);
gcs.AddEdges(regions, target);
auto [traj, result] = gcs.SolvePath(source, target);
EXPECT_TRUE(result.is_success());
EXPECT_EQ(traj.rows(), 2);
EXPECT_EQ(traj.cols(), 1);
EXPECT_TRUE(CompareMatrices(traj.value(traj.start_time()), start, 1e-6));
EXPECT_TRUE(CompareMatrices(traj.value(traj.end_time()), goal, 1e-6));
}
GTEST_TEST(GcsTrajectoryOptimizationTest, MinimumTimeVsPathLength) {
/* This simple 2D example will show the difference between the minimum time
and minimum path length objectives. Bob wants to get from start (S) to goal
(G). Bob can either walk or take an e-scooter. While the e-scooter is faster
(10 m/s in x and y), it can't drive through gravel. However, Bob can walk at 1
m/s (in x and y) through gravel. Bob wants to find the fastest path from S to
G.
░ - wall
▒ - grave
░░░░░░░░░░░░░░░░░░░░░░░░
░░ ░░
░░ ░░
░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░
░░ S ▒▒▒▒▒▒▒▒ G ░░
░░ ▒▒▒▒▒▒▒▒ ░░
░░░░░░░░░░░░░░░░░░░░░░░░
*/
const int kDimension = 2;
const double kWalkingSpeed = 1;
const double kScooterSpeed = 10;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
Vector2d start(-0.5, 0), goal(0.5, 0);
auto& walking_regions =
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeBox(Vector2d(-0.5, -0.5),
Vector2d(0.5, 0.5))),
3);
// Bob can walk at 1 m/s in x and y.
walking_regions.AddVelocityBounds(Vector2d(-kWalkingSpeed, -kWalkingSpeed),
Vector2d(kWalkingSpeed, kWalkingSpeed));
auto& scooter_regions = gcs.AddRegions(
MakeConvexSets(
HPolyhedron::MakeBox(Vector2d(-0.5, -0.5), Vector2d(-0.2, 1)),
HPolyhedron::MakeBox(Vector2d(-0.5, 1), Vector2d(0.5, 1.5)),
HPolyhedron::MakeBox(Vector2d(0.2, -0.5), Vector2d(0.5, 1))),
3);
// Bob can ride an e-scooter at 10 m/s in x and y.
scooter_regions.AddVelocityBounds(Vector2d(-kScooterSpeed, -kScooterSpeed),
Vector2d(kScooterSpeed, kScooterSpeed));
auto& source = gcs.AddRegions(MakeConvexSets(Point(start)), 0);
auto& target = gcs.AddRegions(MakeConvexSets(Point(goal)), 0);
gcs.AddEdges(source, walking_regions);
gcs.AddEdges(walking_regions, target);
gcs.AddEdges(source, scooter_regions);
gcs.AddEdges(scooter_regions, target);
// Add shortest path objective to compare against the minimum time objective.
gcs.AddPathLengthCost();
if (!GurobiOrMosekSolverAvailable()) {
return;
}
auto [shortest_path_traj, shortest_path_result] =
gcs.SolvePath(source, target);
EXPECT_TRUE(shortest_path_result.is_success());
EXPECT_EQ(shortest_path_traj.rows(), 2);
EXPECT_EQ(shortest_path_traj.cols(), 1);
EXPECT_TRUE(CompareMatrices(
shortest_path_traj.value(shortest_path_traj.start_time()), start, 1e-6));
EXPECT_TRUE(CompareMatrices(
shortest_path_traj.value(shortest_path_traj.end_time()), goal, 1e-6));
// We expect the shortest path to be a straight line from S to G, going
// through the gravel. Thus we expect the number of segments in the trajectory
// to be 3 (one for start, one for the walking_regions and one for the goal)
// and the path length to be 1.
EXPECT_EQ(shortest_path_traj.get_number_of_segments(), 3);
const double kExpectedShortestPathLength = 1.0;
double shortest_path_length = 0.0;
const double dt = 0.01; // Time step for numerical integration.
for (double t = shortest_path_traj.start_time();
t < shortest_path_traj.end_time(); t += dt) {
const double t_next = std::min(t + dt, shortest_path_traj.end_time());
const double dx =
(shortest_path_traj.value(t_next) - shortest_path_traj.value(t)).norm();
shortest_path_length += dx;
}
EXPECT_NEAR(shortest_path_length, kExpectedShortestPathLength, 1e-6);
const double shortest_path_duration =
shortest_path_traj.end_time() - shortest_path_traj.start_time();
// Now we want to find the fastest path from S to G.
// Add minimum time objective with a very high weight to overpower the
// shortest path objective.
gcs.AddTimeCost(1e6);
auto [fastest_path_traj, fastest_path_result] = gcs.SolvePath(source, target);
EXPECT_TRUE(fastest_path_result.is_success());
EXPECT_EQ(fastest_path_traj.rows(), 2);
EXPECT_EQ(fastest_path_traj.cols(), 1);
EXPECT_TRUE(CompareMatrices(
fastest_path_traj.value(fastest_path_traj.start_time()), start, 1e-6));
EXPECT_TRUE(CompareMatrices(
fastest_path_traj.value(fastest_path_traj.end_time()), goal, 1e-6));
// We expect the fastest path to take a detour and avoid the going through the
// gravel. Thus we expect the number of segments in the trajectory to be 5
// (one for start, three for the scooter_regions and one for the goal) and the
// path length to be greater than 2.4 (the minimum path length for the
// detour).
EXPECT_EQ(fastest_path_traj.get_number_of_segments(), 5);
const double kLeastExpectedFastestPathLength = 2.4;
double fastest_path_length = 0.0;
// During the path integration, we will also check that the velocity bounds
// are consistently hit.
auto fastest_path_velocity = fastest_path_traj.MakeDerivative();
for (double t = fastest_path_traj.start_time();
t < fastest_path_traj.end_time(); t += dt) {
const double t_next = std::min(t + dt, fastest_path_traj.end_time());
const double dx =
(fastest_path_traj.value(t_next) - fastest_path_traj.value(t)).norm();
// We also expect the fastest path to consistently hit either of the
// velocity bounds.
// TODO(wrangelvid) as we add the start and end points as curves with
// order zero to the composite trajectory, the velocity is not computed
// properly.
if (fastest_path_traj.start_time() < t &&
t < fastest_path_traj.end_time()) {
EXPECT_TRUE((fastest_path_velocity->value(t).cwiseAbs() -
Vector2d(kScooterSpeed, kScooterSpeed))
.cwiseAbs()
.minCoeff() < 1e-6);
}
fastest_path_length += dx;
}
EXPECT_TRUE(fastest_path_length > kLeastExpectedFastestPathLength);
// We expect the fastest path to be longer than the shortest path.
EXPECT_TRUE(fastest_path_length > shortest_path_length);
// The fastest path should be faster than the shortest path.
const double fastest_path_duration =
fastest_path_traj.end_time() - fastest_path_traj.start_time();
EXPECT_TRUE(fastest_path_duration < shortest_path_duration);
}
GTEST_TEST(GcsTrajectoryOptimizationTest, InvalidPositions) {
/* Positions passed into GcsTrajectoryOptimization must be greater than 0.*/
DRAKE_EXPECT_THROWS_MESSAGE(GcsTrajectoryOptimization(0),
".*num_positions.*");
DRAKE_EXPECT_THROWS_MESSAGE(GcsTrajectoryOptimization(-1),
".*num_positions.*");
DRAKE_EXPECT_NO_THROW(GcsTrajectoryOptimization(1));
}
GTEST_TEST(GcsTrajectoryOptimizationTest, InvalidOrder) {
/* Subgraph order must be non-negative.*/
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
DRAKE_EXPECT_THROWS_MESSAGE(
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeUnitBox(kDimension)), -1),
".*order.*");
DRAKE_EXPECT_NO_THROW(
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeUnitBox(kDimension)), 0));
}
GTEST_TEST(GcsTrajectoryOptimizationTest, ZeroOrderPathLengthCost) {
/* Path length cost is not defined for a set of order 0.*/
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
auto& regions =
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeUnitBox(kDimension)), 0);
DRAKE_EXPECT_THROWS_MESSAGE(
regions.AddPathLengthCost(),
"Path length cost is not defined for a set of order 0.");
// This should consider the order of the subgraphs and not add the path length
// cost to the regions.
DRAKE_EXPECT_NO_THROW(gcs.AddPathLengthCost());
}
GTEST_TEST(GcsTrajectoryOptimizationTest, InvalidVelocityBounds) {
/* The velocity of a curve is not defined for a subgraph of order 0.*/
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
auto& regions =
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeUnitBox(kDimension)), 0);
DRAKE_EXPECT_THROWS_MESSAGE(
regions.AddVelocityBounds(Vector2d::Zero(), Vector2d::Zero()),
"Velocity Bounds are not defined for a set of order 0.");
// This should consider the order of the subgraphs and not add the velocity
// bounds to the regions.
DRAKE_EXPECT_NO_THROW(
gcs.AddVelocityBounds(Vector2d::Zero(), Vector2d::Zero()));
// lower and upper bound must have the same dimension as the graph.
DRAKE_EXPECT_THROWS_MESSAGE(
regions.AddVelocityBounds(Vector3d::Zero(), Vector2d::Zero()),
".*size.*.num_positions.*");
DRAKE_EXPECT_THROWS_MESSAGE(
gcs.AddVelocityBounds(Vector2d::Zero(), Vector3d::Zero()),
".*size.*.num_positions.*");
}
GTEST_TEST(GcsTrajectoryOptimizationTest, DisjointGraph) {
/* Adds four points to the graph without any edges. */
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
Vector2d start1(0.2, 0.2), start2(0.3, 3.2);
Vector2d goal1(4.8, 4.8), goal2(4.9, 2.4);
auto& source =
gcs.AddRegions(MakeConvexSets(Point(start1), Point(start2)), 0);
auto& target = gcs.AddRegions(MakeConvexSets(Point(goal1), Point(goal2)), 0);
if (!GurobiOrMosekSolverAvailable()) {
return;
}
// Define solver options.
GraphOfConvexSetsOptions options;
options.max_rounded_paths = 3;
auto [traj, result] = gcs.SolvePath(source, target, options);
EXPECT_FALSE(result.is_success());
// TODO(wrangelvid) Add a better way to check if composite trajectory is
// empty.
DRAKE_EXPECT_THROWS_MESSAGE(traj.rows(), ".*no segments.*");
DRAKE_EXPECT_THROWS_MESSAGE(traj.cols(), ".*no segments.*");
}
GTEST_TEST(GcsTrajectoryOptimizationTest, InvalidSubspace) {
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
auto& regions1 =
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeUnitBox(kDimension)), 0);
auto& regions2 =
gcs.AddRegions(MakeConvexSets(HPolyhedron::MakeUnitBox(kDimension)), 0);
auto subspace_wrong_dimension = HPolyhedron::MakeUnitBox(kDimension + 1);
DRAKE_EXPECT_THROWS_MESSAGE(
gcs.AddEdges(regions1, regions2, &subspace_wrong_dimension),
"Subspace dimension must match the number of positions.");
Eigen::Matrix<double, 2, 4> vertices;
vertices << 5.0, 5.0, 4.4, 4.4, 2.8, 5.0, 5.0, 2.8;
VPolytope vertices_subspace{vertices};
DRAKE_EXPECT_THROWS_MESSAGE(
gcs.AddEdges(regions1, regions2, &vertices_subspace),
"Subspace must be a Point or HPolyhedron.");
}
/* This 2D environment has been presented in the GCS paper.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░ ░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░ G ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░░░░░░ ░░░░░░░░░░░░░░░░░░░
░░ ░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░
░░ S ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
class SimpleEnv2D : public ::testing::Test {
protected:
SimpleEnv2D() {
Eigen::Matrix<double, 2, 4> vertices_1;
// clang-format off
vertices_1 << 0.4, 0.4, 0.0, 0.0,
0.0, 5.0, 5.0, 0.0;
// clang-format on
HPolyhedron region_1{VPolytope(vertices_1)};
Eigen::Matrix<double, 2, 4> vertices_2;
// clang-format off
vertices_2 << 0.4, 1.0, 1.0, 0.4,
2.4, 2.4, 2.6, 2.6;
// clang-format on
HPolyhedron region_2{VPolytope(vertices_2)};
Eigen::Matrix<double, 2, 4> vertices_3;
// clang-format off
vertices_3 << 1.4, 1.4, 1.0, 1.0,
2.2, 4.6, 4.6, 2.2;
// clang-format on
HPolyhedron region_3{VPolytope(vertices_3)};
Eigen::Matrix<double, 2, 4> vertices_4;
// clang-format off
vertices_4 << 1.4, 2.4, 2.4, 1.4,
2.2, 2.6, 2.8, 2.8;
// clang-format on
HPolyhedron region_4{VPolytope(vertices_4)};
Eigen::Matrix<double, 2, 4> vertices_5;
// clang-format off
vertices_5 << 2.2, 2.4, 2.4, 2.2,
2.8, 2.8, 4.6, 4.6;
// clang-format on
HPolyhedron region_5{VPolytope(vertices_5)};
Eigen::Matrix<double, 2, 5> vertices_6;
// clang-format off
vertices_6 << 1.4, 1., 1., 3.8, 3.8,
2.2, 2.2, 0.0, 0.0, 0.2;
// clang-format on
HPolyhedron region_6{VPolytope(vertices_6)};
Eigen::Matrix<double, 2, 4> vertices_7;
// clang-format off
vertices_7 << 3.8, 3.8, 1.0, 1.0,
4.6, 5.0, 5.0, 4.6;
// clang-format on
HPolyhedron region_7{VPolytope(vertices_7)};
Eigen::Matrix<double, 2, 5> vertices_8;
// clang-format off
vertices_8 << 5.0, 5.0, 4.8, 3.8, 3.8,
0.0, 1.2, 1.2, 0.2, 0.0;
// clang-format on
HPolyhedron region_8{VPolytope(vertices_8)};
Eigen::Matrix<double, 2, 4> vertices_9;
// clang-format off
vertices_9 << 3.4, 4.8, 5.0, 5.0,
2.6, 1.2, 1.2, 2.6;
// clang-format on
HPolyhedron region_9{VPolytope(vertices_9)};
Eigen::Matrix<double, 2, 4> vertices_10;
// clang-format off
vertices_10 << 3.4, 3.8, 3.8, 3.4,
2.6, 2.6, 4.6, 4.6;
// clang-format on
HPolyhedron region_10{VPolytope(vertices_10)};
Eigen::Matrix<double, 2, 4> vertices_11;
// clang-format off
vertices_11 << 3.8, 4.4, 4.4, 3.8,
2.8, 2.8, 3.0, 3.0;
// clang-format on
HPolyhedron region_11{VPolytope(vertices_11)};
Eigen::Matrix<double, 2, 4> vertices_12;
// clang-format off
vertices_12 << 5.0, 5.0, 4.4, 4.4,
2.8, 5.0, 5.0, 2.8;
// clang-format on
HPolyhedron region_12{VPolytope(vertices_12)};
regions_ = MakeConvexSets(region_1, region_2, region_3, region_4, region_5,
region_6, region_7, region_8, region_9, region_10,
region_11, region_12);
}
ConvexSets regions_;
};
TEST_F(SimpleEnv2D, BasicShortestPath) {
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
Vector2d start(0.2, 0.2), goal(4.8, 4.8);
auto& regions = gcs.AddRegions(regions_, 1);
auto& source = gcs.AddRegions(MakeConvexSets(Point(start)), 0);
auto& target = gcs.AddRegions(MakeConvexSets(Point(goal)), 0);
gcs.AddEdges(source, regions);
gcs.AddEdges(regions, target);
gcs.AddPathLengthCost();
if (!GurobiOrMosekSolverAvailable()) {
return;
}
// Define solver options.
GraphOfConvexSetsOptions options;
options.max_rounded_paths = 3;
auto [traj, result] = gcs.SolvePath(source, target, options);
EXPECT_TRUE(result.is_success());
EXPECT_TRUE(CompareMatrices(traj.value(traj.start_time()), start, 1e-6));
EXPECT_TRUE(CompareMatrices(traj.value(traj.end_time()), goal, 1e-6));
}
TEST_F(SimpleEnv2D, DurationDelay) {
/* The durations bounds in a subgraph can be used to enforce delays.*/
const int kDimension = 2;
const double kStartDelay = 10;
const double kGoalDelay = 20;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
// Add path length cost to whole graph before adding regions.
// New added regions will inherit this cost.
gcs.AddPathLengthCost();
Vector2d start(0.2, 0.2), goal(4.8, 4.8);
auto& regions = gcs.AddRegions(regions_, 1);
// Setting h_min = h_max = kStartDelay seconds will force to stay a the start
// of the trajectory for kStartDelay seconds.
auto& source =
gcs.AddRegions(MakeConvexSets(Point(start)), 0, kStartDelay, kStartDelay);
// Similarly for the goal, but with a delay of kGoalDelay seconds.
auto& target =
gcs.AddRegions(MakeConvexSets(Point(goal)), 0, kGoalDelay, kGoalDelay);
gcs.AddEdges(source, regions);
gcs.AddEdges(regions, target);
if (!GurobiOrMosekSolverAvailable()) {
return;
}
// Define solver options.
GraphOfConvexSetsOptions options;
options.max_rounded_paths = 3;
auto [traj, result] = gcs.SolvePath(source, target, options);
EXPECT_TRUE(result.is_success());
// The trajectory should stay at the start for kStartDelay seconds.
EXPECT_TRUE(CompareMatrices(traj.value(traj.start_time()), start, 1e-6));
EXPECT_TRUE(CompareMatrices(traj.value(kStartDelay), start, 1e-6));
// The trajectory should stay at the goal for kGaolDelay seconds.
EXPECT_TRUE(CompareMatrices(traj.value(traj.end_time()), goal, 1e-6));
EXPECT_TRUE(
CompareMatrices(traj.value(traj.end_time() - kGoalDelay), goal, 1e-6));
// The total trajectory duration should be at least kStartDelay + kGoalDelay.
EXPECT_TRUE(traj.end_time() - traj.start_time() >= kStartDelay + kGoalDelay);
}
TEST_F(SimpleEnv2D, MultiStartGoal) {
/* Planning between subgraphs also enables to select the shortest path
between multiple start and goal points.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░ ░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░ G1 ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ S2 ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░░░░░░ ░░░░░░░░░░░░░░░░░░░
░░ ░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░░░░░░░░░░░░░░░░░░ G2 ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░
░░ S1 ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
Vector2d start1(0.2, 0.2), start2(0.3, 3.2);
Vector2d goal1(4.8, 4.8), goal2(4.9, 2.4);
const double kSpeed = 1.0;
auto& regions = gcs.AddRegions(regions_, 3);
auto& source =
gcs.AddRegions(MakeConvexSets(Point(start1), Point(start2)), 0);
auto& target = gcs.AddRegions(MakeConvexSets(Point(goal1), Point(goal2)), 0);
gcs.AddEdges(source, regions);
gcs.AddEdges(regions, target);
gcs.AddVelocityBounds(Vector2d(-kSpeed, -kSpeed), Vector2d(kSpeed, kSpeed));
gcs.AddPathLengthCost();
gcs.AddTimeCost();
if (!GurobiOrMosekSolverAvailable()) {
return;
}
// Define solver options.
GraphOfConvexSetsOptions options;
options.max_rounded_paths = 3;
auto [traj, result] = gcs.SolvePath(source, target, options);
EXPECT_TRUE(result.is_success());
EXPECT_TRUE(CompareMatrices(traj.value(traj.start_time()), start2, 1e-6));
EXPECT_TRUE(CompareMatrices(traj.value(traj.end_time()), goal2, 1e-6));
}
TEST_F(SimpleEnv2D, IntermediatePoint) {
/* Connecting two subgraphs with a subspace enables start to
goal planning with a desired intermediate point, I, somewhere along the path
or with a point contained in the subspace (lower right corner).
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
░░ ░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░ G ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ I ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░ ░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░░░░░░ ░░░░░░░░░░░░░░░░░░░
░░ ░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ ░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░xxxxxx░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░░░░░xxxxxxxx░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░░░░░xxxxxxxxxx░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░░░░░xxxxxxxxxxxx░░
░░ ░░░░░░░░ ░░░░░░░░░░░░░xxx Subspace x░░
░░ ░░░░░░░░ xxxxxxxxxxxxxxxx░░
░░ S ░░░░░░░░ xxxxxxxxxxxxxxxx░░
░░ ░░░░░░░░ xxxxxxxxxxxxxxxx░░
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/
const int kDimension = 2;
GcsTrajectoryOptimization gcs(kDimension);
EXPECT_EQ(gcs.num_positions(), kDimension);
Vector2d start(0.2, 0.2), goal(4.8, 4.8), intermediate(2.3, 3.5);
const double kSpeed = 1.0;
// Both a Point and an HPolytope are valid subspaces.
Point subspace_point(intermediate);
Eigen::Matrix<double, 2, 5> vertices;
// clang-format off
vertices << 5.0, 5.0, 4.8, 3.8, 3.8,
0.0, 1.2, 1.2, 0.2, 0.0;
// clang-format on
HPolyhedron subspace_region{VPolytope(vertices)};
// We can have different order subgraphs.
// The trajectory from start to either subspace or intermidiate point will be
// of order 3 and the trajectory from intermediate to goal will be of order 2.
auto& main1 = gcs.AddRegions(regions_, 3, 1e-6, 20, "main1");
auto& main2 = gcs.AddRegions(regions_, 2, 1e-6, 20, "main2");
auto& source = gcs.AddRegions(MakeConvexSets(Point(start)), 0);
auto& target = gcs.AddRegions(MakeConvexSets(Point(goal)), 0);
// The following wiring will give GCS the choice to either go
// through subspace point or the subspace region.
gcs.AddEdges(source, main1);
// Connect the two subgraphs through the intermediate point.
gcs.AddEdges(main1, main2, &subspace_point);
// Connect the two subgraphs through the subspace region.
gcs.AddEdges(main1, main2, &subspace_region);
gcs.AddEdges(main2, target);
// We can add different costs and constraints to the individual subgraphs.
main1.AddPathLengthCost(5);
main1.AddTimeCost(1);
main1.AddVelocityBounds(Vector2d(-kSpeed, -kSpeed), Vector2d(kSpeed, kSpeed));
// This weight matrix penalizes movement in the y direction three times more
// than in the x direction.
Eigen::MatrixXd weight_matrix =
Eigen::MatrixXd::Identity(gcs.num_positions(), gcs.num_positions());
weight_matrix(1, 1) = 3.0;
main2.AddPathLengthCost(weight_matrix);
if (!GurobiOrMosekSolverAvailable()) {
return;
}
// Define solver options.
GraphOfConvexSetsOptions options;
options.max_rounded_paths = 3;
auto [traj, result] = gcs.SolvePath(source, target, options);
EXPECT_TRUE(result.is_success());
EXPECT_TRUE(CompareMatrices(traj.value(traj.start_time()), start, 1e-6));
EXPECT_TRUE(CompareMatrices(traj.value(traj.end_time()), goal, 1e-6));
}
} // namespace
} // namespace trajectory_optimization
} // namespace planning
} // namespace drake