Class GoalLazySamples

Inheritance Relationships

Base Type

Class Documentation

class GoalLazySamples : public ompl::base::GoalStates

Definition of a goal region that can be sampled, but the sampling process can be slow. This class allows sampling to 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.

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 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.

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.

~GoalLazySamples() override
virtual void sampleGoal(State *st) const override

Sample a state in the goal region.

virtual 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()

virtual 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.

inline 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.

inline 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.

inline 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.

virtual 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.

virtual bool hasStates() const override

Check if there are any states in this goal region.

virtual const State *getState(unsigned int index) const override

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

virtual std::size_t getStateCount() const override

Return the number of valid goal states.

virtual void clear() override

Clear all goal states.

virtual unsigned int maxSampleCount() const override

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

Protected Functions

void goalSamplingThread()

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

Protected Attributes

mutable 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.