27 #include <Eigen/Dense> 28 #include <gtest/gtest.h> 37 const std::vector<double>
START_POS = {1.4, 1.4, 0.5};
38 const std::vector<double>
END_POS = {-1.25, 1.0, -0.26};
40 const std::vector<double>
STD_DEV = {1.0, 1.0, 1.0};
56 const std::vector<double>& bias_thresholds,
57 const std::vector<double>& std_dev):
58 parameters_bias_(parameters_bias),
59 bias_thresholds_(bias_thresholds),
64 int num_timesteps = parameters_bias.cols();
65 generateSmoothingMatrix(num_timesteps,1.0,smoothing_M_);
72 std::size_t start_timestep,
73 std::size_t num_timesteps,
76 Eigen::MatrixXd& parameters_noise,
77 Eigen::MatrixXd& noise)
override 80 for(std::size_t
d = 0;
d < parameters.rows();
d++)
82 for(std::size_t t = 0; t < parameters.cols(); t++)
84 rand_noise =
static_cast<double>(rand()%RAND_MAX)/static_cast<double>(RAND_MAX - 1);
85 rand_noise = 2*(0.5 - rand_noise);
86 noise(
d,t) = rand_noise*std_dev_[
d];
90 parameters_noise = parameters + noise;
96 std::size_t start_timestep,
97 std::size_t num_timesteps,
99 Eigen::VectorXd& costs,
100 bool& validity)
override 102 return computeNoisyCosts(parameters,start_timestep,num_timesteps,iteration_number,-1,costs,validity);
106 std::size_t start_timestep,
107 std::size_t num_timesteps,
108 int iteration_number,
110 Eigen::VectorXd& costs,
111 bool& validity)
override 113 costs.setZero(num_timesteps);
118 for(std::size_t t = 0u; t < num_timesteps; t++)
121 for(std::size_t
d = 0u;
d < parameters.rows() ;
d++)
124 diff = std::abs(parameters(
d,t) - parameters_bias_(
d,t));
125 if( diff > std::abs(bias_thresholds_[
d]))
140 std::size_t num_timesteps,
141 int iteration_number,
142 const Eigen::MatrixXd& parameters,
143 Eigen::MatrixXd& updates)
override 145 return smoothParameterUpdates(start_timestep,num_timesteps,iteration_number,updates);
160 std::size_t num_timesteps,
161 int iteration_number,
162 Eigen::MatrixXd& updates)
165 for(
auto d = 0u;
d < updates.rows();
d++)
167 updates.row(
d).transpose() = smoothing_M_*(updates.row(
d).transpose());
189 const std::vector<double>& thresholds)
191 auto num_dimensions = optimized.rows();
192 Trajectory diff = Trajectory::Zero(num_dimensions,optimized.cols());
193 for(
auto d = 0u;
d < num_dimensions ;
d++)
195 diff.row(
d) = optimized.row(
d)- desired.row(
d);
196 diff.row(
d).cwiseAbs();
197 if((diff.row(
d).array() > thresholds[
d] ).any() )
233 void interpolate(
const std::vector<double>& start,
const std::vector<double>& end,
236 auto dimensions = start.size();
237 traj = Eigen::MatrixXd::Zero(dimensions,num_timesteps);
238 for(
auto d = 0u;
d < dimensions;
d++)
240 double delta = (end[
d] - start[
d])/(num_timesteps - 1);
241 for(
auto t = 0u; t < num_timesteps; t++)
243 traj(
d,t) = start[
d] + t*delta;
266 Stomp stomp(config,task);
277 diff = trajectory_bias - optimized;
279 std::string line_separator =
"\n------------------------------------------------------\n";
280 std::cout<<line_separator;
281 std::cout<<stomp_core::toString(trajectory_bias);
282 std::cout<<line_separator;
283 std::cout<<toString(optimized)<<
"\n";
284 std::cout<<
"Differences"<<
"\n"<<toString(diff)<<line_separator;
288 TEST(Stomp3DOF,solve_interpolated_initial)
296 Stomp stomp(config,task);
299 stomp.
solve(trajectory_bias,optimized);
307 diff = trajectory_bias - optimized;
309 std::string line_separator =
"\n------------------------------------------------------\n";
310 std::cout<<line_separator;
311 std::cout<<stomp_core::toString(trajectory_bias);
312 std::cout<<line_separator;
313 std::cout<<toString(optimized)<<
"\n";
314 std::cout<<
"Differences"<<
"\n"<<toString(diff)<<line_separator;
318 TEST(Stomp3DOF,solve_cubic_polynomial_initial)
326 Stomp stomp(config,task);
329 stomp.
solve(trajectory_bias,optimized);
337 diff = trajectory_bias - optimized;
339 std::string line_separator =
"\n------------------------------------------------------\n";
340 std::cout<<line_separator;
341 std::cout<<stomp_core::toString(trajectory_bias);
342 std::cout<<line_separator;
343 std::cout<<toString(optimized)<<
"\n";
344 std::cout<<
"Differences"<<
"\n"<<toString(diff)<<line_separator;
348 TEST(Stomp3DOF,solve_min_control_cost_initial)
356 Stomp stomp(config,task);
359 stomp.
solve(trajectory_bias,optimized);
367 diff = trajectory_bias - optimized;
369 std::string line_separator =
"\n------------------------------------------------------\n";
370 std::cout<<line_separator;
371 std::cout<<stomp_core::toString(trajectory_bias);
372 std::cout<<line_separator;
373 std::cout<<toString(optimized)<<
"\n";
374 std::cout<<
"Differences"<<
"\n"<<toString(diff)<<line_separator;
378 TEST(Stomp3DOF,solve_40_timesteps)
381 int num_timesteps = 40;
389 Stomp stomp(config,task);
395 EXPECT_EQ(optimized.cols(),num_timesteps);
400 diff = trajectory_bias - optimized;
402 std::string line_separator =
"\n------------------------------------------------------\n";
403 std::cout<<line_separator;
404 std::cout<<stomp_core::toString(trajectory_bias);
405 std::cout<<line_separator;
406 std::cout<<toString(optimized)<<
"\n";
407 std::cout<<
"Differences"<<
"\n"<<toString(diff)<<line_separator;
411 TEST(Stomp3DOF,solve_60_timesteps)
414 int num_timesteps = 60;
422 Stomp stomp(config,task);
428 EXPECT_EQ(optimized.cols(),num_timesteps);
433 diff = trajectory_bias - optimized;
435 std::string line_separator =
"\n------------------------------------------------------\n";
436 std::cout<<line_separator;
437 std::cout<<stomp_core::toString(trajectory_bias);
438 std::cout<<line_separator;
439 std::cout<<toString(optimized)<<
"\n";
440 std::cout<<
"Differences"<<
"\n"<<toString(diff)<<line_separator;
bool filterParameterUpdates(std::size_t start_timestep, std::size_t num_timesteps, int iteration_number, const Eigen::MatrixXd ¶meters, Eigen::MatrixXd &updates) override
Filters the given parameters which is applied after the update. It could be used for clipping of join...
std::shared_ptr< Task > TaskPtr
const std::vector< double > STD_DEV
int num_rollouts
Number of noisy trajectories.
const std::vector< double > END_POS
TEST(Stomp3DOF, construction)
This tests the Stomp constructor.
void interpolate(const std::vector< double > &start, const std::vector< double > &end, std::size_t num_timesteps, Trajectory &traj)
Compute a linear interpolated trajectory given a start and end state.
bool smoothParameterUpdates(std::size_t start_timestep, std::size_t num_timesteps, int iteration_number, Eigen::MatrixXd &updates)
Perform a smooth update given a noisy update.
const std::vector< double > START_POS
bool compareDiff(const Trajectory &optimized, const Trajectory &desired, const std::vector< double > &thresholds)
Compares whether two trajectories are close to each other within a threshold.
DummyTask(const Trajectory ¶meters_bias, const std::vector< double > &bias_thresholds, const std::vector< double > &std_dev)
A dummy task for testing Stomp.
Trajectory parameters_bias_
This contains the stomp core algorithm.
int num_iterations
Maximum number of iteration allowed.
double control_cost_weight
Percentage of the trajectory accelerations cost to be applied in the total cost calculation > ...
bool computeNoisyCosts(const Trajectory ¶meters, std::size_t start_timestep, std::size_t num_timesteps, int iteration_number, int rollout_number, Eigen::VectorXd &costs, bool &validity) override
computes the state costs as a function of the noisy parameters for each time step.
int num_dimensions
Parameter dimensionality.
std::vector< double > std_dev_
std::vector< double > bias_thresholds_
A dummy task for testing STOMP.
The data structure used to store STOMP configuration parameters.
bool solve(const std::vector< double > &first, const std::vector< double > &last, Eigen::MatrixXd ¶meters_optimized)
Find the optimal solution provided a start and end goal.
const std::size_t NUM_DIMENSIONS
bool computeCosts(const Trajectory ¶meters, std::size_t start_timestep, std::size_t num_timesteps, int iteration_number, Eigen::VectorXd &costs, bool &validity) override
computes the state costs as a function of the optimized parameters for each time step.
Eigen::MatrixXd smoothing_M_
This defines the stomp task.
int initialization_method
TrajectoryInitializations::TrajectoryInitialization.
StompConfiguration create3DOFConfiguration()
Create the STOMP configuration for the 3 DOF problem.
Eigen::MatrixXd Trajectory
int max_rollouts
The combined number of new and old rollouts during each iteration shouldn't exceed this value...
const std::vector< double > BIAS_THRESHOLD
const std::size_t NUM_TIMESTEPS
Defines the STOMP improvement policy.
double delta_t
Time change between consecutive points.
bool generateNoisyParameters(const Eigen::MatrixXd ¶meters, std::size_t start_timestep, std::size_t num_timesteps, int iteration_number, int rollout_number, Eigen::MatrixXd ¶meters_noise, Eigen::MatrixXd &noise) override
See base clase for documentation.
int num_iterations_after_valid
Stomp will stop optimizing this many iterations after finding a valid solution.
int num_timesteps
Number of timesteps.