ompl/tools/benchmark/Benchmark.h
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Rice University 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Rice University nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 /* Author: Ioan Sucan */ 00036 00037 #ifndef OMPL_TOOLS_BENCHMARK_BENCHMARK_ 00038 #define OMPL_TOOLS_BENCHMARK_BENCHMARK_ 00039 00040 #include "ompl/geometric/SimpleSetup.h" 00041 #include "ompl/control/SimpleSetup.h" 00042 00043 namespace ompl 00044 { 00045 namespace tools 00046 { 00048 class Benchmark 00049 { 00050 public: 00051 00056 struct Status 00057 { 00058 Status() 00059 { 00060 running = false; 00061 activeRun = 0; 00062 progressPercentage = 0.0; 00063 } 00064 00066 bool running; 00067 00069 std::string activePlanner; 00070 00072 unsigned int activeRun; 00073 00075 double progressPercentage; 00076 }; 00077 00080 typedef std::map<std::string, std::string> RunProperties; 00081 00082 typedef std::vector<std::map<std::string, std::string> > RunProgressData; 00083 00085 typedef boost::function<void(const base::PlannerPtr&)> PreSetupEvent; 00086 00088 typedef boost::function<void(const base::PlannerPtr&, RunProperties&)> PostSetupEvent; 00089 00091 struct PlannerExperiment 00092 { 00094 std::string name; 00095 00097 std::vector<RunProperties> runs; 00098 00101 std::vector<std::string> progressPropertyNames; 00102 00105 std::vector<RunProgressData> runsProgressData; 00106 00108 RunProperties common; 00109 00110 bool operator==(const PlannerExperiment& p) const 00111 { 00112 return name==p.name && runs==p.runs && common==p.common; 00113 } 00114 }; 00115 00117 struct CompleteExperiment 00118 { 00120 std::string name; 00121 00123 std::vector<PlannerExperiment> planners; 00124 00126 double maxTime; 00127 00129 double maxMem; 00130 00132 unsigned int runCount; 00133 00135 time::point startTime; 00136 00138 double totalDuration; 00139 00141 std::string setupInfo; 00142 00144 boost::uint32_t seed; 00145 00147 std::string host; 00148 00150 std::string cpuInfo; 00151 }; 00152 00154 struct Request 00155 { 00157 Request(double maxTime = 5.0, double maxMem = 4096.0, 00158 unsigned int runCount = 100, 00159 double timeBetweenUpdates = 0.05, 00160 bool displayProgress = true, 00161 bool saveConsoleOutput = true, bool useThreads = true) 00162 : maxTime(maxTime), maxMem(maxMem), runCount(runCount), 00163 timeBetweenUpdates(timeBetweenUpdates), 00164 displayProgress(displayProgress), saveConsoleOutput(saveConsoleOutput), 00165 useThreads(useThreads) 00166 { 00167 } 00168 00170 double maxTime; 00171 00173 double maxMem; 00174 00176 unsigned int runCount; 00177 00179 double timeBetweenUpdates; 00180 00182 bool displayProgress; 00183 00185 bool saveConsoleOutput; 00186 00188 bool useThreads; 00189 }; 00190 00192 Benchmark(geometric::SimpleSetup &setup, const std::string &name = std::string()) : gsetup_(&setup), csetup_(NULL) 00193 { 00194 exp_.name = name; 00195 } 00196 00198 Benchmark(control::SimpleSetup &setup, const std::string &name = std::string()) : gsetup_(NULL), csetup_(&setup) 00199 { 00200 exp_.name = name; 00201 } 00202 00203 virtual ~Benchmark() 00204 { 00205 } 00206 00208 void setExperimentName(const std::string &name) 00209 { 00210 exp_.name = name; 00211 } 00212 00214 const std::string& getExperimentName() const 00215 { 00216 return exp_.name; 00217 } 00218 00220 void addPlanner(const base::PlannerPtr &planner) 00221 { 00222 if (planner && planner->getSpaceInformation().get() != 00223 (gsetup_ ? gsetup_->getSpaceInformation().get() : csetup_->getSpaceInformation().get())) 00224 throw Exception("Planner instance does not match space information"); 00225 planners_.push_back(planner); 00226 } 00227 00229 void addPlannerAllocator(const base::PlannerAllocator &pa) 00230 { 00231 planners_.push_back(pa(gsetup_ ? gsetup_->getSpaceInformation() : csetup_->getSpaceInformation())); 00232 } 00233 00235 void clearPlanners() 00236 { 00237 planners_.clear(); 00238 } 00239 00241 void setPlannerSwitchEvent(const PreSetupEvent &event) 00242 { 00243 plannerSwitch_ = event; 00244 } 00245 00247 void setPreRunEvent(const PreSetupEvent &event) 00248 { 00249 preRun_ = event; 00250 } 00251 00253 void setPostRunEvent(const PostSetupEvent &event) 00254 { 00255 postRun_ = event; 00256 } 00257 00269 virtual void benchmark(const Request &req); 00270 00272 const Status& getStatus() const 00273 { 00274 return status_; 00275 } 00276 00281 const CompleteExperiment& getRecordedExperimentData() const 00282 { 00283 return exp_; 00284 } 00285 00287 virtual bool saveResultsToStream(std::ostream &out = std::cout) const; 00288 00290 bool saveResultsToFile(const char *filename) const; 00291 00293 bool saveResultsToFile() const; 00294 00295 protected: 00296 00298 geometric::SimpleSetup *gsetup_; 00299 00301 control::SimpleSetup *csetup_; 00302 00304 std::vector<base::PlannerPtr> planners_; 00305 00307 CompleteExperiment exp_; 00308 00310 Status status_; 00311 00313 PreSetupEvent plannerSwitch_; 00314 00316 PreSetupEvent preRun_; 00317 00319 PostSetupEvent postRun_; 00320 00321 }; 00322 } 00323 } 00324 #endif