Syclop.h
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Rice University
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 Rice University 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: Matt Maly */
36 
37 #ifndef OMPL_CONTROL_PLANNERS_SYCLOP_SYCLOP_
38 #define OMPL_CONTROL_PLANNERS_SYCLOP_SYCLOP_
39 
40 #include <boost/graph/astar_search.hpp>
41 #include <boost/graph/graph_traits.hpp>
42 #include <boost/graph/adjacency_list.hpp>
43 #include <boost/unordered_map.hpp>
44 #include "ompl/control/planners/PlannerIncludes.h"
45 #include "ompl/control/planners/syclop/Decomposition.h"
46 #include "ompl/control/planners/syclop/GridDecomposition.h"
47 #include "ompl/datastructures/PDF.h"
48 #include <map>
49 #include <vector>
50 
51 namespace ompl
52 {
53  namespace control
54  {
70  class Syclop : public base::Planner
71  {
72  public:
88  typedef boost::function<double(int, int)> EdgeCostFactorFn;
89 
91  typedef boost::function<void(int, int, std::vector<int>&)> LeadComputeFn;
92 
94  Syclop(const SpaceInformationPtr& si, const DecompositionPtr &d, const std::string& plannerName) : ompl::base::Planner(si, plannerName),
95  numFreeVolSamples_(Defaults::NUM_FREEVOL_SAMPLES),
96  probShortestPath_(Defaults::PROB_SHORTEST_PATH),
97  probKeepAddingToAvail_(Defaults::PROB_KEEP_ADDING_TO_AVAIL),
98  numRegionExpansions_(Defaults::NUM_REGION_EXPANSIONS),
99  numTreeSelections_(Defaults::NUM_TREE_SELECTIONS),
100  probAbandonLeadEarly_(Defaults::PROB_ABANDON_LEAD_EARLY),
101  siC_(si.get()),
102  decomp_(d),
103  covGrid_(Defaults::COVGRID_LENGTH, decomp_),
104  graphReady_(false),
105  numMotions_(0)
106  {
108 
109  Planner::declareParam<int> ("free_volume_samples", this, &Syclop::setNumFreeVolumeSamples, &Syclop::getNumFreeVolumeSamples, "10000:10000:500000");
110  Planner::declareParam<int> ("num_region_expansions", this, &Syclop::setNumRegionExpansions, &Syclop::getNumRegionExpansions, "10:10:500");
111  Planner::declareParam<int> ("num_tree_expansions", this, &Syclop::setNumTreeExpansions, &Syclop::getNumTreeExpansions, "0:1:100");
112  Planner::declareParam<double>("prob_abandon_lead_early", this, &Syclop::setProbAbandonLeadEarly, &Syclop::getProbAbandonLeadEarly, "0.:.05:1.");
113  Planner::declareParam<double>("prob_add_available_regions", this, &Syclop::setProbAddingToAvailableRegions, &Syclop::getProbAddingToAvailableRegions, "0.:.05:1.");
114  Planner::declareParam<double>("prob_shortest_path_lead", this, &Syclop::setProbShortestPathLead, &Syclop::getProbShortestPathLead, "0.:.05:1.");
115  }
116 
117  virtual ~Syclop()
118  {
119  }
120 
123 
124  virtual void setup();
125 
126  virtual void clear();
127 
132 
135 
137  void setLeadComputeFn(const LeadComputeFn& compute);
138 
140  void addEdgeCostFactor(const EdgeCostFactorFn& factor);
141 
143  void clearEdgeCostFactors();
144 
147  {
148  return numFreeVolSamples_;
149  }
150 
153  void setNumFreeVolumeSamples (int numSamples)
154  {
155  numFreeVolSamples_ = numSamples;
156  }
157 
160  double getProbShortestPathLead () const
161  {
162  return probShortestPath_;
163  }
164 
167  void setProbShortestPathLead (double probability)
168  {
169  probShortestPath_ = probability;
170  }
171 
175  {
176  return probKeepAddingToAvail_;
177  }
178 
181  void setProbAddingToAvailableRegions (double probability)
182  {
183  probKeepAddingToAvail_ = probability;
184  }
185 
189  {
190  return numRegionExpansions_;
191  }
192 
195  void setNumRegionExpansions (int regionExpansions)
196  {
197  numRegionExpansions_ = regionExpansions;
198  }
199 
202  int getNumTreeExpansions () const
203  {
204  return numTreeSelections_;
205  }
206 
209  void setNumTreeExpansions (int treeExpansions)
210  {
211  numTreeSelections_ = treeExpansions;
212  }
213 
216  double getProbAbandonLeadEarly () const
217  {
218  return probAbandonLeadEarly_;
219  }
220 
223  void setProbAbandonLeadEarly (double probability)
224  {
225  probAbandonLeadEarly_ = probability;
226  }
228 
230  struct Defaults
231  {
232  static const int NUM_FREEVOL_SAMPLES = 100000;
233  static const int COVGRID_LENGTH = 128;
234  static const int NUM_REGION_EXPANSIONS = 100;
235  static const int NUM_TREE_SELECTIONS = 1;
236  // C++ standard prohibits non-integral static const member initialization
237  // These constants are set in Syclop.cpp. C++11 standard changes this
238  // with the constexpr keyword, but for compatibility this is not done.
239  static const double PROB_ABANDON_LEAD_EARLY /*= 0.25*/;
240  static const double PROB_KEEP_ADDING_TO_AVAIL /*= 0.50*/;
241  static const double PROB_SHORTEST_PATH /*= 0.95*/;
242  };
243 
244  protected:
245 
246  #pragma pack(push, 4) // push default byte alignment to stack and align the following structure to 4 byte boundary
247 
251  class Motion
252  {
253  public:
254  Motion() : state(NULL), control(NULL), parent(NULL), steps(0)
255  {
256  }
258  Motion(const SpaceInformation *si) : state(si->allocState()), control(si->allocControl()), parent(NULL), steps(0)
259  {
260  }
261  virtual ~Motion()
262  {
263  }
269  const Motion *parent;
271  unsigned int steps;
272  };
273  #pragma pack (pop) // Restoring default byte alignment
274 
275  #pragma pack(push, 4) // push default byte alignment to stack and align the following structure to 4 byte boundary
276 
277  class Region
278  {
279  public:
280  Region()
281  {
282  }
283  virtual ~Region()
284  {
285  }
286 
287 #if __cplusplus >= 201103L
288  Region(const Region&) = default;
289  Region& operator=(const Region&) = default;
290  Region(Region&&) = default;
291  Region& operator=(Region&&) = default;
292 #endif
293 
295  void clear()
296  {
297  motions.clear();
298  covGridCells.clear();
299  pdfElem = NULL;
300  }
301 
303  std::set<int> covGridCells;
305  std::vector<Motion*> motions;
307  double volume;
309  double freeVolume;
313  double weight;
315  double alpha;
317  int index;
319  unsigned int numSelections;
322  };
323  #pragma pack (pop) // Restoring default byte alignment
324 
325  #pragma pack(push, 4) // push default byte alignment to stack and align the following structure to 4 byte boundary
326 
328  class Adjacency
329  {
330  public:
331  Adjacency()
332  {
333  }
334  virtual ~Adjacency()
335  {
336  }
338  void clear()
339  {
340  covGridCells.clear();
341  }
344  std::set<int> covGridCells;
346  const Region *source;
348  const Region *target;
350  double cost;
357  bool empty;
358  };
359  #pragma pack (pop) // Restoring default byte alignment
360 
362  virtual Motion* addRoot(const base::State *s) = 0;
363 
366  virtual void selectAndExtend(Region &region, std::vector<Motion*>& newMotions) = 0;
367 
369  inline const Region& getRegionFromIndex(const int rid) const
370  {
371  return graph_[boost::vertex(rid,graph_)];
372  }
373 
376 
379 
382 
385 
388 
391 
394 
397 
400 
401  private:
404  class CoverageGrid : public GridDecomposition
405  {
406  public:
407  CoverageGrid(const int len, const DecompositionPtr& d) : GridDecomposition(len, d->getDimension(), d->getBounds()), decomp(d)
408  {
409  }
410 
411  virtual ~CoverageGrid()
412  {
413  }
414 
417  virtual void project(const base::State *s, std::vector<double>& coord) const
418  {
419  decomp->project(s, coord);
420  }
421 
423  virtual void sampleFullState(const base::StateSamplerPtr& /*sampler*/, const std::vector<double>& /*coord*/, base::State* /*s*/) const
424  {
425  }
426 
427  protected:
428  const DecompositionPtr& decomp;
429  };
430 
431  typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, Region, Adjacency> RegionGraph;
432  typedef boost::graph_traits<RegionGraph>::vertex_descriptor Vertex;
433  typedef boost::graph_traits<RegionGraph>::vertex_iterator VertexIter;
434  typedef boost::property_map<RegionGraph, boost::vertex_index_t>::type VertexIndexMap;
435  typedef boost::graph_traits<RegionGraph>::edge_iterator EdgeIter;
436 
438  friend class DecompositionHeuristic;
439 
440  class DecompositionHeuristic : public boost::astar_heuristic<RegionGraph, double>
441  {
442  public:
443  DecompositionHeuristic(const Syclop *s, const Region &goal) : syclop(s), goalRegion(goal)
444  {
445  }
446 
447  double operator()(Vertex v)
448  {
449  const Region &region = syclop->getRegionFromIndex(v);
450  return region.alpha*goalRegion.alpha;
451  }
452  private:
453  const Syclop *syclop;
454  const Region &goalRegion;
455  };
456 
457  struct found_goal {};
458 
459  class GoalVisitor : public boost::default_astar_visitor
460  {
461  public:
462  GoalVisitor(const int goal) : goalRegion(goal)
463  {
464  }
465  void examine_vertex(Vertex v, const RegionGraph& /*g*/)
466  {
467  if (static_cast<int>(v) == goalRegion)
468  throw found_goal();
469  }
470  private:
471  const int goalRegion;
472  };
474 
476  class RegionSet
477  {
478  public:
479  int sampleUniform()
480  {
481  if (empty())
482  return -1;
483  return regions.sample(rng.uniform01());
484  }
485  void insert(const int r)
486  {
487  if (regToElem.count(r) == 0)
488  regToElem[r] = regions.add(r, 1);
489  else
490  {
491  PDF<int>::Element *elem = regToElem[r];
492  regions.update(elem, regions.getWeight(elem)+1);
493  }
494  }
495  void clear()
496  {
497  regions.clear();
498  regToElem.clear();
499  }
500  std::size_t size() const
501  {
502  return regions.size();
503  }
504  bool empty() const
505  {
506  return regions.empty();
507  }
508  private:
509  RNG rng;
510  PDF<int> regions;
511  boost::unordered_map<const int, PDF<int>::Element*> regToElem;
512  };
514 
516  void initRegion(Region &r);
517 
519  void setupRegionEstimates();
520 
522  void updateRegion(Region &r);
523 
525  void initEdge(Adjacency &a, const Region *source, const Region *target);
526 
528  void setupEdgeEstimates();
529 
531  void updateEdge(Adjacency &a);
532 
535  bool updateCoverageEstimate(Region &r, const base::State *s);
536 
539  bool updateConnectionEstimate(const Region &c, const Region &d, const base::State *s);
540 
543  void buildGraph();
544 
546  void clearGraphDetails();
547 
549  int selectRegion();
550 
552  void computeAvailableRegions();
553 
555  void defaultComputeLead(int startRegion, int goalRegion, std::vector<int>& lead);
556 
558  double defaultEdgeCost(int r, int s);
559 
561  LeadComputeFn leadComputeFn;
563  std::vector<int> lead_;
565  PDF<int> availDist_;
567  std::vector<EdgeCostFactorFn> edgeCostFactors_;
569  CoverageGrid covGrid_;
571  RegionGraph graph_;
573  bool graphReady_;
575  boost::unordered_map<std::pair<int,int>, Adjacency*> regionsToEdge_;
577  unsigned int numMotions_;
579  RegionSet startRegions_;
581  RegionSet goalRegions_;
582  };
583  }
584 }
585 
586 #endif
bool approximateSolutions
Flag indicating whether the planner is able to compute approximate solutions.
Definition: Planner.h:214
Planner(const SpaceInformationPtr &si, const std::string &name)
Constructor.
Definition: Planner.cpp:43
Synergistic Combination of Layers of Planning.
Definition: Syclop.h:70
virtual void setup()
Perform extra configuration steps, if needed. This call will also issue a call to ompl::base::SpaceIn...
Definition: Syclop.cpp:48
double alpha
The coefficient contributed by this region to edge weights in lead computations.
Definition: Syclop.h:315
base::State * state
The state contained by the motion.
Definition: Syclop.h:265
Representation of an adjacency (a directed edge) between two regions in the Decomposition assigned to...
Definition: Syclop.h:328
std::set< int > covGridCells
The cells of the underlying coverage grid that contain tree motions from this region.
Definition: Syclop.h:303
int numLeadInclusions
The number of times this adjacency has been included in a lead.
Definition: Syclop.h:352
int numSelections
The number of times the low-level tree planner has selected motions from the source region when attem...
Definition: Syclop.h:355
Definition of an abstract control.
Definition: Control.h:48
int index
The index of the graph node corresponding to this region.
Definition: Syclop.h:317
RNG rng_
Random number generator.
Definition: Syclop.h:399
PDF< int >::Element * pdfElem
The Element corresponding to this region in the PDF of available regions.
Definition: Syclop.h:321
A boost shared pointer wrapper for ompl::base::StateSampler.
double volume
The volume of this region.
Definition: Syclop.h:307
Representation of a region in the Decomposition assigned to Syclop.
Definition: Syclop.h:277
int getNumTreeExpansions() const
Get the number of calls to selectAndExtend() in the low-level tree planner for a given lead and regio...
Definition: Syclop.h:202
Motion(const SpaceInformation *si)
Constructor that allocates memory for the state and the control.
Definition: Syclop.h:258
double getWeight(const Element *elem) const
Returns the current weight of the given Element.
Definition: PDF.h:171
std::vector< Motion * > motions
The tree motions contained in this region.
Definition: Syclop.h:305
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
double getProbAbandonLeadEarly() const
Get the probability [0,1] that a lead will be abandoned early, before a new region is chosen for expa...
Definition: Syclop.h:216
const SpaceInformation * siC_
Handle to the control::SpaceInformation object.
Definition: Syclop.h:393
DecompositionPtr decomp_
The high level decomposition used to focus tree expansion.
Definition: Syclop.h:396
A GridDecomposition is a Decomposition implemented using a grid.
Representation of a motion.
Definition: Syclop.h:251
int numTreeSelections_
The number of calls to selectAndExtend() in the low-level tree planner for a given lead and region...
Definition: Syclop.h:387
Control * control
The control contained by the motion.
Definition: Syclop.h:267
void clear()
Clears motions and coverage information from this region.
Definition: Syclop.h:295
double getProbShortestPathLead() const
Get the probability [0,1] that a lead will be computed as a shortest-path instead of a random-DFS...
Definition: Syclop.h:160
int numRegionExpansions_
The number of times a new region will be chosen and promoted for expansion from a given lead...
Definition: Syclop.h:384
boost::function< void(int, int, std::vector< int > &)> LeadComputeFn
Leads should consist of a path of adjacent regions in the decomposition that start with the start reg...
Definition: Syclop.h:91
virtual void clear()
Clear all internal datastructures. Planner settings are not affected. Subsequent calls to solve() wil...
Definition: Syclop.cpp:57
A container that supports probabilistic sampling over weighted data.
Definition: PDF.h:48
void clearEdgeCostFactors()
Clears all edge cost factors, making all edge weights equivalent to 1.
Definition: Syclop.cpp:221
const Region & getRegionFromIndex(const int rid) const
Returns a reference to the Region object with the given index. Assumes the index is valid...
Definition: Syclop.h:369
virtual base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc)
Continues solving until a solution is found or a given planner termination condition is met...
Definition: Syclop.cpp:68
Syclop(const SpaceInformationPtr &si, const DecompositionPtr &d, const std::string &plannerName)
Constructor. Requires a Decomposition, which Syclop uses to create high-level leads.
Definition: Syclop.h:94
Main namespace. Contains everything in this library.
Definition: Cost.h:42
Random number generation. An instance of this class cannot be used by multiple threads at once (membe...
Definition: RandomNumbers.h:54
Base class for a planner.
Definition: Planner.h:232
void addEdgeCostFactor(const EdgeCostFactorFn &factor)
Adds an edge cost factor to be used for edge weights between adjacent regions.
Definition: Syclop.cpp:216
bool empty
This value is true if and only if this adjacency&#39;s source and target regions both contain zero tree m...
Definition: Syclop.h:357
std::set< int > covGridCells
The cells of the underlying coverage grid that contain tree motions originating from direct connectio...
Definition: Syclop.h:344
const Region * source
The source region of this adjacency edge.
Definition: Syclop.h:346
void setProbAbandonLeadEarly(double probability)
The probability that a lead will be abandoned early, before a new region is chosen for expansion...
Definition: Syclop.h:223
double cost
The cost of this adjacency edge, used in lead computations.
Definition: Syclop.h:350
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
const Region * target
The target region of this adjacency edge.
Definition: Syclop.h:348
boost::function< double(int, int)> EdgeCostFactorFn
Each edge weight between two adjacent regions in the Decomposition is defined as a product of edge co...
Definition: Syclop.h:88
Definition of an abstract state.
Definition: State.h:50
double percentValidCells
The percent of free volume of this region.
Definition: Syclop.h:311
A boost shared pointer wrapper for ompl::control::SpaceInformation.
PlannerSpecs specs_
The specifications of the planner (its capabilities)
Definition: Planner.h:409
virtual void project(const base::State *s, std::vector< double > &coord) const =0
Project a given State to a set of coordinates in R^k, where k is the dimension of this Decomposition...
double probShortestPath_
The probability that a lead will be computed as a shortest-path instead of a random-DFS.
Definition: Syclop.h:378
void update(Element *elem, const double w)
Updates the data in the given Element with a new weight value.
Definition: PDF.h:155
Contains default values for Syclop parameters.
Definition: Syclop.h:230
void setLeadComputeFn(const LeadComputeFn &compute)
Allows the user to override the lead computation function.
Definition: Syclop.cpp:211
int getNumRegionExpansions() const
Get the number of times a new region will be chosen and promoted for expansion from a given lead...
Definition: Syclop.h:188
unsigned int numSelections
The number of times this region has been selected for expansion.
Definition: Syclop.h:319
void setNumFreeVolumeSamples(int numSamples)
Set the number of states to sample when estimating free volume in the Decomposition.
Definition: Syclop.h:153
void setProbShortestPathLead(double probability)
Set the probability [0,1] that a lead will be computed as a shortest-path instead of a random-DFS...
Definition: Syclop.h:167
double probKeepAddingToAvail_
The probability that the set of available regions will be augmented.
Definition: Syclop.h:381
int getNumFreeVolumeSamples() const
Get the number of states to sample when estimating free volume in the Decomposition.
Definition: Syclop.h:146
virtual void selectAndExtend(Region &region, std::vector< Motion * > &newMotions)=0
Select a Motion from the given Region, and extend the tree from the Motion. Add any new motions creat...
A boost shared pointer wrapper for ompl::control::Decomposition.
double getProbAddingToAvailableRegions() const
Get the probability [0,1] that the set of available regions will be augmented.
Definition: Syclop.h:174
const Motion * parent
The parent motion in the tree.
Definition: Syclop.h:269
void clear()
Clears coverage information from this adjacency.
Definition: Syclop.h:338
double freeVolume
The free volume of this region.
Definition: Syclop.h:309
Space information containing necessary information for planning with controls. setup() needs to be ca...
int numFreeVolSamples_
The number of states to sample to estimate free volume in the Decomposition.
Definition: Syclop.h:375
void setNumTreeExpansions(int treeExpansions)
Set the number of calls to selectAndExtend() in the low-level tree planner for a given lead and regio...
Definition: Syclop.h:209
double probAbandonLeadEarly_
The probability that a lead will be abandoned early, before a new region is chosen for expansion...
Definition: Syclop.h:390
void setNumRegionExpansions(int regionExpansions)
Set the number of times a new region will be chosen and promoted for expansion from a given lead...
Definition: Syclop.h:195
double weight
The probabilistic weight of this region, used when sampling from PDF.
Definition: Syclop.h:313
virtual Motion * addRoot(const base::State *s)=0
Add State s as a new root in the low-level tree, and return the Motion corresponding to s...
void setProbAddingToAvailableRegions(double probability)
Set the probability [0,1] that the set of available regions will be augmented.
Definition: Syclop.h:181
unsigned int steps
The number of steps for which the control is applied.
Definition: Syclop.h:271