Program Listing for File LazyPRM.h

Return to documentation for file (src/ompl/geometric/planners/prm/LazyPRM.h)

/*********************************************************************
* Software License Agreement (BSD License)
*
*  Copyright (c) 2013, Rice University
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Rice University nor the names of its
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/

/* Author: Ioan Sucan, Henning Kayser */

#ifndef OMPL_GEOMETRIC_PLANNERS_PRM_LAZY_PRM_
#define OMPL_GEOMETRIC_PLANNERS_PRM_LAZY_PRM_

#include "ompl/geometric/planners/PlannerIncludes.h"
#include "ompl/datastructures/NearestNeighbors.h"
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <utility>
#include <vector>
#include <map>

namespace ompl
{
    namespace base
    {
        // Forward declare for use in implementation
        OMPL_CLASS_FORWARD(OptimizationObjective);
    }

    namespace geometric
    {
        class LazyPRM : public base::Planner
        {
        public:
            struct vertex_state_t
            {
                using kind = boost::vertex_property_tag;
            };

            struct vertex_flags_t
            {
                using kind = boost::vertex_property_tag;
            };

            struct vertex_component_t
            {
                using kind = boost::vertex_property_tag;
            };

            struct edge_flags_t
            {
                using kind = boost::edge_property_tag;
            };

            using Vertex =
                boost::adjacency_list_traits<boost::vecS, boost::listS, boost::undirectedS>::vertex_descriptor;

            using Graph = boost::adjacency_list<
                boost::vecS, boost::listS, boost::undirectedS,
                boost::property<
                    vertex_state_t, base::State *,
                    boost::property<
                        boost::vertex_index_t, unsigned long int,
                        boost::property<vertex_flags_t, unsigned int,
                                        boost::property<vertex_component_t, unsigned long int,
                                                        boost::property<boost::vertex_predecessor_t, Vertex,
                                                                        boost::property<boost::vertex_rank_t,
                                                                                        unsigned long int>>>>>>,
                boost::property<boost::edge_weight_t, base::Cost, boost::property<edge_flags_t, unsigned int>>>;

            using Edge = boost::graph_traits<Graph>::edge_descriptor;

            using RoadmapNeighbors = std::shared_ptr<NearestNeighbors<Vertex> >;

            using ConnectionStrategy = std::function<const std::vector<Vertex> &(const Vertex)>;

            using ConnectionFilter = std::function<bool (const Vertex &, const Vertex &)>;

            LazyPRM(const base::SpaceInformationPtr &si, bool starStrategy = false);

            LazyPRM(const base::PlannerData &data, bool starStrategy = false);

            ~LazyPRM() override;

            void setRange(double distance);

            double getRange() const
            {
                return maxDistance_;
            }

            template <template <typename T> class NN>
            void setNearestNeighbors()
            {
                if (nn_ && nn_->size() == 0)
                    OMPL_WARN("Calling setNearestNeighbors will clear all states.");
                clear();
                nn_ = std::make_shared<NN<Vertex>>();
                if (!userSetConnectionStrategy_)
                    setDefaultConnectionStrategy();
                if (isSetup())
                    setup();
            }

            void setProblemDefinition(const base::ProblemDefinitionPtr &pdef) override;

            void setConnectionStrategy(const ConnectionStrategy &connectionStrategy)
            {
                connectionStrategy_ = connectionStrategy;
                userSetConnectionStrategy_ = true;
            }
            void setDefaultConnectionStrategy();

            void setMaxNearestNeighbors(unsigned int k);

            void setConnectionFilter(const ConnectionFilter &connectionFilter)
            {
                connectionFilter_ = connectionFilter;
            }

            unsigned long int milestoneCount() const
            {
                return boost::num_vertices(g_);
            }

            unsigned long int edgeCount() const
            {
                return boost::num_edges(g_);
            }

            void getPlannerData(base::PlannerData &data) const override;

            void setup() override;

            void clear() override;

            void clearQuery() override;

            void clearValidity();

            base::PlannerStatus solve(const base::PlannerTerminationCondition &ptc) override;

        protected:
            static const unsigned int VALIDITY_UNKNOWN = 0;

            static const unsigned int VALIDITY_TRUE = 1;

            // Planner progress property functions
            std::string getIterationCount() const
            {
                return std::to_string(iterations_);
            }
            std::string getBestCost() const
            {
                return std::to_string(bestCost_.value());
            }
            std::string getMilestoneCountString() const
            {
                return std::to_string(milestoneCount());
            }
            std::string getEdgeCountString() const
            {
                return std::to_string(edgeCount());
            }

            void freeMemory();

            Vertex addMilestone(base::State *state);

            void uniteComponents(Vertex a, Vertex b);

            void markComponent(Vertex v, unsigned long int newComponent);

            long int solutionComponent(std::pair<std::size_t, std::size_t> *startGoalPair) const;

            ompl::base::PathPtr constructSolution(const Vertex &start, const Vertex &goal);

            double distanceFunction(const Vertex a, const Vertex b) const
            {
                return si_->distance(stateProperty_[a], stateProperty_[b]);
            }

            base::Cost costHeuristic(Vertex u, Vertex v) const;

            bool starStrategy_;

            ConnectionStrategy connectionStrategy_;

            ConnectionFilter connectionFilter_;

            bool userSetConnectionStrategy_{false};

            double maxDistance_{0.};

            base::StateSamplerPtr sampler_;

            RoadmapNeighbors nn_;

            Graph g_;

            std::vector<Vertex> startM_;

            std::vector<Vertex> goalM_;

            boost::property_map<Graph, boost::vertex_index_t>::type indexProperty_;

            boost::property_map<Graph, vertex_state_t>::type stateProperty_;

            boost::property_map<Graph, boost::edge_weight_t>::type weightProperty_;

            boost::property_map<Graph, vertex_component_t>::type vertexComponentProperty_;

            boost::property_map<Graph, vertex_flags_t>::type vertexValidityProperty_;

            boost::property_map<Graph, edge_flags_t>::type edgeValidityProperty_;

            unsigned long int componentCount_{0};

            std::map<unsigned long int, unsigned long int> componentSize_;

            base::OptimizationObjectivePtr opt_;

            base::Cost bestCost_{std::numeric_limits<double>::quiet_NaN()};

            unsigned long int iterations_{0};
        };
    }
}

#endif