class mrpt::hmtslam::CLocalMetricHypothesis

This class is used in HMT-SLAM to represent each of the Local Metric Hypotheses (LMHs).

It has a set of particles representing the robot path in nearby poses.

See also:

CHMTSLAM, CLSLAM_RBPF_2DLASER

#include <mrpt/hmtslam/CLocalMetricHypothesis.h>

class CLocalMetricHypothesis:
    public mrpt::bayes::CParticleFilterData,
    public mrpt::bayes::CParticleFilterDataImpl,
    public mrpt::serialization::CSerializable
{
public:
    // typedefs

    typedef CLSLAMParticleData CParticleDataContent;
    typedef CProbabilityParticle<CLSLAMParticleData, particle_storage_mode::POINTER> CParticleData;
    typedef std::deque<CParticleData> CParticleList;
    typedef double(*)(const bayes::CParticleFilter::TParticleFilterOptions&PF_options, const CParticleFilterCapable*obj, size_t index, const void*action, const void*observation) TParticleProbabilityEvaluator;

    // structs

    struct TRobotPosesPartitioning;
    struct ThreadLocks;

    //
fields

    struct mrpt::hmtslam::CLocalMetricHypothesis::ThreadLocks threadLocks;
    THypothesisID m_ID;
    mrpt::safe_ptr<CHMTSLAM> m_parent;
    TPoseID m_currentRobotPose;
    TNodeIDSet m_neighbors;
    std::map<TPoseID, CHMHMapNode::TNodeID> m_nodeIDmemberships;
    std::map<TPoseID, mrpt::obs::CSensoryFrame> m_SFs;
    TPoseIDList m_posesPendingAddPartitioner;
    TNodeIDList m_areasPendingTBI;
    double m_log_w;
    std::vector<std::map<TPoseID, double>> m_log_w_metric_history;
    mrpt::obs::CActionRobotMovement2D m_accumRobotMovement;
    bool m_accumRobotMovementIsValid;
    struct mrpt::hmtslam::CLocalMetricHypothesis::TRobotPosesPartitioning m_robotPosesGraph;
    CParticleList m_particles;

    // construction

    CLocalMetricHypothesis(CHMTSLAM* parent = nullptr);

    //
methods

    void getAs3DScene(mrpt::opengl::CSetOfObjects::Ptr& objs) const;
    void getMeans(TMapPoseID2Pose3D& outList) const;
    void getPathParticles(std::map<TPoseID, mrpt::poses::CPose3DPDFParticles>& outList) const;
    void getPoseParticles(const TPoseID& poseID, mrpt::poses::CPose3DPDFParticles& outPDF) const;
    void getRelativePose(const TPoseID& reference, const TPoseID& pose, mrpt::poses::CPose3DPDFParticles& outPDF) const;
    void dumpAsText(std::vector<std::string>& st) const;
    void changeCoordinateOrigin(const TPoseID& newOrigin);
    void rebuildMetricMaps();
    void clearRobotPoses();
    const mrpt::poses::CPose3D* getCurrentPose(size_t particleIdx) const;
    mrpt::poses::CPose3D* getCurrentPose(size_t particleIdx);
    void removeAreaFromLMH(const CHMHMapNode::TNodeID areaID);
    void updateAreaFromLMH(const CHMHMapNode::TNodeID areaID, bool eraseSFsFromLMH = false);
    void clearParticles();
    void writeParticlesToStream(STREAM& out) const;
    void readParticlesFromStream(STREAM& in);
    void getWeights(std::vector<double>& out_logWeights) const;
    const CParticleData* getMostLikelyParticle() const;
    const CLocalMetricHypothesis& derived() const;
    CLocalMetricHypothesis& derived();
    virtual double getW(size_t i) const;
    virtual void setW(size_t i, double w);
    virtual size_t particlesCount() const;
    virtual double normalizeWeights(double* out_max_log_w = nullptr);
    virtual double ESS() const;
    virtual void performSubstitution(const std::vector<size_t>& indx);

    void prepareFastDrawSample(
        const bayes::CParticleFilter::TParticleFilterOptions& PF_options,
        TParticleProbabilityEvaluator partEvaluator = defaultEvaluator,
        const void* action = nullptr,
        const void* observation = nullptr
        ) const;

    size_t fastDrawSample(const bayes::CParticleFilter::TParticleFilterOptions& PF_options) const;
    void prediction_and_update(const mrpt::obs::CActionCollection* action, const mrpt::obs::CSensoryFrame* observation, const bayes::CParticleFilter::TParticleFilterOptions& PF_options);
    void performResampling(const bayes::CParticleFilter::TParticleFilterOptions& PF_options, size_t out_particle_count = 0);

    static double defaultEvaluator(
        ] const bayes::CParticleFilter::TParticleFilterOptions& PF_options,
        ] const CParticleFilterCapable* obj,
        size_t index,
        ] const void* action,
        ] const void* observation
        );

    static void computeResampling(
        CParticleFilter::TParticleResamplingAlgorithm method,
        const std::vector<double>& in_logWeights,
        std::vector<size_t>& out_indexes,
        size_t out_particle_count = 0
        );

    static void log2linearWeights(
        const std::vector<double>& in_logWeights,
        std::vector<double>& out_linWeights
        );
};

