InformedStateSampler.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/InformedStateSampler.h"
38 #include "ompl/util/Exception.h"
39 #include "ompl/base/OptimizationObjective.h"
40 // The goal definitions
41 #include "ompl/base/Goal.h"
42 
43 namespace ompl
44 {
45  namespace base
46  {
48  // InformedSampler
49  InformedSampler::InformedSampler(const ProblemDefinitionPtr &probDefn, unsigned int maxNumberCalls)
50  : probDefn_(probDefn), space_(probDefn->getSpaceInformation()->getStateSpace()), numIters_(maxNumberCalls)
51  {
52  // Sanity check the problem.
53  // Check that there is an optimization objective
54  if (probDefn_->hasOptimizationObjective() == false)
55  {
56  throw Exception("InformedSampler: An optimization objective must be specified at construction.");
57  }
58  // No else
59 
60  // Make sure we have at least one start and warn if we have more than one
61  if (probDefn_->getStartStateCount() == 0u)
62  {
63  throw Exception("InformedSampler: At least one start state must be specified at construction.");
64  }
65  // No else
66 
67  // Store the optimization objective for later ease.
68  opt_ = probDefn_->getOptimizationObjective();
69  }
70 
71  double InformedSampler::getInformedMeasure(const Cost &minCost, const Cost &maxCost) const
72  {
73  // Subtract the measures defined by the max and min costs. These will be defined in the deriving class.
74  return getInformedMeasure(maxCost) - getInformedMeasure(minCost);
75  }
76 
78  {
79  // Return the best heuristic estimate of the cost-to-come and cost-to-go from the state considering all
80  // starts.
81 
82  // If there's only one start, be simple:
83  if (probDefn_->getStartStateCount() == 1u)
84  {
85  // Calculate and from the one and only start
86  return opt_->combineCosts(opt_->motionCostHeuristic(probDefn_->getStartState(0u), statePtr),
87  opt_->costToGo(statePtr, probDefn_->getGoal().get()));
88  }
89  else
90  {
91  // Calculate and return the best
92 
93  // Variable
94  // The best cost so far
95  Cost bestCost = opt_->infiniteCost();
96 
97  // Iterate over each start and store the best
98  for (unsigned int i = 0u; i < probDefn_->getStartStateCount(); ++i)
99  {
100  // Store the best
101  bestCost = opt_->betterCost(
102  bestCost, opt_->combineCosts(opt_->motionCostHeuristic(probDefn_->getStartState(i), statePtr),
103  opt_->costToGo(statePtr, probDefn_->getGoal().get())));
104  }
105 
106  // Return the best
107  return bestCost;
108  }
109  }
110 
112  {
113  return probDefn_;
114  }
115 
117  {
118  return numIters_;
119  }
121 
123  // InformedStateSampler
124  InformedStateSampler::InformedStateSampler(const ProblemDefinitionPtr &probDefn, unsigned int maxNumberCalls,
125  const GetCurrentCostFunc &costFunc)
126  : StateSampler(probDefn->getSpaceInformation()->getStateSpace().get())
127  {
128  // Call the common constructor with the default informed sampler
129  commonConstructor(
130  costFunc, probDefn->getOptimizationObjective()->allocInformedStateSampler(probDefn, maxNumberCalls));
131  }
132 
134  const GetCurrentCostFunc &costFunc,
135  const InformedSamplerPtr &infSampler)
136  : StateSampler(probDefn->getSpaceInformation()->getStateSpace().get())
137  {
138  // Call the common constructor with the given informed sampler
139  commonConstructor(costFunc, infSampler);
140  }
141 
142  void InformedStateSampler::commonConstructor(const GetCurrentCostFunc &costFunc,
143  const InformedSamplerPtr &infSampler)
144  {
145  // Store the cost function
146  bestCostFunc_ = costFunc;
147 
148  // Store the informed sampler
149  infSampler_ = infSampler;
150 
151  // Allocate a base sampler
153  }
154 
156  {
157  // Variable
158  // Whether informed sampling was successful
159  bool informedSuccess;
160 
161  // Call sample uniform with the current best cost, check returning function:
162  informedSuccess = infSampler_->sampleUniform(statePtr, bestCostFunc_());
163 
164  // If we were unsuccessful, return a regular sample
165  if (informedSuccess == false)
166  {
167  baseSampler_->sampleUniform(statePtr);
168  }
169  // No else.
170  }
171 
172  void InformedStateSampler::sampleUniformNear(State *statePtr, const State *near, const double distance)
173  {
174  // Warn:
175  OMPL_WARN("sampleUniformNear is not informed.");
176  return baseSampler_->sampleUniformNear(statePtr, near, distance);
177  }
178 
179  void InformedStateSampler::sampleGaussian(State *statePtr, const State *mean, const double stdDev)
180  {
181  // Warn:
182  OMPL_WARN("sampleGaussian is not informed.");
183  return baseSampler_->sampleGaussian(statePtr, mean, stdDev);
184  }
186  }; // base
187 }; // ompl
const StateSpace * space_
The state space this sampler samples.
Definition: StateSampler.h:104
A shared pointer wrapper for ompl::base::ProblemDefinition.
OptimizationObjectivePtr opt_
A copy of the optimization objective.
unsigned int getMaxNumberOfIters() const
virtual double getInformedMeasure(const Cost &currentCost) const =0
The measure of the subset of the state space defined by the current solution cost that is being searc...
Main namespace. Contains everything in this library.
Definition: AppBase.h:21
InformedStateSampler(const ProblemDefinitionPtr &probDefn, unsigned int maxNumberCalls, const GetCurrentCostFunc &costFunc)
Construct a sampler that only generates states with a heuristic solution estimate that is less than t...
void sampleGaussian(State *statePtr, const State *mean, const double stdDev) override
By default sampleGaussian throws. This can be overloaded by a specific informed sampler if desired...
virtual StateSamplerPtr allocDefaultStateSampler() const =0
Allocate an instance of the default uniform state sampler for this space.
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...
Definition of an abstract state.
Definition: State.h:49
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
ProblemDefinitionPtr getProblemDefn() const
The exception type for ompl.
Definition: Exception.h:46
ProblemDefinitionPtr probDefn_
A copy of the problem definition.
Abstract definition of a state space sampler.
Definition: StateSampler.h:64
unsigned int numIters_
The number of iterations I&#39;m allowed to attempt.
std::function< Cost()> GetCurrentCostFunc
The definition of a function pointer for querying the current solution cost.
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
void sampleUniformNear(State *statePtr, const State *near, const double distance) override
By default sampleUniformNear throws. This can be overloaded by a specific informed sampler if desired...
void sampleUniform(State *statePtr) override
Sample uniformly in the subset of the state space whose heuristic solution estimates are less than th...