ParallelPlan.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Willow Garage, Inc.
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 Willow Garage 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 #include "ompl/tools/multiplan/ParallelPlan.h"
38 #include "ompl/geometric/PathHybridization.h"
39 #include <thread>
40 
42  pdef_(pdef), phybrid_(new geometric::PathHybridization(pdef->getSpaceInformation()))
43 {
44 }
45 
46 ompl::tools::ParallelPlan::~ParallelPlan()
47 {
48 }
49 
51 {
52  if (planner && planner->getSpaceInformation().get() != pdef_->getSpaceInformation().get())
53  throw Exception("Planner instance does not match space information");
54  if (planner->getProblemDefinition().get() != pdef_.get())
55  planner->setProblemDefinition(pdef_);
56  planners_.push_back(planner);
57 }
58 
60 {
61  base::PlannerPtr planner = pa(pdef_->getSpaceInformation());
62  planner->setProblemDefinition(pdef_);
63  planners_.push_back(planner);
64 }
65 
67 {
68  planners_.clear();
69 }
70 
72 {
73  phybrid_->clear();
74 }
75 
77 {
78  return solve(solveTime, 1, planners_.size(), hybridize);
79 }
80 
81 ompl::base::PlannerStatus ompl::tools::ParallelPlan::solve(double solveTime, std::size_t minSolCount, std::size_t maxSolCount, bool hybridize)
82 {
83  return solve(base::timedPlannerTerminationCondition(solveTime, std::min(solveTime / 100.0, 0.1)), minSolCount, maxSolCount, hybridize);
84 }
85 
86 
88 {
89  return solve(ptc, 1, planners_.size(), hybridize);
90 }
91 
93  std::size_t maxSolCount, bool hybridize)
94 {
95  if (!pdef_->getSpaceInformation()->isSetup())
96  pdef_->getSpaceInformation()->setup();
97  foundSolCount_ = 0;
98 
99  time::point start = time::now();
100  std::vector<std::thread*> threads(planners_.size());
101 
102  // Decide if we are combining solutions or just taking the first one
103  if (hybridize)
104  for (std::size_t i = 0 ; i < threads.size() ; ++i)
105  threads[i] = new std::thread(std::bind(&ParallelPlan::solveMore, this, planners_[i].get(), minSolCount, maxSolCount, &ptc));
106  else
107  for (std::size_t i = 0 ; i < threads.size() ; ++i)
108  threads[i] = new std::thread(std::bind(&ParallelPlan::solveOne, this, planners_[i].get(), minSolCount, &ptc));
109 
110  for (std::size_t i = 0 ; i < threads.size() ; ++i)
111  {
112  threads[i]->join();
113  delete threads[i];
114  }
115 
116  if (hybridize)
117  {
118  if (phybrid_->pathCount() > 1)
119  if (const base::PathPtr &hsol = phybrid_->getHybridPath())
120  {
121  geometric::PathGeometric *pg = static_cast<geometric::PathGeometric*>(hsol.get());
122  double difference = 0.0;
123  bool approximate = !pdef_->getGoal()->isSatisfied(pg->getStates().back(), &difference);
124  pdef_->addSolutionPath(hsol, approximate, difference, phybrid_->getName()); // name this solution after the hybridization algorithm
125  }
126  }
127 
128  if (pdef_->hasSolution())
129  OMPL_INFORM("ParallelPlan::solve(): Solution found by one or more threads in %f seconds", time::seconds(time::now() - start));
130  else
131  OMPL_WARN("ParallelPlan::solve(): Unable to find solution by any of the threads in %f seconds", time::seconds(time::now() - start));
132 
133  return base::PlannerStatus(pdef_->hasSolution(), pdef_->hasApproximateSolution());
134 }
135 
137 {
138  OMPL_DEBUG("ParallelPlan.solveOne starting planner %s", planner->getName().c_str());
139 
140  time::point start = time::now();
141  if (planner->solve(*ptc))
142  {
143  double duration = time::seconds(time::now() - start);
144  foundSolCountLock_.lock();
145  unsigned int nrSol = ++foundSolCount_;
146  foundSolCountLock_.unlock();
147  if (nrSol >= minSolCount)
148  ptc->terminate();
149  OMPL_DEBUG("ParallelPlan.solveOne: Solution found by %s in %lf seconds", planner->getName().c_str(), duration);
150  }
151 }
152 
153 void ompl::tools::ParallelPlan::solveMore(base::Planner *planner, std::size_t minSolCount, std::size_t maxSolCount,
155 {
156  OMPL_DEBUG("ParallelPlan.solveMore: starting planner %s", planner->getName().c_str());
157 
158  time::point start = time::now();
159  if (planner->solve(*ptc))
160  {
161  double duration = time::seconds(time::now() - start);
162  foundSolCountLock_.lock();
163  unsigned int nrSol = ++foundSolCount_;
164  foundSolCountLock_.unlock();
165 
166  if (nrSol >= maxSolCount)
167  ptc->terminate();
168 
169  OMPL_DEBUG("ParallelPlan.solveMore: Solution found by %s in %lf seconds", planner->getName().c_str(), duration);
170 
171  const std::vector<base::PlannerSolution> &paths = pdef_->getSolutions();
172 
173  std::lock_guard<std::mutex> slock(phlock_);
174  start = time::now();
175  unsigned int attempts = 0;
176  for (std::size_t i = 0 ; i < paths.size() ; ++i)
177  attempts += phybrid_->recordPath(paths[i].path_, false);
178 
179  if (phybrid_->pathCount() >= minSolCount)
180  phybrid_->computeHybridPath();
181 
182  duration = time::seconds(time::now() - start);
183  OMPL_DEBUG("ParallelPlan.solveMore: Spent %f seconds hybridizing %u solution paths (attempted %u connections between paths)", duration,
184  (unsigned int)phybrid_->pathCount(), attempts);
185  }
186 }
void clearHybridizationPaths()
Clear the set of paths recorded for hybrididzation.
A shared pointer wrapper for ompl::base::ProblemDefinition.
void addPlannerAllocator(const base::PlannerAllocator &pa)
Add a planner allocator to use.
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
void solveMore(base::Planner *planner, std::size_t minSolCount, std::size_t maxSolCount, const base::PlannerTerminationCondition *ptc)
Run the planner and collect the solutions. This function is only called if hybridize_ is true...
void clearPlanners()
Clear the set of planners to be executed.
duration seconds(double sec)
Return the time duration representing a given number of seconds.
Definition: Time.h:78
PlannerTerminationCondition timedPlannerTerminationCondition(double duration)
Return a termination condition that will become true duration seconds in the future (wall-time) ...
base::PlannerStatus solve(double solveTime, bool hybridize=true)
Call Planner::solve() for all planners, in parallel, each planner running for at most solveTime secon...
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.
ParallelPlan(const base::ProblemDefinitionPtr &pdef)
Create an instance for a specified space information.
std::vector< base::State * > & getStates()
Get the states that make up the path (as a reference, so it can be modified, hence the function is no...
Base class for a planner.
Definition: Planner.h:230
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
void addPlanner(const base::PlannerPtr &planner)
Add a planner to use.
virtual PlannerStatus solve(const PlannerTerminationCondition &ptc)=0
Function that can solve the motion planning problem. This function can be called multiple times on th...
The exception type for ompl.
Definition: Exception.h:47
#define OMPL_DEBUG(fmt,...)
Log a formatted debugging string.
Definition: Console.h:70
point now()
Get the current time point.
Definition: Time.h:72
void solveOne(base::Planner *planner, std::size_t minSolCount, const base::PlannerTerminationCondition *ptc)
Run the planner and call ompl::base::PlannerTerminationCondition::terminate() for the other planners ...
Definition of a geometric path.
Definition: PathGeometric.h:60
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: Time.h:66
void terminate() const
Notify that the condition for termination should become true, regardless of what eval() returns...
const std::string & getName() const
Get the name of the planner.
Definition: Planner.cpp:55
A shared pointer wrapper for ompl::base::Path.
#define OMPL_INFORM(fmt,...)
Log a formatted information string.
Definition: Console.h:68