Skip to content

Commit 5956e70

Browse files
committed
Merge PR #311: fix Fallbacks
2 parents aef9916 + 4cc1f56 commit 5956e70

File tree

14 files changed

+642
-157
lines changed

14 files changed

+642
-157
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
DOCKER_RUN_OPTS: >-
3333
-e PRELOAD=libasan.so.5
3434
-e LSAN_OPTIONS="suppressions=$PWD/.github/workflows/lsan.suppressions"
35-
TARGET_CMAKE_ARGS: -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer -O1"
35+
TARGET_CMAKE_ARGS: -DCMAKE_CXX_FLAGS="-fsanitize=address -fno-omit-frame-pointer -O1 -g"
3636

3737
env:
3838
CATKIN_LINT: true

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
repos:
1616
# Standard hooks
1717
- repo: https://github.com/pre-commit/pre-commit-hooks
18-
rev: v3.4.0
18+
rev: v4.0.1
1919
hooks:
2020
- id: check-added-large-files
2121
- id: check-case-conflict
@@ -29,7 +29,7 @@ repos:
2929
- id: trailing-whitespace
3030

3131
- repo: https://github.com/psf/black
32-
rev: 20.8b1
32+
rev: 21.11b1
3333
hooks:
3434
- id: black
3535

core/include/moveit/task_constructor/container.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class Alternatives : public ParallelContainerBase
150150
void onNewSolution(const SolutionBase& s) override;
151151
};
152152

153+
class FallbacksPrivate;
153154
/** Plan for different alternatives in sequence.
154155
*
155156
* Try to find feasible solutions using first child. Only if this fails,
@@ -158,17 +159,23 @@ class Alternatives : public ParallelContainerBase
158159
*/
159160
class Fallbacks : public ParallelContainerBase
160161
{
161-
mutable Stage* active_child_ = nullptr;
162+
inline void replaceImpl();
162163

163164
public:
164-
Fallbacks(const std::string& name = "fallbacks") : ParallelContainerBase(name) {}
165+
PRIVATE_CLASS(Fallbacks);
166+
Fallbacks(const std::string& name = "fallbacks");
165167

166168
void reset() override;
167169
void init(const moveit::core::RobotModelConstPtr& robot_model) override;
168-
bool canCompute() const override;
169-
void compute() override;
170170

171+
protected:
172+
Fallbacks(FallbacksPrivate* impl);
171173
void onNewSolution(const SolutionBase& s) override;
174+
175+
private:
176+
// not needed, we directly use corresponding virtual methods of FallbacksPrivate
177+
bool canCompute() const final { return false; }
178+
void compute() final {}
172179
};
173180

174181
class MergerPrivate;

