LightningDB.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 
43 #ifndef OMPL_TOOLS_LIGHTNING_LIGHTNINGDB_
44 #define OMPL_TOOLS_LIGHTNING_LIGHTNINGDB_
45 
46 #include "ompl/base/StateSpace.h"
47 #include "ompl/geometric/PathGeometric.h"
48 #include "ompl/base/PlannerData.h"
49 #include "ompl/base/PlannerDataStorage.h"
50 #include "ompl/base/State.h"
51 #include "ompl/base/SpaceInformation.h"
52 #include "ompl/datastructures/NearestNeighbors.h"
53 
54 namespace ompl
55 {
56 
57  namespace tools
58  {
65  OMPL_CLASS_FORWARD(LightningDB);
68 
74  {
75  public:
76 
80  LightningDB(const base::StateSpacePtr &space);
81 
85  virtual ~LightningDB();
86 
92  bool load(const std::string &fileName);
93 
101  void addPath(geometric::PathGeometric &solutionPath, double &insertionTime);
102  void addPathHelper(geometric::PathGeometric &solutionPath);
103 
109  bool saveIfChanged(const std::string &fileName);
110 
116  bool save(const std::string &fileName);
117 
121  void getAllPlannerDatas(std::vector<ompl::base::PlannerDataPtr> &plannerDatas) const;
122 
126  std::vector<ompl::base::PlannerDataPtr> findNearestStartGoal(int nearestK,
127  const base::State *start, const base::State *goal);
128 
130  std::size_t getExperiencesCount() const;
131 
133  std::size_t getStatesCount() const;
134 
136  int getNumUnsavedPaths() const
137  {
138  return numUnsavedPaths_;
139  }
140 
145  bool isEmpty()
146  {
147  return getExperiencesCount() == 0;
148  }
149 
150  private:
151 
155  double distanceFunction(const ompl::base::PlannerDataPtr a, const ompl::base::PlannerDataPtr b) const;
156 
157  protected:
158 
161 
164 
165  // A nearest-neighbors datastructure containing the tree of start/goal states combined
166  std::shared_ptr< NearestNeighbors<ompl::base::PlannerDataPtr> > nn_;
167 
168  // Reusable plannerData instance for filling in start and goal and performing searches on the tree
169  ompl::base::PlannerDataPtr nnSearchKey_;
170 
171  // Track unsaved paths to determine if a save is required
172  int numUnsavedPaths_;
173 
174  }; // end of class LightningDB
175 
176  } // end of namespace
177 
178 } // end of namespace
179 #endif
Save and load entire paths from file.
Definition: LightningDB.h:73
Object that handles loading/storing a PlannerData object to/from a binary stream. Serialization of ve...
std::size_t getExperiencesCount() const
Get the total number of paths stored in the database.
A shared pointer wrapper for ompl::base::StateSpace.
void addPath(geometric::PathGeometric &solutionPath, double &insertionTime)
Add a new solution path to our database. Des not actually save to file so experience will be lost if ...
LightningDB(const base::StateSpacePtr &space)
Constructor needs the state space used for planning.
Definition: LightningDB.cpp:47
std::vector< ompl::base::PlannerDataPtr > findNearestStartGoal(int nearestK, const base::State *start, const base::State *goal)
Find the k nearest paths to our queries one.
A shared pointer wrapper for ompl::base::SpaceInformation.
bool load(const std::string &fileName)
Load database from file.
Definition: LightningDB.cpp:68
Definition of an abstract state.
Definition: State.h:50
ompl::base::PlannerDataStorage plannerDataStorage_
Helper class for storing each plannerData instance.
Definition: LightningDB.h:163
int getNumUnsavedPaths() const
Get number of unsaved paths.
Definition: LightningDB.h:136
std::size_t getStatesCount() const
Get the total number of states stored in the database, across all paths.
void getAllPlannerDatas(std::vector< ompl::base::PlannerDataPtr > &plannerDatas) const
Get a vector of all the paths in the nearest neighbor tree.
virtual ~LightningDB()
Deconstructor.
Definition: LightningDB.cpp:62
Definition of a geometric path.
Definition: PathGeometric.h:60
base::SpaceInformationPtr si_
The created space information.
Definition: LightningDB.h:160
bool saveIfChanged(const std::string &fileName)
Save loaded database to file, except skips saving if no paths have been added.
bool isEmpty()
Check if anything has been loaded into DB.
Definition: LightningDB.h:145
A shared pointer wrapper for ompl::base::PlannerData.
bool save(const std::string &fileName)
Save loaded database to file.