37#include <ompl/base/SpaceInformation.h>
38#include <ompl/base/objectives/PathLengthOptimizationObjective.h>
39#include <ompl/base/objectives/StateCostIntegralObjective.h>
40#include <ompl/base/objectives/MaximizeMinClearanceObjective.h>
41#include <ompl/base/spaces/RealVectorStateSpace.h>
46#include <ompl/geometric/planners/lazyinformedtrees/BLITstar.h>
47#include <ompl/geometric/planners/informedtrees/AITstar.h>
48#include <ompl/geometric/planners/rrt/AORRTC.h>
49#include <ompl/geometric/planners/informedtrees/BITstar.h>
50#include <ompl/geometric/planners/informedtrees/EITstar.h>
51#include <ompl/geometric/planners/informedtrees/EIRMstar.h>
52#include <ompl/geometric/planners/cforest/CForest.h>
53#include <ompl/geometric/planners/fmt/FMT.h>
54#include <ompl/geometric/planners/fmt/BFMT.h>
55#include <ompl/geometric/planners/prm/PRMstar.h>
56#include <ompl/geometric/planners/rrt/InformedRRTstar.h>
57#include <ompl/geometric/planners/rrt/RRTstar.h>
58#include <ompl/geometric/planners/rrt/SORRTstar.h>
61#include <boost/program_options.hpp>
63#include <boost/algorithm/string.hpp>
94 OBJECTIVE_PATHCLEARANCE,
96 OBJECTIVE_THRESHOLDPATHLENGTH,
97 OBJECTIVE_WEIGHTEDCOMBO
101bool argParse(
int argc,
char **argv,
double *runTimePtr, optimalPlanner *plannerPtr, planningObjective *objectivePtr,
102 std::string *outputFilePtr);
117 bool isValid(
const ob::State *state)
const override
124 double clearance(
const ob::State *state)
const override
131 double x = state2D->values[0];
132 double y = state2D->values[1];
136 return sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) - 0.25;
140ob::OptimizationObjectivePtr getPathLengthObjective(
const ob::SpaceInformationPtr &si);
142ob::OptimizationObjectivePtr getThresholdPathLengthObj(
const ob::SpaceInformationPtr &si);
144ob::OptimizationObjectivePtr getClearanceObjective(
const ob::SpaceInformationPtr &si);
146ob::OptimizationObjectivePtr getBalancedObjective1(
const ob::SpaceInformationPtr &si);
148ob::OptimizationObjectivePtr getBalancedObjective2(
const ob::SpaceInformationPtr &si);
150ob::OptimizationObjectivePtr getPathLengthObjWithCostToGo(
const ob::SpaceInformationPtr &si);
152ob::PlannerPtr allocatePlanner(ob::SpaceInformationPtr si, optimalPlanner plannerType)
156 case PLANNER_AITSTAR:
158 return std::make_shared<og::AITstar>(si);
163 return std::make_shared<og::AORRTC>(si);
166 case PLANNER_BFMTSTAR:
168 return std::make_shared<og::BFMT>(si);
171 case PLANNER_BITSTAR:
173 return std::make_shared<og::BITstar>(si);
176 case PLANNER_BLITSTAR:
178 return std::make_shared<og::BLITstar>(si);
181 case PLANNER_CFOREST:
183 return std::make_shared<og::CForest>(si);
186 case PLANNER_EITSTAR:
188 return std::make_shared<og::EITstar>(si);
191 case PLANNER_EIRMSTAR:
193 return std::make_shared<og::EIRMstar>(si);
196 case PLANNER_FMTSTAR:
198 return std::make_shared<og::FMT>(si);
201 case PLANNER_INF_RRTSTAR:
203 return std::make_shared<og::InformedRRTstar>(si);
206 case PLANNER_PRMSTAR:
208 return std::make_shared<og::PRMstar>(si);
211 case PLANNER_RRTSTAR:
213 return std::make_shared<og::RRTstar>(si);
216 case PLANNER_SORRTSTAR:
218 return std::make_shared<og::SORRTstar>(si);
223 OMPL_ERROR(
"Planner-type enum is not implemented in allocation function.");
224 return ob::PlannerPtr();
230ob::OptimizationObjectivePtr allocateObjective(
const ob::SpaceInformationPtr &si, planningObjective objectiveType)
232 switch (objectiveType)
234 case OBJECTIVE_PATHCLEARANCE:
235 return getClearanceObjective(si);
237 case OBJECTIVE_PATHLENGTH:
238 return getPathLengthObjective(si);
240 case OBJECTIVE_THRESHOLDPATHLENGTH:
241 return getThresholdPathLengthObj(si);
243 case OBJECTIVE_WEIGHTEDCOMBO:
244 return getBalancedObjective1(si);
247 OMPL_ERROR(
"Optimization-objective enum is not implemented in allocation function.");
248 return ob::OptimizationObjectivePtr();
253void plan(
double runTime, optimalPlanner plannerType, planningObjective objectiveType,
const std::string &outputFile)
257 auto space(std::make_shared<ob::RealVectorStateSpace>(2));
260 space->setBounds(0.0, 1.0);
263 auto si(std::make_shared<ob::SpaceInformation>(space));
266 si->setStateValidityChecker(std::make_shared<ValidityChecker>(si));
283 auto pdef(std::make_shared<ob::ProblemDefinition>(si));
286 pdef->setStartAndGoalStates(start, goal);
290 pdef->setOptimizationObjective(allocateObjective(si, objectiveType));
294 ob::PlannerPtr optimizingPlanner = allocatePlanner(si, plannerType);
297 optimizingPlanner->setProblemDefinition(pdef);
298 optimizingPlanner->setup();
306 std::cout << optimizingPlanner->getName() <<
" found a solution of length " << pdef->getSolutionPath()->length()
307 <<
" with an optimization objective value of "
308 << pdef->getSolutionPath()->cost(pdef->getOptimizationObjective()) << std::endl;
312 if (!outputFile.empty())
314 std::ofstream outFile(outputFile.c_str());
315 std::static_pointer_cast<og::PathGeometric>(pdef->getSolutionPath())->printAsMatrix(outFile);
320 std::cout <<
"No solution found." << std::endl;
323int main(
int argc,
char **argv)
327 optimalPlanner plannerType;
328 planningObjective objectiveType;
329 std::string outputFile;
332 if (argParse(argc, argv, &runTime, &plannerType, &objectiveType, &outputFile))
335 plan(runTime, plannerType, objectiveType, outputFile);
348ob::OptimizationObjectivePtr getPathLengthObjective(
const ob::SpaceInformationPtr &si)
350 return std::make_shared<ob::PathLengthOptimizationObjective>(si);
356ob::OptimizationObjectivePtr getThresholdPathLengthObj(
const ob::SpaceInformationPtr &si)
358 auto obj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
359 obj->setCostThreshold(
ob::Cost(1.51));
378 ClearanceObjective(
const ob::SpaceInformationPtr &si) : ob::StateCostIntegralObjective(si, true)
387 ob::Cost
stateCost(
const ob::State *s)
const override
389 return ob::Cost(1 / (si_->getStateValidityChecker()->clearance(s) + std::numeric_limits<double>::min()));
395ob::OptimizationObjectivePtr getClearanceObjective(
const ob::SpaceInformationPtr &si)
397 return std::make_shared<ClearanceObjective>(si);
412ob::OptimizationObjectivePtr getBalancedObjective1(
const ob::SpaceInformationPtr &si)
414 auto lengthObj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
415 auto clearObj(std::make_shared<ClearanceObjective>(si));
416 auto opt(std::make_shared<ob::MultiOptimizationObjective>(si));
417 opt->addObjective(lengthObj, 10.0);
418 opt->addObjective(clearObj, 1.0);
420 return ob::OptimizationObjectivePtr(opt);
426ob::OptimizationObjectivePtr getBalancedObjective2(
const ob::SpaceInformationPtr &si)
428 auto lengthObj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
429 auto clearObj(std::make_shared<ClearanceObjective>(si));
431 return 10.0 * lengthObj + clearObj;
437ob::OptimizationObjectivePtr getPathLengthObjWithCostToGo(
const ob::SpaceInformationPtr &si)
439 auto obj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
445bool argParse(
int argc,
char **argv,
double *runTimePtr, optimalPlanner *plannerPtr, planningObjective *objectivePtr,
446 std::string *outputFilePtr)
448 namespace bpo = boost::program_options;
451 bpo::options_description desc(
"Allowed options");
452 desc.add_options()(
"help,h",
"produce help message")(
"runtime,t", bpo::value<double>()->default_value(1.0),
453 "(Optional) Specify the runtime in seconds. Defaults to 1 and "
454 "must be greater than 0.")(
455 "planner,p", bpo::value<std::string>()->default_value(
"BLITstar"),
456 "(Optional) Specify the optimal planner to use, defaults to RRTstar if not given. "
457 "Valid options are AITstar, "
458 "AORRTC, BFMTstar, BITstar, BLITstar, CForest, EITstar, EIRMstar, FMTstar, InformedRRTstar, PRMstar, RRTstar, "
460 (
"objective,o", bpo::value<std::string>()->default_value(
"PathLength"),
461 "(Optional) Specify the optimization objective, defaults to PathLength if not given. Valid options are "
462 "PathClearance, PathLength, ThresholdPathLength, and WeightedLengthAndClearanceCombo.")
463 (
"file,f", bpo::value<std::string>()->default_value(
""),
464 "(Optional) Specify an output path for the found solution path.")(
465 "info,i", bpo::value<unsigned int>()->default_value(0u),
466 "(Optional) Set the OMPL log level. 0 for WARN, 1 for INFO, 2 for DEBUG. Defaults to WARN.");
467 bpo::variables_map vm;
468 bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
472 if (vm.count(
"help") != 0u)
474 std::cout << desc << std::endl;
479 unsigned int logLevel = vm[
"info"].as<
unsigned int>();
486 else if (logLevel == 1u)
490 else if (logLevel == 2u)
496 std::cout <<
"Invalid log-level integer." << std::endl << std::endl << desc << std::endl;
501 *runTimePtr = vm[
"runtime"].as<
double>();
504 if (*runTimePtr <= 0.0)
506 std::cout <<
"Invalid runtime." << std::endl << std::endl << desc << std::endl;
511 std::string plannerStr = vm[
"planner"].as<std::string>();
514 if (boost::iequals(
"AITstar", plannerStr))
516 *plannerPtr = PLANNER_AITSTAR;
518 else if (boost::iequals(
"AORRTC", plannerStr))
520 *plannerPtr = PLANNER_AORRTC;
522 else if (boost::iequals(
"BFMTstar", plannerStr))
524 *plannerPtr = PLANNER_BFMTSTAR;
526 else if (boost::iequals(
"BITstar", plannerStr))
528 *plannerPtr = PLANNER_BITSTAR;
530 else if (boost::iequals(
"BLITstar", plannerStr))
532 *plannerPtr = PLANNER_BLITSTAR;
534 else if (boost::iequals(
"CForest", plannerStr))
536 *plannerPtr = PLANNER_CFOREST;
538 else if (boost::iequals(
"EITstar", plannerStr))
540 *plannerPtr = PLANNER_EITSTAR;
542 else if (boost::iequals(
"EIRMstar", plannerStr))
544 *plannerPtr = PLANNER_EIRMSTAR;
546 else if (boost::iequals(
"FMTstar", plannerStr))
548 *plannerPtr = PLANNER_FMTSTAR;
550 else if (boost::iequals(
"InformedRRTstar", plannerStr))
552 *plannerPtr = PLANNER_INF_RRTSTAR;
554 else if (boost::iequals(
"PRMstar", plannerStr))
556 *plannerPtr = PLANNER_PRMSTAR;
558 else if (boost::iequals(
"RRTstar", plannerStr))
560 *plannerPtr = PLANNER_RRTSTAR;
562 else if (boost::iequals(
"SORRTstar", plannerStr))
564 *plannerPtr = PLANNER_SORRTSTAR;
568 std::cout <<
"Invalid planner string." << std::endl << std::endl << desc << std::endl;
573 std::string objectiveStr = vm[
"objective"].as<std::string>();
576 if (boost::iequals(
"PathClearance", objectiveStr))
578 *objectivePtr = OBJECTIVE_PATHCLEARANCE;
580 else if (boost::iequals(
"PathLength", objectiveStr))
582 *objectivePtr = OBJECTIVE_PATHLENGTH;
584 else if (boost::iequals(
"ThresholdPathLength", objectiveStr))
586 *objectivePtr = OBJECTIVE_THRESHOLDPATHLENGTH;
588 else if (boost::iequals(
"WeightedLengthAndClearanceCombo", objectiveStr))
590 *objectivePtr = OBJECTIVE_WEIGHTEDCOMBO;
594 std::cout <<
"Invalid objective string." << std::endl << std::endl << desc << std::endl;
599 *outputFilePtr = vm[
"file"].as<std::string>();
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
The definition of a state in Rn.
Definition of a scoped state.
Defines optimization objectives where path cost can be represented as a path integral over a cost fun...
Cost stateCost(const State *s) const override
Returns a cost with a value of 1.
ompl::base::State StateType
Define the type of state allocated by this space.
Abstract definition for a class checking the validity of states. The implementation of this class mus...
virtual double clearance(const State *) const
Report the distance to the nearest invalid state when starting from state. If the distance is negativ...
StateValidityChecker(SpaceInformation *si)
Constructor.
const T * as() const
Cast this instance to a desired type.
#define OMPL_ERROR(fmt,...)
Log a formatted error string.
This namespace contains sampling based planning routines shared by both planning under geometric cons...
Cost goalRegionCostToGo(const State *state, const Goal *goal)
For use when the cost-to-go of a state under the optimization objective is equivalent to the goal reg...
This namespace contains code that is specific to planning under geometric constraints.
void setLogLevel(LogLevel level)
Set the minimum level of logging data to output. Messages with lower logging levels will not be recor...
A class to store the exit status of Planner::solve().