forked from moveit/moveit2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix penalty-based cost function in STOMP
This adds several test cases for STOMP's noise generation and cost functions, and provides the following fixes: * out-of-bounds vector access when tail states of trajectory are invalid * smoothed costs overriding values of previous invalid groups * missing validity check of last state in trajectory * inability to disable cost function interpolation steps * total cost of trajectory not summing up to sum of state penalties
- Loading branch information
1 parent
0e1e376
commit 85b2fd1
Showing
5 changed files
with
182 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
find_package(ament_cmake_gtest REQUIRED) | ||
ament_add_gtest(test_noise_generator test_noise_generator.cpp) | ||
ament_target_dependencies(test_noise_generator | ||
rsl | ||
tf2_eigen | ||
) | ||
|
||
ament_add_gtest(test_cost_functions test_cost_functions.cpp) | ||
ament_target_dependencies(test_cost_functions | ||
rsl | ||
moveit_core | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <gtest/gtest.h> | ||
#include <stomp_moveit/cost_functions.hpp> | ||
|
||
TEST(NoiseGeneratorTest, testGetCostFunctionAllValidStates) | ||
{ | ||
constexpr size_t TIMESTEPS = 100; | ||
constexpr size_t VARIABLES = 6; | ||
|
||
auto state_validator_fn = [](const Eigen::VectorXd& /* state_positions */) { return 0.0; }; | ||
|
||
auto cost_fn = | ||
stomp_moveit::costs::getCostFunctionFromStateValidator(state_validator_fn, 0.1 /* interpolation_step_size */); | ||
|
||
// Build trajectory with TIMESTEPS states, interpolating from 0.0 to 1.0 joint values | ||
Eigen::MatrixXd values = Eigen::MatrixXd::Zero(VARIABLES, TIMESTEPS); | ||
const int last_timestep = values.cols() - 1; | ||
for (int timestep = 0; timestep <= last_timestep; ++timestep) | ||
{ | ||
values.col(timestep).fill(1.0 * timestep / last_timestep); | ||
} | ||
|
||
Eigen::VectorXd costs(TIMESTEPS); | ||
bool validity; | ||
|
||
ASSERT_TRUE(cost_fn(values, costs, validity)); | ||
EXPECT_TRUE(validity); | ||
EXPECT_EQ(costs.sum(), 0.0); | ||
} | ||
|
||
TEST(NoiseGeneratorTest, testGetCostFunctionInvalidStates) | ||
{ | ||
constexpr size_t TIMESTEPS = 100; | ||
constexpr size_t VARIABLES = 6; | ||
constexpr double PENALTY = 1.0; | ||
|
||
// We are simulating a state validator by tagging specific timesteps as invalid. | ||
// The state validation function is called once per timestep since interpolation is disabled. | ||
// These assumptions are tested | ||
static const std::set<int> invalid_timesteps( | ||
{ 10, 11, 12, 13, 25, 26, 27, 46, 63, 64, 65, 66, 67, 68, 69, 97, 98, 99 }); | ||
size_t timestep_counter = 0; | ||
auto state_validator_fn = [&](const Eigen::VectorXd& /* state_positions */) { | ||
return PENALTY * invalid_timesteps.count(timestep_counter++); | ||
}; | ||
|
||
auto cost_fn = | ||
stomp_moveit::costs::getCostFunctionFromStateValidator(state_validator_fn, 0.0 /* interpolation disabled */); | ||
|
||
// Build trajectory with TIMESTEPS states, interpolating from 0.0 to 1.0 joint values | ||
Eigen::MatrixXd values = Eigen::MatrixXd::Zero(VARIABLES, TIMESTEPS); | ||
const int last_timestep = values.cols() - 1; | ||
for (int timestep = 0; timestep <= last_timestep; ++timestep) | ||
{ | ||
values.col(timestep).fill(1.0 * timestep / last_timestep); | ||
} | ||
|
||
// Test boundary assumptions about cost function outputs, costs and validity | ||
Eigen::VectorXd costs(TIMESTEPS); | ||
bool validity; | ||
ASSERT_TRUE(cost_fn(values, costs, validity)); | ||
EXPECT_EQ(timestep_counter, 100u); // 100 timesteps checked without interpolation | ||
EXPECT_FALSE(validity); // invalid states must result in an invalid trajectory | ||
EXPECT_LE(costs.maxCoeff(), PENALTY); // the highest cost must not be higher than the configured penalty | ||
EXPECT_GE(costs.minCoeff(), 0.0); // no negative cost values should be computed | ||
|
||
// The total cost must equal the sum of penalties produced by all invalid timesteps | ||
EXPECT_DOUBLE_EQ(costs.sum(), PENALTY * invalid_timesteps.size()); | ||
// Invalid timesteps should account for the majority of the total cost. | ||
// We expect that invalid windows cover at least 2*sigma (=68.1%) of each cost distribution. | ||
const std::vector<int> invalid_timesteps_vec(invalid_timesteps.begin(), invalid_timesteps.end()); | ||
EXPECT_GE(costs(invalid_timesteps_vec).sum(), 0.681 * PENALTY); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <gtest/gtest.h> | ||
#include <stomp_moveit/noise_generators.hpp> | ||
|
||
TEST(NoiseGeneratorTest, testStartEndUnchanged) | ||
{ | ||
constexpr size_t TIMESTEPS = 100; | ||
constexpr size_t VARIABLES = 6; | ||
static const std::vector<double> STDDEV(VARIABLES, 0.2); | ||
|
||
auto noise_gen = stomp_moveit::noise::getNormalDistributionGenerator(TIMESTEPS, STDDEV); | ||
|
||
const Eigen::MatrixXd VALUES = Eigen::MatrixXd::Constant(VARIABLES, TIMESTEPS, 1.0); | ||
const Eigen::MatrixXd NOISY_VALUES = Eigen::MatrixXd::Constant(VARIABLES, TIMESTEPS, 0.0); | ||
const Eigen::MatrixXd NOISE = Eigen::MatrixXd::Constant(VARIABLES, TIMESTEPS, 0.0); | ||
|
||
auto noise = NOISE; | ||
auto noisy_values = NOISY_VALUES; | ||
|
||
noise_gen(VALUES, noisy_values, noise); | ||
|
||
EXPECT_NE(noise, NOISE); | ||
EXPECT_NE(noisy_values, NOISY_VALUES); | ||
|
||
EXPECT_EQ(noise.size(), NOISE.size()); | ||
EXPECT_EQ(noisy_values.size(), NOISY_VALUES.size()); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
testing::InitGoogleTest(&argc, argv); | ||
return RUN_ALL_TESTS(); | ||
} |