bayesian_ik_solver.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 // This code is based on algorithm developed by Marc Toussaint
31 // M. Toussaint: Robot Trajectory Optimization using Approximate Inference. In Proc. of the Int. Conf. on Machine Learning (ICML 2009), 1049-1056, ACM, 2009.
32 // http://ipvs.informatik.uni-stuttgart.de/mlr/papers/09-toussaint-ICML.pdf
33 // Original code available at http://ipvs.informatik.uni-stuttgart.de/mlr/marc/source-code/index.html
34 
36 #include <exotica_core/server.h>
37 
39 
40 namespace exotica
41 {
42 void BayesianIKSolver::Instantiate(const BayesianIKSolverInitializer& init)
43 {
44  parameters_ = init;
45  std::string mode = init.SweepMode;
46  if (mode == "Forwardly")
47  sweep_mode_ = FORWARD;
48  else if (mode == "Symmetric")
49  sweep_mode_ = SYMMETRIC;
50  else if (mode == "LocalGaussNewton")
51  sweep_mode_ = LOCAL_GAUSS_NEWTON;
52  else if (mode == "LocalGaussNewtonDamped")
53  sweep_mode_ = LOCAL_GAUSS_NEWTON_DAMPED;
54  else
55  {
56  ThrowNamed("Unknown sweep mode '" << init.SweepMode << "'");
57  }
58  max_backtrack_iterations_ = init.MaxBacktrackIterations;
59  minimum_step_tolerance_ = init.MinStep;
60  step_tolerance_ = init.StepTolerance;
61  function_tolerance_ = init.FunctionTolerance;
62  damping_init_ = init.Damping;
63  use_bwd_msg_ = init.UseBackwardMessage;
64  verbose_ = init.Verbose;
65 }
66 
67 void BayesianIKSolver::SpecifyProblem(PlanningProblemPtr problem)
68 {
69  if (problem->type() != "exotica::UnconstrainedEndPoseProblem")
70  {
71  ThrowNamed("This solver can't use problem of type '" << problem->type() << "'!");
72  }
73  MotionSolver::SpecifyProblem(problem);
74  prob_ = std::static_pointer_cast<UnconstrainedEndPoseProblem>(problem);
75 
76  InitMessages();
77 }
78 
79 void BayesianIKSolver::Solve(Eigen::MatrixXd& solution)
80 {
81  prob_->ResetCostEvolution(GetNumberOfMaxIterations() + 1);
82  prob_->termination_criterion = TerminationCriterion::NotStarted;
83  planning_time_ = -1;
84 
85  Eigen::VectorXd q0 = prob_->ApplyStartState();
86 
87  Timer timer;
88  if (verbose_) ROS_WARN_STREAM("BayesianIKSolver: Setting up the solver");
89  update_count_ = 0;
90  damping = damping_init_;
91  double d;
92  iteration_count_ = -1;
93  InitTrajectory(q0);
94  if (verbose_) ROS_WARN_STREAM("BayesianIKSolver: Solving");
95 
96  // Reset sweep and iteration count
97  sweep_ = 0;
98  iteration_count_ = 0;
99  while (iteration_count_ < GetNumberOfMaxIterations())
100  {
101  // Check whether user interrupted (Ctrl+C)
102  if (Server::IsRos() && !ros::ok())
103  {
104  if (debug_) HIGHLIGHT("Solving cancelled by user");
105  prob_->termination_criterion = TerminationCriterion::UserDefined;
106  break;
107  }
108 
109  d = Step();
110  if (d < 0)
111  {
112  ThrowNamed("Negative step size!");
113  }
114 
115  // 0. Check maximum backtrack iterations
116  if (sweep_ >= max_backtrack_iterations_)
117  {
118  if (debug_) HIGHLIGHT("Maximum backtrack iterations reached, exiting.");
119  prob_->termination_criterion = TerminationCriterion::BacktrackIterationLimit;
120  break;
121  }
122 
123  // Check stopping criteria
124  if (iteration_count_ > 1)
125  {
126  // Check convergence if
127  // a) damping is on and the iteration has concluded (the sweep improved the cost)
128  // b) damping is off [each sweep equals one iteration]
129  if ((damping != 0.0 && sweep_improved_cost_) || !(damping != 0.0))
130  {
131  // 1. Check step tolerance
132  // || x_t-x_t-1 || <= stepTolerance * max(1, || x_t ||)
133  // TODO(#257): TODO(#256): move to Eigen::MatrixXd to make this easier to compute, in the meantime use old check
134  //
135  // TODO(#256): OLD TOLERANCE CHECK - TODO REMOVE
136  if (d < minimum_step_tolerance_)
137  {
138  if (debug_) HIGHLIGHT("Satisfied tolerance\titer=" << iteration_count_ << "\td=" << d << "\tminimum_step_tolerance=" << minimum_step_tolerance_);
139  prob_->termination_criterion = TerminationCriterion::StepTolerance;
140  break;
141  }
142 
143  // 2. Check function tolerance
144  // (f_t-1 - f_t) <= functionTolerance * max(1, abs(f_t))
145  if ((cost_prev_ - cost_) <= function_tolerance_ * std::max(1.0, std::abs(cost_)))
146  {
147  if (debug_) HIGHLIGHT("Function tolerance achieved: " << (cost_prev_ - cost_) << " <= " << function_tolerance_ * std::max(1.0, std::abs(cost_)));
148  prob_->termination_criterion = TerminationCriterion::FunctionTolerance;
149  break;
150  }
151  cost_prev_ = cost_;
152  }
153  }
154  }
155 
156  // Check whether maximum iteration count was reached
157  if (iteration_count_ == GetNumberOfMaxIterations())
158  {
159  if (debug_) HIGHLIGHT("Maximum iterations reached");
160  prob_->termination_criterion = TerminationCriterion::IterationLimit;
161  }
162 
163  solution.resize(1, prob_->N);
164  solution.row(0) = q;
165  planning_time_ = timer.GetDuration();
166 }
167 
168 void BayesianIKSolver::InitMessages()
169 {
170  s = Eigen::VectorXd::Zero(prob_->N);
171  Sinv = Eigen::MatrixXd::Zero(prob_->N, prob_->N);
172  v = Eigen::VectorXd::Zero(prob_->N);
173  Vinv = Eigen::MatrixXd::Zero(prob_->N, prob_->N);
174  // if (use_bwd_msg_)
175  // {
176  // if (bwd_msg_v_.rows() == prob_->N && bwd_msg_Vinv_.rows() == prob_->N && bwd_msg_Vinv_.cols() == prob_->N)
177  // {
178  // v[prob_->GetT() - 1] = bwd_msg_v_;
179  // Vinv[prob_->GetT() - 1] = bwd_msg_Vinv_;
180  // }
181  // else
182  // {
183  // use_bwd_msg_ = false;
184  // WARNING("Backward message initialisation skipped, matrices have incorrect dimensions.");
185  // }
186  // }
187  b = Eigen::VectorXd::Zero(prob_->N);
188  damping_reference_ = Eigen::VectorXd::Zero(prob_->N);
189  Binv = Eigen::MatrixXd::Zero(prob_->N, prob_->N);
190  r = Eigen::VectorXd::Zero(prob_->N);
191  R = Eigen::MatrixXd::Zero(prob_->N, prob_->N);
192  rhat = 0;
193  qhat = Eigen::VectorXd::Zero(prob_->N);
194  q = b;
195 }
196 
197 void BayesianIKSolver::InitTrajectory(const Eigen::VectorXd& q_init)
198 {
199  qhat = q_init;
200  q = q_init;
201  damping_reference_ = q_init;
202  b = q_init;
203  s = q_init;
204  v = q_init;
205  Sinv.setZero();
206  Sinv.diagonal().setConstant(damping);
207  Vinv.setZero();
208  Vinv.diagonal().setConstant(damping);
209 
210  // W is still writable, check dimension
211  if (prob_->W.rows() != prob_->N)
212  {
213  ThrowNamed(prob_->W.rows() << "!=" << prob_->N);
214  }
215 
216  // Set constant W,Win,H,Hinv
217  W = prob_->W;
218  Winv = W.inverse();
219 
220  // Compute task message reference
221  UpdateTaskMessage(b, 0.0);
222 
223  cost_ = EvaluateTrajectory(b, true); // The problem will be updated via UpdateTaskMessage, i.e. do not update on this roll-out
224  cost_prev_ = cost_;
225  prob_->SetCostEvolution(0, cost_);
226  if (cost_ < 0) ThrowNamed("Invalid cost! " << cost_);
227  if (verbose_) HIGHLIGHT("Initial cost, updates: " << update_count_ << ", cost: " << cost_);
228  RememberOldState();
229 }
230 
231 void BayesianIKSolver::UpdateFwdMessage()
232 {
233  Eigen::MatrixXd barS(prob_->N, prob_->N), St;
234  inverseSymPosDef(barS, Sinv + R);
235  s = barS * (Sinv * s + r);
236  St = Winv + barS;
237  inverseSymPosDef(Sinv, St);
238 }
239 
240 void BayesianIKSolver::UpdateBwdMessage()
241 {
242  Eigen::MatrixXd barV(prob_->N, prob_->N), Vt;
243 
244  if (!use_bwd_msg_)
245  {
246  v = b;
247  Vinv.diagonal().setConstant(1);
248  }
249  else
250  {
251  v = bwd_msg_v_;
252  Vinv = bwd_msg_Vinv_;
253  }
254 }
255 
256 void BayesianIKSolver::UpdateTaskMessage(const Eigen::Ref<const Eigen::VectorXd>& qhat_t, double tolerance,
257  double max_step_size)
258 {
259  Eigen::VectorXd diff = qhat_t - qhat;
260  if ((diff.array().abs().maxCoeff() < tolerance)) return;
261  double nrm = diff.norm();
262  if (max_step_size > 0. && nrm > max_step_size)
263  {
264  qhat += diff * (max_step_size / nrm);
265  }
266  else
267  {
268  qhat = qhat_t;
269  }
270 
271  prob_->Update(qhat);
272  ++update_count_;
273  GetTaskCosts();
274  // q_stat_.addw(c > 0 ? 1.0 / (1.0 + c) : 1.0, qhat_t);
275 }
276 
277 void BayesianIKSolver::GetTaskCosts()
278 {
279  Eigen::MatrixXd Jt;
280  double prec;
281  rhat = 0;
282  R.setZero();
283  r.setZero();
284  for (int i = 0; i < prob_->cost.num_tasks; ++i)
285  {
286  prec = prob_->cost.rho(i);
287  if (prec > 0)
288  {
289  const int& start = prob_->cost.indexing[i].start_jacobian;
290  const int& len = prob_->cost.indexing[i].length_jacobian;
291  Jt = prob_->cost.jacobian.middleRows(start, len).transpose();
292  R += prec * Jt * prob_->cost.jacobian.middleRows(start, len);
293  r += prec * Jt * (-prob_->cost.ydiff.segment(start, len) + prob_->cost.jacobian.middleRows(start, len) * qhat);
294  rhat += prec * (-prob_->cost.ydiff.segment(start, len) + prob_->cost.jacobian.middleRows(start, len) * qhat).squaredNorm();
295  }
296  }
297 }
298 
299 void BayesianIKSolver::UpdateTimestep(bool update_fwd, bool update_bwd,
300  int max_relocation_iterations, double tolerance, bool force_relocation,
301  double max_step_size)
302 {
303  if (update_fwd) UpdateFwdMessage();
304  if (update_bwd) UpdateBwdMessage();
305 
306  if (damping != 0.0)
307  {
308  Binv = Sinv + Vinv + R + Eigen::MatrixXd::Identity(prob_->N, prob_->N) * damping;
309  AinvBSymPosDef(b, Binv, Sinv * s + Vinv * v + r + damping * damping_reference_);
310  }
311  else
312  {
313  Binv = Sinv + Vinv + R;
314  AinvBSymPosDef(b, Binv, Sinv * s + Vinv * v + r);
315  }
316 
317  for (int k = 0; k < max_relocation_iterations && !(Server::IsRos() && !ros::ok()); ++k)
318  {
319  if (!((!k && force_relocation) || (b - qhat).array().abs().maxCoeff() > tolerance)) break;
320 
321  UpdateTaskMessage(b, 0., max_step_size);
322 
323  //optional reUpdate fwd or bwd message (if the Dynamics might have changed...)
324  if (update_fwd) UpdateFwdMessage();
325  if (update_bwd) UpdateBwdMessage();
326 
327  if (damping != 0.0)
328  {
329  Binv = Sinv + Vinv + R + Eigen::MatrixXd::Identity(prob_->N, prob_->N) * damping;
330  AinvBSymPosDef(b, Binv, Sinv * s + Vinv * v + r + damping * damping_reference_);
331  }
332  else
333  {
334  Binv = Sinv + Vinv + R;
335  AinvBSymPosDef(b, Binv, Sinv * s + Vinv * v + r);
336  }
337  }
338 }
339 
340 void BayesianIKSolver::UpdateTimestepGaussNewton(bool update_fwd,
341  bool update_bwd, int max_relocation_iterations, double tolerance,
342  double max_step_size)
343 {
344  // TODO: implement UpdateTimestepGaussNewton
345  ThrowNamed("Not implemented yet!");
346 }
347 
348 double BayesianIKSolver::EvaluateTrajectory(const Eigen::VectorXd& x, bool skip_update)
349 {
350  if (verbose_) ROS_WARN_STREAM("Evaluating, iteration " << iteration_count_ << ", sweep_ " << sweep_);
351  q = x;
352 
353  // Perform update / roll-out
354  if (!skip_update)
355  {
356  ++update_count_;
357  prob_->Update(q);
358  }
359 
360  // Task cost
361  return prob_->GetScalarCost();
362 }
363 
364 double BayesianIKSolver::Step()
365 {
366  RememberOldState();
367  switch (sweep_mode_)
368  {
369  //NOTE: the dependence on (Sweep?..:..) could perhaps be replaced by (DampingReference.N?..:..)
370  case FORWARD:
371  UpdateTimestep(true, false, 1, minimum_step_tolerance_, !iteration_count_, 1.); //relocate once on fwd Sweep
372  UpdateTimestep(false, true, 0, minimum_step_tolerance_, false, 1.); //...not on bwd Sweep
373  break;
374  case SYMMETRIC:
375  UpdateTimestep(true, false, 1, minimum_step_tolerance_, !iteration_count_, 1.); //relocate once on fwd & bwd Sweep
376  UpdateTimestep(false, true, (iteration_count_ ? 1 : 0), minimum_step_tolerance_, false, 1.);
377  break;
378  case LOCAL_GAUSS_NEWTON:
379  // UpdateTimestep(t, true, false, (iteration_count_ ? 5 : 1), minimum_step_tolerance_, !iteration_count_, 1.); //relocate iteratively on
380  // UpdateTimestep(t, false, true, (iteration_count_ ? 5 : 0), minimum_step_tolerance_, false, 1.); //...fwd & bwd Sweep
381  break;
382  case LOCAL_GAUSS_NEWTON_DAMPED:
383  // UpdateTimestepGaussNewton(t, true, false, (iteration_count_ ? 5 : 1),
384  // minimum_step_tolerance_, 1.); //GaussNewton in fwd & bwd Sweep
385  // UpdateTimestep(t, false, true, (iteration_count_ ? 5 : 0), minimum_step_tolerance_, false, 1.);
386  break;
387  default:
388  ThrowNamed("non-existing Sweep mode");
389  }
390  b_step_ = std::max((b_old - b).array().abs().maxCoeff(), 0.0);
391  damping_reference_ = b;
392  // q is set inside of EvaluateTrajectory() function
393  cost_ = EvaluateTrajectory(b);
394  if (debug_) HIGHLIGHT("Iteration: " << iteration_count_ << ", Sweep: " << sweep_ << ", updates: " << update_count_ << ", cost: " << cost_ << " (dq=" << b_step_ << ", damping=" << damping << ")");
395  if (cost_ < 0) return -1.0;
396  best_sweep_ = sweep_;
397 
398  // If damping (similar to line-search) is being used, consider reverting this step
399  if (damping != 0.0) PerhapsUndoStep();
400 
401  ++sweep_;
402  if (sweep_improved_cost_)
403  {
404  // HIGHLIGHT("Sweep improved cost, increasing iteration count and resetting sweep count");
405  ++iteration_count_;
406  sweep_ = 0;
407  prob_->SetCostEvolution(iteration_count_, cost_);
408  }
409 
410  return b_step_;
411 }
412 
413 void BayesianIKSolver::RememberOldState()
414 {
415  s_old = s;
416  Sinv_old = Sinv;
417  v_old = v;
418  Vinv_old = Vinv;
419  r_old = r;
420  R_old = R;
421  Binv_old = Binv;
422  rhat_old = rhat;
423  b_old = b;
424  r_old = r;
425  q_old = q;
426  qhat_old = qhat;
427  cost_old_ = cost_;
428 
429  best_sweep_old_ = best_sweep_;
430  b_step_old_ = b_step_;
431 }
432 
433 void BayesianIKSolver::PerhapsUndoStep()
434 {
435  if (cost_ > cost_old_)
436  {
437  sweep_improved_cost_ = false;
438  damping *= 10.;
439  s = s_old;
440  Sinv = Sinv_old;
441  v = v_old;
442  Vinv = Vinv_old;
443  r = r_old;
444  R = R_old;
445  Binv = Binv_old;
446  rhat = rhat_old;
447  b = b_old;
448  r = r_old;
449  q = q_old;
450  qhat = qhat_old;
451  cost_ = cost_old_;
452  damping_reference_ = b_old;
453  best_sweep_ = best_sweep_old_;
454  b_step_ = b_step_old_;
455  if (verbose_) HIGHLIGHT("Reverting to previous line-search step (" << best_sweep_ << ")");
456  }
457  else
458  {
459  sweep_improved_cost_ = true;
460  damping /= 5.;
461  }
462 }
463 } // namespace exotica
d
#define HIGHLIGHT(x)
static void AinvBSymPosDef(T1 &x, const T2 &A, const T3 &b)
Computes the solution to the linear problem for symmetric positive definite matrix A...
Solves motion planning problem using Approximate Inference Control method.
XmlRpcServer s
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
#define REGISTER_MOTIONSOLVER_TYPE(TYPE, DERIV)
#define ThrowNamed(m)
ROSCPP_DECL bool ok()
#define ROS_WARN_STREAM(args)
static void inverseSymPosDef(T1 &Ainv, const T2 &A)
Computes an inverse of a symmetric positive definite matrix.
INLINE Rall1d< T, V, S > abs(const Rall1d< T, V, S > &x)
double GetDuration() const
std::shared_ptr< PlanningProblem > PlanningProblemPtr
r


exotica_aico_solver
Author(s):
autogenerated on Sat Apr 10 2021 02:35:19