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