libmove3d-planners
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Groups Pages
covariant_trajectory_policy.hpp
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
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 Willow Garage 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 
37 #ifndef COVARIANT_TRAJECTORY_POLICY_H_
38 #define COVARIANT_TRAJECTORY_POLICY_H_
39 
40 #include <Eigen/Core>
41 #include "planner/TrajectoryOptim/Chomp/chompUtils.hpp"
42 #include "policy.hpp"
43 
44 namespace stomp_motion_planner
45 {
47 {
48 public:
50  virtual ~CovariantTrajectoryPolicy();
51 
52 
53  // Functions that are specific to CovariantTrajectoryPolicy:
54  bool initialize(/*ros::NodeHandle& node_handle*/);
55  bool initialize(/*ros::NodeHandle& node_handle,*/
56  const int num_time_steps,
57  const int num_dimensions,
58  const double movement_duration,
59  const double cost_ridge_factor,
60  const std::vector<double>& derivative_costs);
61 
62  bool setToMinControlCost(Eigen::VectorXd& start, Eigen::VectorXd& goal);
63  bool getParametersAll(std::vector<Eigen::VectorXd>& parameters);
64  void setFileNameBase(const std::string& file_name_base);
65  void setPrintDebug(bool print_debug) { print_debug_ = print_debug; }
66 
67  // Functions inherited from Policy:
68 
74  bool setNumTimeSteps(const int num_time_steps);
75 
81  bool getNumTimeSteps(int& num_time_steps);
82 
88  bool getNumDimensions(int& num_dimensions);
89 
96  bool getNumParameters(std::vector<int>& num_params);
97 
103  bool getBasisFunctions(std::vector<Eigen::MatrixXd>& basis_functions);
104 
112  bool getControlCosts(std::vector<Eigen::MatrixXd>& control_costs);
113 
119  bool updateParameters(const std::vector<Eigen::MatrixXd>& updates);
120 
124  void printParameters();
125 
131  bool getParameters(std::vector<Eigen::VectorXd>& parameters);
132 
138  bool setParameters(const std::vector<Eigen::VectorXd>& parameters);
139 
148  bool computeControlCosts(const std::vector<Eigen::MatrixXd>& control_cost_matrices, const std::vector<std::vector<Eigen::VectorXd> >& parameters,
149  const double weight, std::vector<Eigen::VectorXd>& control_costs);
150 
151  bool computeControlCosts(const std::vector<Eigen::MatrixXd>& control_cost_matrices, const std::vector<Eigen::VectorXd>& parameters,
152  const std::vector<Eigen::VectorXd>& noise, const double weight, std::vector<Eigen::VectorXd>& control_costs);
153 
154  // Functions inherited from LibraryItem:
155 
156  std::string getInfoString();
157  std::string getClassName();
158  bool readFromDisc(const std::string directory_name, const int item_id, const int trial_id = 0);
159  bool writeToDisc(const int trial_id = 0);
160  bool readFromDisc(const std::string abs_file_name);
161  bool writeToDisc(const std::string abs_file_name);
162  std::string getFileName(const int trial_id);
163 
164  double movement_dt_;
165 
166 private:
167 // ros::NodeHandle node_handle_;
168 
169  std::string file_name_base_;
170  bool print_debug_;
171 
172  int num_time_steps_;
173  int num_vars_free_;
174  int num_vars_all_;
175  int free_vars_start_index_;
176  int free_vars_end_index_;
177  int num_dimensions_;
178  double movement_duration_;
179  double cost_ridge_factor_;
180  std::vector<double> derivative_costs_;
181 
182  std::vector<int> num_parameters_;
183  std::vector<Eigen::MatrixXd> basis_functions_;
184  std::vector<Eigen::MatrixXd> control_costs_;
185  std::vector<Eigen::MatrixXd> inv_control_costs_;
186  std::vector<Eigen::MatrixXd> control_costs_all_;
187 
188  std::vector<Eigen::VectorXd> linear_control_costs_;
189 
190  std::vector<Eigen::VectorXd> parameters_all_;
191 
192  std::vector<Eigen::MatrixXd> differentiation_matrices_;
193  void createDifferentiationMatrices();
194  bool readParameters();
195  bool initializeVariables();
196  bool initializeCosts();
197  bool initializeBasisFunctions();
198 
199  bool computeLinearControlCosts();
200  bool computeMinControlCostParameters();
201 };
202 
203 // inline functions follow
204 
205 inline bool CovariantTrajectoryPolicy::getParameters(std::vector<Eigen::VectorXd>& parameters)
206 {
207  if (int(parameters.size()) != num_dimensions_)
208  {
209  parameters.resize(num_dimensions_, Eigen::VectorXd::Zero(num_time_steps_));
210  }
211  for (int d=0; d<num_dimensions_; ++d)
212  {
213  parameters[d] = parameters_all_[d].segment(free_vars_start_index_, num_vars_free_);
214  }
215  return true;
216 }
217 
219 {
220  Eigen::MatrixXd parameters( num_time_steps_, num_dimensions_);
221 
222  for (int d=0; d<num_dimensions_; ++d)
223  {
224  parameters.col(d) = parameters_all_[d].segment(free_vars_start_index_, num_vars_free_);
225  }
226 
227  std::cout << parameters << std::endl;
228 }
229 
230 inline bool CovariantTrajectoryPolicy::getParametersAll(std::vector<Eigen::VectorXd>& parameters)
231 {
232  parameters = parameters_all_;
233  return true;
234 }
235 
236 inline bool CovariantTrajectoryPolicy::setParameters(const std::vector<Eigen::VectorXd>& parameters)
237 {
238  //ROS_ASSERT(int(parameters.size()) == num_dimensions_);
239  for (int d=0; d<num_dimensions_; ++d)
240  {
241  parameters_all_[d].segment(free_vars_start_index_, num_vars_free_) = parameters[d];
242  }
243  //std::cout << "CovariantTrajectoryPolicy::setParameters => num_vars_free_ : " << num_vars_free_ << std::endl;
244  return true;
245 }
246 
247 inline bool CovariantTrajectoryPolicy::getBasisFunctions(std::vector<Eigen::MatrixXd>& basis_functions)
248 {
249  basis_functions = basis_functions_;
250  return true;
251 }
252 
253 inline bool CovariantTrajectoryPolicy::getControlCosts(std::vector<Eigen::MatrixXd>& control_costs)
254 {
255  control_costs = control_costs_;
256  return true;
257 }
258 
259 inline bool CovariantTrajectoryPolicy::getNumTimeSteps(int& num_time_steps)
260 {
261  num_time_steps = num_time_steps_;
262  return true;
263 }
264 
265 inline bool CovariantTrajectoryPolicy::getNumDimensions(int& num_dimensions)
266 {
267  num_dimensions = num_dimensions_;
268  return true;
269 }
270 
271 inline bool CovariantTrajectoryPolicy::getNumParameters(std::vector<int>& num_params)
272 {
273  num_params = num_parameters_;
274  return true;
275 }
276 
277 inline bool CovariantTrajectoryPolicy::setNumTimeSteps(const int num_time_steps)
278 {
279  //ROS_ASSERT_MSG(num_time_steps_ == num_time_steps, "%d != %d", num_time_steps_, num_time_steps);
280  return true;
281 }
282 
283 inline std::string CovariantTrajectoryPolicy::getInfoString()
284 {
285  return "";
286 }
287 
288 inline std::string CovariantTrajectoryPolicy::getClassName()
289 {
290  return "CovariantTrajectoryPolicy";
291 }
292 
293 inline void CovariantTrajectoryPolicy::setFileNameBase(const std::string& file_name_base)
294 {
295  file_name_base_ = file_name_base;
296 }
297 
298 
299 }
300 
301 #endif /* COVARIANT_TRAJECTORY_POLICY_H_ */
bool setParameters(const std::vector< Eigen::VectorXd > &parameters)
Set the policy parameters per dimension.
Definition: covariant_trajectory_policy.hpp:236
bool getNumDimensions(int &num_dimensions)
Gets the number of dimensions.
Definition: covariant_trajectory_policy.hpp:265
Definition: policy.hpp:47
void printParameters()
print paramters in standard output
Definition: covariant_trajectory_policy.hpp:218
bool updateParameters(const std::vector< Eigen::MatrixXd > &updates)
Update the policy parameters based on the updates per timestep.
Definition: covariant_trajectory_policy.cpp:395
bool getBasisFunctions(std::vector< Eigen::MatrixXd > &basis_functions)
Gets the basis functions that multiply the policy parameters in the dynamical system.
Definition: covariant_trajectory_policy.hpp:247
bool getNumTimeSteps(int &num_time_steps)
Gets the number of time steps used in reinforcement learning.
Definition: covariant_trajectory_policy.hpp:259
bool getControlCosts(std::vector< Eigen::MatrixXd > &control_costs)
Gets the positive semi-definite matrix of the quadratic control cost The weight of this control cost ...
Definition: covariant_trajectory_policy.hpp:253
bool setToMinControlCost(Eigen::VectorXd &start, Eigen::VectorXd &goal)
compute the minmal control costs given a start and goal state
Definition: covariant_trajectory_policy.cpp:147
bool getParameters(std::vector< Eigen::VectorXd > &parameters)
Get the policy parameters per dimension.
Definition: covariant_trajectory_policy.hpp:205
bool setNumTimeSteps(const int num_time_steps)
Sets the number of time steps used in reinforcement learning.
Definition: covariant_trajectory_policy.hpp:277
bool getNumParameters(std::vector< int > &num_params)
Gets the number of policy parameters per dimension.
Definition: covariant_trajectory_policy.hpp:271
Definition: covariant_trajectory_policy.hpp:46
bool computeControlCosts(const std::vector< Eigen::MatrixXd > &control_cost_matrices, const std::vector< std::vector< Eigen::VectorXd > > &parameters, const double weight, std::vector< Eigen::VectorXd > &control_costs)
Compute the control costs over time, given the control cost matrix per dimension and parameters over ...
Definition: covariant_trajectory_policy.cpp:347