RejectionInfSampler.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2014, University of Toronto
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 University of Toronto 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 /* Authors: Jonathan Gammell */
36 
37 #include "ompl/base/samplers/informed/RejectionInfSampler.h"
38 #include "ompl/base/OptimizationObjective.h"
39 
40 namespace ompl
41 {
42  namespace base
43  {
44  // The default rejection-sampling class:
45  RejectionInfSampler::RejectionInfSampler(const ProblemDefinitionPtr probDefn, unsigned int maxNumberCalls)
46  : InformedSampler(probDefn, maxNumberCalls)
47  {
48  // Create the basic sampler
49  baseSampler_ = InformedSampler::space_->allocDefaultStateSampler();
50 
51  // Warn if a cost-to-go heuristic is not defined
52  if (InformedSampler::opt_->hasCostToGoHeuristic() == false)
53  {
54  OMPL_WARN("RejectionInfSampler: The optimization objective does not have a cost-to-go heuristic defined. Informed sampling will likely have little to no effect.");
55  }
56  // No else
57  }
58 
59  bool RejectionInfSampler::sampleUniform(State *statePtr, const Cost &maxCost)
60  {
61  // Variable
62  // The persistent iteration counter:
63  unsigned int iter = 0u;
64 
65  //Call the sampleUniform helper function with my iteration counter:
66  return sampleUniform(statePtr, maxCost, &iter);
67  }
68 
69  bool RejectionInfSampler::sampleUniform(State *statePtr, const Cost &minCost, const Cost &maxCost)
70  {
71  // Variable
72  // Whether we were successful in creating an informed sample. Initially not:
73  bool foundSample = false;
74 
75  // Spend numIters_ iterations trying to find an informed sample:
76  for (unsigned int i = 0u; i < InformedSampler::numIters_ && foundSample == false; ++i)
77  {
78  // Call the helper function for the larger cost. It will move our iteration counter:
79  foundSample = sampleUniform(statePtr, maxCost, &i);
80 
81  // Did we find a sample?
82  if (foundSample == true)
83  {
84  // We did, but it only satisfied the upper bound. Check that it meets the lower bound.
85 
86  // Variables
87  // The cost of the sample we found:
88  Cost sampledCost = InformedSampler::heuristicSolnCost(statePtr);
89 
90  // Check if the sample's cost is greater than or equal to the lower bound
91  foundSample = InformedSampler::opt_->isCostEquivalentTo(minCost, sampledCost) || InformedSampler::opt_->isCostBetterThan(minCost, sampledCost);
92  }
93  // No else, no sample was found.
94  }
95 
96  // One way or the other, we're done:
97  return foundSample;
98  }
99 
101  {
102  return false;
103  }
104 
105  double RejectionInfSampler::getInformedMeasure(const Cost &/*currentCost*/) const
106  {
107  return InformedSampler::space_->getMeasure();
108  }
109 
110  double RejectionInfSampler::getInformedMeasure(const Cost &/*minCost*/, const Cost &/*maxCost*/) const
111  {
112  return InformedSampler::space_->getMeasure();
113  }
114 
115 
116 
117  bool RejectionInfSampler::sampleUniform(State *statePtr, const Cost &maxCost, unsigned int *iterPtr)
118  {
119  // Variable
120  // Whether we were successful in creating an informed sample. Initially not:
121  bool foundSample = false;
122 
123  // Make numIters_ attempts at finding a sample whose heuristic estimate of solution cost through the sample is better than maxCost by sampling the entire planning domain
124  for (/* Provided iteration counter */; *iterPtr < InformedSampler::numIters_ && foundSample == false; ++(*iterPtr))
125  {
126  // Get a sample:
127  baseSampler_->sampleUniform(statePtr);
128 
129  // Check if it's found, i.e., if f(state) <= maxCost
130  foundSample = InformedSampler::opt_->isCostBetterThan(InformedSampler::heuristicSolnCost(statePtr), maxCost);
131  }
132 
133  // All done, one way or the other:
134  return foundSample;
135  }
136  }; // base
137 }; // ompl
virtual double getInformedMeasure(const Cost &) const
The measure of the subset of the state space defined by the current solution cost that is being searc...
A shared pointer wrapper for ompl::base::ProblemDefinition.
virtual Cost heuristicSolnCost(const State *statePtr) const
A helper function to calculate the heuristic estimate of the solution cost for a given state using th...
OptimizationObjectivePtr opt_
A copy of the optimization objective.
An abstract class for the concept of using information about the state space and the current solution...
StateSpacePtr space_
A copy of the state space.
Main namespace. Contains everything in this library.
Definition: Cost.h:42
Definition of an abstract state.
Definition: State.h:50
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
virtual bool hasInformedMeasure() const
Whether the sampler can provide a measure of the informed subset.
RejectionInfSampler(const ProblemDefinitionPtr probDefn, unsigned int maxNumberCalls)
Construct a rejection sampler that only generates states with a heuristic solution estimate that is l...
unsigned int numIters_
The number of iterations I&#39;m allowed to attempt.
virtual bool sampleUniform(State *statePtr, const Cost &maxCost)
Sample uniformly in the subset of the state space whose heuristic solution estimates are less than th...
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47