Skip to content

Commit 336ad44

Browse files
authored
LimitSolutions wrapper stage (#710)
* Add python bindings for PassThrough * Add LimitSolutions stage
1 parent ed99e6b commit 336ad44

7 files changed

Lines changed: 204 additions & 4 deletions

File tree

core/include/moveit/task_constructor/stages.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@
4848
#include "stages/generate_place_pose.h"
4949
#include "stages/generate_pose.h"
5050
#include "stages/generate_random_pose.h"
51+
#include "stages/limit_solutions.h"
5152
#include "stages/modify_planning_scene.h"
5253
#include "stages/move_relative.h"
5354
#include "stages/move_to.h"
55+
#include "stages/passthrough.h"
5456
#include "stages/predicate_filter.h"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*********************************************************************
2+
* Software License Agreement (BSD License)
3+
*
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following
13+
* disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
* * Neither the name of the copyright holders nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*********************************************************************/
32+
/* Authors: Joseph Moore */
33+
34+
#pragma once
35+
36+
#include <moveit/task_constructor/container.h>
37+
38+
namespace moveit {
39+
namespace task_constructor {
40+
namespace stages {
41+
42+
/** A wrapper which lazily passes a limited number of solutions
43+
*
44+
* The best solution at each call to compute is passed on
45+
*
46+
*/
47+
class LimitSolutions : public WrapperBase
48+
{
49+
public:
50+
LimitSolutions(const std::string& name = "LimitSolutions", Stage::pointer&& child = Stage::pointer());
51+
52+
void reset() override;
53+
bool canCompute() const override;
54+
void compute() override;
55+
void onNewSolution(const SolutionBase& s) override;
56+
57+
void setMaxSolutions(uint32_t max_solutions) { setProperty("max_solutions", max_solutions); }
58+
59+
private:
60+
ordered<const SolutionBase*> upstream_solutions_;
61+
uint32_t forwarded_solutions;
62+
};
63+
} // namespace stages
64+
} // namespace task_constructor
65+
} // namespace moveit

core/python/bindings/src/stages.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ void export_stages(pybind11::module& m) {
171171
str: Default joint pose of the active group
172172
(defines cost of the inverse kinematics).
173173
)")
174-
.property<uint32_t>("max_ik_solutions", R"(
175-
int: Set the maximum number of inverse
176-
kinematic solutions thats should be generated.
177-
)")
178174
.property<uint32_t>("max_ik_solutions", "uint: max number of solutions to return")
179175
.property<bool>("ignore_collisions", R"(
180176
bool: Specify if collisions with other members of
@@ -548,6 +544,23 @@ void export_stages(pybind11::module& m) {
548544
.property<std::string>("grasp", "str: Name of the grasp pose")
549545
.def(py::init<Stage::pointer&&, const std::string&>(), "pose_generator"_a,
550546
"name"_a = std::string("ungrasp"));
547+
548+
properties::class_<PassThrough, Stage>(m, "PassThrough", R"(
549+
Wrapper which simply forwards all solutions of a child stage,
550+
allowing the wrapper to redefine costs, w/o loosing original costs.
551+
)")
552+
.def(py::init<const std::string&, Stage::pointer&&>(), "name"_a, "stage"_a);
553+
554+
properties::class_<LimitSolutions, Stage>(m, "LimitSolutions", R"(
555+
Wrapper for any stage to limit the total number of solutions returned.
556+
557+
The wrapper stores solutions of its child stage, and on each compute will
558+
pass on the lowest cost solution available, until the maximum number of solutions
559+
is reached.
560+
)")
561+
.property<uint32_t>("max_solutions", "uint: maximum number of solutions that should be passed on")
562+
.def(py::init<const std::string&, Stage::pointer&&>(), "name"_a, "stage"_a);
563+
551564
}
552565
} // namespace python
553566
} // namespace moveit

