Planner.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Rice University
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 Rice University 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 /* Author: Ioan Sucan */
36 
37 #ifndef OMPL_BASE_PLANNER_
38 #define OMPL_BASE_PLANNER_
39 
40 #include "ompl/base/SpaceInformation.h"
41 #include "ompl/base/ProblemDefinition.h"
42 #include "ompl/base/PlannerData.h"
43 #include "ompl/base/PlannerStatus.h"
44 #include "ompl/base/PlannerTerminationCondition.h"
45 #include "ompl/base/GenericParam.h"
46 #include "ompl/util/Console.h"
47 #include "ompl/util/Time.h"
48 #include "ompl/util/ClassForward.h"
49 #include "ompl/util/Deprecation.h"
50 #include <functional>
51 #include <boost/concept_check.hpp>
52 #include <string>
53 #include <map>
54 
55 namespace ompl
56 {
57 
58  namespace base
59  {
60 
62 
63  OMPL_CLASS_FORWARD(Planner);
65 
82  {
83  public:
84 
86  PlannerInputStates(const PlannerPtr &planner) : planner_(planner.get())
87  {
88  tempState_ = nullptr;
89  update();
90  }
91 
93  PlannerInputStates(const Planner *planner) : planner_(planner)
94  {
95  tempState_ = nullptr;
96  update();
97  }
98 
102  PlannerInputStates() : planner_(nullptr)
103  {
104  tempState_ = nullptr;
105  clear();
106  }
107 
110  {
111  clear();
112  }
113 
115  void clear();
116 
120  void restart();
121 
127  bool update();
128 
134  bool use(const ProblemDefinitionPtr &pdef);
135 
141  bool use(const ProblemDefinition *pdef);
142 
145  void checkValidity() const;
146 
149  const State* nextStart();
150 
159  const State* nextGoal(const PlannerTerminationCondition &ptc);
160 
162  const State* nextGoal();
163 
165  bool haveMoreStartStates() const;
166 
168  bool haveMoreGoalStates() const;
169 
173  unsigned int getSeenStartStatesCount() const
174  {
175  return addedStartStates_;
176  }
177 
179  unsigned int getSampledGoalsCount() const
180  {
181  return sampledGoalsCount_;
182  }
183 
184  private:
185 
186  const Planner *planner_;
187 
188  unsigned int addedStartStates_;
189  unsigned int sampledGoalsCount_;
190  State *tempState_;
191 
192  const ProblemDefinition *pdef_;
193  const SpaceInformation *si_;
194  };
195 
198  {
199  PlannerSpecs() : recognizedGoal(GOAL_ANY), multithreaded(false), approximateSolutions(false),
200  optimizingPaths(false), directed(false), provingSolutionNonExistence(false),
201  canReportIntermediateSolutions(false)
202  {
203  }
204 
207 
210 
213 
217 
220  bool directed;
221 
224 
227  };
228 
230  class Planner
231  {
232 
233  public:
234  // non-copyable
235  Planner(const Planner&) = delete;
236  Planner& operator=(const Planner&) = delete;
237 
239  Planner(const SpaceInformationPtr &si, const std::string &name);
240 
242  virtual ~Planner()
243  {
244  }
245 
247  template<class T>
248  T* as()
249  {
251  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, Planner*>));
252 
253  return static_cast<T*>(this);
254  }
255 
257  template<class T>
258  const T* as() const
259  {
261  BOOST_CONCEPT_ASSERT((boost::Convertible<T*, Planner*>));
262 
263  return static_cast<const T*>(this);
264  }
265 
267  const SpaceInformationPtr& getSpaceInformation() const;
268 
270  const ProblemDefinitionPtr& getProblemDefinition() const;
271 
273  const PlannerInputStates& getPlannerInputStates() const;
274 
279  virtual void setProblemDefinition(const ProblemDefinitionPtr &pdef);
280 
293  virtual PlannerStatus solve(const PlannerTerminationCondition &ptc) = 0;
294 
297  PlannerStatus solve(const PlannerTerminationConditionFn &ptc, double checkInterval);
298 
302  PlannerStatus solve(double solveTime);
303 
307  virtual void clear();
308 
315  virtual void getPlannerData(PlannerData &data) const;
316 
318  const std::string& getName() const;
319 
321  void setName(const std::string &name);
322 
324  const PlannerSpecs& getSpecs() const;
325 
330  virtual void setup();
331 
336  virtual void checkValidity();
337 
339  bool isSetup() const;
340 
343  {
344  return params_;
345  }
346 
348  const ParamSet& params() const
349  {
350  return params_;
351  }
352 
354  typedef std::function<std::string ()> PlannerProgressProperty;
355 
357  typedef std::map<std::string, PlannerProgressProperty> PlannerProgressProperties;
358 
360  const PlannerProgressProperties& getPlannerProgressProperties() const
361  {
362  return plannerProgressProperties_;
363  }
364 
366  virtual void printProperties(std::ostream &out) const;
367 
369  virtual void printSettings(std::ostream &out) const;
370 
371  protected:
372 
374  template<typename T, typename PlannerType, typename SetterType, typename GetterType>
375  void declareParam(const std::string &name, const PlannerType &planner, const SetterType& setter, const GetterType& getter, const std::string &rangeSuggestion = "")
376  {
377  params_.declareParam<T>(name, std::bind(setter, planner, std::placeholders::_1), std::bind(getter, planner));
378  if (!rangeSuggestion.empty())
379  params_[name].setRangeSuggestion(rangeSuggestion);
380  }
381 
383  template<typename T, typename PlannerType, typename SetterType>
384  void declareParam(const std::string &name, const PlannerType &planner, const SetterType& setter, const std::string &rangeSuggestion = "")
385  {
386  params_.declareParam<T>(name, std::bind(setter, planner, std::placeholders::_1));
387  if (!rangeSuggestion.empty())
388  params_[name].setRangeSuggestion(rangeSuggestion);
389  }
390 
392  void addPlannerProgressProperty(const std::string& progressPropertyName, const PlannerProgressProperty& prop)
393  {
394  plannerProgressProperties_[progressPropertyName] = prop;
395  }
396 
399 
402 
405 
407  std::string name_;
408 
411 
414 
416  PlannerProgressProperties plannerProgressProperties_;
417 
419  bool setup_;
420  };
421 
423  typedef std::function<PlannerPtr(const SpaceInformationPtr&)> PlannerAllocator;
424  }
425 }
426 
427 #endif
bool approximateSolutions
Flag indicating whether the planner is able to compute approximate solutions.
Definition: Planner.h:212
void addPlannerProgressProperty(const std::string &progressPropertyName, const PlannerProgressProperty &prop)
Add a planner progress property called progressPropertyName with a property querying function prop to...
Definition: Planner.h:392
Properties that planners may have.
Definition: Planner.h:197
void declareParam(const std::string &name, const PlannerType &planner, const SetterType &setter, const GetterType &getter, const std::string &rangeSuggestion="")
This function declares a parameter for this planner instance, and specifies the setter and getter fun...
Definition: Planner.h:375
bool haveMoreGoalStates() const
Check if there are more potential goal states.
Definition: Planner.cpp:352
A shared pointer wrapper for ompl::base::ProblemDefinition.
GoalType recognizedGoal
The type of goal specification the planner can use.
Definition: Planner.h:206
ParamSet & params()
Get the parameters for this planner.
Definition: Planner.h:342
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
std::function< bool()> PlannerTerminationConditionFn
Signature for functions that decide whether termination conditions have been met for a planner...
bool canReportIntermediateSolutions
Flag indicating whether the planner is able to report the computation of intermediate paths...
Definition: Planner.h:226
virtual ~Planner()
Destructor.
Definition: Planner.h:242
unsigned int getSeenStartStatesCount() const
Get the number of start states from the problem definition that were already seen, including invalid ones.
Definition: Planner.h:173
ProblemDefinitionPtr pdef_
The user set problem definition.
Definition: Planner.h:401
bool multithreaded
Flag indicating whether multiple threads are used in the computation of the planner.
Definition: Planner.h:209
bool directed
Flag indicating whether the planner is able to account for the fact that the validity of a motion fro...
Definition: Planner.h:220
Maintain a set of parameters.
Definition: GenericParam.h:233
const ParamSet & params() const
Get the parameters for this planner.
Definition: Planner.h:348
PlannerInputStates(const Planner *planner)
Default constructor. No work is performed.
Definition: Planner.h:93
~PlannerInputStates()
Destructor. Clear allocated memory.
Definition: Planner.h:109
ParamSet params_
A map from parameter names to parameter instances for this planner. This field is populated by the de...
Definition: Planner.h:413
bool setup_
Flag indicating whether setup() has been called.
Definition: Planner.h:419
PlannerInputStates(const PlannerPtr &planner)
Default constructor. No work is performed.
Definition: Planner.h:86
bool provingSolutionNonExistence
Flag indicating whether the planner is able to prove that no solution path exists.
Definition: Planner.h:223
Main namespace. Contains everything in this library.
Definition: Cost.h:42
std::function< PlannerPtr(const SpaceInformationPtr &)> PlannerAllocator
Definition of a function that can allocate a planner.
Definition: Planner.h:423
A shared pointer wrapper for ompl::base::Planner.
const State * nextGoal()
Same as above but only one attempt is made to find a valid goal.
Definition: Planner.cpp:264
Base class for a planner.
Definition: Planner.h:230
void declareParam(const std::string &name, const PlannerType &planner, const SetterType &setter, const std::string &rangeSuggestion="")
This function declares a parameter for this planner instance, and specifies the setter function...
Definition: Planner.h:384
void clear()
Clear all stored information.
Definition: Planner.cpp:157
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
A shared pointer wrapper for ompl::base::SpaceInformation.
The base class for space information. This contains all the information about the space planning is d...
unsigned int getSampledGoalsCount() const
Get the number of sampled goal states, including invalid ones.
Definition: Planner.h:179
Definition of an abstract state.
Definition: State.h:50
PlannerInputStates pis_
Utility class to extract valid input states.
Definition: Planner.h:404
bool haveMoreStartStates() const
Check if there are more potential start states.
Definition: Planner.cpp:345
PlannerSpecs specs_
The specifications of the planner (its capabilities)
Definition: Planner.h:410
const State * nextStart()
Return the next valid start state or nullptr if no more valid start states are available.
Definition: Planner.cpp:230
GoalType
The type of goal.
Definition: GoalTypes.h:46
Definition of a problem to be solved. This includes the start state(s) for the system and a goal spec...
bool update()
Set the space information and problem definition this class operates on, based on the available plann...
Definition: Planner.cpp:176
const PlannerProgressProperties & getPlannerProgressProperties() const
Retrieve a planner&#39;s planner progress property map.
Definition: Planner.h:360
std::string name_
The name of this planner.
Definition: Planner.h:407
bool optimizingPaths
Flag indicating whether the planner attempts to optimize the path and reduce its length until the max...
Definition: Planner.h:216
Helper class to extract valid start & goal states. Usually used internally by planners.
Definition: Planner.h:81
void checkValidity() const
Check if the problem definition was set, start state are available and goal was set.
Definition: Planner.cpp:183
void restart()
Forget how many states were returned by nextStart() and nextGoal() and return all states again...
Definition: Planner.cpp:170
const T * as() const
Cast this instance to a desired type.
Definition: Planner.h:258
SpaceInformationPtr si_
The space information for which planning is done.
Definition: Planner.h:398
std::map< std::string, PlannerProgressProperty > PlannerProgressProperties
A dictionary which maps the name of a progress property to the function to be used for querying that ...
Definition: Planner.h:357
PlannerProgressProperties plannerProgressProperties_
A mapping between this planner&#39;s progress property names and the functions used for querying those pr...
Definition: Planner.h:416
PlannerInputStates()
Default constructor. No work is performed. A call to use() needs to be made, before making any calls ...
Definition: Planner.h:102
T * as()
Cast this instance to a desired type.
Definition: Planner.h:248
std::function< std::string()> PlannerProgressProperty
Definition of a function which returns a property about the planner&#39;s progress that can be queried by...
Definition: Planner.h:354
bool use(const ProblemDefinitionPtr &pdef)
Set the problem definition this class operates on. If a planner is not set in the constructor argumen...
Definition: Planner.cpp:207
This bit is set if casting to generic goal regions (ompl::base::Goal) is possible. This bit shold always be set.
Definition: GoalTypes.h:49