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),
51  space_(probDefn->getSpaceInformation()->getStateSpace()),
52  numIters_(maxNumberCalls)
53  {
54  // Sanity check the problem.
55  // Check that there is an optimization objective
56  if (probDefn_->hasOptimizationObjective() == false)
57  {
58  throw Exception ("InformedSampler: An optimization objective must be specified at construction.");
59  }
60  // No else
61 
62  // Make sure we have at least one start and warn if we have more than one
63  if (probDefn_->getStartStateCount() == 0u)
64  {
65  throw Exception ("InformedSampler: At least one start state must be specified at construction.");
66  }
67  // No else
68 
69  // Store the optimization objective for later ease.
70  opt_ = probDefn_->getOptimizationObjective();
71  }
72 
73  double InformedSampler::getInformedMeasure(const Cost &minCost, const Cost &maxCost) const
74  {
75  // Subtract the measures defined by the max and min costs. These will be defined in the deriving class.
76  return getInformedMeasure(maxCost) - getInformedMeasure(minCost);
77  }
78 
80  {
81  // Return the best heuristic estimate of the cost-to-come and cost-to-go from the state considering all starts.
82 
83  // If there's only one start, be simple:
84  if (probDefn_->getStartStateCount() == 1u)
85  {
86  // Calculate and from the one and only start
87  return opt_->combineCosts(opt_->motionCostHeuristic(probDefn_->getStartState(0u), statePtr), 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(bestCost, opt_->combineCosts(opt_->motionCostHeuristic(probDefn_->getStartState(i), statePtr), opt_->costToGo(statePtr, probDefn_->getGoal().get())));
102  }
103 
104  // Return the best
105  return bestCost;
106  }
107  }
109 
110 
111 
112 
113 
114 
116  //InformedStateSampler
117  InformedStateSampler::InformedStateSampler(const ProblemDefinitionPtr &probDefn, unsigned int maxNumberCalls, const GetCurrentCostFunc &costFunc)
118  : StateSampler(probDefn->getSpaceInformation()->getStateSpace().get())
119  {
120  // Call the common constructor with the default informed sampler
121  commonConstructor(costFunc, probDefn->getOptimizationObjective()->allocInformedStateSampler(probDefn, maxNumberCalls));
122  }
123 
124  InformedStateSampler::InformedStateSampler(const ProblemDefinitionPtr &probDefn, const GetCurrentCostFunc &costFunc, const InformedSamplerPtr &infSampler)
125  : StateSampler(probDefn->getSpaceInformation()->getStateSpace().get())
126  {
127  // Call the common constructor with the given informed sampler
128  commonConstructor(costFunc, infSampler);
129  }
130 
131  void InformedStateSampler::commonConstructor(const GetCurrentCostFunc &costFunc, const InformedSamplerPtr &infSampler)
132  {
133  // Store the cost function
134  bestCostFunc_ = costFunc;
135 
136  // Store the informed sampler
137  infSampler_ = infSampler;
138 
139  // Allocate a base sampler
141  }
142 
144  {
145  // Variable
146  // Whether informed sampling was successful
147  bool informedSuccess;
148 
149  // Call sample uniform with the current best cost, check returning function:
150  informedSuccess = infSampler_->sampleUniform(statePtr, bestCostFunc_());
151 
152  // If we were unsuccessful, return a regular sample
153  if (informedSuccess == false)
154  {
155  baseSampler_->sampleUniform(statePtr);
156  }
157  // No else.
158  }
159 
160  void InformedStateSampler::sampleUniformNear(State *statePtr, const State *near, const double distance)
161  {
162  //Warn:
163  OMPL_WARN("sampleUniformNear is not informed.");
164  return baseSampler_->sampleUniformNear(statePtr, near, distance);
165  }
166 
167  void InformedStateSampler::sampleGaussian(State *statePtr, const State *mean, const double stdDev)
168  {
169  //Warn:
170  OMPL_WARN("sampleGaussian is not informed.");
171  return baseSampler_->sampleGaussian(statePtr, mean, stdDev);
172  }
174  }; // base
175 }; // ompl
const StateSpace * space_
The state space this sampler samples.
Definition: StateSampler.h:107
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.
virtual void sampleUniformNear(State *statePtr, const State *near, const double distance)
By default sampleUniformNear throws. This can be overloaded by a specific informed sampler if desired...
virtual void sampleUniform(State *statePtr)
Sample uniformly in the subset of the state space whose heuristic solution estimates are less than th...
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: Cost.h:42
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...
virtual StateSamplerPtr allocDefaultStateSampler() const =0
Allocate an instance of the default uniform state sampler for this space.
Definition of an abstract state.
Definition: State.h:50
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition: Console.h:66
The exception type for ompl.
Definition: Exception.h:47
ProblemDefinitionPtr probDefn_
A copy of the problem definition.
Abstract definition of a state space sampler.
Definition: StateSampler.h:65
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
virtual void sampleGaussian(State *statePtr, const State *mean, const double stdDev)
By default sampleGaussian throws. This can be overloaded by a specific informed sampler if desired...