Loading...
Searching...
No Matches
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
48namespace ob = ompl::base;
49namespace og = ompl::geometric;
50
51enum PlannerType
52{
53 VFRRT = 0,
54 TRRT,
55 RRTSTAR
56};
57
59Eigen::VectorXd field(const ob::State *state)
60{
62 Eigen::VectorXd v(2);
63 v[0] = std::cos(x[0]) * std::sin(x[1]);
64 v[1] = std::sin(x[0]) * std::cos(x[1]);
65 return -v;
66}
67
69og::SimpleSetupPtr setupProblem(PlannerType plannerType)
70{
71 // construct the state space we are planning in
72 auto space(std::make_shared<ob::RealVectorStateSpace>(2));
73 auto si(std::make_shared<ob::SpaceInformation>(space));
74
75 ob::RealVectorBounds bounds(2);
76 bounds.setLow(-10);
77 bounds.setHigh(10);
78
79 space->setBounds(bounds);
80
81 // define a simple setup class
82 auto ss(std::make_shared<og::SimpleSetup>(space));
83
84 // set state validity checking for this space
85 ss->setStateValidityChecker(std::make_shared<ob::AllValidStateValidityChecker>(si));
86
87 // create a start state
88 ob::ScopedState<> start(space);
89 start[0] = -5;
90 start[1] = -2;
91
92 // create a goal state
93 ob::ScopedState<> goal(space);
94 goal[0] = 5;
95 goal[1] = 3;
96
97 // set the start and goal states
98 ss->setStartAndGoalStates(start, goal, 0.1);
99
100 // make the optimization objectives for TRRT and RRT*, and set the planner
101 if (plannerType == TRRT)
102 {
103 ss->setOptimizationObjective(std::make_shared<ob::VFMechanicalWorkOptimizationObjective>(si, field));
104 ss->setPlanner(std::make_shared<og::TRRT>(ss->getSpaceInformation()));
105 }
106 else if (plannerType == RRTSTAR)
107 {
108 ss->setOptimizationObjective(std::make_shared<ob::VFUpstreamCriterionOptimizationObjective>(si, field));
109 ss->setPlanner(std::make_shared<og::RRTstar>(ss->getSpaceInformation()));
110 }
111 else if (plannerType == VFRRT)
112 {
113 double explorationSetting = 0.7;
114 double lambda = 1;
115 unsigned int update_freq = 100;
116 ss->setPlanner(
117 std::make_shared<og::VFRRT>(ss->getSpaceInformation(), field, explorationSetting, lambda, update_freq));
118 }
119 else
120 {
121 std::cout << "Bad problem number.\n";
122 exit(-1);
123 }
124
125 ss->setup();
126
127 return ss;
128}
129
131std::string problemName(PlannerType plannerType)
132{
133 if (plannerType == VFRRT)
134 return std::string("vfrrt-conservative.path");
135 if (plannerType == TRRT)
136 return std::string("trrt-conservative.path");
137 else if (plannerType == RRTSTAR)
138 return std::string("rrtstar-conservative.path");
139 else
140 {
141 std::cout << "Bad problem number.\n";
142 exit(-1);
143 }
144}
145
146int main(int, char **)
147{
148 // Run all three problems
149 for (unsigned int n = 0; n < 3; n++)
150 {
151 // initialize the planner
152 og::SimpleSetupPtr ss = setupProblem(PlannerType(n));
153
154 // attempt to solve the problem
155 ob::PlannerStatus solved = ss->solve(10.0);
156
157 if (solved)
158 {
160 std::cout << "Found solution.\n";
161 else
162 std::cout << "Found approximate solution.\n";
163
164 // Set up to write the path
165 std::ofstream f(problemName(PlannerType(n)).c_str());
166 ompl::geometric::PathGeometric p = ss->getSolutionPath();
167 p.interpolate();
168 auto upstream(
169 std::make_shared<ob::VFUpstreamCriterionOptimizationObjective>(ss->getSpaceInformation(), field));
170 p.printAsMatrix(f);
171 std::cout << "Total upstream cost: " << p.cost(upstream) << "\n";
172 }
173 else
174 std::cout << "No solution found.\n";
175 }
176
177 return 0;
178}
The lower and upper bounds for an Rn space.
Definition of a scoped state.
Definition ScopedState.h:57
Definition of an abstract state.
Definition State.h:50
const T * as() const
Cast this instance to a desired type.
Definition State.h:66
Definition of a geometric path.
void interpolate(unsigned int count)
Insert a number of states in a path so that the path is made up of exactly count states....
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....
base::Cost cost(const base::OptimizationObjectivePtr &obj) const override
The sum of the costs for the sequence of segments that make up the path, computed using OptimizationO...
This namespace contains sampling based planning routines shared by both planning under geometric cons...
This namespace contains code that is specific to planning under geometric constraints.
A class to store the exit status of Planner::solve().
@ EXACT_SOLUTION
The planner found an exact solution.