core/include/moveit/task_constructor/container_p.h

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ namespace task_constructor {
7676
class ContainerBasePrivate : public StagePrivate
7777
{
7878
friend class ContainerBase;
79-
friend class ConnectingPrivate; // needs to call protected setStatus()
80-
friend void swap(StagePrivate*& lhs, StagePrivate*& rhs);
79+
friend class ConnectingPrivate; // needed propagate setStatus() in newState()
8180

8281
public:
8382
using container_type = StagePrivate::container_type;
@@ -132,10 +131,11 @@ class ContainerBasePrivate : public StagePrivate
132131
inline const auto& externalToInternalMap() const { return internal_external_.by<EXTERNAL>(); }
133132

134133
/// called by a (direct) child when a solution failed
135-
void onNewFailure(const Stage& child, const InterfaceState* from, const InterfaceState* to);
134+
virtual void onNewFailure(const Stage& child, const InterfaceState* from, const InterfaceState* to);
136135

137136
protected:
138137
ContainerBasePrivate(ContainerBase* me, const std::string& name);
138+
ContainerBasePrivate& operator=(ContainerBasePrivate&& other);
139139

140140
// Set child's push interfaces: allow pushing if child requires it.
141141
inline void setChildsPushBackwardInterface(StagePrivate* child) {
@@ -154,10 +154,14 @@ class ContainerBasePrivate : public StagePrivate
154154
void setStatus(const Stage* creator, const InterfaceState* source, const InterfaceState* target,
155155
InterfaceState::Status status);
156156

157-
/// copy external_state to a child's interface and remember the link in internal_external map
157+
/// Copy external_state to a child's interface and remember the link in internal_external map
158158
template <Interface::Direction>
159159
void copyState(Interface::iterator external, const InterfacePtr& target, Interface::UpdateFlags updated);
160-
/// lift solution from internal to external level
160+
// non-template version
161+
void copyState(Interface::Direction dir, Interface::iterator external, const InterfacePtr& target,
162+
Interface::UpdateFlags updated);
163+
164+
/// Lift solution from internal to external level
161165
void liftSolution(const SolutionBasePtr& solution, const InterfaceState* internal_from,
162166
const InterfaceState* internal_to);
163167

@@ -230,12 +234,91 @@ class ParallelContainerBasePrivate : public ContainerBasePrivate
230234
void validateInterfaces(const StagePrivate& child, InterfaceFlags& external, bool first = false) const;
231235

232236
private:
233-
/// notify callback for new externally received states
237+
/// notify callback for new externally received interface states
234238
template <typename Interface::Direction>
235-
void onNewExternalState(Interface::iterator external, Interface::UpdateFlags updated);
239+
void propagateStateToAllChildren(Interface::iterator external, Interface::UpdateFlags updated);
240+
241+
// override to customize behavior on received interface states (default: propagateStateToAllChildren())
242+
virtual void initializeExternalInterfaces();
236243
};
237244
PIMPL_FUNCTIONS(ParallelContainerBase)
238245

246+
/* The Fallbacks container needs to implement different behaviour based on its interface.
247+
* Thus, we implement 3 different classes: for Generator, Propagator, and Connect-like interfaces.
248+
* FallbacksPrivate is the common base class for all of them, defining the common API
249+
* to be used by the Fallbacks container.
250+
* The actual interface-specific class is instantiated in initializeExternalInterfaces()
251+
* resp. Fallbacks::replaceImpl() when the actual interface is known.
252+
* The key difference between the 3 variants is how they advance to the next job. */
253+
class FallbacksPrivate : public ParallelContainerBasePrivate
254+
{
255+
public:
256+
FallbacksPrivate(Fallbacks* me, const std::string& name);
257+
FallbacksPrivate(FallbacksPrivate&& other);
258+
259+
void initializeExternalInterfaces() final;
260+
void onNewFailure(const Stage& child, const InterfaceState* from, const InterfaceState* to) override;
261+
262+
// virtual methods specific to each variant
263+
virtual void onNewSolution(const SolutionBase& s);
264+
virtual void reset() {}
265+
};
266+
PIMPL_FUNCTIONS(Fallbacks)
267+
268+
/* Class shared between FallbacksPrivateGenerator and FallbacksPrivatePropagator,
269+
which both have the notion of a currently active child stage */
270+
class FallbacksPrivateCommon : public FallbacksPrivate
271+
{
272+
public:
273+
FallbacksPrivateCommon(FallbacksPrivate&& other) : FallbacksPrivate(std::move(other)) {}
274+
275+
/// Advance to next child
276+
inline void nextChild();
277+
/// Advance to the next job, assuming that the current child is exhausted on the current job.
278+
virtual bool nextJob() = 0;
279+
280+
void reset() override;
281+
bool canCompute() const override;
282+
void compute() override;
283+
284+
container_type::const_iterator current_; // currently active child
285+
};
286+
287+
/// Fallbacks implementation for GENERATOR interface
288+
struct FallbacksPrivateGenerator : FallbacksPrivateCommon
289+
{
290+
FallbacksPrivateGenerator(FallbacksPrivate&& old);
291+
bool nextJob() override;
292+
};
293+
294+
/// Fallbacks implementation for FORWARD or BACKWARD interface
295+
struct FallbacksPrivatePropagator : FallbacksPrivateCommon
296+
{
297+
FallbacksPrivatePropagator(FallbacksPrivate&& old);
298+
void reset() override;
299+
void onNewSolution(const SolutionBase& s) override;
300+
bool nextJob() override;
301+
302+
Interface::Direction dir_; // propagation direction
303+
Interface::iterator job_; // pointer to currently processed external state
304+
bool job_has_solutions_; // flag indicating whether the current job generated solutions
305+
};
306+
307+
/// Fallbacks implementation for CONNECT interface
308+
struct FallbacksPrivateConnect : FallbacksPrivate
309+
{
310+
FallbacksPrivateConnect(FallbacksPrivate&& old);
311+
void reset() override;
312+
bool canCompute() const override;
313+
void compute() override;
314+
void onNewFailure(const Stage& child, const InterfaceState* from, const InterfaceState* to) override;
315+
316+
template <Interface::Direction dir>
317+
void propagateStateUpdate(Interface::iterator external, Interface::UpdateFlags updated);
318+
319+
mutable container_type::const_iterator active_; // child picked for compute()
320+
};
321+
239322
class WrapperBasePrivate : public ParallelContainerBasePrivate
240323
{
241324
friend class WrapperBase;

core/include/moveit/task_constructor/stage_p.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class StagePrivate
6161
{
6262
friend class Stage;
6363
friend std::ostream& operator<<(std::ostream& os, const StagePrivate& stage);
64-
friend void swap(StagePrivate*& lhs, StagePrivate*& rhs);
6564

6665
public:
6766
/// container type used to store children
@@ -103,6 +102,8 @@ class StagePrivate
103102
/// direction-based access to pull interface
104103
template <Interface::Direction dir>
105104
inline InterfacePtr pullInterface();
105+
// non-template version
106+
inline InterfacePtr pullInterface(Interface::Direction dir) { return dir == Interface::FORWARD ? starts_ : ends_; }
106107

107108
/// set parent of stage
108109
/// enforce only one parent exists
@@ -157,6 +158,8 @@ class StagePrivate
157158
void computeCost(const InterfaceState& from, const InterfaceState& to, SolutionBase& solution);
158159

159160
protected:
161+
StagePrivate& operator=(StagePrivate&& other);
162+
160163
// associated/owning Stage instance
161164
Stage* me_;
162165

@@ -301,6 +304,7 @@ PIMPL_FUNCTIONS(MonitoringGenerator)
301304
class ConnectingPrivate : public ComputeBasePrivate
302305
{
303306
friend class Connecting;
307+
friend struct FallbacksPrivateConnect;
304308

305309
public:
306310
struct StatePair : std::pair<Interface::const_iterator, Interface::const_iterator>

core/include/moveit/task_constructor/storage.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class Interface : public ordered<InterfaceState*>
178178
class iterator : public base_type::iterator
179179
{
180180
public:
181+
iterator() = default;
181182
iterator(base_type::iterator other) : base_type::iterator(other) {}
182183

183184
InterfaceState& operator*() const noexcept { return *base_type::iterator::operator*(); }
@@ -247,6 +248,16 @@ class Interface : public ordered<InterfaceState*>
247248
std::ostream& operator<<(std::ostream& os, const InterfaceState::Priority& prio);
248249
std::ostream& operator<<(std::ostream& os, const Interface& interface);
249250

251+
/// Find index of the iterator in the container. Counting starts at 1. Zero corresponds to not found.
252+
template <typename T>
253+
size_t getIndex(const T& container, typename T::const_iterator search) {
254+
size_t index = 1;
255+
for (typename T::const_iterator it = container.begin(), end = container.end(); it != end; ++it, ++index)
256+
if (it == search)
257+
return index;
258+
return 0;
259+
}
260+
250261
class CostTerm;
251262
class StagePrivate;
252263
class ContainerBasePrivate;

core/include/moveit/task_constructor/task_p.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ namespace task_constructor {
5151
class TaskPrivate : public WrapperBasePrivate
5252
{
5353
friend class Task;
54+
TaskPrivate& operator=(TaskPrivate&& other);
5455

5556
public:
5657
TaskPrivate(Task* me, const std::string& ns);
58+
5759
const std::string& ns() const { return ns_; }
5860
const ContainerBase* stages() const;
5961

60-
protected:
61-
static void swap(StagePrivate*& lhs, StagePrivate*& rhs);
62-
6362
private:
6463
std::string ns_;
6564
robot_model_loader::RobotModelLoaderPtr robot_model_loader_;

0 commit comments

Comments
 (0)