ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
bordergraph.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Daniel Heß - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 #include <vector>
17 #include <unordered_map>
18 #include <queue>
20 
21 namespace adore
22 {
23 namespace env
24 {
25 namespace BorderBased
26 {
27 struct Node
28 {
29 public:
33  double m_g;
34  double m_h;
35 
36 public:
37  Node(const Node& value)
38  : m_border(value.m_border)
40  , m_successor(value.m_successor)
41  , m_g(value.m_g)
42  , m_h(value.m_h)
43  {
44  }
45  Node() : m_border(0), m_predecessor(0), m_successor(0), m_g(0), m_h(0)
46  {
47  }
48  Node(Border* border) : m_border(border), m_predecessor(0), m_successor(0), m_g(0), m_h(0)
49  {
50  }
51  void setBorder(Border* border)
52  {
53  m_border = border;
54  }
55  void setG(double g)
56  {
57  m_g = g;
58  }
59  void setH(double h)
60  {
61  m_h = h;
62  }
63  void setSuccessor(Node* successor)
64  {
65  m_successor = successor;
66  }
67  void setPredecessor(Node* predecessor)
68  {
69  m_predecessor = predecessor;
70  }
71  double g() const
72  {
73  return m_g;
74  }
75  double h() const
76  {
77  return m_h;
78  }
79  double f() const
80  {
81  return m_g + m_h;
82  }
83  bool operator<(const Node& other) const
84  {
85  return this->f() < other.f();
86  }
87  bool operator>(const Node& other) const
88  {
89  return this->f() > other.f();
90  }
91 };
92 struct NodeHasher
93 {
95  std::size_t operator()(const Node& b) const
96  {
97  return bh.operator()(b.m_border->m_id);
98  }
99  std::size_t operator()(const Node* b) const
100  {
101  return bh.operator()(b->m_border->m_id);
102  }
103 };
108 {
109 public:
111  {
112  }
113  virtual void assign(Node* n) = 0;
114  virtual double getCostGuard(){return 1.0e10;}
115  virtual bool isCostBounded(double value){return -1.0e9<value && value<1.0e9;}
116 };
117 
119 {
120 public:
121  virtual void assign(Node* n) override
122  {
123  if (n->m_predecessor != 0)
124  {
126  {
127  n->setG(n->m_predecessor->g() + n->m_predecessor->m_border->getLength());
128  }
129  else
130  {
131  n->setG(n->m_predecessor->g() +
134  }
135  }
136  else
137  {
138  if (n->m_successor != 0)
139  {
141  {
142  n->setG(n->m_successor->g() - n->m_border->getLength());
143  }
144  else
145  {
146  n->setG(n->m_successor->g() -
149  }
150  }
151  }
152  n->setH(0.0);
153  }
154 };
155 
157 {
158 private:
160 
161 public:
162  BorderGraphCostWithLaneChanges(double laneChangePenalty = 100.0) : m_laneChangePenalty(laneChangePenalty)
163  {
164  }
165  void setLaneChangePenalty(double value)
166  {
167  m_laneChangePenalty = value;
168  }
169  virtual void assign(Node* n) override
170  {
171  n->setH(0.0);
172  if (n->m_predecessor != 0)
173  {
174  n->setG(getCostGuard());
176  {
177  n->setG(n->m_predecessor->g() + n->m_predecessor->m_border->getLength());
178  }
179  else
180  {
182  {
184  }
185  }
186  }
187  else
188  {
189  if (n->m_successor != 0)
190  {
191  n->setG(-getCostGuard());
193  {
194  n->setG(n->m_successor->g() - n->m_border->getLength());
195  }
196  else
197  {
199  {
201  }
202  }
203  }
204  }
205  }
206 };
207 
213 {
214 private:
217 
218 public:
219  typedef std::unordered_map<adore::env::BorderBased::BorderID, double, adore::env::BorderBased::BorderIDHasher>
221 
223  {
224  }
225  BorderGraph(BorderSet* borderSet) : m_borderSet(borderSet)
226  {
228  }
229  BorderGraph(BorderSet* borderSet, ABorderGraphCost* cost) : m_borderSet(borderSet), m_cost(cost)
230  {
231  }
232  virtual ~BorderGraph()
233  {
234  delete m_cost;
235  }
236  void init(BorderSet* borderSet)
237  {
238  m_borderSet = borderSet;
240  }
241  void init(BorderSet* borderSet, ABorderGraphCost* cost)
242  {
243  m_borderSet = borderSet;
244  m_cost = cost;
245  }
246 
248  {
249  std::priority_queue<adore::env::BorderBased::Node, std::vector<adore::env::BorderBased::Node>,
250  std::less<adore::env::BorderBased::Node>>
251  openList;
252  openList.push(*startNode);
253  startNode->setG(0);
254 
255  while (!openList.empty())
256  {
257  auto closingNode = openList.top();
258  openList.pop();
259  if (closedList->find(closingNode.m_border->m_id) == closedList->end())
260  {
261  closedList->insert(
262  std::pair<adore::env::BorderBased::BorderID, double>(closingNode.m_border->m_id, closingNode.g()));
263 
264  auto expansion = getPredecessors(&closingNode);
265  while (expansion.hasMore())
266  {
267  auto newNode = expansion.getNext();
268 
269  if (closedList->find(newNode.m_border->m_id) == closedList->end())
270  {
271  if (newNode.m_border->m_type == adore::env::BorderBased::BorderType::DRIVING)
272  {
273  if(m_cost->isCostBounded(newNode.g()))
274  {
275  openList.push(newNode);
276  }
277  }
278  }
279  }
280  }
281  }
282  }
283 
284  class Expansion
285  {
286  private:
290  std::vector<Border*> m_connectedBorders;
292 
293  public:
295  bool search_forward, bool allow_direction_inversion = false)
296  : m_it(it), m_n(n), m_cost(cost), m_search_forward(search_forward)
297  {
298  if (left != 0 && (allow_direction_inversion || n->m_border->getNeighborDirection() == Border::SAME_DIRECTION))
299  {
300  m_connectedBorders.push_back(left);
301  }
302  if (right != 0 && (allow_direction_inversion || right->getNeighborDirection() == Border::SAME_DIRECTION))
303  {
304  m_connectedBorders.push_back(right);
305  }
306  for (; it.first != it.second; it.first++)
307  {
308  Border* k = it.first->second;
309  if(m_search_forward)
310  {
312  {
313  m_connectedBorders.push_back(k);
314  }
315  }
316  else
317  {
319  {
320  m_connectedBorders.push_back(k);
321  }
322  }
323  }
324  }
325  bool hasMore()
326  {
327  return !m_connectedBorders.empty();
328  }
330  {
331  Node k;
332  k.setBorder(m_connectedBorders.back());
333  m_connectedBorders.pop_back();
334  if (m_search_forward)
335  k.setPredecessor(m_n);
336  else
337  k.setSuccessor(m_n);
338  m_cost->assign(&k);
339  return k;
340  }
341  };
346  inline Expansion getPredecessors(Node* n, bool left_neighbors = true, bool right_neighbors = true,
347  bool allow_direction_inversion = false)
348  {
349  auto predecessors = m_borderSet->getPredecessors(n->m_border);
350  return Expansion(n, left_neighbors ? m_borderSet->getLeftNeighbor(n->m_border) : 0,
351  right_neighbors ? m_borderSet->getRightNeighbor(n->m_border) : 0, predecessors, m_cost, false,
352  allow_direction_inversion);
353  }
358  inline Expansion getSuccessors(Node* n, bool left_neighbors = true, bool right_neighbors = true,
359  bool allow_direction_inversion = false)
360  {
361  auto successors = m_borderSet->getSuccessors(n->m_border);
362  return Expansion(n, left_neighbors ? m_borderSet->getLeftNeighbor(n->m_border) : 0,
363  right_neighbors ? m_borderSet->getRightNeighbor(n->m_border) : 0, successors, m_cost, false,
364  allow_direction_inversion);
365  }
366 };
367 } // namespace BorderBased
368 } // namespace env
369 } // namespace adore
Definition: bordergraph.h:108
virtual double getCostGuard()
Definition: bordergraph.h:114
virtual ~ABorderGraphCost()
Definition: bordergraph.h:110
virtual bool isCostBounded(double value)
Definition: bordergraph.h:115
BorderGraphCostWithLaneChanges(double laneChangePenalty=100.0)
Definition: bordergraph.h:162
double m_laneChangePenalty
Definition: bordergraph.h:159
void setLaneChangePenalty(double value)
Definition: bordergraph.h:165
virtual void assign(Node *n) override
Definition: bordergraph.h:169
Expansion(Node *n, Border *left, Border *right, itCoordinate2Border &it, ABorderGraphCost *cost, bool search_forward, bool allow_direction_inversion=false)
Definition: bordergraph.h:294
std::vector< Border * > m_connectedBorders
Definition: bordergraph.h:290
Node * m_n
Definition: bordergraph.h:288
bool m_search_forward
Definition: bordergraph.h:291
Node getNext()
Definition: bordergraph.h:329
bool hasMore()
Definition: bordergraph.h:325
itCoordinate2Border m_it
Definition: bordergraph.h:287
ABorderGraphCost * m_cost
Definition: bordergraph.h:289
Definition: bordergraph.h:213
Expansion getPredecessors(Node *n, bool left_neighbors=true, bool right_neighbors=true, bool allow_direction_inversion=false)
Definition: bordergraph.h:346
BorderGraph()
Definition: bordergraph.h:222
std::unordered_map< adore::env::BorderBased::BorderID, double, adore::env::BorderBased::BorderIDHasher > BorderCostMap
Definition: bordergraph.h:220
void djikstra(adore::env::BorderBased::Node *startNode, BorderCostMap *closedList)
Definition: bordergraph.h:247
BorderGraph(BorderSet *borderSet, ABorderGraphCost *cost)
Definition: bordergraph.h:229
Expansion getSuccessors(Node *n, bool left_neighbors=true, bool right_neighbors=true, bool allow_direction_inversion=false)
Definition: bordergraph.h:358
void init(BorderSet *borderSet)
Definition: bordergraph.h:236
virtual ~BorderGraph()
Definition: bordergraph.h:232
ABorderGraphCost * m_cost
Definition: bordergraph.h:216
BorderSet * m_borderSet
Definition: bordergraph.h:215
void init(BorderSet *borderSet, ABorderGraphCost *cost)
Definition: bordergraph.h:241
BorderGraph(BorderSet *borderSet)
Definition: bordergraph.h:225
efficiently store borders in boost R-tree
Definition: borderset.h:99
Border * getRightNeighbor(Border *b)
get the right neighbor of a border
Definition: borderset.h:1279
itCoordinate2Border getSuccessors(Border *b)
get an interator pair for all borders which follow after b
Definition: borderset.h:996
Border * getLeftNeighbor(Border *b)
Get left neighbor of a border.
Definition: borderset.h:1252
itCoordinate2Border getPredecessors(Border *b)
get an interator pair for all borders which lead to b
Definition: borderset.h:1017
virtual void assign(Node *n) override
Definition: bordergraph.h:121
@ DRIVING
Definition: border.h:39
@ right
Definition: indicator_hint.h:36
@ left
Definition: indicator_hint.h:35
T min(T a, T b, T c, T d)
Definition: adoremath.h:663
Definition: areaofeffectconverter.h:20
a functor, which hashes a BorderID object -> std::unordered_set<BorderID,BorderIDHasher> amap(0);
Definition: borderid.h:176
Coordinate m_last
Definition: borderid.h:32
Coordinate m_first
Definition: borderid.h:32
The border struct contains data of the smallest.
Definition: border.h:62
@ SAME_DIRECTION
Definition: border.h:507
bool isPredecessorOf(const BorderID &successorID)
Check whether border is a direct predecessor of another border.
Definition: border.h:333
bool isContinuousPredecessorOf(Border *other)
Check whether the border is a continuous predecessor of another border.
Definition: border.h:363
Direction getNeighborDirection()
Get the direction of the left neighbor.
Definition: border.h:517
bool isLaneChangeNeighborOf(Border *other)
Check whether the border is a lane-change-neighbor of another border.
Definition: border.h:486
double getLength()
Get the length of the border.
Definition: border.h:703
BorderID m_id
Definition: border.h:68
bool isSuccessorOf(const BorderID &predecessorID)
Check whether border is a direct successors of another border.
Definition: border.h:319
bool isContinuousSuccessorOf(Border *parent)
Check whether the border is a continuous successor of another border.
Definition: border.h:348
double distance(const Coordinate &other) const
Calculate the distance between two Coordinates.
Definition: coordinate.h:133
Definition: bordergraph.h:93
std::size_t operator()(const Node *b) const
Definition: bordergraph.h:99
BorderIDHasher bh
Definition: bordergraph.h:94
std::size_t operator()(const Node &b) const
Definition: bordergraph.h:95
Definition: bordergraph.h:28
double f() const
Definition: bordergraph.h:79
void setPredecessor(Node *predecessor)
Definition: bordergraph.h:67
Node * m_successor
Definition: bordergraph.h:32
void setG(double g)
Definition: bordergraph.h:55
void setBorder(Border *border)
Definition: bordergraph.h:51
double m_g
Definition: bordergraph.h:33
double g() const
Definition: bordergraph.h:71
Node()
Definition: bordergraph.h:45
void setH(double h)
Definition: bordergraph.h:59
double m_h
Definition: bordergraph.h:34
bool operator<(const Node &other) const
Definition: bordergraph.h:83
Node * m_predecessor
Definition: bordergraph.h:31
void setSuccessor(Node *successor)
Definition: bordergraph.h:63
double h() const
Definition: bordergraph.h:75
Node(const Node &value)
Definition: bordergraph.h:37
bool operator>(const Node &other) const
Definition: bordergraph.h:87
Border * m_border
Definition: bordergraph.h:30
Node(Border *border)
Definition: bordergraph.h:48
T1 first
Definition: borderset.h:47
T2 second
Definition: borderset.h:48