Lightning.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014, JSK, The University of Tokyo.
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 JSK, The University of Tokyo 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: Dave Coleman
36  Desc: Implementation of the Lightning Framework for experienced-based planning
37 
38  Paper: Berenson, Dmitry, Pieter Abbeel, and Ken Goldberg.
39  "A robot path planning framework that learns from experience."
40  Robotics and Automation (ICRA), 2012 IEEE International Conference on. IEEE, 2012.
41 
42  Notes: The user of this class should invoke the loading and saving from file, otherwise experiences
43  will be lost.
44 */
45 
46 #ifndef OMPL_TOOLS_LIGHTNING_LIGHTNING_
47 #define OMPL_TOOLS_LIGHTNING_LIGHTNING_
48 
49 #include "ompl/tools/experience/ExperienceSetup.h"
50 #include "ompl/base/Planner.h"
51 #include "ompl/base/PlannerData.h"
52 #include "ompl/base/ProblemDefinition.h"
53 #include "ompl/base/SpaceInformation.h"
54 #include "ompl/base/ProblemDefinition.h"
55 
56 #include "ompl/geometric/PathGeometric.h"
57 #include "ompl/geometric/PathSimplifier.h"
58 #include "ompl/geometric/planners/experience/LightningRetrieveRepair.h"
59 
60 #include "ompl/tools/multiplan/ParallelPlan.h"
61 #include "ompl/tools/config/SelfConfig.h"
62 
63 #include "ompl/util/Console.h"
64 #include "ompl/util/Exception.h"
65 
66 #include "ompl/tools/lightning/DynamicTimeWarp.h"
67 
68 namespace ompl
69 {
70 
71  namespace tools
72  {
73  //class LightningDB; // forward declaration
74  OMPL_CLASS_FORWARD(LightningDB);
75  OMPL_CLASS_FORWARD(ParallelPlan);
76 
90  OMPL_CLASS_FORWARD(Lightning);
93 
99  {
100  public:
101 
103  explicit
105 
109  explicit
110  Lightning(const base::StateSpacePtr &space);
111 
112  private:
113 
117  void initialize();
118 
119  public:
120 
122  void printResultsInfo(std::ostream &out = std::cout) const;
123 
125  void printLogs(std::ostream &out = std::cout) const;
126 
131  {
132  return static_cast<ompl::geometric::LightningRetrieveRepair&>(*rrPlanner_);
133  }
134 
138  void setRepairPlanner(const base::PlannerPtr &planner)
139  {
140  static_cast<og::LightningRetrieveRepair&>(*rrPlanner_).setRepairPlanner(planner);
141  }
142 
147  {
148  pa_ = pa;
149  planner_.reset();
150  // note: the rrPlanner_ never uses the allocator so does not need to be reset
151  configured_ = false;
152  }
153 
155  virtual base::PlannerStatus solve(double time = 1.0);
156 
159 
161  bool save();
162 
164  bool saveIfChanged();
165 
169  virtual void clear();
170 
172  virtual void print(std::ostream &out = std::cout) const;
173 
177  virtual void setup();
178 
180  void getAllPlannerDatas(std::vector<ompl::base::PlannerDataPtr> &plannerDatas) const;
181 
183  std::size_t getExperiencesCount() const;
184 
191 
193  const ompl::tools::DynamicTimeWarpPtr& getDynamicTimeWarp() const
194  {
195  return dtw_;
196  }
197 
198  protected:
199 
207 
210 
212  ompl::tools::ParallelPlanPtr pp_;
213 
215  ompl::tools::LightningDBPtr experienceDB_;
216 
218  ompl::tools::DynamicTimeWarpPtr dtw_;
219 
220  }; // end of class Lightning
221 
222  } // end of namespace
223 
224 } // end of namespace
225 #endif
void setRepairPlanner(const base::PlannerPtr &planner)
Set the planner that will be used for repairing invalid paths recalled from experience.
base::PlannerAllocator pa_
The optional planner allocator.
Definition: SimpleSetup.h:286
A shared pointer wrapper for ompl::base::StateSpace.
void setRepairPlanner(const base::PlannerPtr &planner)
Set the planner to use for repairing experience paths inside the LightningRetrieveRepair planner...
Definition: Lightning.h:138
bool saveIfChanged()
Save the experience database to file if there has been a change.
Definition: Lightning.cpp:362
virtual void print(std::ostream &out=std::cout) const
Print information about the current setup.
Definition: Lightning.cpp:384
virtual void setup()
This method will create the necessary classes for planning. The solve() method will call this functio...
Definition: Lightning.cpp:74
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
void getAllPlannerDatas(std::vector< ompl::base::PlannerDataPtr > &plannerDatas) const
Get a vector of all the planning data in the database.
Definition: Lightning.cpp:430
void printResultsInfo(std::ostream &out=std::cout) const
Display debug data about potential available solutions.
Definition: Lightning.cpp:372
std::size_t getExperiencesCount() const
Get the total number of paths stored in the database.
Definition: Lightning.cpp:425
ompl::tools::LightningDBPtr experienceDB_
A shared object between all the planners for saving and loading previous experience.
Definition: Lightning.h:215
bool reversePathIfNecessary(ompl::geometric::PathGeometric &path1, ompl::geometric::PathGeometric &path2)
If path1 and path2 have a better start/goal match when reverse, then reverse path2.
Definition: Lightning.cpp:442
ompl::tools::DynamicTimeWarpPtr dtw_
Tool for comparing two paths and scoring them.
Definition: Lightning.h:218
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.
base::PlannerPtr planner_
The maintained planner instance.
Definition: SimpleSetup.h:283
void convertPlannerData(const ompl::base::PlannerDataPtr plannerData, ompl::geometric::PathGeometric &path)
Convert PlannerData to PathGeometric. Assume ordering of verticies is order of path.
Definition: Lightning.cpp:435
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
A shared pointer wrapper for ompl::base::SpaceInformation.
ompl::geometric::LightningRetrieveRepair & getLightningRetrieveRepairPlanner() const
Get a pointer to the retrieve repair planner.
Definition: Lightning.h:130
The Lightning Framework&#39;s Retrieve-Repair component.
Built off of SimpleSetup but provides support for planning from experience.
Definition: Lightning.h:98
virtual void clear()
Clear all planning data. This only includes data generated by motion plan computation. Planner settings, start & goal states are not affected.
Definition: Lightning.cpp:147
bool configured_
Flag indicating whether the classes needed for planning are set up.
Definition: SimpleSetup.h:292
base::PlannerPtr rrPlanner_
The maintained experience planner instance.
Definition: Lightning.h:209
Definition of a geometric path.
Definition: PathGeometric.h:60
void printLogs(std::ostream &out=std::cout) const
Display debug data about overall results from Lightning since being loaded.
Definition: Lightning.cpp:405
const ompl::tools::DynamicTimeWarpPtr & getDynamicTimeWarp() const
Tool for comparing two paths and scoring them.
Definition: Lightning.h:193
virtual base::PlannerStatus solve(double time=1.0)
Run the planner for up to a specified amount of time (default is 1 second)
Definition: Lightning.cpp:346
bool save()
Save the experience database to file.
Definition: Lightning.cpp:352
ompl::tools::ParallelPlanPtr pp_
Instance of parallel planning to use for computing solutions in parallel.
Definition: Lightning.h:212
void setPlannerAllocator(const base::PlannerAllocator &pa)
Set the planner allocator to use. This is only used if no planner has been set. This is optional – a...
Definition: Lightning.h:146
A shared pointer wrapper for ompl::base::PlannerData.
Lightning(const base::SpaceInformationPtr &si)
Constructor needs the state space used for planning.
Definition: Lightning.cpp:45
Create the set of classes typically needed to solve a geometric problem.