Program Listing for File KPIECE1.h

Return to documentation for file (src/ompl/control/planners/kpiece/KPIECE1.h)

/*********************************************************************
* Software License Agreement (BSD License)
*
*  Copyright (c) 2010, 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 */

#ifndef OMPL_CONTROL_PLANNERS_KPIECE_KPIECE1_
#define OMPL_CONTROL_PLANNERS_KPIECE_KPIECE1_

#include "ompl/control/planners/PlannerIncludes.h"
#include "ompl/base/ProjectionEvaluator.h"
#include "ompl/datastructures/GridB.h"
#include <vector>
#include <set>

namespace ompl
{
    namespace control
    {
        class KPIECE1 : public base::Planner
        {
        public:
            KPIECE1(const SpaceInformationPtr &si);

            ~KPIECE1() override;

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

            void clear() override;

            void setGoalBias(double goalBias)
            {
                goalBias_ = goalBias;
            }

            double getGoalBias() const
            {
                return goalBias_;
            }

            void setBorderFraction(double bp)
            {
                selectBorderFraction_ = bp;
            }

            double getBorderFraction() const
            {
                return selectBorderFraction_;
            }

            void setCellScoreFactor(double good, double bad)
            {
                setGoodCellScoreFactor(good);
                setBadCellScoreFactor(bad);
            }

            void setBadCellScoreFactor(double bad)
            {
                badScoreFactor_ = bad;
            }

            void setGoodCellScoreFactor(double good)
            {
                goodScoreFactor_ = good;
            }

            double getGoodCellScoreFactor() const
            {
                return goodScoreFactor_;
            }

            double getBadCellScoreFactor() const
            {
                return badScoreFactor_;
            }

            void setMaxCloseSamplesCount(unsigned int nCloseSamples)
            {
                nCloseSamples_ = nCloseSamples;
            }

            unsigned int getMaxCloseSamplesCount() const
            {
                return nCloseSamples_;
            }

            void setProjectionEvaluator(const base::ProjectionEvaluatorPtr &projectionEvaluator)
            {
                projectionEvaluator_ = projectionEvaluator;
            }

            void setProjectionEvaluator(const std::string &name)
            {
                projectionEvaluator_ = si_->getStateSpace()->getProjection(name);
            }

            const base::ProjectionEvaluatorPtr &getProjectionEvaluator() const
            {
                return projectionEvaluator_;
            }

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

        protected:
            struct Motion
            {
                Motion() = default;

                Motion(const SpaceInformation *si)
                  : state(si->allocState()), control(si->allocControl())
                {
                }

                ~Motion() = default;

                base::State *state{nullptr};

                Control *control{nullptr};

                unsigned int steps{0};

                Motion *parent{nullptr};
            };

            struct CellData
            {
                CellData() = default;

                ~CellData() = default;

                std::vector<Motion *> motions;

                double coverage{0.0};

                unsigned int selections{1};

                double score{1.0};

                unsigned int iteration{0};

                double importance{0.0};
            };

            struct OrderCellsByImportance
            {
                bool operator()(const CellData *const a, const CellData *const b) const
                {
                    return a->importance > b->importance;
                }
            };

            using Grid = GridB<CellData *, OrderCellsByImportance>;

            struct CloseSample
            {
                CloseSample(Grid::Cell *c, Motion *m, double d) : cell(c), motion(m), distance(d)
                {
                }

                Grid::Cell *cell;

                Motion *motion;

                double distance;

                bool operator<(const CloseSample &other) const
                {
                    return distance < other.distance;
                }
            };

            struct CloseSamples
            {
                CloseSamples(unsigned int size) : maxSize(size)
                {
                }

                bool consider(Grid::Cell *cell, Motion *motion, double distance);

                bool selectMotion(Motion *&smotion, Grid::Cell *&scell);

                bool canSample() const
                {
                    return !samples.empty();
                }

                unsigned int maxSize;

                std::set<CloseSample> samples;
            };

            struct TreeData
            {
                TreeData() = default;

                Grid grid{0};

                unsigned int size{0};

                unsigned int iteration{1};
            };

            static void computeImportance(Grid::Cell *cell, void * /*unused*/)
            {
                CellData &cd = *(cell->data);
                cd.importance = cd.score / ((cell->neighbors + 1) * cd.coverage * cd.selections);
            }

            void freeMemory();

            void freeGridMotions(Grid &grid);

            void freeCellData(CellData *cdata);

            void freeMotion(Motion *motion);

            Grid::Cell *addMotion(Motion *motion, double dist);

            bool selectMotion(Motion *&smotion, Grid::Cell *&scell);

            unsigned int findNextMotion(const std::vector<Grid::Coord> &coords, unsigned int index, unsigned int count);

            ControlSamplerPtr controlSampler_;

            TreeData tree_;

            const SpaceInformation *siC_;

            base::ProjectionEvaluatorPtr projectionEvaluator_;

            double goodScoreFactor_{0.9};

            double badScoreFactor_{0.45};

            unsigned int nCloseSamples_{30};

            double selectBorderFraction_{0.8};

            double goalBias_{0.05};

            RNG rng_;

            Motion *lastGoalMotion_{nullptr};
        };
    }
}

#endif