Loading...
Searching...
No Matches

Definition of a goal region that can be sampled, but the sampling process can be slow. This class allows sampling the happen in a separate thread, and the number of goals may increase, as the planner is running, in a thread-safe manner. More...

#include <ompl/base/goals/GoalLazySamples.h>

Inheritance diagram for ompl::base::GoalLazySamples:

Public Types

using NewStateCallbackFn = std::function< void(const base::State *)>
 When new samples are generated and added to the list of possible samples, a callback can be called. This type specifies the signature of that callback.
 

Public Member Functions

 GoalLazySamples (const SpaceInformationPtr &si, GoalSamplingFn samplerFunc, bool autoStart=true, double minDist=std::numeric_limits< double >::epsilon())
 Create a goal region that can be sampled in a lazy fashion. A function (samplerFunc) that produces samples from that region needs to be passed to this constructor. The sampling thread is automatically started if autoStart is true. The sampling function is not called in parallel by OMPL. Hence, the function is not required to be thread safe, unless the user issues additional calls in parallel. The instance of GoalLazySamples remains thread safe however.
 
void sampleGoal (State *st) const override
 Sample a state in the goal region.
 
double distanceGoal (const State *st) const override
 Compute the distance to the goal (heuristic). This function is the one used in computing the distance to the goal in a call to isSatisfied()
 
void addState (const State *st) override
 Add a goal state.
 
void startSampling ()
 Start the goal sampling thread.
 
void stopSampling ()
 Stop the goal sampling thread.
 
bool isSampling () const
 Return true if the sampling thread is active.
 
void setMinNewSampleDistance (double dist)
 Set the minimum distance that a new state returned by the sampling thread needs to be away from previously added states, so that it is added to the list of goal states.
 
double getMinNewSampleDistance () const
 Get the minimum distance that a new state returned by the sampling thread needs to be away from previously added states, so that it is added to the list of goal states.
 
unsigned int samplingAttemptsCount () const
 The number of times the sampling function was called and it returned true.
 
void setNewStateCallback (const NewStateCallbackFn &callback)
 Set the callback function to be called when a new state is added to the list of possible samples. This function is not required to be thread safe, as calls are made one at a time.
 
bool addStateIfDifferent (const State *st, double minDistance)
 Add a state st if it further away that minDistance from previously added states. Return true if the state was added.
 
bool couldSample () const override
 Return true if GoalStates::couldSample() is true or if the sampling thread is active, as in this case it is possible a sample can be produced at some point.
 
bool hasStates () const override
 Check if there are any states in this goal region.
 
const StategetState (unsigned int index) const override
 Return a pointer to the indexth state in the state list.
 
std::size_t getStateCount () const override
 Return the number of valid goal states.
 
void clear () override
 Clear all goal states.
 
unsigned int maxSampleCount () const override
 Return the maximum number of samples that can be asked for before repeating.
 
- Public Member Functions inherited from ompl::base::GoalStates
 GoalStates (const SpaceInformationPtr &si)
 Create a goal representation that is in fact a set of states

 
void sampleGoal (State *st) const override
 Sample a state in the goal region.
 
unsigned int maxSampleCount () const override
 Return the maximum number of samples that can be asked for before repeating.
 
double distanceGoal (const State *st) const override
 Compute the distance to the goal (heuristic). This function is the one used in computing the distance to the goal in a call to isSatisfied()
 
void print (std::ostream &out=std::cout) const override
 Print information about the goal data structure to a stream.
 
virtual void addState (const State *st)
 Add a goal state.
 
void addState (const ScopedState<> &st)
 Add a goal state (calls the previous definition of addState())
 
virtual void clear ()
 Clear all goal states.
 
virtual bool hasStates () const
 Check if there are any states in this goal region.
 
virtual const StategetState (unsigned int index) const
 Return a pointer to the indexth state in the state list.
 
virtual std::size_t getStateCount () const
 Return the number of valid goal states.
 
- Public Member Functions inherited from ompl::base::GoalSampleableRegion
 GoalSampleableRegion (const SpaceInformationPtr &si)
 Create a goal region that can be sampled.
 
virtual void sampleGoal (State *st) const =0
 Sample a state in the goal region.
 
