Skip to content

Commit 2bacb75

Browse files
lianglia-apolloycool
authored andcommitted
Planning: added test for trajectory cost. Seperated comparable cost from trajectory cost.
1 parent ab40d3c commit 2bacb75

File tree

5 files changed

+245
-137
lines changed

5 files changed

+245
-137
lines changed

modules/planning/tasks/dp_poly_path/BUILD

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cc_library(
99
"trajectory_cost.cc",
1010
],
1111
hdrs = [
12+
"comparable_cost.h",
1213
"dp_road_graph.h",
1314
"trajectory_cost.h",
1415
],
@@ -28,7 +29,7 @@ cc_library(
2829
"//modules/planning/math/curve1d:quintic_polynomial_curve1d",
2930
"//modules/planning/proto:dp_poly_path_config_proto",
3031
"//modules/planning/reference_line",
31-
"@eigen//:eigen",
32+
"@eigen",
3233
],
3334
)
3435

@@ -46,6 +47,18 @@ cc_library(
4647
],
4748
)
4849

50+
cc_test(
51+
name = "comparable_cost_test",
52+
size = "small",
53+
srcs = [
54+
"comparable_cost_test.cc",
55+
],
56+
deps = [
57+
":dp_poly_path",
58+
"@gtest//:main",
59+
],
60+
)
61+
4962
cc_test(
5063
name = "trajectory_cost_test",
5164
size = "small",
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/******************************************************************************
2+
* Copyright 2017 The Apollo Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*****************************************************************************/
16+
17+
/**
18+
* @file
19+
**/
20+
21+
#ifndef MODULES_PLANNING_TASKS_DP_POLY_PATH_COMPARABLE_COST_H_
22+
#define MODULES_PLANNING_TASKS_DP_POLY_PATH_COMPARABLE_COST_H_
23+
24+
#include <cmath>
25+
#include <cstdlib>
26+
27+
#include <array>
28+
#include <vector>
29+
30+
namespace apollo {
31+
namespace planning {
32+
33+
class ComparableCost {
34+
public:
35+
ComparableCost() = default;
36+
ComparableCost(const bool has_collision, const bool out_of_boundary,
37+
const bool out_of_lane, const double safety_cost_,
38+
const double smoothness_cost_)
39+
: safety_cost(safety_cost_), smoothness_cost(smoothness_cost_) {
40+
cost_items[HAS_COLLISION] = has_collision;
41+
cost_items[OUT_OF_BOUNDARY] = out_of_boundary;
42+
cost_items[OUT_OF_LANE] = out_of_lane;
43+
}
44+
ComparableCost(const ComparableCost &) = default;
45+
46+
int CompareTo(const ComparableCost &other) const {
47+
for (size_t i = 0; i < cost_items.size(); ++i) {
48+
if (cost_items[i]) {
49+
if (other.cost_items[i]) {
50+
continue;
51+
} else {
52+
return 1;
53+
}
54+
} else {
55+
if (other.cost_items[i]) {
56+
return -1;
57+
} else {
58+
continue;
59+
}
60+
}
61+
}
62+
63+
constexpr double kEpsilon = 1e-12;
64+
const double diff = safety_cost + smoothness_cost - other.safety_cost -
65+
other.smoothness_cost;
66+
if (std::fabs(diff) < kEpsilon) {
67+
return 0;
68+
} else if (diff > 0) {
69+
return 1;
70+
} else {
71+
return -1;
72+
}
73+
}
74+
ComparableCost operator+(const ComparableCost &other) {
75+
ComparableCost lhs = *this;
76+
return lhs += other;
77+
}
78+
ComparableCost &operator+=(const ComparableCost &other) {
79+
for (size_t i = 0; i < cost_items.size(); ++i) {
80+
cost_items[i] = (cost_items[i] || other.cost_items[i]);
81+
}
82+
safety_cost += other.safety_cost;
83+
smoothness_cost += other.smoothness_cost;
84+
return *this;
85+
}
86+
bool operator>(const ComparableCost &other) const {
87+
return this->CompareTo(other) > 0;
88+
}
89+
bool operator>=(const ComparableCost &other) const {
90+
return this->CompareTo(other) >= 0;
91+
}
92+
bool operator<(const ComparableCost &other) const {
93+
return this->CompareTo(other) < 0;
94+
}
95+
bool operator<=(const ComparableCost &other) const {
96+
return this->CompareTo(other) <= 0;
97+
}
98+
/*
99+
* cost_items represents an array of factors that affect the cost,
100+
* The level is from most critical to less critical.
101+
* It includes:
102+
* (0) has_collision or out_of_boundary
103+
* (1) out_of_lane
104+
*
105+
* NOTICE: Items could have same critical levels
106+
*/
107+
static const size_t HAS_COLLISION = 0;
108+
static const size_t OUT_OF_BOUNDARY = 1;
109+
static const size_t OUT_OF_LANE = 2;
110+
std::array<bool, 3> cost_items = {{false, false, false}};
111+
112+
// cost from distance to obstacles or boundaries
113+
double safety_cost = 0.0;
114+
// cost from deviation from lane center, path curvature etc
115+
double smoothness_cost = 0.0;
116+
};
117+
118+
} // namespace planning
119+
} // namespace apollo
120+
121+
#endif // MODULES_PLANNING_TASKS_DP_POLY_PATH_COMPARABLE_COST_H_
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/******************************************************************************
2+
* Copyright 2017 The Apollo Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*****************************************************************************/
16+
17+
#include "modules/planning/tasks/dp_poly_path/comparable_cost.h"
18+
19+
#include "gtest/gtest.h"
20+
21+
namespace apollo {
22+
namespace planning {
23+
24+
TEST(ComparableCost, simple) {
25+
ComparableCost cc;
26+
EXPECT_DOUBLE_EQ(cc.safety_cost, 0.0);
27+
EXPECT_DOUBLE_EQ(cc.smoothness_cost, 0.0);
28+
EXPECT_FALSE(cc.cost_items[ComparableCost::HAS_COLLISION]);
29+
EXPECT_FALSE(cc.cost_items[ComparableCost::OUT_OF_BOUNDARY]);
30+
EXPECT_FALSE(cc.cost_items[ComparableCost::OUT_OF_LANE]);
31+
}
32+
33+
TEST(ComparableCost, add_cost) {
34+
ComparableCost cc1(true, false, false, 10.12, 2.51);
35+
ComparableCost cc2(false, false, true, 6.1, 3.45);
36+
37+
ComparableCost cc = cc1 + cc2;
38+
39+
EXPECT_TRUE(cc.cost_items[ComparableCost::HAS_COLLISION]);
40+
EXPECT_FALSE(cc.cost_items[ComparableCost::OUT_OF_BOUNDARY]);
41+
EXPECT_TRUE(cc.cost_items[ComparableCost::OUT_OF_LANE]);
42+
EXPECT_DOUBLE_EQ(cc.safety_cost, 16.22);
43+
EXPECT_DOUBLE_EQ(cc.smoothness_cost, 5.96);
44+
45+
EXPECT_TRUE(cc1 > cc2);
46+
47+
cc1 += cc2;
48+
49+
EXPECT_TRUE(cc1.cost_items[ComparableCost::HAS_COLLISION]);
50+
EXPECT_FALSE(cc1.cost_items[ComparableCost::OUT_OF_BOUNDARY]);
51+
EXPECT_TRUE(cc1.cost_items[ComparableCost::OUT_OF_LANE]);
52+
EXPECT_DOUBLE_EQ(cc1.safety_cost, 16.22);
53+
EXPECT_DOUBLE_EQ(cc1.smoothness_cost, 5.96);
54+
55+
ComparableCost cc3(true, false, false, 10.12, 2.51);
56+
ComparableCost cc4(false, true, true, 6.1, 3.45);
57+
58+
EXPECT_TRUE(cc3 > cc4);
59+
60+
ComparableCost cc5(false, false, false, 10.12, 2.51);
61+
ComparableCost cc6(false, true, true, 6.1, 3.45);
62+
63+
EXPECT_TRUE(cc5 < cc6);
64+
65+
ComparableCost cc7 = cc5 + cc6;
66+
67+
EXPECT_FALSE(cc7.cost_items[ComparableCost::HAS_COLLISION]);
68+
EXPECT_TRUE(cc7.cost_items[ComparableCost::OUT_OF_BOUNDARY]);
69+
EXPECT_TRUE(cc7.cost_items[ComparableCost::OUT_OF_LANE]);
70+
EXPECT_DOUBLE_EQ(cc7.safety_cost, 16.22);
71+
EXPECT_DOUBLE_EQ(cc7.smoothness_cost, 5.96);
72+
73+
EXPECT_TRUE(cc5 < cc6);
74+
}
75+
76+
} // namespace planning
77+
} // namespace apollo

modules/planning/tasks/dp_poly_path/trajectory_cost.h

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*****************************************************************************/
1616

1717
/**
18-
* @file trajectory_cost.h
18+
* @file
1919
**/
2020

2121
#ifndef MODULES_PLANNING_TASKS_DP_POLY_PATH_TRAJECTORY_COST_H_
@@ -32,97 +32,14 @@
3232
#include "modules/planning/common/speed/speed_data.h"
3333
#include "modules/planning/math/curve1d/quintic_polynomial_curve1d.h"
3434
#include "modules/planning/reference_line/reference_line.h"
35+
#include "modules/planning/tasks/dp_poly_path/comparable_cost.h"
3536

3637
namespace apollo {
3738
namespace planning {
3839

39-
class ComparableCost {
40-
public:
41-
ComparableCost() = default;
42-
ComparableCost(const bool has_collision, const bool out_of_boundary,
43-
const bool out_of_lane, const double safety_cost_,
44-
const double smoothness_cost_)
45-
: safety_cost(safety_cost_), smoothness_cost(smoothness_cost_) {
46-
cost_items[HAS_COLLISION] = has_collision;
47-
cost_items[OUT_OF_BOUNDARY] = out_of_boundary;
48-
cost_items[OUT_OF_LANE] = out_of_lane;
49-
}
50-
ComparableCost(const ComparableCost &) = default;
51-
52-
int CompareTo(const ComparableCost &other) const {
53-
for (size_t i = 0; i < cost_items.size(); ++i) {
54-
if (cost_items[i]) {
55-
if (other.cost_items[i]) {
56-
continue;
57-
} else {
58-
return 1;
59-
}
60-
} else {
61-
if (other.cost_items[i]) {
62-
return -1;
63-
} else {
64-
continue;
65-
}
66-
}
67-
}
68-
69-
constexpr double kEpsilon = 1e-12;
70-
const double diff = safety_cost + smoothness_cost - other.safety_cost -
71-
other.smoothness_cost;
72-
if (std::fabs(diff) < kEpsilon) {
73-
return 0;
74-
} else if (diff > 0) {
75-
return 1;
76-
} else {
77-
return -1;
78-
}
79-
}
80-
ComparableCost operator+(const ComparableCost &other) {
81-
ComparableCost lhs = *this;
82-
return lhs += other;
83-
}
84-
ComparableCost &operator+=(const ComparableCost &other) {
85-
for (size_t i = 0; i < cost_items.size(); ++i) {
86-
cost_items[i] = (cost_items[i] || other.cost_items[i]);
87-
}
88-
safety_cost += other.safety_cost;
89-
smoothness_cost += other.smoothness_cost;
90-
return *this;
91-
}
92-
bool operator>(const ComparableCost &other) const {
93-
return this->CompareTo(other) > 0;
94-
}
95-
bool operator>=(const ComparableCost &other) const {
96-
return this->CompareTo(other) >= 0;
97-
}
98-
bool operator<(const ComparableCost &other) const {
99-
return this->CompareTo(other) < 0;
100-
}
101-
bool operator<=(const ComparableCost &other) const {
102-
return this->CompareTo(other) <= 0;
103-
}
104-
/*
105-
* cost_items represents an array of factors that affect the cost,
106-
* The level is from most critical to less critical.
107-
* It includes:
108-
* (0) has_collision or out_of_boundary
109-
* (1) out_of_lane
110-
*
111-
* NOTICE: Items could have same critical levels
112-
*/
113-
static const size_t HAS_COLLISION = 0;
114-
static const size_t OUT_OF_BOUNDARY = 1;
115-
static const size_t OUT_OF_LANE = 2;
116-
std::array<bool, 3> cost_items = {{false, false, false}};
117-
118-
// cost from distance to obstacles or boundaries
119-
double safety_cost = 0.0;
120-
// cost from deviation from lane center, path curvature etc
121-
double smoothness_cost = 0.0;
122-
};
123-
12440
class TrajectoryCost {
12541
public:
42+
TrajectoryCost() = default;
12643
explicit TrajectoryCost(const DpPolyPathConfig &config,
12744
const ReferenceLine &reference_line,
12845
const bool is_change_lane_path,
@@ -150,6 +67,7 @@ class TrajectoryCost {
15067
const common::math::Box2d &ego_box,
15168
const common::math::Box2d &obstacle_box) const;
15269

70+
FRIEND_TEST(AllTrajectoryTests, GetCostFromObsSL);
15371
ComparableCost GetCostFromObsSL(const double adc_s, const double adc_l,
15472
const SLBoundary &obs_sl_boundary);
15573

0 commit comments

Comments
 (0)