core/src/stages/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_library(${PROJECT_NAME}_stages
1313
${PROJECT_INCLUDE}/stages/passthrough.h
1414
${PROJECT_INCLUDE}/stages/noop.h
1515
${PROJECT_INCLUDE}/stages/predicate_filter.h
16+
${PROJECT_INCLUDE}/stages/limit_solutions.h
1617

1718
${PROJECT_INCLUDE}/stages/connect.h
1819
${PROJECT_INCLUDE}/stages/move_to.h
@@ -34,6 +35,7 @@ add_library(${PROJECT_NAME}_stages
3435
compute_ik.cpp
3536
passthrough.cpp
3637
predicate_filter.cpp
38+
limit_solutions.cpp
3739

3840
connect.cpp
3941
move_to.cpp
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*********************************************************************
2+
* Software License Agreement (BSD License)
3+
*
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following
13+
* disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
* * Neither the name of Bielefeld University nor the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*********************************************************************/
32+
33+
/* Authors: Joseph Moore */
34+
35+
#include <moveit/task_constructor/stages/limit_solutions.h>
36+
#include <moveit/task_constructor/storage.h>
37+
#include <moveit/task_constructor/cost_terms.h>
38+
#include <fmt/format.h>
39+
40+
namespace moveit {
41+
namespace task_constructor {
42+
namespace stages {
43+
44+
LimitSolutions::LimitSolutions(const std::string& name, Stage::pointer&& child) : WrapperBase(name, std::move(child)) {
45+
auto& p = properties();
46+
p.declare<uint32_t>("max_solutions", "maximum number of solutions returned by this wrapper");
47+
forwarded_solutions = 0;
48+
}
49+
50+
void LimitSolutions::reset() {
51+
upstream_solutions_.clear();
52+
forwarded_solutions = 0;
53+
WrapperBase::reset();
54+
}
55+
56+
void LimitSolutions::onNewSolution(const SolutionBase& s) {
57+
uint32_t max_solutions = properties().get<uint32_t>("max_solutions");
58+
if (forwarded_solutions + upstream_solutions_.size() < max_solutions)
59+
upstream_solutions_.push(&s);
60+
}
61+
62+
bool LimitSolutions::canCompute() const {
63+
return !upstream_solutions_.empty() || WrapperBase::canCompute();
64+
}
65+
66+
void LimitSolutions::compute() {
67+
if (WrapperBase::canCompute())
68+
WrapperBase::compute();
69+
70+
if (upstream_solutions_.empty())
71+
return;
72+
73+
++forwarded_solutions;
74+
liftSolution(*upstream_solutions_.pop());
75+
}
76+
} // namespace stages
77+
} // namespace task_constructor
78+
} // namespace moveit

core/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ if (CATKIN_ENABLE_TESTING)
3636

3737
mtc_add_gmock(test_mockups.cpp)
3838
mtc_add_gtest(test_stage.cpp)
39+
mtc_add_gtest(test_limit_solutions.cpp)
3940
mtc_add_gtest(test_container.cpp)
4041
mtc_add_gmock(test_serial.cpp)
4142
mtc_add_gmock(test_pruning.cpp)

core/test/test_limit_solutions.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <moveit/task_constructor/stages/limit_solutions.h>
2+
3+
#include "stage_mockups.h"
4+
5+
using namespace moveit::task_constructor;
6+
7+
using LimitSolutionsTest = TaskTestBase;
8+
9+
TEST_F(LimitSolutionsTest, returnsMaxSolutions) {
10+
auto g = std::make_unique<GeneratorMockup>(PredefinedCosts({ 2.0, 1.0, 3.0 }));
11+
auto ls = std::make_unique<stages::LimitSolutions>("LimitSolutions", std::move(g));
12+
ls->setMaxSolutions(2);
13+
t.add(std::move(ls));
14+
15+
EXPECT_TRUE(t.plan());
16+
EXPECT_EQ(t.solutions().size(), 2u);
17+
}
18+
19+
TEST_F(LimitSolutionsTest, returnsAllChildSolutionsIfFewerThanMax) {
20+
auto g = std::make_unique<GeneratorMockup>(PredefinedCosts({ 2.0, 1.0, 3.0 }));
21+
auto ls = std::make_unique<stages::LimitSolutions>("LimitSolutions", std::move(g));
22+
ls->setMaxSolutions(5);
23+
t.add(std::move(ls));
24+
25+
EXPECT_TRUE(t.plan());
26+
EXPECT_EQ(t.solutions().size(), 3u);
27+
}
28+
29+
TEST_F(LimitSolutionsTest, returnsBestSolutions) {
30+
// generator can create solutions with costs 1-4 in a single call to compute
31+
auto g = std::make_unique<GeneratorMockup>(PredefinedCosts({ 4.0, 2.0, 3.0, 1.0 }), 4);
32+
auto ls = std::make_unique<stages::LimitSolutions>("LimitSolutions", std::move(g));
33+
ls->setMaxSolutions(3); // however, only the first 3 solutions will be considered
34+
t.add(std::move(ls));
35+
36+
EXPECT_TRUE(t.plan());
37+
EXPECT_EQ(t.solutions().size(), 3u); // only 3 solutions should have been generated
38+
EXPECT_EQ(t.solutions().front()->cost(), 2.0); // solution with cost 1.0 was not considered anymore
39+
}

0 commit comments

Comments
 (0)