virtual unsigned int maxSampleCount () const =0
 Return the maximum number of samples that can be asked for before repeating.
 
bool canSample () const
 Return true if maxSampleCount() > 0, since in this case samples can certainly be produced.
 
virtual bool couldSample () const
 Return true if samples could be generated by this sampler at some point in the future. By default this is equivalent to canSample(), but for GoalLazySamples, this call also reflects the fact that a sampling thread is active and although no samples are produced yet, some may become available at some point in the future.
 
- Public Member Functions inherited from ompl::base::GoalRegion
 GoalRegion (const SpaceInformationPtr &si)
 Create a goal region.
 
bool isSatisfied (const State *st) const override
 Equivalent to calling isSatisfied(const State *, double *) with a nullptr second argument.
 
bool isSatisfied (const State *st, double *distance) const override
 Decide whether a given state is part of the goal region. Returns true if the distance to goal is less than the threshold (using distanceGoal())
 
virtual double distanceGoal (const State *st) const =0
 Compute the distance to the goal (heuristic). This function is the one used in computing the distance to the goal in a call to isSatisfied()
 
void print (std::ostream &out=std::cout) const override
 Print information about the goal data structure to a stream.
 
void setThreshold (double threshold)
 Set the distance to the goal that is allowed for a state to be considered in the goal region.
 
double getThreshold () const
 Get the distance to the goal that is allowed for a state to be considered in the goal region.
 
- Public Member Functions inherited from ompl::base::Goal
 Goal (const Goal &)=delete
 
Goaloperator= (const Goal &)=delete
 
 Goal (SpaceInformationPtr si)
 Constructor. The goal must always know the space information it is part of.
 
virtual ~Goal ()=default
 Destructor.
 
template<class T >
T * as ()
 Cast this instance to a desired type.
 
template<class T >
const T * as () const
 Cast this instance to a desired type.
 
GoalType getType () const
 Return the goal type.
 
bool hasType (GoalType type) const
 Check if this goal can be cast to a particular goal type.
 
const SpaceInformationPtrgetSpaceInformation () const
 Get the space information this goal is for.
 
virtual bool isSatisfied (const State *st) const =0
 Return true if the state satisfies the goal constraints.
 
virtual bool isSatisfied (const State *st, double *distance) const
 Return true if the state satisfies the goal constraints and compute the distance between the state given as argument and the goal (even if the goal is not satisfied). This distance can be an approximation. It can even be set to a constant, if such a computation is not possible.
 
virtual bool isStartGoalPairValid (const State *, const State *) const
 Since there can be multiple starting states (and multiple goal states) it is possible certain pairs are not to be allowed. By default we however assume all such pairs are allowed. Note: if this function returns true, isSatisfied() need not be called.
 
virtual void print (std::ostream &out=std::cout) const
 Print information about the goal.
 

Protected Member Functions

void goalSamplingThread ()
 The function that samples goals by calling samplerFunc_ in a separate thread.
 

Protected Attributes

std::mutex lock_
 Lock for updating the set of states.
 
GoalSamplingFn samplerFunc_
 Function that produces samples.
 
bool terminateSamplingThread_
 Flag used to notify the sampling thread to terminate sampling.
 
std::thread * samplingThread_
 Additional thread for sampling goal states.
 
unsigned int samplingAttempts_
 The number of times the sampling function was called and it returned true.
 
double minDist_
 Samples returned by the sampling thread are added to the list of states only if they are at least minDist_ away from already added samples.
 
NewStateCallbackFn callback_
 If defined, this function is called when a new state is added to the list of possible samples.
 
- Protected Attributes inherited from ompl::base::GoalStates
std::vector< State * > states_
 The goal states. Only ones that are valid are considered by the motion planner.
 
- Protected Attributes inherited from ompl::base::GoalRegion
double threshold_
 The maximum distance that is allowed to the goal. By default, this is initialized to the minimum epsilon value a double can represent.
 
- Protected Attributes inherited from ompl::base::Goal
GoalType type_
 Goal type.
 
SpaceInformationPtr si_
 The space information for this goal.
 

Detailed Description

