utils.cpp
1 
26 #include <stomp_core/utils.h>
27 #include <cmath>
28 #include <iostream>
29 #include <Eigen/Dense>
30 
31 namespace stomp_core
32 {
33 
34 
35 
36 void generateFiniteDifferenceMatrix(int num_time_steps,
37  DerivativeOrders::DerivativeOrder order,
38  double dt, Eigen::MatrixXd& diff_matrix)
39 {
40 
41  diff_matrix = Eigen::MatrixXd::Zero(num_time_steps, num_time_steps);
42  double multiplier = 1.0/pow(dt,(int)order);
43  for (int i=0; i<num_time_steps; ++i)
44  {
45  for (int j=-FINITE_DIFF_RULE_LENGTH/2; j<=FINITE_DIFF_RULE_LENGTH/2; ++j)
46  {
47  int index = i+j;
48  if (index < 0)
49  {
50  index = 0;
51  continue;
52  }
53  if (index >= num_time_steps)
54  {
55  index = num_time_steps-1;
56  continue;
57  }
58 
59  diff_matrix(i,index) = multiplier * FINITE_CENTRAL_DIFF_COEFFS[order][j+FINITE_DIFF_RULE_LENGTH/2];
60  }
61  }
62 }
63 
64 void generateSmoothingMatrix(int num_timesteps,double dt, Eigen::MatrixXd& projection_matrix_M)
65 {
66  using namespace Eigen;
67 
68  // generate augmented finite differencing matrix
69  int start_index_padded = FINITE_DIFF_RULE_LENGTH-1;
70  int num_timesteps_padded = num_timesteps + 2*(FINITE_DIFF_RULE_LENGTH-1);
71  MatrixXd finite_diff_matrix_A_padded;
72  generateFiniteDifferenceMatrix(num_timesteps_padded,DerivativeOrders::STOMP_ACCELERATION,
73  dt,finite_diff_matrix_A_padded);
74 
75 
76  /* computing control cost matrix (R = A_transpose * A):
77  * Note: Original code multiplies the A product by the time interval. However this is not
78  * what was described in the literature
79  */
80  MatrixXd control_cost_matrix_R_padded = dt*finite_diff_matrix_A_padded.transpose() * finite_diff_matrix_A_padded;
81  MatrixXd control_cost_matrix_R = control_cost_matrix_R_padded.block(
82  start_index_padded,start_index_padded,num_timesteps,num_timesteps);
83  MatrixXd inv_control_cost_matrix_R = control_cost_matrix_R.fullPivLu().inverse();
84 
85  // computing projection matrix M
86  projection_matrix_M = inv_control_cost_matrix_R;
87  double max = 0;
88  for(auto t = 0u; t < num_timesteps; t++)
89  {
90  max = projection_matrix_M(t,t);
91  projection_matrix_M.col(t)*= (1.0/(num_timesteps*max)); // scaling such that the maximum value is 1/num_timesteps
92  }
93 }
94 
95 void differentiate(const Eigen::VectorXd& parameters, DerivativeOrders::DerivativeOrder order,
96  double dt, Eigen::VectorXd& derivatives )
97 {
98 
99  using namespace Eigen;
100 
101  derivatives = Eigen::VectorXd::Zero(parameters.size());
102 
103 
104  // coefficient arrays
105  VectorXd central_coeffs = VectorXd::Map(&FINITE_CENTRAL_DIFF_COEFFS[order][0],FINITE_DIFF_RULE_LENGTH);
106  VectorXd forward_coeffs = VectorXd::Map(&FINITE_FORWARD_DIFF_COEFFS[order][0],FINITE_DIFF_RULE_LENGTH);
107  VectorXd backward_coeffs = forward_coeffs.reverse();
108 
109  // check order eveness
110  if(order %2 != 0)
111  {
112  backward_coeffs*= -1.0;
113  }
114 
115  // creating difference matrix
116  int rule_length = FINITE_DIFF_RULE_LENGTH;
117  int size = parameters.size();
118  int skip = FINITE_DIFF_RULE_LENGTH/2;
119  Eigen::MatrixXd A = Eigen::MatrixXd::Zero(size,size);
120  int start_ind, end_ind;
121  for(auto i = 0; i < size; i++)
122  {
123  if(i < skip)
124  {
125  start_ind = i;
126  A.row(i).segment(start_ind,rule_length) = forward_coeffs;
127  }
128  else if(i >= skip && i < size - skip)
129  {
130  start_ind = i - skip;
131  A.row(i).segment(start_ind,rule_length) = central_coeffs;
132  }
133  else
134  {
135  start_ind = i - rule_length+1;
136  A.row(i).segment(start_ind,rule_length) = backward_coeffs;
137  }
138  }
139 
140  derivatives = A*parameters/std::pow(dt,2);
141 }
142 
143 void toVector(const Eigen::MatrixXd& m,std::vector<Eigen::VectorXd>& v)
144 {
145  v.resize(m.rows(),Eigen::VectorXd::Zero(m.cols()));
146  for(auto d = 0u; d < m.rows();d++)
147  {
148  v[d] = m.row(d);
149  }
150 }
151 
152 std::string toString(const std::vector<Eigen::VectorXd>& data)
153 {
154  Eigen::IOFormat clean_format(4, 0, ", ", "\n", "[", "]");
155  Eigen::MatrixXd m = Eigen::MatrixXd::Zero(data.size(),data.front().size());
156  std::stringstream ss;
157  for(auto d = 0u; d < data.size(); d++)
158  {
159  m.row(d) = data[d].transpose();
160  }
161 
162  ss<<m.format(clean_format);
163  return ss.str();
164 }
165 
166 std::string toString(const Eigen::MatrixXd& data)
167 {
168  Eigen::IOFormat clean_format(4, 0, ", ", "\n", "[", "]");
169  std::stringstream ss;
170  ss<<data.format(clean_format);
171  return ss.str();
172 }
173 
174 std::string toString(const Eigen::VectorXd& data)
175 {
176  Eigen::IOFormat clean_format(4, 0, ", ", "\n", "[", "]");
177  std::stringstream ss;
178  ss<<data.transpose().format(clean_format);
179  return ss.str();
180 }
181 
182 }
183 
184 
185 
186 
187 
d


stomp_core
Author(s): Jorge Nicho
autogenerated on Fri May 8 2020 03:35:43