Loading...
Searching...
No Matches
CostHelper.h
1/*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, University of Toronto
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the University of Toronto nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34
35/* Authors: Jonathan Gammell, Marlin Strub */
36
37#ifndef OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_BITSTAR_COSTHELPER_
38#define OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_BITSTAR_COSTHELPER_
39
40// OMPL:
41// The cost class:
42#include "ompl/base/Cost.h"
43// The optimization objective class:
44#include "ompl/base/OptimizationObjective.h"
45// For exceptions:
46#include "ompl/util/Exception.h"
47
48// BIT*:
49// I am member class of the BITstar class (i.e., I am in it's namespace), so I need to include it's definition to be
50// aware of the class BITstar. It has a forward declaration to me and the other helper classes but I will need to
51// include any I use in my .cpp (to avoid dependency loops).
52#include "ompl/geometric/planners/informedtrees/BITstar.h"
53// The vertex class
54#include "ompl/geometric/planners/informedtrees/bitstar/Vertex.h"
55// The graph class
56#include "ompl/geometric/planners/informedtrees/bitstar/ImplicitGraph.h"
57
58namespace ompl
59{
60 namespace geometric
61 {
66
69 {
70 public:
72 // Public functions:
74 CostHelper() = default;
75
76 virtual ~CostHelper() = default;
77
79 inline void setup(const ompl::base::OptimizationObjectivePtr &opt, ImplicitGraph *graph)
80 {
81 opt_ = opt;
82 graphPtr_ = graph;
83 };
84
86 inline void reset()
87 {
88 opt_.reset();
89 graphPtr_ = nullptr;
90 };
91
93 inline ompl::base::OptimizationObjectivePtr getOptObj() const
94 {
95 return opt_;
96 };
97
99 // Heuristic helper functions
104 {
105#ifdef BITSTAR_DEBUG
106 if (vertex->isPruned())
107 {
108 throw ompl::Exception("Computing the lower bound heuristic through a pruned vertex.");
109 }
110#endif // BITSTAR_DEBUG
111 return this->combineCosts(this->costToComeHeuristic(vertex), this->costToGoHeuristic(vertex));
112 };
113
118 {
119 return this->combineCosts(vertex->getCost(), this->costToGoHeuristic(vertex));
120 };
121
126 {
127 return this->combineCosts(this->lowerBoundHeuristicToTarget(edgePair),
128 this->costToGoHeuristic(edgePair.second));
129 };
130
135 {
136 return this->combineCosts(this->currentHeuristicToTarget(edgePair),
137 this->costToGoHeuristic(edgePair.second));
138 };
139
144 {
145 return this->combineCosts(this->costToComeHeuristic(edgePair.first), this->edgeCostHeuristic(edgePair));
146 };
147
152 {
153 return this->combineCosts(edgePair.first->getCost(), this->edgeCostHeuristic(edgePair));
154 };
155
158 {
159#ifdef BITSTAR_DEBUG
160 if (vertex->isPruned())
161 {
162 throw ompl::Exception("Computing the cost to come heuristic to a pruned vertex.");
163 }
164#endif // BITSTAR_DEBUG
165 // Variable
166 // The current best cost to the state, initialize to infinity
167 ompl::base::Cost curBest = this->infiniteCost();
168
169 // Iterate over the vector of starts, finding the minimum estimated cost-to-come to the state
170 for (auto startIter = graphPtr_->startVerticesBeginConst();
171 startIter != graphPtr_->startVerticesEndConst(); ++startIter)
172 {
173 // Update the cost-to-come as the better of the best so far and the new one
174 curBest =
175 this->betterCost(curBest, this->motionCostHeuristic((*startIter)->state(), vertex->state()));
176 }
177
178 // Return
179 return curBest;
180 };
181
184 {
185 return this->motionCostHeuristic(edgePair.first->state(), edgePair.second->state());
186 };
187
190 {
191 // Variable
192 // The current best cost to a goal from the state, initialize to infinity
193 ompl::base::Cost curBest = this->infiniteCost();
194
195 // Iterate over the vector of goals, finding the minimum estimated cost-to-go from the state
196 for (auto goalIter = graphPtr_->goalVerticesBeginConst(); goalIter != graphPtr_->goalVerticesEndConst();
197 ++goalIter)
198 {
199 // Update the cost-to-go as the better of the best so far and the new one
200 curBest =
201 this->betterCost(curBest, this->motionCostHeuristic(vertex->state(), (*goalIter)->state()));
202 }
203
204 // Return
205 return curBest;
206 };
207
208
210 // Cost-calculation functions
213 {
214 return this->motionCost(edgePair.first->state(), edgePair.second->state());
215 };
216
218 template <typename... Costs>
219 inline ompl::base::Cost combineCosts(const ompl::base::Cost &cost, const Costs &...costs) const
220 {
221 return this->combineCosts(cost, this->combineCosts(costs...));
222 }
223
225 inline ompl::base::Cost inflateCost(const ompl::base::Cost &cost, double factor) const
226 {
227 return ompl::base::Cost(factor * cost.value());
228 }
229
231 // Cost-comparison functions
233 inline bool isCostWorseThan(const ompl::base::Cost &a, const ompl::base::Cost &b) const
234 {
235 // If b is better than a, then a is worse than b
236 return this->isCostBetterThan(b, a);
237 };
238
241 inline bool isCostNotEquivalentTo(const ompl::base::Cost &a, const ompl::base::Cost &b) const
242 {
243 // If a is better than b, or b is better than a, then they are not equal
244 return this->isCostBetterThan(a, b) || this->isCostBetterThan(b, a);
245 };
246
250 {
251 // If b is not better than a, then a is better than, or equal to, b
252 return !this->isCostBetterThan(b, a);
253 };
254
258 {
259 // If a is not better than b, than a is worse than, or equal to, b
260 return !this->isCostBetterThan(a, b);
261 };
262
265 inline double fractionalChange(const ompl::base::Cost &newCost, const ompl::base::Cost &oldCost) const
266 {
267 return this->fractionalChange(newCost, oldCost, oldCost);
268 };
269
272 inline double fractionalChange(const ompl::base::Cost &newCost, const ompl::base::Cost &oldCost,
273 const ompl::base::Cost &refCost) const
274 {
275 // If the old cost is not finite, than we call that infinite percent improvement
276 if (!this->isFinite(oldCost))
277 {
278 // Return infinity (but not beyond)
279 return std::numeric_limits<double>::infinity();
280 }
281 // Calculate and return
282 return (newCost.value() - oldCost.value()) / refCost.value();
283 };
284
285
287 // Straight pass-throughs to OptimizationObjective
288 inline bool isSatisfied(const ompl::base::Cost &a) const
289 {
290 return opt_->isSatisfied(a);
291 };
292 inline bool isFinite(const ompl::base::Cost &a) const
293 {
294 return opt_->isFinite(a);
295 };
296 inline bool isCostEquivalentTo(const ompl::base::Cost &a, const ompl::base::Cost &b) const
297 {
298 return opt_->isCostEquivalentTo(a, b);
299 };
300 inline bool isCostBetterThan(const ompl::base::Cost &a, const ompl::base::Cost &b) const
301 {
302 return opt_->isCostBetterThan(a, b);
303 };
304 inline ompl::base::Cost betterCost(const ompl::base::Cost &a, const ompl::base::Cost &b) const
305 {
306 return opt_->betterCost(a, b);
307 };
308 inline ompl::base::Cost combineCosts(const ompl::base::Cost &a, const ompl::base::Cost &b) const
309 {
310 return opt_->combineCosts(a, b);
311 };
312 inline ompl::base::Cost infiniteCost() const
313 {
314 return opt_->infiniteCost();
315 };
316 inline ompl::base::Cost identityCost() const
317 {
318 return opt_->identityCost();
319 };
320 inline ompl::base::Cost motionCostHeuristic(const ompl::base::State *a, const ompl::base::State *b) const
321 {
322 return opt_->motionCostHeuristic(a, b);
323 };
324 inline ompl::base::Cost motionCost(const ompl::base::State *a, const ompl::base::State *b) const
325 {
326 return opt_->motionCost(a, b);
327 };
330
331 private:
333 // Member variables:
335 ompl::base::OptimizationObjectivePtr opt_;
336
339 ImplicitGraph *graphPtr_;
341 }; // class CostHelper
342 } // namespace geometric
343} // namespace ompl
344#endif // OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_BITSTAR_COSTHELPER_
The exception type for ompl.
Definition Exception.h:47
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition Cost.h:48
double value() const
The value of the cost.
Definition Cost.h:56
A helper class to handle the various heuristic functions in one place.
Definition CostHelper.h:69
ompl::base::Cost lowerBoundHeuristicEdge(const VertexConstPtrPair &edgePair) const
Calculates a heuristic estimate of the cost of a solution constrained to go through an edge,...
Definition CostHelper.h:125
ompl::base::Cost currentHeuristicVertex(const VertexConstPtr &vertex) const
Calculates a heuristic estimate of the cost of a solution constrained to pass through a vertex,...
Definition CostHelper.h:117
ompl::base::OptimizationObjectivePtr getOptObj() const
Get the underling OptimizationObjective.
Definition CostHelper.h:93
bool isCostWorseThanOrEquivalentTo(const ompl::base::Cost &a, const ompl::base::Cost &b) const
Compare whether cost a is worse or equivalent to cost b by checking that a is not better than b.
Definition CostHelper.h:257
ompl::base::Cost lowerBoundHeuristicToTarget(const VertexConstPtrPair &edgePair) const
Calculates a heuristic estimate of the cost of a path to the target of an edge, independent of the cu...
Definition CostHelper.h:143
bool isCostBetterThanOrEquivalentTo(const ompl::base::Cost &a, const ompl::base::Cost &b) const
Compare whether cost a is better or equivalent to cost b by checking that b is not better than a.
Definition CostHelper.h:249
bool isCostWorseThan(const ompl::base::Cost &a, const ompl::base::Cost &b) const
Compare whether cost a is worse than cost b by checking whether b is better than a.
Definition CostHelper.h:233
bool isCostNotEquivalentTo(const ompl::base::Cost &a, const ompl::base::Cost &b) const
Compare whether cost a and cost b are not equivalent by checking if either a or b is better than the ...
Definition CostHelper.h:241
ompl::base::Cost edgeCostHeuristic(const VertexConstPtrPair &edgePair) const
Calculate a heuristic estimate of the cost of an edge between two Vertices.
Definition CostHelper.h:183
CostHelper()=default
Construct the heuristic helper, must be setup before use.
ompl::base::Cost currentHeuristicToTarget(const VertexConstPtrPair &edgePair) const
Calculates a heuristic estimate of the cost of a path to the target of an edge, dependent on the cost...
Definition CostHelper.h:151
ompl::base::Cost costToGoHeuristic(const VertexConstPtr &vertex) const
Calculate a heuristic estimate of the cost-to-go for a Vertex.
Definition CostHelper.h:189
ompl::base::Cost currentHeuristicEdge(const VertexConstPtrPair &edgePair) const
Calculates a heuristic estimate of the cost of a solution constrained to go through an edge,...
Definition CostHelper.h:134
void setup(const ompl::base::OptimizationObjectivePtr &opt, ImplicitGraph *graph)
Setup the CostHelper, must be called before use.
Definition CostHelper.h:79
ompl::base::Cost inflateCost(const ompl::base::Cost &cost, double factor) const
Inflate a cost by a given factor.
Definition CostHelper.h:225
ompl::base::Cost combineCosts(const ompl::base::Cost &cost, const Costs &...costs) const
Combine multiple costs.
Definition CostHelper.h:219
void reset()
Reset the CostHelper, returns to state at construction.
Definition CostHelper.h:86
double fractionalChange(const ompl::base::Cost &newCost, const ompl::base::Cost &oldCost) const
Calculate the fractional change of cost "newCost" from "oldCost" relative to "oldCost",...
Definition CostHelper.h:265
ompl::base::Cost trueEdgeCost(const VertexConstPtrPair &edgePair) const
The true cost of an edge, including constraints.
Definition CostHelper.h:212
double fractionalChange(const ompl::base::Cost &newCost, const ompl::base::Cost &oldCost, const ompl::base::Cost &refCost) const
Calculate the fractional change of cost "newCost" from "oldCost" relative to "refCost",...
Definition CostHelper.h:272
ompl::base::Cost costToComeHeuristic(const VertexConstPtr &vertex) const
Calculate a heuristic estimate of the cost-to-come for a Vertex.
Definition CostHelper.h:157
ompl::base::Cost lowerBoundHeuristicVertex(const VertexConstPtr &vertex) const
Calculates a heuristic estimate of the cost of a solution constrained to pass through a vertex,...
Definition CostHelper.h:103
A conceptual representation of samples as an edge-implicit random geometric graph.
std::pair< VertexConstPtr, VertexConstPtr > VertexConstPtrPair
A pair of const vertices, i.e., an edge.
Definition BITstar.h:137
std::shared_ptr< const Vertex > VertexConstPtr
A shared pointer to a const vertex.
Definition BITstar.h:119
This namespace contains code that is specific to planning under geometric constraints.
Main namespace. Contains everything in this library.