Loading...
Searching...
No Matches
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 <atomic>
40#include <thread>
41#include <utility>
42
43namespace ompl
44{
45 namespace base
46 {
48 class PlannerTerminationCondition::PlannerTerminationConditionImpl
49 {
50 public:
51 PlannerTerminationConditionImpl(PlannerTerminationConditionFn fn, double period)
52 : fn_(std::move(fn))
53 , period_(period)
54 , terminate_(false)
55 , thread_(nullptr)
56 , evalValue_(false)
57 , signalThreadStop_(false)
58 {
59 if (period_ > 0.0)
60 startEvalThread();
61 }
62
63 ~PlannerTerminationConditionImpl()
64 {
65 stopEvalThread();
66 }
67
68 bool eval() const
69 {
70 if (terminate_)
71 return true;
72 if (period_ > 0.0)
73 return evalValue_;
74 return fn_();
75 }
76
77 void terminate() const
78 {
79 // it is ok to have unprotected write here
80 terminate_ = true;
81 }
82
83 private:
85 void startEvalThread()
86 {
87 if (thread_ == nullptr)
88 {
89 signalThreadStop_ = false;
90 evalValue_ = false;
91 thread_ = new std::thread([this] { periodicEval(); });
92 }
93 }
94
96 void stopEvalThread()
97 {
98 signalThreadStop_ = true;
99 if (thread_ != nullptr)
100 {
101 thread_->join();
102 delete thread_;
103 thread_ = nullptr;
104 }
105 }
106
108 void periodicEval()
109 {
110 // we want to check for termination at least once every ms;
111 // even though we may evaluate the condition itself more rarely
112
113 unsigned int count = 1;
114 time::duration s = time::seconds(period_);
115 if (period_ > 0.001)
116 {
117 count = 0.5 + period_ / 0.001;
118 s = time::seconds(period_ / (double)count);
119 }
120
121 while (!terminate_ && !signalThreadStop_)
122 {
123 evalValue_ = fn_();
124 for (unsigned int i = 0; i < count; ++i)
125 {
126 if (terminate_ || signalThreadStop_)
127 break;
128 std::this_thread::sleep_for(s);
129 }
130 }
131 }
132
136
138 double period_;
139
142 mutable bool terminate_;
143
145 std::thread *thread_;
146
148 std::atomic<bool> evalValue_;
149
151 std::atomic<bool> signalThreadStop_;
152 };
153
155 } // namespace base
156} // namespace ompl
157
159 : impl_(std::make_shared<PlannerTerminationConditionImpl>(fn, -1.0))
160{
161}
162
164 double period)
165 : impl_(std::make_shared<PlannerTerminationConditionImpl>(fn, period))
166{
167}
168
170{
171 impl_->terminate();
172}
173
175{
176 return impl_->eval();
177}
178
183
188
194
200
205
207{
208 const time::point endTime(time::now() + duration);
209 return PlannerTerminationCondition([endTime] { return time::now() > endTime; });
210}
211
213{
214 if (interval > duration)
215 interval = duration;
216 const time::point endTime(time::now() + time::seconds(duration));
217 return PlannerTerminationCondition([endTime] { return time::now() > endTime; }, interval);
218}
219
221ompl::base::exactSolnPlannerTerminationCondition(const ompl::base::ProblemDefinitionPtr &pdef)
222{
223 return PlannerTerminationCondition([pdef] { return pdef->hasExactSolution(); });
224}
Encapsulate a termination condition for a motion planner. Planners will call operator() to decide whe...
PlannerTerminationCondition(const PlannerTerminationConditionFn &fn)
Construct a termination condition. By default, eval() will call the externally specified function fn ...
bool eval() const
The implementation of some termination condition. By default, this just calls fn_().
void terminate() const
Notify that the condition for termination should become true, regardless of what eval() returns....
This namespace contains sampling based planning routines shared by both planning under geometric cons...
PlannerTerminationCondition plannerAlwaysTerminatingCondition()
Simple termination condition that always returns true. The termination condition will always be met.
PlannerTerminationCondition plannerAndTerminationCondition(const PlannerTerminationCondition &c1, const PlannerTerminationCondition &c2)
Combine two termination conditions into one. Both termination conditions need to return true for this...
PlannerTerminationCondition exactSolnPlannerTerminationCondition(const ompl::base::ProblemDefinitionPtr &pdef)
Return a termination condition that will become true as soon as the problem definition has an exact s...
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.
std::function< bool()> PlannerTerminationConditionFn
Signature for functions that decide whether termination conditions have been met for a planner,...
PlannerTerminationCondition timedPlannerTerminationCondition(double duration)
Return a termination condition that will become true duration seconds in the future (wall-time).
std::chrono::system_clock::time_point point
Representation of a point in time.
Definition Time.h:52
std::chrono::system_clock::duration duration
Representation of a time duration.
Definition Time.h:55
point now()
Get the current time point.
Definition Time.h:58
duration seconds(double sec)
Return the time duration representing a given number of seconds.
Definition Time.h:64
Main namespace. Contains everything in this library.
STL namespace.