PlannerTerminationCondition.cpp
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2011, Rice University
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 Rice University 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 
37 #include "ompl/base/PlannerTerminationCondition.h"
38 #include "ompl/util/Time.h"
39 #include <functional>
40 #include <thread>
41 #include <utility>
42 
43 namespace ompl
44 {
45  namespace base
46  {
47 
49  class PlannerTerminationCondition::PlannerTerminationConditionImpl
50  {
51  public:
52  PlannerTerminationConditionImpl(const PlannerTerminationConditionFn &fn, double period) :
53  fn_(fn),
54  period_(period),
55  terminate_(false),
56  thread_(nullptr),
57  evalValue_(false),
58  signalThreadStop_(false)
59  {
60  if (period_ > 0.0)
61  startEvalThread();
62  }
63 
64  ~PlannerTerminationConditionImpl()
65  {
66  stopEvalThread();
67  }
68 
69  bool eval() const
70  {
71  if (terminate_)
72  return true;
73  if (period_ > 0.0)
74  return evalValue_;
75  return fn_();
76  }
77 
78  void terminate() const
79  {
80  // it is ok to have unprotected write here
81  terminate_ = true;
82  }
83 
84  private:
85 
87  void startEvalThread()
88  {
89  if (!thread_)
90  {
91  signalThreadStop_ = false;
92  evalValue_ = false;
93  thread_ = new std::thread(std::bind(&PlannerTerminationConditionImpl::periodicEval, this));
94  }
95  }
96 
98  void stopEvalThread()
99  {
100  signalThreadStop_ = true;
101  if (thread_)
102  {
103  thread_->join();
104  delete thread_;
105  thread_ = nullptr;
106  }
107  }
108 
110  void periodicEval()
111  {
112  // we want to check for termination at least once every ms;
113  // even though we may evaluate the condition itself more rarely
114 
115  unsigned int count = 1;
116  time::duration s = time::seconds(period_);
117  if (period_ > 0.001)
118  {
119  count = 0.5 + period_ / 0.001;
120  s = time::seconds(period_ / (double) count);
121  }
122 
123  while (!terminate_ && !signalThreadStop_)
124  {
125  evalValue_ = fn_();
126  for (unsigned int i = 0 ; i < count ; ++i)
127  {
128  if (terminate_ || signalThreadStop_)
129  break;
130  std::this_thread::sleep_for(s);
131  }
132  }
133  }
134 
137 
139  double period_;
140 
142  mutable bool terminate_;
143 
145  std::thread *thread_;
146 
148  bool evalValue_;
149 
151  bool signalThreadStop_;
152  };
153 
155  }
156 }
157 
159 impl_(new PlannerTerminationConditionImpl(fn, -1.0))
160 {
161 }
162 
164 impl_(new PlannerTerminationConditionImpl(fn, period))
165 {
166 }
167 
169 {
170  impl_->terminate();
171 }
172 
174 {
175  return impl_->eval();
176 }
177 
179 {
180  return PlannerTerminationCondition([] { return false; });
181 }
182 
184 {
185  return PlannerTerminationCondition([] { return true; });
186 }
187 
189 namespace ompl
190 {
191  namespace base
192  {
193  static bool plannerOrTerminationConditionAux(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2)
194  {
195  return c1() || c2();
196  }
197 
198  static bool plannerAndTerminationConditionAux(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2)
199  {
200  return c1() && c2();
201  }
202 
203  // return true if a certain point in time has passed
204  static bool timePassed(const time::point &endTime)
205  {
206  return time::now() > endTime;
207  }
208  }
209 }
211 
213 {
214  return PlannerTerminationCondition(std::bind(&plannerOrTerminationConditionAux, c1, c2));
215 }
216 
218 {
219  return PlannerTerminationCondition(std::bind(&plannerAndTerminationConditionAux, c1, c2));
220 }
221 
223 {
225 }
226 
228 {
229  return PlannerTerminationCondition(std::bind(&timePassed, time::now() + duration));
230 }
231 
233 {
234  if (interval > duration)
235  interval = duration;
236  return PlannerTerminationCondition(std::bind(&timePassed, time::now() + time::seconds(duration)), interval);
237 }
238 
240 {
242 }
243 
244 namespace ompl
245 {
246  namespace base
247  {
249  : maxCalls_(numIterations),
250  timesCalled_(0u)
251  {
252  }
253 
255  {
256  ++timesCalled_;
257 
258  return (timesCalled_ > maxCalls_);
259  }
260 
262  {
263  timesCalled_ = 0u;
264  }
265 
266  IterationTerminationCondition::operator PlannerTerminationCondition()
267  {
269  }
270  }
271 }
void reset()
Reset the number of times the IterationTeriminationCondition has been called.
PlannerTerminationCondition plannerOrTerminationCondition(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2)
Combine two termination conditions into one. If either termination condition returns true...
PlannerTerminationCondition plannerNonTerminatingCondition()
Simple termination condition that always returns false. The termination condition will never be met...
A shared pointer wrapper for ompl::base::ProblemDefinition.
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
PlannerTerminationCondition plannerAndTerminationCondition(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2)
Combine two termination conditions into one. Both termination conditions need to return true for this...
std::function< bool()> PlannerTerminationConditionFn
Signature for functions that decide whether termination conditions have been met for a planner...
PlannerTerminationCondition plannerAlwaysTerminatingCondition()
Simple termination condition that always returns true. The termination condition will always be met...
duration seconds(double sec)
Return the time duration representing a given number of seconds.
Definition: Time.h:78
PlannerTerminationCondition timedPlannerTerminationCondition(double duration)
Return a termination condition that will become true duration seconds in the future (wall-time) ...
Main namespace. Contains everything in this library.
Definition: Cost.h:42
PlannerTerminationCondition exactSolnPlannerTerminationCondition(ompl::base::ProblemDefinitionPtr pdef)
Return a termination condition that will become true as soon as the problem definition has an exact s...
std::chrono::system_clock::duration duration
Representation of a time duration.
Definition: Time.h:69
PlannerTerminationCondition(const PlannerTerminationConditionFn &fn)
Construct a termination condition. By default, eval() will call the externally specified function fn ...
IterationTerminationCondition(unsigned int numIterations)
Construct a termination condition that can be evaluated numIterations times before returning true...
bool eval() const
The implementation of some termination condition. By default, this just calls fn_() ...
bool hasExactSolution() const
Returns true if an exact solution path has been found. Specifically returns hasSolution && !hasApprox...
point now()
Get the current time point.
Definition: Time.h:72
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition: Time.h:66
void terminate() const
Notify that the condition for termination should become true, regardless of what eval() returns...
bool eval()
Increment the number of times eval has been called and check if the planner should now terminate...