simple_scored_sampling_planner.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Copyright (c) 2008, Willow Garage, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Willow Garage, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: TKruse
36  *********************************************************************/
37 
39 
40 #include <ros/console.h>
41 
42 namespace base_local_planner {
43 
44  SimpleScoredSamplingPlanner::SimpleScoredSamplingPlanner(std::vector<TrajectorySampleGenerator*> gen_list, std::vector<TrajectoryCostFunction*>& critics, int max_samples) {
45  max_samples_ = max_samples;
46  gen_list_ = gen_list;
47  critics_ = critics;
48  }
49 
50  double SimpleScoredSamplingPlanner::scoreTrajectory(Trajectory& traj, double best_traj_cost) {
51  double traj_cost = 0;
52  int gen_id = 0;
53  for(std::vector<TrajectoryCostFunction*>::iterator score_function = critics_.begin(); score_function != critics_.end(); ++score_function) {
54  TrajectoryCostFunction* score_function_p = *score_function;
55  if (score_function_p->getScale() == 0) {
56  continue;
57  }
58  double cost = score_function_p->scoreTrajectory(traj);
59  if (cost < 0) {
60  ROS_DEBUG("Velocity %.3lf, %.3lf, %.3lf discarded by cost function %d with cost: %f", traj.xv_, traj.yv_, traj.thetav_, gen_id, cost);
61  traj_cost = cost;
62  break;
63  }
64  if (cost != 0) {
65  cost *= score_function_p->getScale();
66  }
67  traj_cost += cost;
68  if (best_traj_cost > 0) {
69  // since we keep adding positives, once we are worse than the best, we will stay worse
70  if (traj_cost > best_traj_cost) {
71  break;
72  }
73  }
74  gen_id ++;
75  }
76 
77 
78  return traj_cost;
79  }
80 
81  bool SimpleScoredSamplingPlanner::findBestTrajectory(Trajectory& traj, std::vector<Trajectory>* all_explored) {
82  Trajectory loop_traj;
83  Trajectory best_traj;
84  double loop_traj_cost, best_traj_cost = -1;
85  bool gen_success;
86  int count, count_valid;
87  for (std::vector<TrajectoryCostFunction*>::iterator loop_critic = critics_.begin(); loop_critic != critics_.end(); ++loop_critic) {
88  TrajectoryCostFunction* loop_critic_p = *loop_critic;
89  if (loop_critic_p->prepare() == false) {
90  ROS_WARN("A scoring function failed to prepare");
91  return false;
92  }
93  }
94 
95  for (std::vector<TrajectorySampleGenerator*>::iterator loop_gen = gen_list_.begin(); loop_gen != gen_list_.end(); ++loop_gen) {
96  count = 0;
97  count_valid = 0;
98  TrajectorySampleGenerator* gen_ = *loop_gen;
99  while (gen_->hasMoreTrajectories()) {
100  gen_success = gen_->nextTrajectory(loop_traj);
101  if (gen_success == false) {
102  // TODO use this for debugging
103  continue;
104  }
105  loop_traj_cost = scoreTrajectory(loop_traj, best_traj_cost);
106  if (all_explored != NULL) {
107  loop_traj.cost_ = loop_traj_cost;
108  all_explored->push_back(loop_traj);
109  }
110 
111  if (loop_traj_cost >= 0) {
112  count_valid++;
113  if (best_traj_cost < 0 || loop_traj_cost < best_traj_cost) {
114  best_traj_cost = loop_traj_cost;
115  best_traj = loop_traj;
116  }
117  }
118  count++;
119  if (max_samples_ > 0 && count >= max_samples_) {
120  break;
121  }
122  }
123  if (best_traj_cost >= 0) {
124  traj.xv_ = best_traj.xv_;
125  traj.yv_ = best_traj.yv_;
126  traj.thetav_ = best_traj.thetav_;
127  traj.cost_ = best_traj_cost;
128  traj.resetPoints();
129  double px, py, pth;
130  for (unsigned int i = 0; i < best_traj.getPointsSize(); i++) {
131  best_traj.getPoint(i, px, py, pth);
132  traj.addPoint(px, py, pth);
133  }
134  }
135  ROS_DEBUG("Evaluated %d trajectories, found %d valid", count, count_valid);
136  if (best_traj_cost >= 0) {
137  // do not try fallback generators
138  break;
139  }
140  }
141  return best_traj_cost >= 0;
142  }
143 
144 
145 }// namespace
std::vector< TrajectorySampleGenerator * > gen_list_
virtual double scoreTrajectory(Trajectory &traj)=0
void getPoint(unsigned int index, double &x, double &y, double &th) const
Get a point within the trajectory.
Definition: trajectory.cpp:47
double cost_
The cost/score of the trajectory.
Definition: trajectory.h:62
#define ROS_WARN(...)
double scoreTrajectory(Trajectory &traj, double best_traj_cost)
void resetPoints()
Clear the trajectory&#39;s points.
Definition: trajectory.cpp:65
bool findBestTrajectory(Trajectory &traj, std::vector< Trajectory > *all_explored=0)
virtual bool nextTrajectory(Trajectory &traj)=0
double thetav_
The x, y, and theta velocities of the trajectory.
Definition: trajectory.h:60
unsigned int getPointsSize() const
Return the number of points in the trajectory.
Definition: trajectory.cpp:77
void addPoint(double x, double y, double th)
Add a point to the end of a trajectory.
Definition: trajectory.cpp:59
Provides an interface for navigation trajectory generators.
Provides an interface for critics of trajectories During each sampling run, a batch of many trajector...
Holds a trajectory generated by considering an x, y, and theta velocity.
Definition: trajectory.h:44
#define ROS_DEBUG(...)


base_local_planner
Author(s): Eitan Marder-Eppstein, Eric Perko, contradict@gmail.com
autogenerated on Sun Mar 3 2019 03:44:25