Apollo 10.0
自动驾驶开放平台
grid_search.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 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
22
23namespace apollo {
24namespace planning {
25
27
29 xy_grid_resolution_ =
31 node_radius_ =
32 open_space_conf.warm_start_config().node_radius();
33}
34
35double GridSearch::EuclidDistance(
36 const double x1, const double y1, const double x2, const double y2) {
37 return std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
38}
39
40bool GridSearch::CheckConstraints(std::shared_ptr<Node2d> node) {
41 const double node_grid_x = node->GetGridX();
42 const double node_grid_y = node->GetGridY();
43 if (node_grid_x > max_grid_x_ ||
44 node_grid_x < 0 ||
45 node_grid_y > max_grid_y_ ||
46 node_grid_y < 0) {
47 return false;
48 }
49 if (obstacles_linesegments_vec_.empty()) {
50 return true;
51 }
52 for (const auto& obstacle_linesegments : obstacles_linesegments_vec_) {
53 for (const common::math::LineSegment2d& linesegment :
54 obstacle_linesegments) {
55 if (linesegment.DistanceTo({node->GetGridX(), node->GetGridY()})
56 < node_radius_) {
57 return false;
58 }
59 }
60 }
61 return true;
62}
63
64std::vector<std::shared_ptr<Node2d>> GridSearch::GenerateNextNodes(
65 std::shared_ptr<Node2d> current_node) {
66 double current_node_x = current_node->GetGridX();
67 double current_node_y = current_node->GetGridY();
68 double current_node_path_cost = current_node->GetPathCost();
69 double diagonal_distance = std::sqrt(2.0);
70 std::vector<std::shared_ptr<Node2d>> next_nodes;
71 std::shared_ptr<Node2d> up =
72 std::make_shared<Node2d>(current_node_x, current_node_y + 1.0, XYbounds_);
73 up->SetPathCost(current_node_path_cost + 1.0);
74 std::shared_ptr<Node2d> up_right = std::make_shared<Node2d>(
75 current_node_x + 1.0, current_node_y + 1.0, XYbounds_);
76 up_right->SetPathCost(current_node_path_cost + diagonal_distance);
77 std::shared_ptr<Node2d> right =
78 std::make_shared<Node2d>(current_node_x + 1.0, current_node_y, XYbounds_);
79 right->SetPathCost(current_node_path_cost + 1.0);
80 std::shared_ptr<Node2d> down_right = std::make_shared<Node2d>(
81 current_node_x + 1.0, current_node_y - 1.0, XYbounds_);
82 down_right->SetPathCost(current_node_path_cost + diagonal_distance);
83 std::shared_ptr<Node2d> down =
84 std::make_shared<Node2d>(current_node_x, current_node_y - 1.0, XYbounds_);
85 down->SetPathCost(current_node_path_cost + 1.0);
86 std::shared_ptr<Node2d> down_left = std::make_shared<Node2d>(
87 current_node_x - 1.0, current_node_y - 1.0, XYbounds_);
88 down_left->SetPathCost(current_node_path_cost + diagonal_distance);
89 std::shared_ptr<Node2d> left =
90 std::make_shared<Node2d>(current_node_x - 1.0, current_node_y, XYbounds_);
91 left->SetPathCost(current_node_path_cost + 1.0);
92 std::shared_ptr<Node2d> up_left = std::make_shared<Node2d>(
93 current_node_x - 1.0, current_node_y + 1.0, XYbounds_);
94 up_left->SetPathCost(current_node_path_cost + diagonal_distance);
95
96 next_nodes.emplace_back(up);
97 next_nodes.emplace_back(up_right);
98 next_nodes.emplace_back(right);
99 next_nodes.emplace_back(down_right);
100 next_nodes.emplace_back(down);
101 next_nodes.emplace_back(down_left);
102 next_nodes.emplace_back(left);
103 next_nodes.emplace_back(up_left);
104 return next_nodes;
105}
106
108 const double sx, const double sy, const double ex, const double ey,
109 const std::vector<double>& XYbounds,
110 const std::vector<std::vector<common::math::LineSegment2d>>&
111 obstacles_linesegments_vec,
112 GridAStartResult* result) {
113 std::priority_queue<std::pair<std::string, double>,
114 std::vector<std::pair<std::string, double>>, cmp>
115 open_pq;
116 std::unordered_map<std::string, std::shared_ptr<Node2d>> open_set;
117 std::unordered_map<std::string, std::shared_ptr<Node2d>> close_set;
118 XYbounds_ = XYbounds;
119 std::shared_ptr<Node2d> start_node =
120 std::make_shared<Node2d>(sx, sy, xy_grid_resolution_, XYbounds_);
121 std::shared_ptr<Node2d> end_node =
122 std::make_shared<Node2d>(ex, ey, xy_grid_resolution_, XYbounds_);
123 std::shared_ptr<Node2d> final_node_ = nullptr;
124 obstacles_linesegments_vec_ = obstacles_linesegments_vec;
125 open_set.emplace(start_node->GetIndex(), start_node);
126 open_pq.emplace(start_node->GetIndex(), start_node->GetCost());
127
128 // Grid a star begins
129 size_t explored_node_num = 0;
130 while (!open_pq.empty()) {
131 std::string current_id = open_pq.top().first;
132 open_pq.pop();
133 std::shared_ptr<Node2d> current_node = open_set[current_id];
134 // Check destination
135 if (*(current_node) == *(end_node)) {
136 final_node_ = current_node;
137 break;
138 }
139 close_set.emplace(current_node->GetIndex(), current_node);
140 std::vector<std::shared_ptr<Node2d>> next_nodes =
141 std::move(GenerateNextNodes(current_node));
142 for (auto& next_node : next_nodes) {
143 if (!CheckConstraints(next_node)) {
144 continue;
145 }
146 if (close_set.find(next_node->GetIndex()) != close_set.end()) {
147 continue;
148 }
149 if (open_set.find(next_node->GetIndex()) == open_set.end()) {
150 ++explored_node_num;
151 next_node->SetHeuristic(
152 EuclidDistance(next_node->GetGridX(), next_node->GetGridY(),
153 end_node->GetGridX(), end_node->GetGridY()));
154 next_node->SetPreNode(current_node);
155 open_set.emplace(next_node->GetIndex(), next_node);
156 open_pq.emplace(next_node->GetIndex(), next_node->GetCost());
157 }
158 }
159 }
160
161 if (final_node_ == nullptr) {
162 AERROR << "Grid A searching return null ptr(open_set ran out)";
163 return false;
164 }
165 LoadGridAStarResult(result);
166 ADEBUG << "explored node num is " << explored_node_num;
167 return true;
168}
169
171 const double ex,
172 const double ey,
173 const std::vector<double>& XYbounds,
174 const std::vector<std::vector<common::math::LineSegment2d>>&
175 obstacles_linesegments_vec,
176 const std::vector<std::vector<common::math::LineSegment2d>>&
177 soft_boundary_linesegments_vec) {
178 std::priority_queue<std::pair<std::string, double>,
179 std::vector<std::pair<std::string, double>>, cmp>
180 open_pq;
181 std::unordered_map<std::string, std::shared_ptr<Node2d>> open_set;
182 dp_map_ = decltype(dp_map_)();
183 XYbounds_ = XYbounds;
184 // XYbounds with xmin, xmax, ymin, ymax
185 max_grid_y_ = std::round((XYbounds_[3] - XYbounds_[2]) / xy_grid_resolution_);
186 max_grid_x_ = std::round((XYbounds_[1] - XYbounds_[0]) / xy_grid_resolution_);
187 std::shared_ptr<Node2d> end_node =
188 std::make_shared<Node2d>(ex, ey, xy_grid_resolution_, XYbounds_);
189 obstacles_linesegments_vec_ = obstacles_linesegments_vec;
190 open_set.emplace(end_node->GetIndex(), end_node);
191 open_pq.emplace(end_node->GetIndex(), end_node->GetCost());
192
193 // Grid a star begins
194 size_t explored_node_num = 0;
195 while (!open_pq.empty()) {
196 const std::string current_id = open_pq.top().first;
197 open_pq.pop();
198 std::shared_ptr<Node2d> current_node = open_set[current_id];
199 dp_map_.emplace(current_node->GetIndex(), current_node);
200 std::vector<std::shared_ptr<Node2d>> next_nodes =
201 std::move(GenerateNextNodes(current_node));
202 for (auto& next_node : next_nodes) {
203 if (!CheckConstraints(next_node)) {
204 continue;
205 }
206 if (dp_map_.find(next_node->GetIndex()) != dp_map_.end()) {
207 continue;
208 }
209 if (open_set.find(next_node->GetIndex()) == open_set.end()) {
210 ++explored_node_num;
211 next_node->SetPreNode(current_node);
212 open_set.emplace(next_node->GetIndex(), next_node);
213 open_pq.emplace(next_node->GetIndex(), next_node->GetCost());
214 } else {
215 if (open_set[next_node->GetIndex()]->GetCost() > next_node->GetCost()) {
216 open_set[next_node->GetIndex()]->SetCost(next_node->GetCost());
217 open_set[next_node->GetIndex()]->SetPreNode(current_node);
218 }
219 }
220 }
221 }
222 ADEBUG << "explored node num is " << explored_node_num;
223 return true;
224}
225
226double GridSearch::CheckDpMap(const double sx, const double sy) {
227 std::string index = Node2d::CalcIndex(sx, sy, xy_grid_resolution_, XYbounds_);
228 if (dp_map_.find(index) != dp_map_.end()) {
229 return dp_map_[index]->GetCost() * xy_grid_resolution_;
230 } else {
231 return std::numeric_limits<double>::infinity();
232 }
233}
234
235void GridSearch::LoadGridAStarResult(GridAStartResult* result) {
236 (*result).path_cost = final_node_->GetPathCost() * xy_grid_resolution_;
237 std::shared_ptr<Node2d> current_node = final_node_;
238 std::vector<double> grid_a_x;
239 std::vector<double> grid_a_y;
240 while (current_node->GetPreNode() != nullptr) {
241 grid_a_x.push_back(current_node->GetGridX() * xy_grid_resolution_ +
242 XYbounds_[0]);
243 grid_a_y.push_back(current_node->GetGridY() * xy_grid_resolution_ +
244 XYbounds_[2]);
245 current_node = current_node->GetPreNode();
246 }
247 std::reverse(grid_a_x.begin(), grid_a_x.end());
248 std::reverse(grid_a_y.begin(), grid_a_y.end());
249 (*result).x = std::move(grid_a_x);
250 (*result).y = std::move(grid_a_y);
251}
252} // namespace planning
253} // namespace apollo
Implements a class of 2-dimensional vectors.
Definition vec2d.h:42
GridSearch(const PlannerOpenSpaceConfig &open_space_conf)
double CheckDpMap(const double sx, const double sy)
bool GenerateDpMap(const double ex, const double ey, const std::vector< double > &XYbounds, const std::vector< std::vector< common::math::LineSegment2d > > &obstacles_linesegments_vec, const std::vector< std::vector< common::math::LineSegment2d > > &soft_boundary_linesegments_vec={{}})
bool GenerateAStarPath(const double sx, const double sy, const double ex, const double ey, const std::vector< double > &XYbounds, const std::vector< std::vector< common::math::LineSegment2d > > &obstacles_linesegments_vec, GridAStartResult *result)
static std::string CalcIndex(const double x, const double y, const double xy_resolution, const std::vector< double > &XYbounds)
Definition grid_search.h:80
Planning module main class.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37