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/informedtrees/AITstar.h>
47 #include <ompl/geometric/planners/informedtrees/BITstar.h>
48 #include <ompl/geometric/planners/informedtrees/EITstar.h>
49 #include <ompl/geometric/planners/informedtrees/EIRMstar.h>
50 #include <ompl/geometric/planners/cforest/CForest.h>
51 #include <ompl/geometric/planners/fmt/FMT.h>
52 #include <ompl/geometric/planners/fmt/BFMT.h>
53 #include <ompl/geometric/planners/prm/PRMstar.h>
54 #include <ompl/geometric/planners/rrt/InformedRRTstar.h>
55 #include <ompl/geometric/planners/rrt/RRTstar.h>
56 #include <ompl/geometric/planners/rrt/SORRTstar.h>
59 #include <boost/program_options.hpp>
61 #include <boost/algorithm/string.hpp>
88 enum planningObjective
90 OBJECTIVE_PATHCLEARANCE,
92 OBJECTIVE_THRESHOLDPATHLENGTH,
93 OBJECTIVE_WEIGHTEDCOMBO
97 bool argParse(
int argc,
char **argv,
double *runTimePtr, optimalPlanner *plannerPtr, planningObjective *objectivePtr,
98 std::string *outputFilePtr);
127 double x = state2D->values[0];
128 double y = state2D->values[1];
132 return sqrt((x - 0.5) * (x - 0.5) + (y - 0.5) * (y - 0.5)) - 0.25;
136 ob::OptimizationObjectivePtr getPathLengthObjective(
const ob::SpaceInformationPtr &si);
138 ob::OptimizationObjectivePtr getThresholdPathLengthObj(
const ob::SpaceInformationPtr &si);
140 ob::OptimizationObjectivePtr getClearanceObjective(
const ob::SpaceInformationPtr &si);
142 ob::OptimizationObjectivePtr getBalancedObjective1(
const ob::SpaceInformationPtr &si);
144 ob::OptimizationObjectivePtr getBalancedObjective2(
const ob::SpaceInformationPtr &si);
146 ob::OptimizationObjectivePtr getPathLengthObjWithCostToGo(
const ob::SpaceInformationPtr &si);
148 ob::PlannerPtr allocatePlanner(ob::SpaceInformationPtr si, optimalPlanner plannerType)
152 case PLANNER_AITSTAR:
154 return std::make_shared<og::AITstar>(si);
157 case PLANNER_BFMTSTAR:
159 return std::make_shared<og::BFMT>(si);
162 case PLANNER_BITSTAR:
164 return std::make_shared<og::BITstar>(si);
167 case PLANNER_CFOREST:
169 return std::make_shared<og::CForest>(si);
172 case PLANNER_EITSTAR:
174 return std::make_shared<og::EITstar>(si);
177 case PLANNER_EIRMSTAR:
179 return std::make_shared<og::EIRMstar>(si);
182 case PLANNER_FMTSTAR:
184 return std::make_shared<og::FMT>(si);
187 case PLANNER_INF_RRTSTAR:
189 return std::make_shared<og::InformedRRTstar>(si);
192 case PLANNER_PRMSTAR:
194 return std::make_shared<og::PRMstar>(si);
197 case PLANNER_RRTSTAR:
199 return std::make_shared<og::RRTstar>(si);
202 case PLANNER_SORRTSTAR:
204 return std::make_shared<og::SORRTstar>(si);
209 OMPL_ERROR(
"Planner-type enum is not implemented in allocation function.");
210 return ob::PlannerPtr();
216 ob::OptimizationObjectivePtr allocateObjective(
const ob::SpaceInformationPtr &si, planningObjective objectiveType)
218 switch (objectiveType)
220 case OBJECTIVE_PATHCLEARANCE:
221 return getClearanceObjective(si);
223 case OBJECTIVE_PATHLENGTH:
224 return getPathLengthObjective(si);
226 case OBJECTIVE_THRESHOLDPATHLENGTH:
227 return getThresholdPathLengthObj(si);
229 case OBJECTIVE_WEIGHTEDCOMBO:
230 return getBalancedObjective1(si);
233 OMPL_ERROR(
"Optimization-objective enum is not implemented in allocation function.");
234 return ob::OptimizationObjectivePtr();
239 void plan(
double runTime, optimalPlanner plannerType, planningObjective objectiveType,
const std::string &outputFile)
243 auto space(std::make_shared<ob::RealVectorStateSpace>(2));
246 space->setBounds(0.0, 1.0);
249 auto si(std::make_shared<ob::SpaceInformation>(space));
252 si->setStateValidityChecker(std::make_shared<ValidityChecker>(si));
269 auto pdef(std::make_shared<ob::ProblemDefinition>(si));
272 pdef->setStartAndGoalStates(start, goal);
276 pdef->setOptimizationObjective(allocateObjective(si, objectiveType));
280 ob::PlannerPtr optimizingPlanner = allocatePlanner(si, plannerType);
283 optimizingPlanner->setProblemDefinition(pdef);
284 optimizingPlanner->setup();
292 std::cout << optimizingPlanner->getName() <<
" found a solution of length " << pdef->getSolutionPath()->length()
293 <<
" with an optimization objective value of "
294 << pdef->getSolutionPath()->cost(pdef->getOptimizationObjective()) << std::endl;
298 if (!outputFile.empty())
300 std::ofstream outFile(outputFile.c_str());
301 std::static_pointer_cast<og::PathGeometric>(pdef->getSolutionPath())->printAsMatrix(outFile);
306 std::cout <<
"No solution found." << std::endl;
309 int main(
int argc,
char **argv)
313 optimalPlanner plannerType;
314 planningObjective objectiveType;
315 std::string outputFile;
318 if (argParse(argc, argv, &runTime, &plannerType, &objectiveType, &outputFile))
321 plan(runTime, plannerType, objectiveType, outputFile);
334 ob::OptimizationObjectivePtr getPathLengthObjective(
const ob::SpaceInformationPtr &si)
336 return std::make_shared<ob::PathLengthOptimizationObjective>(si);
342 ob::OptimizationObjectivePtr getThresholdPathLengthObj(
const ob::SpaceInformationPtr &si)
344 auto obj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
345 obj->setCostThreshold(
ob::Cost(1.51));
364 ClearanceObjective(
const ob::SpaceInformationPtr &si) :
ob::StateCostIntegralObjective(si, true)
375 return ob::Cost(1 / (si_->getStateValidityChecker()->clearance(s) + std::numeric_limits<double>::min()));
381 ob::OptimizationObjectivePtr getClearanceObjective(
const ob::SpaceInformationPtr &si)
383 return std::make_shared<ClearanceObjective>(si);
398 ob::OptimizationObjectivePtr getBalancedObjective1(
const ob::SpaceInformationPtr &si)
400 auto lengthObj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
401 auto clearObj(std::make_shared<ClearanceObjective>(si));
402 auto opt(std::make_shared<ob::MultiOptimizationObjective>(si));
403 opt->addObjective(lengthObj, 10.0);
404 opt->addObjective(clearObj, 1.0);
406 return ob::OptimizationObjectivePtr(opt);
412 ob::OptimizationObjectivePtr getBalancedObjective2(
const ob::SpaceInformationPtr &si)
414 auto lengthObj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
415 auto clearObj(std::make_shared<ClearanceObjective>(si));
417 return 10.0 * lengthObj + clearObj;
423 ob::OptimizationObjectivePtr getPathLengthObjWithCostToGo(
const ob::SpaceInformationPtr &si)
425 auto obj(std::make_shared<ob::PathLengthOptimizationObjective>(si));
426 obj->setCostToGoHeuristic(&ob::goalRegionCostToGo);
431 bool argParse(
int argc,
char **argv,
double *runTimePtr, optimalPlanner *plannerPtr, planningObjective *objectivePtr,
432 std::string *outputFilePtr)
434 namespace bpo = boost::program_options;
437 bpo::options_description desc(
"Allowed options");
438 desc.add_options()(
"help,h",
"produce help message")(
439 "runtime,t", bpo::value<double>()->default_value(1.0),
440 "(Optional) Specify the runtime in seconds. Defaults to 1 and "
441 "must be greater than 0.")(
"planner,p", bpo::value<std::string>()->default_value(
"RRTstar"),
442 "(Optional) Specify the optimal planner to use, defaults to RRTstar if not given. "
443 "Valid options are AITstar, "
444 "BFMTstar, BITstar, CForest, EITstar, EIRMstar, FMTstar, InformedRRTstar, PRMstar, RRTstar, "
446 (
"objective,o", bpo::value<std::string>()->default_value(
"PathLength"),
447 "(Optional) Specify the optimization objective, defaults to PathLength if not given. Valid options are "
448 "PathClearance, PathLength, ThresholdPathLength, and WeightedLengthAndClearanceCombo.")
449 (
"file,f", bpo::value<std::string>()->default_value(
""),
450 "(Optional) Specify an output path for the found solution path.")(
451 "info,i", bpo::value<unsigned int>()->default_value(0u),
452 "(Optional) Set the OMPL log level. 0 for WARN, 1 for INFO, 2 for DEBUG. Defaults to WARN.");
453 bpo::variables_map vm;
454 bpo::store(bpo::parse_command_line(argc, argv, desc), vm);
458 if (vm.count(
"help") != 0u)
460 std::cout << desc << std::endl;
465 unsigned int logLevel = vm[
"info"].as<
unsigned int>();
472 else if (logLevel == 1u)
476 else if (logLevel == 2u)
482 std::cout <<
"Invalid log-level integer." << std::endl << std::endl << desc << std::endl;
487 *runTimePtr = vm[
"runtime"].as<
double>();
490 if (*runTimePtr <= 0.0)
492 std::cout <<
"Invalid runtime." << std::endl << std::endl << desc << std::endl;
497 std::string plannerStr = vm[
"planner"].as<std::string>();
500 if (boost::iequals(
"AITstar", plannerStr))
502 *plannerPtr = PLANNER_AITSTAR;
504 else if (boost::iequals(
"BFMTstar", plannerStr))
506 *plannerPtr = PLANNER_BFMTSTAR;
508 else if (boost::iequals(
"BITstar", plannerStr))
510 *plannerPtr = PLANNER_BITSTAR;
512 else if (boost::iequals(
"CForest", plannerStr))
514 *plannerPtr = PLANNER_CFOREST;
516 else if (boost::iequals(
"EITstar", plannerStr))
518 *plannerPtr = PLANNER_EITSTAR;
520 else if (boost::iequals(
"EIRMstar", plannerStr))
522 *plannerPtr = PLANNER_EIRMSTAR;
524 else if (boost::iequals(
"FMTstar", plannerStr))
526 *plannerPtr = PLANNER_FMTSTAR;
528 else if (boost::iequals(
"InformedRRTstar", plannerStr))
530 *plannerPtr = PLANNER_INF_RRTSTAR;
532 else if (boost::iequals(
"PRMstar", plannerStr))
534 *plannerPtr = PLANNER_PRMSTAR;
536 else if (boost::iequals(
"RRTstar", plannerStr))
538 *plannerPtr = PLANNER_RRTSTAR;
540 else if (boost::iequals(
"SORRTstar", plannerStr))
542 *plannerPtr = PLANNER_SORRTSTAR;
546 std::cout <<
"Invalid planner string." << std::endl << std::endl << desc << std::endl;
551 std::string objectiveStr = vm[
"objective"].as<std::string>();
554 if (boost::iequals(
"PathClearance", objectiveStr))
556 *objectivePtr = OBJECTIVE_PATHCLEARANCE;
558 else if (boost::iequals(
"PathLength", objectiveStr))
560 *objectivePtr = OBJECTIVE_PATHLENGTH;
562 else if (boost::iequals(
"ThresholdPathLength", objectiveStr))
564 *objectivePtr = OBJECTIVE_THRESHOLDPATHLENGTH;
566 else if (boost::iequals(
"WeightedLengthAndClearanceCombo", objectiveStr))
568 *objectivePtr = OBJECTIVE_WEIGHTEDCOMBO;
572 std::cout <<
"Invalid objective string." << std::endl << std::endl << desc << std::endl;
577 *outputFilePtr = vm[
"file"].as<std::string>();