Inherited Members

public:
    // structs

    struct TFastDrawAuxVars;

    //
methods

    virtual double getW(size_t i) const = 0;
    virtual void setW(size_t i, double w) = 0;
    virtual size_t particlesCount() const = 0;
    virtual void performSubstitution(const std::vector<size_t>& indx) = 0;
    virtual double normalizeWeights(double* out_max_log_w = nullptr) = 0;
    virtual double ESS() const = 0;

Typedefs

typedef CLSLAMParticleData CParticleDataContent

This is the type inside the corresponding CParticleData class.

typedef CProbabilityParticle<CLSLAMParticleData, particle_storage_mode::POINTER> CParticleData

Use this to refer to each element in the m_particles array.

typedef std::deque<CParticleData> CParticleList

Use this type to refer to the list of particles m_particles.

typedef double(*)(const bayes::CParticleFilter::TParticleFilterOptions&PF_options, const CParticleFilterCapable*obj, size_t index, const void*action, const void*observation) TParticleProbabilityEvaluator

A callback function type for evaluating the probability of m_particles of being selected, used in “fastDrawSample”.

The default evaluator function “defaultEvaluator” simply returns the particle weight.

Parameters:

index

This is the index of the particle its probability is being computed.

action

The value of this is the parameter passed to “prepareFastDrawSample”

observation

The value of this is the parameter passed to “prepareFastDrawSample” The action and the observation are declared as “void*” for a greater flexibility.

See also:

prepareFastDrawSample

Fields

THypothesisID m_ID

The unique ID of the hypothesis (Used for accessing mrpt::slam::CHierarchicalMHMap).

mrpt::safe_ptr<CHMTSLAM> m_parent

For quick access to our parent object.

TPoseID m_currentRobotPose

The current robot pose (its global unique ID) for this hypothesis.

TNodeIDSet m_neighbors

The list of all areas sourronding the current one (this includes the current area itself).

std::map<TPoseID, CHMHMapNode::TNodeID> m_nodeIDmemberships

The hybrid map node membership for each robot pose.

std::map<TPoseID, mrpt::obs::CSensoryFrame> m_SFs

The SF gathered at each robot pose.

TPoseIDList m_posesPendingAddPartitioner

The list of poseIDs waiting to be added to the graph partitioner, what happens in the LSLAM thread main loop.

TNodeIDList m_areasPendingTBI

The list of area IDs waiting to be processed by the TBI (topological bayesian inference) engines to search for potential loop-closures.

Set in CHMTSLAM::LSLAM_process_message_from_AA, read in

double m_log_w

Log-weight of this hypothesis.

std::vector<std::map<TPoseID, double>> m_log_w_metric_history

The historic log-weights of the metric observations inserted in this LMH, for each particle.

mrpt::obs::CActionRobotMovement2D m_accumRobotMovement

Used in CLSLAM_RBPF_2DLASER.

bool m_accumRobotMovementIsValid

Used in CLSLAM_RBPF_2DLASER.

CParticleList m_particles

The array of particles.

Construction

CLocalMetricHypothesis(CHMTSLAM* parent = nullptr)

Constructor (Default param only used from STL classes)

Methods

void getAs3DScene(mrpt::opengl::CSetOfObjects::Ptr& objs) const

Returns a 3D representation of the the current robot pose, all the poses in the auxiliary graph, and each of the areas they belong to.

The metric maps are not included here for convenience, call m_metricMaps.getAs3DScene(). The previous contents of “objs” will be discarded

void getMeans(TMapPoseID2Pose3D& outList) const

Returns the mean of each robot pose in this LMH, as computed from the set of particles.

See also:

getPathParticles, getRelativePose

void getPathParticles(std::map<TPoseID, mrpt::poses::CPose3DPDFParticles>& outList) const

Returns the mean and covariance of each robot pose in this LMH, as computed from the set of particles.

See also:

getMeans, getPoseParticles

void getPoseParticles(const TPoseID& poseID, mrpt::poses::CPose3DPDFParticles& outPDF) const

