Apollo  6.0
Open source self driving car software
comparable_cost.h
Go to the documentation of this file.
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 
21 #pragma once
22 
23 namespace apollo {
24 namespace planning {
25 
27  public:
28  ComparableCost() = default;
29  ComparableCost(const bool has_collision, const bool out_of_boundary,
30  const bool out_of_lane, const double safety_cost_,
31  const double smoothness_cost_)
32  : safety_cost(safety_cost_), smoothness_cost(smoothness_cost_) {
33  cost_items[HAS_COLLISION] = has_collision;
34  cost_items[OUT_OF_BOUNDARY] = out_of_boundary;
35  cost_items[OUT_OF_LANE] = out_of_lane;
36  }
37  ComparableCost(const ComparableCost &) = default;
38 
39  int CompareTo(const ComparableCost &other) const {
40  for (size_t i = 0; i < cost_items.size(); ++i) {
41  if (cost_items[i]) {
42  if (other.cost_items[i]) {
43  continue;
44  } else {
45  return 1;
46  }
47  } else {
48  if (other.cost_items[i]) {
49  return -1;
50  } else {
51  continue;
52  }
53  }
54  }
55 
56  static constexpr double kEpsilon = 1e-12;
57  const double diff = safety_cost + smoothness_cost - other.safety_cost -
58  other.smoothness_cost;
59  if (std::fabs(diff) < kEpsilon) {
60  return 0;
61  } else if (diff > 0) {
62  return 1;
63  } else {
64  return -1;
65  }
66  }
68  ComparableCost lhs = *this;
69  lhs += other;
70  return lhs;
71  }
73  for (size_t i = 0; i < cost_items.size(); ++i) {
74  cost_items[i] = (cost_items[i] || other.cost_items[i]);
75  }
76  safety_cost += other.safety_cost;
78  return *this;
79  }
80  bool operator>(const ComparableCost &other) const {
81  return this->CompareTo(other) > 0;
82  }
83  bool operator>=(const ComparableCost &other) const {
84  return this->CompareTo(other) >= 0;
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  /*
93  * cost_items represents an array of factors that affect the cost,
94  * The level is from most critical to less critical.
95  * It includes:
96  * (0) has_collision or out_of_boundary
97  * (1) out_of_lane
98  *
99  * NOTICE: Items could have same critical levels
100  */
101  static const size_t HAS_COLLISION = 0;
102  static const size_t OUT_OF_BOUNDARY = 1;
103  static const size_t OUT_OF_LANE = 2;
104  std::array<bool, 3> cost_items = {{false, false, false}};
105 
106  // cost from distance to obstacles or boundaries
107  double safety_cost = 0.0f;
108  // cost from deviation from lane center, path curvature etc
109  double smoothness_cost = 0.0f;
110 };
111 
112 } // namespace planning
113 } // namespace apollo
ComparableCost & operator+=(const ComparableCost &other)
Definition: comparable_cost.h:72
ComparableCost operator+(const ComparableCost &other)
Definition: comparable_cost.h:67
static const size_t OUT_OF_BOUNDARY
Definition: comparable_cost.h:102
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
static const size_t HAS_COLLISION
Definition: comparable_cost.h:101
int CompareTo(const ComparableCost &other) const
Definition: comparable_cost.h:39
Planning module main class. It processes GPS and IMU as input, to generate planning info...
double safety_cost
Definition: comparable_cost.h:107
double smoothness_cost
Definition: comparable_cost.h:109
bool operator<=(const ComparableCost &other) const
Definition: comparable_cost.h:89
Definition: comparable_cost.h:26
bool operator<(const ComparableCost &other) const
Definition: comparable_cost.h:86
bool operator>(const ComparableCost &other) const
Definition: comparable_cost.h:80
std::array< bool, 3 > cost_items
Definition: comparable_cost.h:104
float diff(Image< float > *I, int x1, int y1, int x2, int y2)
Definition: segment_image.h:44
ComparableCost(const bool has_collision, const bool out_of_boundary, const bool out_of_lane, const double safety_cost_, const double smoothness_cost_)
Definition: comparable_cost.h:29
static const size_t OUT_OF_LANE
Definition: comparable_cost.h:103
bool operator>=(const ComparableCost &other) const
Definition: comparable_cost.h:83