Definition of a goal region that can be sampled, but the sampling process can be slow. This class allows sampling the happen in a separate thread, and the number of goals may increase, as the planner is running, in a thread-safe manner.

Todo:
The Python bindings for GoalLazySamples class are still broken. The OMPL C++ code creates a new thread from which you should be able to call a python Goal sampling function. Acquiring the right threads and locks and messing around with the Python Global Interpreter Lock (GIL) is very tricky. See ompl/py-bindings/generate_bindings.py for an initial attempt to make this work.

Definition at line 71 of file GoalLazySamples.h.

Member Typedef Documentation

◆ NewStateCallbackFn

using ompl::base::GoalLazySamples::NewStateCallbackFn = std::function<void(const base::State *)>

When new samples are generated and added to the list of possible samples, a callback can be called. This type specifies the signature of that callback.

Definition at line 77 of file GoalLazySamples.h.

Constructor & Destructor Documentation

◆ GoalLazySamples()

ompl::base::GoalLazySamples::GoalLazySamples ( const SpaceInformationPtr si,
GoalSamplingFn  samplerFunc,
bool  autoStart = true,
double  minDist = std::numeric_limits<double>::epsilon() 
)

Create a goal region that can be sampled in a lazy fashion. A function (samplerFunc) that produces samples from that region needs to be passed to this constructor. The sampling thread is automatically started if autoStart is true. The sampling function is not called in parallel by OMPL. Hence, the function is not required to be thread safe, unless the user issues additional calls in parallel. The instance of GoalLazySamples remains thread safe however.

The function samplerFunc returns a truth value. If the return value is true, further calls to the function can be made. If the return is false, no more calls should be made. The function takes two arguments: the instance of GoalLazySamples making the call and the state to fill with a goal state. For every state filled in by samplerFunc, addStateIfDifferent() is called. A state computed by the sampling thread is added if it is "sufficiently different" from previously added states. A state is considered "sufficiently different" if it is at least minDist away from previously added states.

Definition at line 43 of file GoalLazySamples.cpp.

◆ ~GoalLazySamples()

ompl::base::GoalLazySamples::~GoalLazySamples ( )
override

Definition at line 57 of file GoalLazySamples.cpp.

Member Function Documentation

◆ addState()

void ompl::base::GoalLazySamples::addState ( const State st)
overridevirtual

Add a goal state.

Reimplemented from ompl::base::GoalStates.

Definition at line 174 of file GoalLazySamples.cpp.

◆ addStateIfDifferent()

bool ompl::base::GoalLazySamples::addStateIfDifferent ( const State st,
double  minDistance 
)

Add a state st if it further away that minDistance from previously added states. Return true if the state was added.

Definition at line 204 of file GoalLazySamples.cpp.

◆ clear()

void ompl::base::GoalLazySamples::clear ( )
overridevirtual

Clear all goal states.

Reimplemented from ompl::base::GoalStates.

Definition at line 151 of file GoalLazySamples.cpp.

◆ couldSample()

bool ompl::base::GoalLazySamples::couldSample ( ) const
overridevirtual

Return true if GoalStates::couldSample() is true or if the sampling thread is active, as in this case it is possible a sample can be produced at some point.

Reimplemented from ompl::base::GoalSampleableRegion.

Definition at line 146 of file GoalLazySamples.cpp.

◆ distanceGoal()

double ompl::base::GoalLazySamples::distanceGoal ( const State st) const
overridevirtual

Compute the distance to the goal (heuristic). This function is the one used in computing the distance to the goal in a call to isSatisfied()

Implements ompl::base::GoalRegion.

Definition at line 157 of file GoalLazySamples.cpp.

◆ getMinNewSampleDistance()

double ompl::base::GoalLazySamples::getMinNewSampleDistance ( ) const
inline

Get the minimum distance that a new state returned by the sampling thread needs to be away from previously added states, so that it is added to the list of goal states.

Definition at line 131 of file GoalLazySamples.h.

◆ getState()

const ompl::base::State * ompl::base::GoalLazySamples::getState ( unsigned int  index) const
overridevirtual

Return a pointer to the indexth state in the state list.

Reimplemented from ompl::base::GoalStates.

Definition at line 180 of file GoalLazySamples.cpp.

◆ getStateCount()