Returns the mean and covariance of each robot pose in this LMH, as computed from the set of particles.

See also:

getMeans, getPathParticles

void getRelativePose(const TPoseID& reference, const TPoseID& pose, mrpt::poses::CPose3DPDFParticles& outPDF) const

Returns the pose PDF of some pose relative to some other pose ID (both must be part of the the LMH).

See also:

getMeans, getPoseParticles

void dumpAsText(std::vector<std::string>& st) const

Describes the LMH in text.

void changeCoordinateOrigin(const TPoseID& newOrigin)

Change all coordinates to set a given robot pose as the new coordinate origin, and rebuild metric maps and change coords in the partitioning subsystem as well.

void rebuildMetricMaps()

Rebuild the metric maps of all particles from the observations and their estimated poses.

void clearRobotPoses()

Rebuild the auxiliary metric maps in “m_robotPosesGraph” from the observations “m_SFs” and their estimated poses.

Sets the number of particles to the initial number according to the PF options, and initialize them with no robot poses & empty metric maps.

const mrpt::poses::CPose3D* getCurrentPose(size_t particleIdx) const

Returns the i’th particle hypothesis for the current robot pose.

mrpt::poses::CPose3D* getCurrentPose(size_t particleIdx)

Returns the i’th particle hypothesis for the current robot pose.

void removeAreaFromLMH(const CHMHMapNode::TNodeID areaID)

Removes a given area from the LMH:

  • The corresponding node in the HMT map is updated with the robot poses & SFs in the LMH.

  • Robot poses belonging to that area are removed from:

    • the particles.

    • the graph partitioner.

    • the list of SFs.

    • the list m_nodeIDmemberships.

  • m_neighbors is updated.

The weights of all particles are changed to remove the effects of the removed metric observations.

  • After calling this the metric maps should be updated.

This method internally calls updateAreaFromLMH

  • The corresponding node in the HMT map is updated with the robot poses & SFs in the LMH.

  • Robot poses belonging to that area are removed from:

    • the particles.

    • the graph partitioner.

    • the list of SFs.

    • the list m_nodeIDmemberships.

    • The weights of all particles are changed to remove the effects of the removed metric observations.

  • After calling this the metric maps should be updated.

void updateAreaFromLMH(const CHMHMapNode::TNodeID areaID, bool eraseSFsFromLMH = false)

The corresponding node in the HMT map is updated with the robot poses & SFs in the LMH: the poses are referenced to the area’s reference poseID, such as that reference is at the origin.

If eraseSFsFromLMH=true, the sensoryframes are moved rather than copied to the area, and removed from the LMH. The critical section m_map_cs is locked internally, unlock it before calling this.

void clearParticles()

Free the memory of all the particles and reset the array “m_particles” to length zero.

void writeParticlesToStream(STREAM& out) const

Dumps the sequence of particles and their weights to a stream (requires T implementing CSerializable).

See also:

readParticlesFromStream

void readParticlesFromStream(STREAM& in)

Reads the sequence of particles and their weights from a stream (requires T implementing CSerializable).

See also:

writeParticlesToStream

void getWeights(std::vector<double>& out_logWeights) const

Returns a vector with the sequence of the logaritmic weights of all the samples.

const CParticleData* getMostLikelyParticle() const

Returns the particle with the highest weight.

const CLocalMetricHypothesis& derived() const

CRTP helper method.

CLocalMetricHypothesis& derived()

CRTP helper method.

virtual double getW(size_t i) const

Access to i’th particle (logarithm) weight, where first one is index 0.

virtual void setW(size_t i, double w)

Modifies i’th particle (logarithm) weight, where first one is index 0.

virtual size_t particlesCount() const

Get the m_particles count.

virtual double normalizeWeights(double* out_max_log_w = nullptr)

Normalize the (logarithmic) weights, such as the maximum weight is zero.

Parameters:

out_max_log_w

If provided, will return with the maximum log_w before normalizing, such as new_weights = old_weights - max_log_w.

Returns:

The max/min ratio of weights (“dynamic range”)

virtual double ESS() const

Returns the normalized ESS (Estimated Sample Size), in the range [0,1].

Note that you do NOT need to normalize the weights before calling this.

virtual void performSubstitution(const std::vector<size_t>& indx)

Replaces the old particles by copies determined by the indexes in “indx”, performing an efficient copy of the necesary particles only and allowing the number of particles to change.

void prepareFastDrawSample(
    const bayes::CParticleFilter::TParticleFilterOptions& PF_options,
    TParticleProbabilityEvaluator partEvaluator = defaultEvaluator,
    const void* action = nullptr,
    const void* observation = nullptr
    ) const

