ExperienceSetup.h
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014, University of Colorado, Boulder
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 Univ of CO, Boulder 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  */
37 
38 #ifndef OMPL_TOOLS_EXPERIENCE__EXPERIENCE_SETUP_
39 #define OMPL_TOOLS_EXPERIENCE__EXPERIENCE_SETUP_
40 
41 #include "ompl/geometric/SimpleSetup.h"
42 
43 namespace ompl
44 {
45  namespace tools
46  {
48  OMPL_CLASS_FORWARD(ExperienceSetup);
50 
56  class ExperienceSetup : public geometric::SimpleSetup
57  {
58  public:
62  struct ExperienceStats
63  {
64  ExperienceStats() = default;
65 
66  double getAveragePlanningTime() const
67  {
68  if (numProblems_ == 0.0)
69  return 0.0;
70 
71  return totalPlanningTime_ / numProblems_;
72  }
73 
74  double getAverageInsertionTime() const
75  {
76  if (numProblems_ == 0.0)
77  return 0.0;
78 
79  // Clean up output
80  double time = totalInsertionTime_ / numProblems_;
81  if (time < 1e-8)
82  return 0.0;
83  return totalInsertionTime_ / numProblems_;
84  }
85 
86  double numSolutionsFromRecall_{0.};
87  double numSolutionsFromRecallSaved_{0.};
88  double numSolutionsFromScratch_{0.};
89  double numSolutionsFailed_{0.};
90  double numSolutionsTimedout_{0.};
91  double numSolutionsApproximate_{0.};
92  double numSolutionsTooShort_{0.}; // less than 3 states
93  double numProblems_{0.}; // input requests
94  double totalPlanningTime_{0.}; // of all input requests, used for averaging
95  double totalInsertionTime_{0.}; // of all input requests, used for averaging
96  };
97 
101  struct ExperienceLog
102  {
103  ExperienceLog() = default;
104  // Times
105  double planning_time{0.0};
106  double insertion_time{0.0};
107  // Solution properties
108  std::string planner{"NA"};
109  std::string result{"NA"};
110  std::string is_saved{"NA"};
111  // Failure booleans
112  bool approximate{false};
113  bool too_short{false};
114  bool insertion_failed{false};
115  // Lightning properties
116  double score{0.0};
117  // Thunder (SPARS) properties
118  std::size_t num_vertices{0};
119  std::size_t num_edges{0};
120  std::size_t num_connected_components{0};
121  };
122 
124  explicit ExperienceSetup(const base::SpaceInformationPtr &si);
125 
127  explicit ExperienceSetup(const base::StateSpacePtr &space);
128 
130  void logInitialize();
131 
133  void convertLogToString(const ExperienceLog &log);
134 
136  virtual void printResultsInfo(std::ostream &out = std::cout) const = 0;
137 
139  virtual void printLogs(std::ostream &out = std::cout) const = 0;
140 
142  virtual void saveDataLog(std::ostream &out = std::cout);
143 
147  virtual void setRepairPlanner(const base::PlannerPtr &planner) = 0;
148 
150  virtual bool save() = 0;
151 
153  virtual bool saveIfChanged() = 0;
154 
156  void enablePlanningFromRecall(bool enable);
157 
161  void enablePlanningFromScratch(bool enable);
162 
164  virtual void getAllPlannerDatas(std::vector<ompl::base::PlannerDataPtr> &plannerDatas) const = 0;
165 
167  virtual std::size_t getExperiencesCount() const = 0;
168 
171  virtual const std::string &getFilePath() const;
172 
176  virtual bool setFilePath(const std::string &filePath);
177 
181  const ExperienceStats &getStats() const
182  {
183  return stats_;
184  }
185 
189  virtual bool doPostProcessing()
190  {
191  return true;
192  }
193 
194  protected:
196  bool recallEnabled_{true};
197 
199  bool scratchEnabled_{true};
200 
202  std::string filePath_;
203 
204  // output data to file to analyze performance externally
205  std::stringstream csvDataLogStream_;
206 
208  ExperienceStats stats_;
209  };
210  }
211 }
212 #endif
void logInitialize()
Load the header (first row) of the csv file.
virtual void getAllPlannerDatas(std::vector< ompl::base::PlannerDataPtr > &plannerDatas) const =0
Get a vector of all the planning data in the database.
ExperienceSetup(const base::SpaceInformationPtr &si)
Constructor needs the state space used for planning.
virtual void printResultsInfo(std::ostream &out=std::cout) const =0
Display debug data about potential available solutions.
virtual void saveDataLog(std::ostream &out=std::cout)
Save debug data about overall results since being loaded.
virtual std::size_t getExperiencesCount() const =0
Get the total number of paths stored in the database.
Simple logging functionality encapsled in a struct.
virtual const std::string & getFilePath() const
After setFile() is called, access the generated file path for loading and saving the experience datab...
virtual void setRepairPlanner(const base::PlannerPtr &planner)=0
Set the planner to use for repairing experience paths inside the RetrieveRepair planner....
void convertLogToString(const ExperienceLog &log)
Move data to string format and put in buffer.
virtual bool saveIfChanged()=0
Save the experience database to file if there has been a change.
void log(const char *file, int line, LogLevel level, const char *m,...)
Root level logging function. This should not be invoked directly, but rather used via a logging macro...
Definition: Console.cpp:120
bool scratchEnabled_
Flag indicating whether planning from scratch should be used to find solutions. Enabled by default.
virtual bool setFilePath(const std::string &filePath)
Set the database file to load. Actual loading occurs when setup() is called.
bool recallEnabled_
Flag indicating whether recalled plans should be used to find solutions. Enabled by default.
virtual bool save()=0
Save the experience database to file.
std::string filePath_
File location of database.
virtual bool doPostProcessing()
Allow accumlated experiences to be processed.
void enablePlanningFromScratch(bool enable)
Optionally disable the ability to plan from scratch Note: Lightning can still save modified experienc...
const ExperienceStats & getStats() const
Getter for logging data.
void enablePlanningFromRecall(bool enable)
Optionally disable the ability to use previous plans in solutions (but will still save them)
virtual void printLogs(std::ostream &out=std::cout) const =0
Display debug data about overall results since being loaded.
ExperienceStats stats_
States data for display to console
Main namespace. Contains everything in this library.
Definition: AppBase.h:21