VectorFieldConservative.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2015, Caleb Voss and Wilson Beebe
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 Willow Garage 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 /* Authors: Caleb Voss, Wilson Beebe */
36 
37 #include <fstream>
38 
39 #include <ompl/base/StateSpace.h>
40 #include <ompl/base/objectives/VFMechanicalWorkOptimizationObjective.h>
41 #include <ompl/base/objectives/VFUpstreamCriterionOptimizationObjective.h>
42 #include <ompl/base/spaces/RealVectorStateSpace.h>
43 #include <ompl/geometric/planners/rrt/RRTstar.h>
44 #include <ompl/geometric/planners/rrt/TRRT.h>
45 #include <ompl/geometric/planners/rrt/VFRRT.h>
46 #include <ompl/geometric/SimpleSetup.h>
47 
48 namespace ob = ompl::base;
49 namespace og = ompl::geometric;
50 
51 enum PlannerType { VFRRT = 0, TRRT, RRTSTAR };
52 
54 Eigen::VectorXd field(const ob::State *state)
55 {
57  Eigen::VectorXd v(2);
58  v[0] = std::cos(x[0]) * std::sin(x[1]);
59  v[1] = std::sin(x[0]) * std::cos(x[1]);
60  return -v;
61 }
62 
64 og::SimpleSetupPtr setupProblem(PlannerType plannerType)
65 {
66  // construct the state space we are planning in
69 
70  ob::RealVectorBounds bounds(2);
71  bounds.setLow(-10);
72  bounds.setHigh(10);
73 
74  space->as<ob::RealVectorStateSpace>()->setBounds(bounds);
75 
76  // define a simple setup class
77  og::SimpleSetupPtr ss(new og::SimpleSetup(space));
78 
79  // set state validity checking for this space
80  ss->setStateValidityChecker(ob::StateValidityCheckerPtr(
82 
83  // create a start state
84  ob::ScopedState<> start(space);
85  start[0] = -5;
86  start[1] = -2;
87 
88  // create a goal state
89  ob::ScopedState<> goal(space);
90  goal[0] = 5;
91  goal[1] = 3;
92 
93  // set the start and goal states
94  ss->setStartAndGoalStates(start, goal, 0.1);
95 
96  // make the optimization objectives for TRRT and RRT*, and set the planner
97  if (plannerType == TRRT)
98  {
99  ss->setOptimizationObjective(ob::OptimizationObjectivePtr(
101  ss->setPlanner(ob::PlannerPtr(new og::TRRT(ss->getSpaceInformation())));
102  }
103  else if (plannerType == RRTSTAR)
104  {
105  ss->setOptimizationObjective(ob::OptimizationObjectivePtr(
107  ss->setPlanner(ob::PlannerPtr(new og::RRTstar(ss->getSpaceInformation())));
108  }
109  else if (plannerType == VFRRT)
110  {
111  double explorationSetting = 0.7;
112  double lambda = 1;
113  unsigned int update_freq = 100;
114  ss->setPlanner(ob::PlannerPtr(new og::VFRRT(ss->getSpaceInformation(), field, explorationSetting, lambda, update_freq)));
115  }
116  else
117  {
118  std::cout << "Bad problem number.\n";
119  exit(-1);
120  }
121 
122  ss->setup();
123 
124  return ss;
125 }
126 
128 std::string problemName(PlannerType plannerType)
129 {
130  if (plannerType == VFRRT)
131  return std::string("vfrrt-conservative.path");
132  else if (plannerType == TRRT)
133  return std::string("trrt-conservative.path");
134  else if (plannerType == RRTSTAR)
135  return std::string("rrtstar-conservative.path");
136  else
137  {
138  std::cout << "Bad problem number.\n";
139  exit(-1);
140  }
141 }
142 
143 int main(int argc, char **argv)
144 {
145  // Run all three problems
146  for (unsigned int n = 0; n < 3; n++)
147  {
148  // initialize the planner
149  og::SimpleSetupPtr ss = setupProblem(PlannerType(n));
150 
151  // attempt to solve the problem
152  ob::PlannerStatus solved = ss->solve(10.0);
153 
154  if (solved)
155  {
156  if (solved == ob::PlannerStatus::EXACT_SOLUTION)
157  std::cout << "Found solution.\n";
158  else
159  std::cout << "Found approximate solution.\n";
160 
161  // Set up to write the path
162  std::ofstream f(problemName(PlannerType(n)).c_str());
163  ompl::geometric::PathGeometric p = ss->getSolutionPath();
164  p.interpolate();
166  ss->getSpaceInformation(), field));
167  p.printAsMatrix(f);
168  std::cout << "Total upstream cost: " << p.cost(upstream) << "\n";
169  }
170  else
171  std::cout << "No solution found.\n";
172  }
173 
174  return 0;
175 }
Optimal Rapidly-exploring Random Trees.
Definition: RRTstar.h:79
Definition of a scoped state.
Definition: ScopedState.h:56
A shared pointer wrapper for ompl::base::StateSpace.
virtual void printAsMatrix(std::ostream &out) const
Print the path as a real-valued matrix where the i-th row represents the i-th state along the path...
State StateType
Define the type of state allocated by this space.
Definition: StateSpace.h:80
virtual base::Cost cost(const base::OptimizationObjectivePtr &obj) const
The sum of the costs for the sequence of segments that make up the path, computed using OptimizationO...
A shared pointer wrapper for ompl::base::StateValidityChecker.
The simplest state validity checker: all states are valid.
Create the set of classes typically needed to solve a geometric problem.
Definition: SimpleSetup.h:65
A shared pointer wrapper for ompl::geometric::SimpleSetup.
A shared pointer wrapper for ompl::base::Planner.
void interpolate(unsigned int count)
Insert a number of states in a path so that the path is made up of exactly count states. States are inserted uniformly (more states on longer segments). Changes are performed only if a path has less than count states.
The planner found an exact solution.
Definition: PlannerStatus.h:66
A class to store the exit status of Planner::solve()
Definition: PlannerStatus.h:48
A shared pointer wrapper for ompl::base::SpaceInformation.
A state space representing Rn. The distance function is the L2 norm.
The base class for space information. This contains all the information about the space planning is d...
Definition of an abstract state.
Definition: State.h:50
A shared pointer wrapper for ompl::base::OptimizationObjective.
The lower and upper bounds for an Rn space.
Transition-based Rapidly-exploring Random Trees.
Definition: TRRT.h:80
Definition of a geometric path.
Definition: PathGeometric.h:60
const T * as() const
Cast this instance to a desired type.
Definition: State.h:74