Apollo 11.0
自动驾驶开放平台
grid_search.h
浏览该文件的文档.
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
21#pragma once
22
23#include <limits>
24#include <memory>
25#include <queue>
26#include <string>
27#include <unordered_map>
28#include <utility>
29#include <unordered_set>
30#include <vector>
31
32#include "absl/strings/str_cat.h"
33
34#include "modules/planning/planning_open_space/proto/planner_open_space_config.pb.h"
35
36#include "cyber/common/log.h"
38
39namespace apollo {
40namespace planning {
41
42class Node2d {
43public:
44 Node2d(const double x, const double y, const double xy_resolution, const std::vector<double>& XYbounds) {
45 // XYbounds with xmin, xmax, ymin, ymax
46 grid_x_ = static_cast<int>((x - XYbounds[0]) / xy_resolution);
47 grid_y_ = static_cast<int>((y - XYbounds[2]) / xy_resolution);
48 index_ = ComputeStringIndex(grid_x_, grid_y_);
49 }
50 Node2d(const int grid_x, const int grid_y, const std::vector<double>& XYbounds) {
51 grid_x_ = grid_x;
52 grid_y_ = grid_y;
53 index_ = ComputeStringIndex(grid_x_, grid_y_);
54 }
55 void SetPathCost(const double path_cost) {
56 path_cost_ = path_cost;
57 cost_ = path_cost_ + heuristic_;
58 }
59 void SetHeuristic(const double heuristic) {
60 heuristic_ = heuristic;
61 cost_ = path_cost_ + heuristic_;
62 }
63 void SetCost(const double cost) {
64 cost_ = cost;
65 }
66 void SetDistanceToObstacle(const double dist) {
67 distance_to_obstacle_ = dist;
68 }
69 void SetPreNode(std::shared_ptr<Node2d> pre_node) {
70 pre_node_ = pre_node;
71 }
72 double GetGridX() const {
73 return grid_x_;
74 }
75 double GetGridY() const {
76 return grid_y_;
77 }
78 double GetPathCost() const {
79 return path_cost_;
80 }
81 double GetHeuCost() const {
82 return heuristic_;
83 }
84 double GetCost() const {
85 return cost_;
86 }
87 double GetDistanceToObstacle() const {
88 return distance_to_obstacle_;
89 }
90 const std::string& GetIndex() const {
91 return index_;
92 }
93 std::shared_ptr<Node2d> GetPreNode() const {
94 return pre_node_;
95 }
96 static std::string CalcIndex(
97 const double x,
98 const double y,
99 const double xy_resolution,
100 const std::vector<double>& XYbounds) {
101 // XYbounds with xmin, xmax, ymin, ymax
102 int grid_x = static_cast<int>((x - XYbounds[0]) / xy_resolution);
103 int grid_y = static_cast<int>((y - XYbounds[2]) / xy_resolution);
104 return ComputeStringIndex(grid_x, grid_y);
105 }
106 bool operator==(const Node2d& right) const {
107 return right.GetIndex() == index_;
108 }
109
110private:
111 static std::string ComputeStringIndex(int x_grid, int y_grid) {
112 return absl::StrCat(x_grid, "_", y_grid);
113 }
114
115private:
116 int grid_x_ = 0;
117 int grid_y_ = 0;
118 double path_cost_ = 0.0;
119 double heuristic_ = 0.0;
120 double cost_ = 0.0;
121 double distance_to_obstacle_ = std::numeric_limits<double>::max();
122 std::string index_;
123 std::shared_ptr<Node2d> pre_node_ = nullptr;
124};
125
127 std::vector<double> x;
128 std::vector<double> y;
129 double path_cost = 0.0;
130};
131
133public:
134 explicit GridSearch(const PlannerOpenSpaceConfig& open_space_conf);
135 virtual ~GridSearch() = default;
137 const double sx,
138 const double sy,
139 const double ex,
140 const double ey,
141 const std::vector<double>& XYbounds,
142 const std::vector<std::vector<common::math::LineSegment2d>>& obstacles_linesegments_vec,
143 GridAStartResult* result);
144 bool GenerateDpMap(
145 const double ex,
146 const double ey,
147 const std::vector<double>& XYbounds,
148 const std::vector<std::vector<common::math::LineSegment2d>>& obstacles_linesegments_vec,
149 const std::vector<std::vector<common::math::LineSegment2d>>& soft_boundary_linesegments_vec = {{}});
150 double CheckDpMap(const double sx, const double sy);
151
152private:
153 double EuclidDistance(const double x1, const double y1, const double x2, const double y2);
154 std::vector<std::shared_ptr<Node2d>> GenerateNextNodes(std::shared_ptr<Node2d> node);
155 bool CheckConstraints(std::shared_ptr<Node2d> node);
156 void LoadGridAStarResult(GridAStartResult* result);
157
158private:
159 double xy_grid_resolution_ = 0.0;
160 double node_radius_ = 0.0;
161 std::vector<double> XYbounds_;
162 double max_grid_x_ = 0.0;
163 double max_grid_y_ = 0.0;
164 std::shared_ptr<Node2d> start_node_;
165 std::shared_ptr<Node2d> end_node_;
166 std::shared_ptr<Node2d> final_node_;
167 std::vector<std::vector<common::math::LineSegment2d>> obstacles_linesegments_vec_;
168
169 struct cmp {
170 bool operator()(const std::pair<std::string, double>& left, const std::pair<std::string, double>& right) const {
171 return left.second >= right.second;
172 }
173 };
174 std::unordered_map<std::string, std::shared_ptr<Node2d>> dp_map_;
175
176 // park generic
177public:
178 explicit GridSearch(const WarmStartConfig& warm_start_conf);
179 double GetObstacleDistance(const double x, const double y);
180
181private:
182 void AddSoftCost(const std::vector<std::vector<common::math::LineSegment2d>>& soft_boundary);
183 void ExtendNode(
184 const int& x,
185 const int& y,
186 common::math::Vec2d origin_index,
187 std::unordered_set<std::string>& close_set);
188 std::vector<std::vector<common::math::LineSegment2d>> soft_boundary_linesegments_vec_;
189 double esdf_range_ = 0.0;
190 bool use_esdf_ = false;
191 std::vector<int> dx_{1, 0, -1, 0};
192 std::vector<int> dy_{0, -1, 0, 1};
193};
194} // namespace planning
195} // namespace apollo
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)
double GetObstacleDistance(const double x, const double y)
virtual ~GridSearch()=default
double GetPathCost() const
Definition grid_search.h:78
const std::string & GetIndex() const
Definition grid_search.h:90
Node2d(const double x, const double y, const double xy_resolution, const std::vector< double > &XYbounds)
Definition grid_search.h:44
double GetDistanceToObstacle() const
Definition grid_search.h:87
double GetCost() const
Definition grid_search.h:84
std::shared_ptr< Node2d > GetPreNode() const
Definition grid_search.h:93
double GetGridX() const
Definition grid_search.h:72
void SetDistanceToObstacle(const double dist)
Definition grid_search.h:66
double GetGridY() const
Definition grid_search.h:75
void SetPreNode(std::shared_ptr< Node2d > pre_node)
Definition grid_search.h:69
Node2d(const int grid_x, const int grid_y, const std::vector< double > &XYbounds)
Definition grid_search.h:50
void SetPathCost(const double path_cost)
Definition grid_search.h:55
bool operator==(const Node2d &right) const
static std::string CalcIndex(const double x, const double y, const double xy_resolution, const std::vector< double > &XYbounds)
Definition grid_search.h:96
double GetHeuCost() const
Definition grid_search.h:81
void SetCost(const double cost)
Definition grid_search.h:63
void SetHeuristic(const double heuristic)
Definition grid_search.h:59
Planning module main class.
Define the LineSegment2d class.
class register implement
Definition arena_queue.h:37