ProblemDefinition.h
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 #ifndef OMPL_BASE_PROBLEM_DEFINITION_
38 #define OMPL_BASE_PROBLEM_DEFINITION_
39 
40 #include "ompl/base/State.h"
41 #include "ompl/base/Goal.h"
42 #include "ompl/base/Path.h"
43 #include "ompl/base/Cost.h"
44 #include "ompl/base/SpaceInformation.h"
45 #include "ompl/base/SolutionNonExistenceProof.h"
46 #include "ompl/util/Console.h"
47 #include "ompl/util/ClassForward.h"
48 #include "ompl/base/ScopedState.h"
49 
50 #include <vector>
51 #include <cstdlib>
52 #include <iostream>
53 #include <limits>
54 
55 namespace ompl
56 {
57  namespace base
58  {
59 
61 
62  OMPL_CLASS_FORWARD(ProblemDefinition);
63  OMPL_CLASS_FORWARD(OptimizationObjective);
65 
71  {
73  PlannerSolution(const PathPtr &path) :
74  index_(-1), path_(path),
75  length_(path ? path->length() : std::numeric_limits<double>::infinity()),
76  approximate_(false), difference_(-1), optimized_(false)
77  {
78  }
79 
81  bool operator==(const PlannerSolution &p) const
82  {
83  return path_ == p.path_;
84  }
85 
87  bool operator<(const PlannerSolution &b) const;
88 
90  void setApproximate(double difference)
91  {
92  approximate_ = true;
93  difference_ = difference;
94  }
95 
97  void setOptimized(const OptimizationObjectivePtr &opt, Cost cost, bool meetsObjective)
98  {
99  opt_ = opt;
100  cost_ = cost;
101  optimized_ = meetsObjective;
102  }
103 
105  void setPlannerName(const std::string &name)
106  {
107  plannerName_ = name;
108  }
109 
111  int index_;
112 
115 
117  double length_;
118 
121 
123  double difference_;
124 
127 
130 
133 
135  std::string plannerName_;
136  };
137 
138  class Planner;
139 
142  typedef std::function<void(const Planner*, const std::vector<const base::State*> &, const Cost)> ReportIntermediateSolutionFn;
143 
144  OMPL_CLASS_FORWARD(OptimizationObjective);
145 
150  {
151  public:
152  // non-copyable
153  ProblemDefinition(const ProblemDefinition&) = delete;
154  ProblemDefinition& operator=(const ProblemDefinition&) = delete;
155 
158 
159  virtual ~ProblemDefinition()
160  {
162  }
163 
166  {
167  return si_;
168  }
169 
171  void addStartState(const State *state)
172  {
173  startStates_.push_back(si_->cloneState(state));
174  }
175 
177  void addStartState(const ScopedState<> &state)
178  {
179  startStates_.push_back(si_->cloneState(state.get()));
180  }
181 
185  bool hasStartState(const State *state, unsigned int *startIndex = nullptr) const;
186 
189  {
190  for (unsigned int i = 0 ; i < startStates_.size() ; ++i)
191  si_->freeState(startStates_[i]);
192  startStates_.clear();
193  }
194 
196  unsigned int getStartStateCount() const
197  {
198  return startStates_.size();
199  }
200 
202  const State* getStartState(unsigned int index) const
203  {
204  return startStates_[index];
205  }
206 
208  State* getStartState(unsigned int index)
209  {
210  return startStates_[index];
211  }
212 
214  void setGoal(const GoalPtr &goal)
215  {
216  goal_ = goal;
217  }
218 
220  void clearGoal()
221  {
222  goal_.reset();
223  }
224 
226  const GoalPtr& getGoal() const
227  {
228  return goal_;
229  }
230 
235  void getInputStates(std::vector<const State*> &states) const;
236 
244  void setStartAndGoalStates(const State *start, const State *goal, const double threshold = std::numeric_limits<double>::epsilon());
245 
247  void setGoalState(const State *goal, const double threshold = std::numeric_limits<double>::epsilon());
248 
250  void setStartAndGoalStates(const ScopedState<> &start, const ScopedState<> &goal, const double threshold = std::numeric_limits<double>::epsilon())
251  {
252  setStartAndGoalStates(start.get(), goal.get(), threshold);
253  }
254 
256  void setGoalState(const ScopedState<> &goal, const double threshold = std::numeric_limits<double>::epsilon())
257  {
258  setGoalState(goal.get(), threshold);
259  }
260 
263  {
264  return optimizationObjective_.get();
265  }
266 
269  {
270  return optimizationObjective_;
271  }
272 
274  void setOptimizationObjective(const OptimizationObjectivePtr &optimizationObjective)
275  {
276  optimizationObjective_ = optimizationObjective;
277  }
278 
282  {
284  }
285 
289  }
290 
296  bool isTrivial(unsigned int *startIndex = nullptr, double *distance = nullptr) const;
297 
311 
316  bool fixInvalidInputStates(double distStart, double distGoal, unsigned int attempts);
317 
319  bool hasSolution() const;
320 
322  bool hasExactSolution() const
323  {
324  return this->hasSolution() && !this->hasApproximateSolution();
325  }
326 
330  bool hasApproximateSolution() const;
331 
333  double getSolutionDifference() const;
334 
336  bool hasOptimizedSolution() const;
337 
342  PathPtr getSolutionPath() const;
343 
347  bool getSolution(PlannerSolution& solution) const;
348 
354  void addSolutionPath(const PathPtr &path, bool approximate = false, double difference = -1.0, const std::string& plannerName = "Unknown") const;
355 
357  void addSolutionPath(const PlannerSolution &sol) const;
358 
360  std::size_t getSolutionCount() const;
361 
363  std::vector<PlannerSolution> getSolutions() const;
364 
366  void clearSolutionPaths() const;
367 
369  bool hasSolutionNonExistenceProof() const;
370 
373 
376 
378  void setSolutionNonExistenceProof(const SolutionNonExistenceProofPtr& nonExistenceProof);
379 
381  void print(std::ostream &out = std::cout) const;
382 
383  protected:
384 
386  bool fixInvalidInputState(State *state, double dist, bool start, unsigned int attempts);
387 
390 
392  std::vector<State*> startStates_;
393 
396 
399 
402 
405 
406  private:
407 
409  OMPL_CLASS_FORWARD(PlannerSolutionSet);
411 
413  PlannerSolutionSetPtr solutions_;
414  };
415  }
416 }
417 
418 #endif
const OptimizationObjectivePtr & getOptimizationObjective() const
Get the optimization objective to be considered during planning.
GoalPtr goal_
The goal representation.
void setGoal(const GoalPtr &goal)
Set the goal.
PlannerSolution(const PathPtr &path)
Construct a solution that consists of a path and its attributes (whether it is approximate and the di...
void setApproximate(double difference)
Specify that the solution is approximate and set the difference to the goal.
bool optimized_
True if the solution was optimized to meet the specified optimization criterion.
void setOptimizationObjective(const OptimizationObjectivePtr &optimizationObjective)
Set the optimization objective to be considered during planning.
Representation of a solution to a planning problem.
void clearSolutionNonExistenceProof()
Removes any existing instance of SolutionNonExistenceProof.
Definition of a scoped state.
Definition: ScopedState.h:56
State * getStartState(unsigned int index)
Returns a specific start state.
A shared pointer wrapper for ompl::base::SolutionNonExistenceProof.
ReportIntermediateSolutionFn intermediateSolutionCallback_
Callback function which is called when a new intermediate solution has been found.
void print(std::ostream &out=std::cout) const
Print information about the start and goal states and the optimization objective. ...
PathPtr path_
Solution path.
bool isTrivial(unsigned int *startIndex=nullptr, double *distance=nullptr) const
A problem is trivial if a given starting state already in the goal region, so we need no motion plann...
double getSolutionDifference() const
Get the distance to the desired goal for the top solution. Return -1.0 if there are no solutions avai...
void setGoalState(const State *goal, const double threshold=std::numeric_limits< double >::epsilon())
A simple form of setting the goal. This is called by setStartAndGoalStates(). A more general form is ...
std::function< void(const Planner *, const std::vector< const base::State * > &, const Cost)> ReportIntermediateSolutionFn
When a planner has an intermediate solution (e.g., optimizing planners), a function with this signatu...
const SpaceInformationPtr & getSpaceInformation() const
Get the space information this problem definition is for.
void setStartAndGoalStates(const State *start, const State *goal, const double threshold=std::numeric_limits< double >::epsilon())
In the simplest case possible, we have a single starting state and a single goal state.
void addStartState(const ScopedState<> &state)
Add a start state. The state is copied.
const GoalPtr & getGoal() const
Return the current goal.
PathPtr getSolutionPath() const
Return the top solution path, if one is found. The top path is the shortest one that was found...
SolutionNonExistenceProofPtr nonExistenceProof_
A Representation of a proof of non-existence of a solution for this problem definition.
const State * getStartState(unsigned int index) const
Returns a specific start state.
bool hasOptimizedSolution() const
Return true if the top found solution is optimized (satisfies the specified optimization objective) ...
StateType * get()
Returns a pointer to the contained state.
Definition: ScopedState.h:396
unsigned int getStartStateCount() const
Returns the number of start states.
bool hasSolution() const
Returns true if a solution path has been found (could be approximate)
bool approximate_
True if goal was not achieved, but an approximate solution was found.
bool hasOptimizationObjective() const
Check if an optimization objective was defined for planning.
bool operator<(const PlannerSolution &b) const
Define a ranking for solutions.
void clearSolutionPaths() const
Forget the solution paths (thread safe). Memory is freed.
void addSolutionPath(const PathPtr &path, bool approximate=false, double difference=-1.0, const std::string &plannerName="Unknown") const
Add a solution path in a thread-safe manner. Multiple solutions can be set for a goal. If a solution does not reach the desired goal it is considered approximate. Optionally, the distance between the desired goal and the one actually achieved is set by difference. Optionally, the name of the planner that generated the solution.
void setStartAndGoalStates(const ScopedState<> &start, const ScopedState<> &goal, const double threshold=std::numeric_limits< double >::epsilon())
In the simplest case possible, we have a single starting state and a single goal state.
Base class for a planner.
Definition: Planner.h:230
bool hasApproximateSolution() const
Return true if the top found solution is approximate (does not actually reach the desired goal...
void setSolutionNonExistenceProof(const SolutionNonExistenceProofPtr &nonExistenceProof)
Set the instance of SolutionNonExistenceProof for this problem definition.
void setGoalState(const ScopedState<> &goal, const double threshold=std::numeric_limits< double >::epsilon())
A simple form of setting the goal. This is called by setStartAndGoalStates(). A more general form is ...
OptimizationObjectivePtr optimizationObjective_
The objective to be optimized while solving the planning problem.
int index_
When multiple solutions are found, each is given a number starting at 0, so that the order in which t...
const ReportIntermediateSolutionFn & getIntermediateSolutionCallback() const
When this function returns a valid function pointer, that function should be called by planners that ...
bool fixInvalidInputStates(double distStart, double distGoal, unsigned int attempts)
Many times the start or goal state will barely touch an obstacle. In this case, we may want to automa...
std::vector< State * > startStates_
The set of start states.
A shared pointer wrapper for ompl::base::SpaceInformation.
Cost cost_
The cost of this solution path, with respect to the optimization objective.
Definition of an abstract state.
Definition: State.h:50
PathPtr isStraightLinePathValid() const
Check if a straight line path is valid. If it is, return an instance of a path that represents the st...
bool operator==(const PlannerSolution &p) const
Return true if two solutions are the same.
std::vector< PlannerSolution > getSolutions() const
Get all the solution paths available for this goal.
void clearGoal()
Clear the goal. Memory is freed.
Abstract definition of optimization objectives.
void setOptimized(const OptimizationObjectivePtr &opt, Cost cost, bool meetsObjective)
Set the optimization objective used to optimize this solution, the cost of the solution and whether i...
bool fixInvalidInputState(State *state, double dist, bool start, unsigned int attempts)
Helper function for fixInvalidInputStates(). Attempts to fix an individual state. ...
bool hasExactSolution() const
Returns true if an exact solution path has been found. Specifically returns hasSolution && !hasApprox...
Definition of a problem to be solved. This includes the start state(s) for the system and a goal spec...
A shared pointer wrapper for ompl::base::OptimizationObjective.
OptimizationObjectivePtr opt_
Optimization objective that was used to optimize this solution.
bool hasStartState(const State *state, unsigned int *startIndex=nullptr) const
Check whether a specified starting state is already included in the problem definition and optionally...
bool getSolution(PlannerSolution &solution) const
Return true if a top solution is found, with the top solution passed by reference in the function hea...
std::string plannerName_
Name of planner type that generated this solution, as received from Planner::getName() ...
SpaceInformationPtr si_
The space information this problem definition is for.
bool hasSolutionNonExistenceProof() const
Returns true if the problem definition has a proof of non existence for a solution.
void addStartState(const State *state)
Add a start state. The state is copied.
A shared pointer wrapper for ompl::base::Goal.
std::size_t getSolutionCount() const
Get the number of solutions already found.
const SolutionNonExistenceProofPtr & getSolutionNonExistenceProof() const
Retrieve a pointer to the SolutionNonExistenceProof instance for this problem definition.
double difference_
The achieved difference between the found solution and the desired goal.
void getInputStates(std::vector< const State * > &states) const
Get all the input states. This includes start states and states that are part of goal regions that ca...
double length_
For efficiency reasons, keep the length of the path as well.
void setIntermediateSolutionCallback(const ReportIntermediateSolutionFn &callback)
Set the callback to be called by planners that can compute intermediate solutions.
void setPlannerName(const std::string &name)
Set the name of the planner used to compute this solution.
Definition of a cost value. Can represent the cost of a motion or the cost of a state.
Definition: Cost.h:47
A shared pointer wrapper for ompl::base::Path.
void clearStartStates()
Clear all start states (memory is freed)