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  gen_id++;
57  continue;
58  }
59  double cost = score_function_p->scoreTrajectory(traj);
60  if (cost < 0) {
61  ROS_DEBUG("Velocity %.3lf, %.3lf, %.3lf discarded by cost function %d with cost: %f", traj.xv_, traj.yv_, traj.thetav_, gen_id, cost);
62  traj_cost = cost;
63  break;
64  }
65  if (cost != 0) {
66  cost *= score_function_p->getScale();
67  }
68  traj_cost += cost;
69  if (best_traj_cost > 0) {
70  // since we keep adding positives, once we are worse than the best, we will stay worse
71  if (traj_cost > best_traj_cost) {
72  break;
73  }
74  }
75  gen_id ++;
76  }
77 
78 
79  return traj_cost;
80  }
81 
82  bool SimpleScoredSamplingPlanner::findBestTrajectory(Trajectory& traj, std::vector<Trajectory>* all_explored) {
83  Trajectory loop_traj;
84  Trajectory best_traj;
85  double loop_traj_cost, best_traj_cost = -1;
86  bool gen_success;
87  int count, count_valid;
88  for (std::vector<TrajectoryCostFunction*>::iterator loop_critic = critics_.begin(); loop_critic != critics_.end(); ++loop_critic) {
89  TrajectoryCostFunction* loop_critic_p = *loop_critic;
90  if (loop_critic_p->prepare() == false) {
91  ROS_WARN("A scoring function failed to prepare");
92  return false;
93  }
94  }
95 
96  for (std::vector<TrajectorySampleGenerator*>::iterator loop_gen = gen_list_.begin(); loop_gen != gen_list_.end(); ++loop_gen) {
97  count = 0;
98  count_valid = 0;
99  TrajectorySampleGenerator* gen_ = *loop_gen;
100  while (gen_->hasMoreTrajectories()) {
101  gen_success = gen_->nextTrajectory(loop_traj);
102  if (gen_success == false) {
103  // TODO use this for debugging
104  continue;
105  }
106  loop_traj_cost = scoreTrajectory(loop_traj, best_traj_cost);
107  if (all_explored != NULL) {
108  loop_traj.cost_ = loop_traj_cost;
109  all_explored->push_back(loop_traj);
110  }
111 
112  if (loop_traj_cost >= 0) {
113  count_valid++;
114  if (best_traj_cost < 0 || loop_traj_cost < best_traj_cost) {
115  best_traj_cost = loop_traj_cost;
116  best_traj = loop_traj;
117  }
118  }
119  count++;
120  if (max_samples_ > 0 && count >= max_samples_) {
121  break;
122  }
123  }
124  if (best_traj_cost >= 0) {
125  traj.xv_ = best_traj.xv_;
126  traj.yv_ = best_traj.yv_;
127  traj.thetav_ = best_traj.thetav_;
128  traj.cost_ = best_traj_cost;
129  traj.resetPoints();
130  double px, py, pth;
131  for (unsigned int i = 0; i < best_traj.getPointsSize(); i++) {
132  best_traj.getPoint(i, px, py, pth);
133  traj.addPoint(px, py, pth);
134  }
135  }
136  ROS_DEBUG("Evaluated %d trajectories, found %d valid", count, count_valid);
137  if (best_traj_cost >= 0) {
138  // do not try fallback generators
139  break;
140  }
141  }
142  return best_traj_cost >= 0;
143  }
144 
145 
146 }// 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 Thu Jan 21 2021 04:05:49