std::size_t ompl::base::GoalLazySamples::getStateCount ( ) const
overridevirtual

Return the number of valid goal states.

Reimplemented from ompl::base::GoalStates.

Definition at line 192 of file GoalLazySamples.cpp.

◆ goalSamplingThread()

void ompl::base::GoalLazySamples::goalSamplingThread ( )
protected

The function that samples goals by calling samplerFunc_ in a separate thread.

Definition at line 94 of file GoalLazySamples.cpp.

◆ hasStates()

bool ompl::base::GoalLazySamples::hasStates ( ) const
overridevirtual

Check if there are any states in this goal region.

Reimplemented from ompl::base::GoalStates.

Definition at line 186 of file GoalLazySamples.cpp.

◆ isSampling()

bool ompl::base::GoalLazySamples::isSampling ( ) const

Return true if the sampling thread is active.

Definition at line 140 of file GoalLazySamples.cpp.

◆ maxSampleCount()

unsigned int ompl::base::GoalLazySamples::maxSampleCount ( ) const
overridevirtual

Return the maximum number of samples that can be asked for before repeating.

Implements ompl::base::GoalSampleableRegion.

Definition at line 198 of file GoalLazySamples.cpp.

◆ sampleGoal()

void ompl::base::GoalLazySamples::sampleGoal ( State st) const
overridevirtual

Sample a state in the goal region.

Implements ompl::base::GoalSampleableRegion.

Definition at line 163 of file GoalLazySamples.cpp.

◆ samplingAttemptsCount()

unsigned int ompl::base::GoalLazySamples::samplingAttemptsCount ( ) const
inline

The number of times the sampling function was called and it returned true.

Definition at line 137 of file GoalLazySamples.h.

◆ setMinNewSampleDistance()

void ompl::base::GoalLazySamples::setMinNewSampleDistance ( double  dist)
inline

Set the minimum distance that a new state returned by the sampling thread needs to be away from previously added states, so that it is added to the list of goal states.

Definition at line 124 of file GoalLazySamples.h.

◆ setNewStateCallback()

void ompl::base::GoalLazySamples::setNewStateCallback ( const NewStateCallbackFn callback)

Set the callback function to be called when a new state is added to the list of possible samples. This function is not required to be thread safe, as calls are made one at a time.

Definition at line 169 of file GoalLazySamples.cpp.

◆ startSampling()

void ompl::base::GoalLazySamples::startSampling ( )

Start the goal sampling thread.

Definition at line 62 of file GoalLazySamples.cpp.

◆ stopSampling()

void ompl::base::GoalLazySamples::stopSampling ( )

Stop the goal sampling thread.

Definition at line 73 of file GoalLazySamples.cpp.

Member Data Documentation

◆ callback_

NewStateCallbackFn ompl::base::GoalLazySamples::callback_
protected

If defined, this function is called when a new state is added to the list of possible samples.

Definition at line 187 of file GoalLazySamples.h.

◆ lock_

std::mutex ompl::base::GoalLazySamples::lock_
mutableprotected

Lock for updating the set of states.

Definition at line 168 of file GoalLazySamples.h.

◆ minDist_

double ompl::base::GoalLazySamples::minDist_
protected

Samples returned by the sampling thread are added to the list of states only if they are at least minDist_ away from already added samples.

Definition at line 184 of file GoalLazySamples.h.

◆ samplerFunc_

GoalSamplingFn ompl::base::GoalLazySamples::samplerFunc_
protected

Function that produces samples.

Definition at line 171 of file GoalLazySamples.h.

◆ samplingAttempts_

unsigned int ompl::base::GoalLazySamples::samplingAttempts_
protected

The number of times the sampling function was called and it returned true.

Definition at line 180 of file GoalLazySamples.h.

◆ samplingThread_

std::thread* ompl::base::GoalLazySamples::samplingThread_
protected

Additional thread for sampling goal states.

Definition at line 177 of file GoalLazySamples.h.

◆ terminateSamplingThread_

bool ompl::base::GoalLazySamples::terminateSamplingThread_
protected

Flag used to notify the sampling thread to terminate sampling.

Definition at line 174 of file GoalLazySamples.h.


The documentation for this class was generated from the following files: