RandomGeometricGraph.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, University of Oxford
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: Marlin Strub
36 
37 #ifndef OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_EITSTAR_RANDOM_GEOMETRIC_GRAPH_
38 #define OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_EITSTAR_RANDOM_GEOMETRIC_GRAPH_
39 
40 #include <limits>
41 #include <memory>
42 #include <iostream> // This is needed for ompl's nearest neighbors struct...
43 
44 #include "ompl/datastructures/NearestNeighborsGNATNoThreadSafety.h"
45 #include "ompl/base/samplers/InformedStateSampler.h"
46 #include "ompl/base/Planner.h"
47 #include "ompl/base/ProblemDefinition.h"
48 #include "ompl/base/OptimizationObjective.h"
49 
50 #include "ompl/geometric/planners/informedtrees/eitstar/State.h"
51 #include "ompl/geometric/planners/informedtrees/eitstar/Edge.h"
52 
53 namespace ompl
54 {
55  namespace geometric
56  {
57  namespace eitstar
58  {
59  class RandomGeometricGraph
60  {
61  public:
64  RandomGeometricGraph(const std::shared_ptr<ompl::base::SpaceInformation> &spaceInfo,
65  const ompl::base::Cost &solutionCost);
66 
68  ~RandomGeometricGraph() = default;
69 
71  void setup(const std::shared_ptr<ompl::base::ProblemDefinition> &problem,
72  ompl::base::PlannerInputStates *inputStates);
73 
76  void clear();
77 
79  void clearQuery();
80 
84  ompl::base::PlannerInputStates *inputStates);
85 
89 
91  void setRadiusFactor(double factor);
92 
94  double getRadiusFactor() const;
95 
97  void setEffortThreshold(const unsigned int threshold);
98 
100  unsigned int getEffortThreshold() const;
101 
103  void enablePruning(bool prune);
104 
106  bool isPruningEnabled() const;
107 
109  void enableMultiquery(bool multiquery);
110 
112  bool isMultiqueryEnabled() const;
113 
115  void setUseKNearest(bool useKNearest);
116 
119  bool getUseKNearest() const;
120 
122  void setMaxNumberOfGoals(unsigned int maxNumberOfGoals);
123 
125  unsigned int getMaxNumberOfGoals() const;
126 
128  bool addStates(std::size_t numStates,
129  const ompl::base::PlannerTerminationCondition &terminationCondition);
130 
132  void prune();
133 
135  std::vector<std::weak_ptr<State>> getNeighbors(const std::shared_ptr<State> &state) const;
136 
138  const std::vector<std::shared_ptr<State>> &getStartStates() const;
139 
141  const std::vector<std::shared_ptr<State>> &getGoalStates() const;
142 
144  unsigned int getNumberOfSampledStates() const;
145 
147  unsigned int getNumberOfValidSamples() const;
148 
150  unsigned int getNumberOfNearestNeighborCalls() const;
151 
153  std::shared_ptr<State> registerStartState(const ompl::base::State *start);
154 
156  std::shared_ptr<State> registerGoalState(const ompl::base::State *goal);
157 
159  void registerWhitelistedState(const std::shared_ptr<State> &state) const;
160 
162  bool hasStartState() const;
163 
165  bool hasGoalState() const;
166 
168  bool isStart(const std::shared_ptr<State> &state) const;
169 
171  bool isGoal(const std::shared_ptr<State> &state) const;
172 
174  std::vector<std::shared_ptr<State>> getStates() const;
175 
177  void registerInvalidEdge(const Edge &edge) const;
178 
180  std::size_t getTag() const;
181 
183  unsigned int inadmissibleEffortToCome(const std::shared_ptr<State> &state) const;
184 
185  private:
189  std::shared_ptr<State> getNewSample(const ompl::base::PlannerTerminationCondition& terminationCondition);
190 
192  std::size_t countSamplesInInformedSet() const;
193 
196  bool canBePruned(const std::shared_ptr<State> &state) const;
197 
199  void pruneStartsAndGoals();
200 
202  ompl::base::Cost lowerBoundCostToCome(const std::shared_ptr<State> &state) const;
203 
205  unsigned int lowerBoundEffortToCome(const std::shared_ptr<State> &state) const;
206 
208  ompl::base::Cost lowerBoundCostToGo(const std::shared_ptr<State> &state) const;
209 
211  void initializeState(const std::shared_ptr<State> &state);
212 
214  std::size_t computeNumberOfNeighbors(std::size_t numInformedSamples) const;
215 
217  double computeRadius(std::size_t numInformedSamples) const;
218 
220  mutable std::vector<std::shared_ptr<State>> whitelistedStates_{};
221 
223  std::size_t tag_{1u};
224 
226  std::vector<std::shared_ptr<State>> buffer_;
227 
229  std::vector<std::shared_ptr<State>> startGoalBuffer_;
230 
232  std::size_t currentNumSamples_{0u};
233 
235  NearestNeighborsGNATNoThreadSafety<std::shared_ptr<State>> samples_;
236 
238  std::vector<std::shared_ptr<State>> newSamples_;
239 
241  std::shared_ptr<ompl::base::InformedSampler> sampler_{nullptr};
242 
244  std::shared_ptr<ompl::base::SpaceInformation> spaceInfo_;
245 
247  std::shared_ptr<ompl::base::StateSpace> space_;
248 
250  std::shared_ptr<ompl::base::ProblemDefinition> problem_;
251 
253  std::shared_ptr<ompl::base::OptimizationObjective> objective_;
254 
256  std::vector<std::shared_ptr<State>> startStates_;
257 
259  std::vector<std::shared_ptr<State>> goalStates_;
260 
263  std::vector<std::shared_ptr<State>> prunedStartStates_;
264 
267  std::vector<std::shared_ptr<State>> prunedGoalStates_;
268 
270  bool isPruningEnabled_{true};
271 
273  bool isMultiqueryEnabled_{false};
274 
276  bool useKNearest_{true};
277 
279  unsigned int maxNumGoals_{1u};
280 
283  std::size_t numNeighbors_{std::numeric_limits<std::size_t>::max()};
284 
286  std::size_t k_rgg_{std::numeric_limits<std::size_t>::max()};
287 
289  double radius_{std::numeric_limits<double>::infinity()};
290 
292  double radiusFactor_{1.001};
293 
295  unsigned int effortThreshold_{50000};
296 
298  const double dimension_;
299 
301  const double unitNBallMeasure_;
302 
304  const ompl::base::Cost &solutionCost_;
305 
307  ompl::base::Cost minPossibleCost_{std::numeric_limits<double>::signaling_NaN()};
308 
310  mutable unsigned int numSampledStates_{0u};
311 
313  mutable unsigned int numValidSamples_{0u};
314 
316  mutable unsigned int numNearestNeighborCalls_{0u};
317  };
318 
319  } // namespace eitstar
320 
321  } // namespace geometric
322 
323 } // namespace ompl
324 
325 #endif // OMPL_GEOMETRIC_PLANNERS_INFORMEDTREES_EITSTAR_RANDOM_GEOMETRIC_GRAPH_
Helper class to extract valid start & goal states. Usually used internally by planners.
Definition: Planner.h:141
bool hasStartState() const
Returns whether a start state is available.
void enableMultiquery(bool multiquery)
Enable multiquery usage of the graph.
void setMaxNumberOfGoals(unsigned int maxNumberOfGoals)
Sets the maximum number of goals EIT* will sample from sampleable goal regions.
void setUseKNearest(bool useKNearest)
Set whether to use a k-nearest connection model. If false, it uses an r-disc model.
void updateStartAndGoalStates(const ompl::base::PlannerTerminationCondition &terminationCondition, ompl::base::PlannerInputStates *inputStates)
Adds new starts and goals to the graph if available and creates a new informed sampler if necessary.
unsigned int getMaxNumberOfGoals() const
Returns the maximum number of goals EIT* will sample from sampleable goal regions.
RandomGeometricGraph(const std::shared_ptr< ompl::base::SpaceInformation > &spaceInfo, const ompl::base::Cost &solutionCost)
Constructs a random geometric graph with the given space information and reference to the current sol...
bool isMultiqueryEnabled() const
Returns Whether multiquery usage of the graph is enabled.
Definition of an abstract state.
Definition: State.h:113
bool getUseKNearest() const
Returns whether the graph uses a k-nearest connection model. If false, it uses an r-disc model.
unsigned int inadmissibleEffortToCome(const std::shared_ptr< State > &state) const
Returns the inadmissible effort to come.
bool isGoal(const std::shared_ptr< State > &state) const
Returns whether the given state is a goal state.
unsigned int getNumberOfValidSamples() const
Returns the number of valid samples.
void setRadiusFactor(double factor)
Sets the radius factor (eta in the paper).
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:111
bool isStart(const std::shared_ptr< State > &state) const
Returns whether the given state is a start state.
std::size_t getTag() const
Returns the tag of the current RGG.
unsigned int getEffortThreshold() const
Gets the effort threshold.
unsigned int getNumberOfSampledStates() const
Returns the number of sampled states.
bool addStates(std::size_t numStates, const ompl::base::PlannerTerminationCondition &terminationCondition)
Samples random states and adds them to the graph.
const std::vector< std::shared_ptr< State > > & getStartStates() const
Returns the start states.
const std::vector< std::shared_ptr< State > > & getGoalStates() const
Returns the goal states.
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
void enablePruning(bool prune)
Enable pruning of the graph.
void setup(const std::shared_ptr< ompl::base::ProblemDefinition > &problem, ompl::base::PlannerInputStates *inputStates)
Setup the graph with the given problem definition and planner input states.
std::shared_ptr< State > registerStartState(const ompl::base::State *start)
Sets the start state.
void prune()
Prunes the graph of states that can not improve the current solution.
void clearQuery()
Clears all query-specific structures, such as start and goal states.
ompl::base::Cost minPossibleCost() const
Returns the minimum possible cost for the current problem, using admissible cost estimates to calcula...
void clear()
Clears all internal planner structures but retains settings. Subsequent calls to solve() will start f...
bool hasGoalState() const
Returns whether a goal state is available.
unsigned int getNumberOfNearestNeighborCalls() const
Returns the number of nearest neighbor calls.
void setEffortThreshold(const unsigned int threshold)
Sets the effort threshold.
std::vector< std::weak_ptr< State > > getNeighbors(const std::shared_ptr< State > &state) const
Returns the neighbors of a state.
std::shared_ptr< State > registerGoalState(const ompl::base::State *goal)
Sets the goal state.
void registerWhitelistedState(const std::shared_ptr< State > &state) const
Registers a whitelisted state.
std::vector< std::shared_ptr< State > > getStates() const
Returns all sampled states (that have not been pruned).
void registerInvalidEdge(const Edge &edge) const
Registers an invalid edge.
~RandomGeometricGraph()=default
Destricts this random geometric graph.
bool isPruningEnabled() const
Returns Whether pruning is enabled.
Main namespace. Contains everything in this library.
Definition: AppBase.h:21
double getRadiusFactor() const
Returns the radius factor (eta in the paper).