constrained_goal_sampler.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2011, Willow Garage, Inc.
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 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 /* Author: Ioan Sucan */
36 
41 
42 #include <utility>
43 
44 namespace ompl_interface
45 {
46 constexpr char LOGNAME[] = "constrained_goal_sampler";
47 } // namespace ompl_interface
48 
50  kinematic_constraints::KinematicConstraintSetPtr ks,
51  constraint_samplers::ConstraintSamplerPtr cs)
52  : ob::GoalLazySamples(
53  pc->getOMPLSimpleSetup()->getSpaceInformation(),
54  [this](const GoalLazySamples* gls, ompl::base::State* state) {
55  return sampleUsingConstraintSampler(gls, state);
56  },
57  false)
58  , planning_context_(pc)
59  , kinematic_constraint_set_(std::move(ks))
60  , constraint_sampler_(std::move(cs))
61  , work_state_(pc->getCompleteInitialRobotState())
62  , invalid_sampled_constraints_(0)
63  , warned_invalid_samples_(false)
64  , verbose_display_(0)
65 {
66  if (!constraint_sampler_)
67  default_sampler_ = si_->allocStateSampler();
68  ROS_DEBUG_NAMED(LOGNAME, "Constructed a ConstrainedGoalSampler instance at address %p", this);
69  startSampling();
70 }
71 
73  const moveit::core::RobotState& state,
74  bool verbose) const
75 {
76  planning_context_->getOMPLStateSpace()->copyToOMPLState(new_goal, state);
77  return static_cast<const StateValidityChecker*>(si_->getStateValidityChecker().get())->isValid(new_goal, verbose);
78 }
79 
81  moveit::core::RobotState const* state,
83  const double* jpos, bool verbose) const
84 {
85  // we copy the state to not change the seed state
86  moveit::core::RobotState solution_state(*state);
87  solution_state.setJointGroupPositions(jmg, jpos);
88  solution_state.update();
89  return checkStateValidity(new_goal, solution_state, verbose);
90 }
91 
93  ob::State* new_goal)
94 {
95  // moveit::Profiler::ScopedBlock sblock("ConstrainedGoalSampler::sampleUsingConstraintSampler");
96 
97  unsigned int max_attempts = planning_context_->getMaximumGoalSamplingAttempts();
98  unsigned int attempts_so_far = gls->samplingAttemptsCount();
99 
100  // terminate after too many attempts
101  if (attempts_so_far >= max_attempts)
102  return false;
103 
104  // terminate after a maximum number of samples
105  if (gls->getStateCount() >= planning_context_->getMaximumGoalSamples())
106  return false;
107 
108  // terminate the sampling thread when a solution has been found
109  if (planning_context_->getOMPLSimpleSetup()->getProblemDefinition()->hasSolution())
110  return false;
111 
112  unsigned int max_attempts_div2 = max_attempts / 2;
113  for (unsigned int a = gls->samplingAttemptsCount(); a < max_attempts && gls->isSampling(); ++a)
114  {
115  bool verbose = false;
116  if (gls->getStateCount() == 0 && a >= max_attempts_div2)
117  if (verbose_display_ < 1)
118  {
119  verbose = true;
120  verbose_display_++;
121  }
122 
123  if (constraint_sampler_)
124  {
125  // makes the constraint sampler also perform a validity callback
126  moveit::core::GroupStateValidityCallbackFn gsvcf = [this, new_goal,
127  verbose](moveit::core::RobotState* robot_state,
128  const moveit::core::JointModelGroup* joint_group,
129  const double* joint_group_variable_values) {
130  return stateValidityCallback(new_goal, robot_state, joint_group, joint_group_variable_values, verbose);
131  };
132  constraint_sampler_->setGroupStateValidityCallback(gsvcf);
133 
134  if (constraint_sampler_->sample(work_state_, planning_context_->getMaximumStateSamplingAttempts()))
135  {
136  work_state_.update();
137  if (kinematic_constraint_set_->decide(work_state_, verbose).satisfied)
138  {
139  if (checkStateValidity(new_goal, work_state_, verbose))
140  return true;
141  }
142  else
143  {
144  invalid_sampled_constraints_++;
145  if (!warned_invalid_samples_ && invalid_sampled_constraints_ >= (attempts_so_far * 8) / 10)
146  {
147  warned_invalid_samples_ = true;
148  ROS_WARN_NAMED(LOGNAME, "More than 80%% of the sampled goal states "
149  "fail to satisfy the constraints imposed on the goal sampler. "
150  "Is the constrained sampler working correctly?");
151  }
152  }
153  }
154  }
155  else
156  {
157  default_sampler_->sampleUniform(new_goal);
158  if (static_cast<const StateValidityChecker*>(si_->getStateValidityChecker().get())->isValid(new_goal, verbose))
159  {
160  planning_context_->getOMPLStateSpace()->copyToRobotState(work_state_, new_goal);
161  if (kinematic_constraint_set_->decide(work_state_, verbose).satisfied)
162  return true;
163  }
164  }
165  }
166  return false;
167 }
ompl_interface::ConstrainedGoalSampler::sampleUsingConstraintSampler
bool sampleUsingConstraintSampler(const ompl::base::GoalLazySamples *gls, ompl::base::State *new_goal)
Definition: constrained_goal_sampler.cpp:92
moveit::core::GroupStateValidityCallbackFn
boost::function< bool(RobotState *robot_state, const JointModelGroup *joint_group, const double *joint_group_variable_values)> GroupStateValidityCallbackFn
model_based_planning_context.h
ompl_interface::ModelBasedPlanningContext
Definition: model_based_planning_context.h:105
ompl_interface::ConstrainedGoalSampler::ConstrainedGoalSampler
ConstrainedGoalSampler(const ModelBasedPlanningContext *pc, kinematic_constraints::KinematicConstraintSetPtr ks, constraint_samplers::ConstraintSamplerPtr cs=constraint_samplers::ConstraintSamplerPtr())
Definition: constrained_goal_sampler.cpp:49
ompl_interface
The MoveIt interface to OMPL.
Definition: constrained_goal_sampler.h:46
moveit::core::RobotState
constrained_goal_sampler.h
moveit::core::RobotState::update
void update(bool force=false)
ROS_DEBUG_NAMED
#define ROS_DEBUG_NAMED(name,...)
ompl_interface::StateValidityChecker
An interface for a OMPL state validity checker.
Definition: state_validity_checker.h:80
ompl_interface::LOGNAME
constexpr char LOGNAME[]
Definition: constrained_goal_sampler.cpp:78
state_validity_checker.h
ompl_interface::ConstrainedGoalSampler::checkStateValidity
bool checkStateValidity(ompl::base::State *new_goal, const moveit::core::RobotState &state, bool verbose=false) const
Definition: constrained_goal_sampler.cpp:72
ompl_interface::ConstrainedGoalSampler::stateValidityCallback
bool stateValidityCallback(ompl::base::State *new_goal, moveit::core::RobotState const *state, const moveit::core::JointModelGroup *, const double *, bool verbose=false) const
Definition: constrained_goal_sampler.cpp:80
ROS_WARN_NAMED
#define ROS_WARN_NAMED(name,...)
moveit::core::JointModelGroup
profiler.h
moveit::core::RobotState::setJointGroupPositions
void setJointGroupPositions(const std::string &joint_group_name, const double *gstate)
ompl_interface::StateValidityChecker::isValid
bool isValid(const ompl::base::State *state) const override
Definition: state_validity_checker.h:85
verbose
bool verbose


ompl
Author(s): Ioan Sucan
autogenerated on Sat Apr 27 2024 02:26:21