Prepares data structures for calling fastDrawSample method next.

This method must be called once before using “fastDrawSample” (calling this more than once has no effect, but it takes time for nothing!) The behavior depends on the configuration of the PF (see CParticleFilter::TParticleFilterOptions):

  • DYNAMIC SAMPLE SIZE=NO : In this case this method fills out an internal array (m_fastDrawAuxiliary.alreadyDrawnIndexes) with the random indexes generated according to the selected resample scheme in TParticleFilterOptions. Those indexes are read sequentially by subsequent calls to fastDrawSample.

  • DYNAMIC SAMPLE SIZE=YES : Then:

    • If TParticleFilterOptions.resamplingMethod = prMultinomial, the internal buffers will be filled out (m_fastDrawAuxiliary.CDF, CDF_indexes & PDF) and then fastDrawSample can be called an arbitrary number of times to generate random indexes.

    • For the rest of resampling algorithms, an exception will be raised since they are not appropriate for a dynamic (unknown in advance) number of particles.

The function pointed by “partEvaluator” should take into account the particle filter algorithm selected in “m_PFAlgorithm”. If called without arguments (defaultEvaluator), the default behavior is to draw samples with a probability proportional to their current weights. The action and the observation are declared as “void*” for a greater flexibility. For a more detailed information see the Particle Filter tutorial. Custom supplied “partEvaluator” functions must take into account the previous particle weight, i.e. multiplying the current observation likelihood by the weights.

See also:

fastDrawSample

size_t fastDrawSample(const bayes::CParticleFilter::TParticleFilterOptions& PF_options) const

Draws a random sample from the particle filter, in such a way that each particle has a probability proportional to its weight (in the standard PF algorithm).

This method can be used to generate a variable number of m_particles when resampling: to vary the number of m_particles in the filter. See prepareFastDrawSample for more information, or the Particle Filter tutorial.

NOTES:

  • You MUST call “prepareFastDrawSample” ONCE before calling this method. That method must be called after modifying the particle filter (executing one step, resampling, etc…)

  • This method returns ONE index for the selected (“drawn”) particle, in the range [0,M-1]

  • You do not need to call “normalizeWeights” before calling this.

See also:

prepareFastDrawSample

void prediction_and_update(
    const mrpt::obs::CActionCollection* action,
    const mrpt::obs::CSensoryFrame* observation,
    const bayes::CParticleFilter::TParticleFilterOptions& PF_options
    )

Performs the prediction stage of the Particle Filter.

This method simply selects the appropiate protected method according to the particle filter algorithm to run.

See also:

prediction_and_update_pfStandardProposal, prediction_and_update_pfAuxiliaryPFStandard, prediction_and_update_pfOptimalProposal, prediction_and_update_pfAuxiliaryPFOptimal

void performResampling(const bayes::CParticleFilter::TParticleFilterOptions& PF_options, size_t out_particle_count = 0)

Performs a resample of the m_particles, using the method selected in the constructor.

After computing the surviving samples, this method internally calls “performSubstitution” to actually perform the particle replacement. This method is called automatically by CParticleFilter::execute, andshould not be invoked manually normally. To just obtaining the sequence of resampled indexes from a sequence of weights, use “resample”

Parameters:

out_particle_count

The desired number of output particles after resampling; 0 means don’t modify the current number.

See also:

resample

static double defaultEvaluator(
    ] const bayes::CParticleFilter::TParticleFilterOptions& PF_options,
    ] const CParticleFilterCapable* obj,
    size_t index,
    ] const void* action,
    ] const void* observation
    )

The default evaluator function, which simply returns the particle weight.

The action and the observation are declared as “void*” for a greater flexibility.

See also:

prepareFastDrawSample

static void computeResampling(
    CParticleFilter::TParticleResamplingAlgorithm method,
    const std::vector<double>& in_logWeights,
    std::vector<size_t>& out_indexes,
    size_t out_particle_count = 0
    )

A static method to perform the computation of the samples resulting from resampling a given set of particles, given their logarithmic weights, and a resampling method.

It returns the sequence of indexes from the resampling. The number of output samples is the same than the input population. This generic method just computes these indexes, to actually perform a resampling in a particle filter object, call performResampling

Parameters:

out_particle_count

The desired number of output particles after resampling; 0 means don’t modify the current number.

See also:

performResampling

static void log2linearWeights(
    const std::vector<double>& in_logWeights,
    std::vector<double>& out_linWeights
    )

A static method to compute the linear, normalized (the sum the unity) weights from log-weights.

See also:

performResampling