Loading...
Searching...
No Matches
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
43namespace 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())
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
90 // Calculate and return the best
91
92 // Variable
93 // The best cost so far
94 Cost bestCost = opt_->infiniteCost();
95
96 // Iterate over each start and store the best
97 for (unsigned int i = 0u; i < probDefn_->getStartStateCount(); ++i)
98 {
99 // Store the best
100 bestCost = opt_->betterCost(
101 bestCost, opt_->combineCosts(opt_->motionCostHeuristic(probDefn_->getStartState(i), statePtr),
102 opt_->costToGo(statePtr, probDefn_->getGoal().get())));
103 }
104
105 // Return the best
106 return bestCost;
107 }
108
113
115 {
116 return numIters_;
117 }
118
119
121 // InformedStateSampler
122 InformedStateSampler::InformedStateSampler(const ProblemDefinitionPtr &probDefn, unsigned int maxNumberCalls,
123 const GetCurrentCostFunc &costFunc)
124 : StateSampler(probDefn->getSpaceInformation()->getStateSpace().get())
125 {
126 // Call the common constructor with the default informed sampler
127 commonConstructor(
128 costFunc, probDefn->getOptimizationObjective()->allocInformedStateSampler(probDefn, maxNumberCalls));
129 }
130
132 const GetCurrentCostFunc &costFunc,
133 const InformedSamplerPtr &infSampler)
134 : StateSampler(probDefn->getSpaceInformation()->getStateSpace().get())
135 {
136 // Call the common constructor with the given informed sampler
137 commonConstructor(costFunc, infSampler);
138 }
139
140 void InformedStateSampler::commonConstructor(const GetCurrentCostFunc &costFunc,
141 const InformedSamplerPtr &infSampler)
142 {
143 // Store the cost function
144 bestCostFunc_ = costFunc;
145
146 // Store the informed sampler
147 infSampler_ = infSampler;
148
149 // Allocate a base sampler
150 baseSampler_ = StateSampler::space_->allocDefaultStateSampler();
151 }
152
154 {
155 // Variable
156 // Whether informed sampling was successful
157 bool informedSuccess;
158
159 // Call sample uniform with the current best cost, check returning function:
160 informedSuccess = infSampler_->sampleUniform(statePtr, bestCostFunc_());
161
162 // If we were unsuccessful, return a regular sample
163 if (!informedSuccess)
164 {
165 baseSampler_->sampleUniform(statePtr);
166 }
167 // No else.
168 }
169
170 void InformedStateSampler::sampleUniformNear(State *statePtr, const State *near, const double distance)
171 {
172 // Warn:
173 OMPL_WARN("sampleUniformNear is not informed.");
174 return baseSampler_->sampleUniformNear(statePtr, near, distance);
175 }
176
177 void InformedStateSampler::sampleGaussian(State *statePtr, const State *mean, const double stdDev)
178 {
179 // Warn:
180 OMPL_WARN("sampleGaussian is not informed.");
181 return baseSampler_->sampleGaussian(statePtr, mean, stdDev);
182 }
183
184 }; // namespace base
185}; // namespace ompl
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition Cost.h:48
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...
OptimizationObjectivePtr opt_
A copy of the optimization objective.
unsigned int getMaxNumberOfIters() const
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...
ProblemDefinitionPtr probDefn_
A copy of the problem definition.
unsigned int numIters_
The number of iterations I'm allowed to attempt.
ProblemDefinitionPtr getProblemDefn() const
StateSpacePtr space_
A copy of the state space.
void sampleUniform(State *statePtr) override
Sample uniformly in the subset of the state space whose heuristic solution estimates are less than th...
std::function< Cost()> GetCurrentCostFunc
The definition of a function pointer for querying the current solution cost.
void sampleUniformNear(State *statePtr, const State *near, double distance) override
By default sampleUniformNear throws. This can be overloaded by a specific informed sampler if desired...
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, double stdDev) override
By default sampleGaussian throws. This can be overloaded by a specific informed sampler if desired.
A shared pointer wrapper for ompl::base::ProblemDefinition.
const StateSpace * space_
The state space this sampler samples.
Definition of an abstract state.
Definition State.h:50
#define OMPL_WARN(fmt,...)
Log a formatted warning string.
Definition Console.h:66
This namespace contains sampling based planning routines shared by both planning under geometric cons...
Main namespace. Contains everything in this library.