SimpleSetup.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2010, Rice University
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of the Rice University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *********************************************************************/
34 
35 /* Author: Ioan Sucan */
36 
37 #include "ompl/control/SimpleSetup.h"
38 #include "ompl/tools/config/SelfConfig.h"
39 
41  : configured_(false), planTime_(0.0), last_status_(base::PlannerStatus::UNKNOWN)
42 {
43  si_ = si;
44  pdef_ = std::make_shared<base::ProblemDefinition>(si_);
45 }
46 
48  : configured_(false), planTime_(0.0), last_status_(base::PlannerStatus::UNKNOWN)
49 {
50  si_ = std::make_shared<SpaceInformation>(space->getStateSpace(), space);
51  pdef_ = std::make_shared<base::ProblemDefinition>(si_);
52 }
53 
55 {
56  if (!configured_ || !si_->isSetup() || !planner_->isSetup())
57  {
58  if (!si_->isSetup())
59  si_->setup();
60  if (!planner_)
61  {
62  if (pa_)
63  planner_ = pa_(si_);
64  if (!planner_)
65  {
66  OMPL_INFORM("No planner specified. Using default.");
67  planner_ = tools::SelfConfig::getDefaultPlanner(getGoal());
68  }
69  }
70  planner_->setProblemDefinition(pdef_);
71  if (!planner_->isSetup())
72  planner_->setup();
73 
74  configured_ = true;
75  }
76 }
77 
79 {
80  if (planner_)
81  planner_->clear();
82  if (pdef_)
83  pdef_->clearSolutionPaths();
84 }
85 
86 // we provide a duplicate implementation here to allow the planner to choose how the time is turned into a planner
87 // termination condition
89 {
90  setup();
91  last_status_ = base::PlannerStatus::UNKNOWN;
92  time::point start = time::now();
93  last_status_ = planner_->solve(time);
94  planTime_ = time::seconds(time::now() - start);
95  if (last_status_)
96  OMPL_INFORM("Solution found in %f seconds", planTime_);
97  else
98  OMPL_INFORM("No solution found after %f seconds", planTime_);
99  return last_status_;
100 }
101 
103 {
104  setup();
105  last_status_ = base::PlannerStatus::UNKNOWN;
106  time::point start = time::now();
107  last_status_ = planner_->solve(ptc);
108  planTime_ = time::seconds(time::now() - start);
109  if (last_status_)
110  OMPL_INFORM("Solution found in %f seconds", planTime_);
111  else
112  OMPL_INFORM("No solution found after %f seconds", planTime_);
113  return last_status_;
114 }
115 
117 {
118  if (pdef_)
119  {
120  const base::PathPtr &p = pdef_->getSolutionPath();
121  if (p)
122  return static_cast<PathControl &>(*p);
123  }
124  throw Exception("No solution path");
125 }
126 
128 {
129  return haveSolutionPath() && (!pdef_->hasApproximateSolution() ||
130  pdef_->getSolutionDifference() < std::numeric_limits<double>::epsilon());
131 }
132 
134 {
135  pd.clear();
136  if (planner_)
137  planner_->getPlannerData(pd);
138 }
139 
140 void ompl::control::SimpleSetup::print(std::ostream &out) const
141 {
142  if (si_)
143  {
144  si_->printProperties(out);
145  si_->printSettings(out);
146  }
147  if (planner_)
148  {
149  planner_->printProperties(out);
150  planner_->printSettings(out);
151  }
152  if (pdef_)
153  pdef_->print(out);
154 }
The exception type for ompl.
Definition: Exception.h:47
Object containing planner generated vertex and edge data. It is assumed that all vertices are unique,...
Definition: PlannerData.h:175
virtual void clear()
Clears the entire data structure.
Definition: PlannerData.cpp:74
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
A shared pointer wrapper for ompl::base::SpaceInformation.
A shared pointer wrapper for ompl::control::ControlSpace.
Definition of a control path.
Definition: PathControl.h:61
base::ProblemDefinitionPtr pdef_
The created problem definition.
Definition: SimpleSetup.h:279
bool haveExactSolutionPath() const
Return true if a solution path is available (previous call to solve() was successful) and the solutio...
virtual void setup()
This method will create the necessary classes for planning. The solve() method will call this functio...
Definition: SimpleSetup.cpp:54
PathControl & getSolutionPath() const
Get the solution path. Throw an exception if no solution is available.
SimpleSetup(const SpaceInformationPtr &si)
Constructor needs the control space used for planning.
Definition: SimpleSetup.cpp:40
virtual base::PlannerStatus solve(double time=1.0)
Run the planner for a specified amount of time (default is 1 second)
Definition: SimpleSetup.cpp:88
virtual void print(std::ostream &out=std::cout) const
Print information about the current setup.
void getPlannerData(base::PlannerData &pd) const
Get information about the exploration data structure the motion planner used.
virtual void clear()
Clear all planning data. This only includes data generated by motion plan computation....
Definition: SimpleSetup.cpp:78
SpaceInformationPtr si_
The created space information.
Definition: SimpleSetup.h:276
static base::PlannerPtr getDefaultPlanner(const base::GoalPtr &goal)
Given a goal specification, decide on a planner for that goal.
Definition: SelfConfig.cpp:243
#define OMPL_INFORM(fmt,...)
Log a formatted information string.
Definition: Console.h:68
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: Time.h:52
point now()
Get the current time point.
Definition: Time.h:58
duration seconds(double sec)
Return the time duration representing a given number of seconds.
Definition: Time.h:64
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:49
@ UNKNOWN
Uninitialized status.
Definition: PlannerStatus.h:54