hpp-constraints  4.9.1
Definition of basic geometric constraints for motion planning
hierarchical-iterative.hh
Go to the documentation of this file.
1 // Copyright (c) 2017, Joseph Mirabel
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of hpp-constraints.
5 // hpp-constraints is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // hpp-constraints is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // hpp-constraints. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HPP_CONSTRAINTS_SOLVER_IMPL_HIERARCHICAL_ITERATIVE_HH
18 #define HPP_CONSTRAINTS_SOLVER_IMPL_HIERARCHICAL_ITERATIVE_HH
19 
20 #include <hpp/constraints/svd.hh>
21 
22 namespace hpp {
23  namespace constraints {
24  namespace solver {
25  namespace lineSearch {
26  template <typename SolverType>
27  inline bool Constant::operator() (const SolverType& solver, vectorOut_t arg, vectorOut_t darg)
28  {
29  solver.integrate (arg, darg, arg);
30  return true;
31  }
32 
33  template <typename SolverType>
34  inline bool Backtracking::operator() (const SolverType& solver, vectorOut_t arg, vectorOut_t u)
35  {
36  arg_darg.resize(arg.size());
37 
38  const value_type slope = computeLocalSlope(solver);
39  const value_type t = 2 * c * slope;
40  const value_type f_arg_norm2 = solver.residualError();
41 
42  if (t > 0) {
43  hppDout (error, "The descent direction is not valid: " << t/c);
44  } else {
45  value_type alpha = 1;
46  /* TODO add a threshold to avoid too large steps.
47  const value_type u2 = u.squaredNorm();
48  if (u2 > 1.) alpha = 1. / std::sqrt(u2);
49  */
50 
51  while (alpha > smallAlpha) {
52  darg = alpha * u;
53  solver.integrate (arg, darg, arg_darg);
54  solver.template computeValue<false> (arg_darg);
55  solver.computeError ();
56  // Check if we are doing better than the linear approximation with coef
57  // multiplied by c < 1
58  // t < 0 must hold
59  const value_type f_arg_darg_norm2 = solver.residualError();
60  if (f_arg_norm2 - f_arg_darg_norm2 >= - alpha * t) {
61  arg = arg_darg;
62  u = darg;
63  return true;
64  }
65  // Prepare next step
66  alpha *= tau;
67  }
68  hppDout (error, "Could find alpha such that ||f(q)||**2 + "
69  << c << " * 2*(f(q)^T * J * dq) is doing worse than "
70  "||f(q + alpha * dq)||**2");
71  }
72 
73  u *= smallAlpha;
74  solver.integrate (arg, u, arg);
75  return false;
76  }
77 
78  template <typename SolverType>
79  inline value_type Backtracking::computeLocalSlope(const SolverType& solver) const
80  {
81  value_type slope = 0;
82  for (std::size_t i = 0; i < solver.stacks_.size (); ++i) {
83  typename SolverType::Data& d = solver.datas_[i];
84  const size_type nrows = d.reducedJ.rows();
85  if (df.size() < nrows) df.resize(nrows);
86  df.head(nrows).noalias() = d.reducedJ * solver.dqSmall_;
87  slope += df.head(nrows).dot(d.activeRowsOfJ.keepRows().rview(d.error).eval());
88  }
89  return slope;
90  }
91 
92  template <typename SolverType>
93  inline bool FixedSequence::operator() (const SolverType& solver, vectorOut_t arg, vectorOut_t darg)
94  {
95  darg *= alpha;
96  alpha = alphaMax - K * (alphaMax - alpha);
97  solver.integrate (arg, darg, arg);
98  return true;
99  }
100 
101  template <typename SolverType>
102  inline bool ErrorNormBased::operator() (const SolverType& solver, vectorOut_t arg, vectorOut_t darg)
103  {
104  const value_type r = solver.residualError() / solver.squaredErrorThreshold();
105  const value_type alpha = C - K * std::tanh(a * r + b);
106  darg *= alpha;
107  solver.integrate (arg, darg, arg);
108  return true;
109  }
110  }
111 
112  template <typename LineSearchType>
114  vectorOut_t arg,
115  LineSearchType lineSearch) const
116  {
117  hppDout (info, "before projection: " << arg.transpose ());
118  assert (!arg.hasNaN());
119 
120  size_type errorDecreased = 3, iter = 0;
121  value_type previousSquaredNorm =
122  std::numeric_limits<value_type>::infinity();
123  static const value_type dqMinSquaredNorm = Eigen::NumTraits<value_type>::dummy_precision();
124 
125  // Fill value and Jacobian
126  computeValue<true> (arg);
127  computeError();
128 
129  if (squaredNorm_ > squaredErrorThreshold_
130  && reducedDimension_ == 0) return INFEASIBLE;
131 
132  Status status;
133  while (squaredNorm_ > squaredErrorThreshold_ && errorDecreased &&
134  iter < maxIterations_) {
135 
136  computeSaturation(arg);
137  computeDescentDirection ();
138  if (dq_.squaredNorm () < dqMinSquaredNorm) {
139  // TODO INFEASIBLE means that we have reached a local minima.
140  // The problem may still be feasible from a different starting point.
141  status = INFEASIBLE;
142  break;
143  }
144  lineSearch (*this, arg, dq_);
145 
146  computeValue<true> (arg);
147  computeError ();
148 
149  hppDout (info, "squareNorm = " << squaredNorm_);
150  --errorDecreased;
151  if (squaredNorm_ < previousSquaredNorm)
152  errorDecreased = 3;
153  else
154  status = ERROR_INCREASED;
155  previousSquaredNorm = squaredNorm_;
156  ++iter;
157 
158  }
159 
160  hppDout (info, "number of iterations: " << iter);
161  if (squaredNorm_ > squaredErrorThreshold_) {
162  hppDout (info, "Projection failed.");
163  return (iter >= maxIterations_) ? MAX_ITERATION_REACHED : status;
164  }
165  hppDout (info, "After projection: " << arg.transpose ());
166  assert (!arg.hasNaN());
167  return SUCCESS;
168  }
169  } // namespace solver
170  } // namespace constraints
171 } // namespace hpp
172 
173 #endif //HPP_CONSTRAINTS_SOLVER_IMPL_HIERARCHICAL_ITERATIVE_HH
value_type computeLocalSlope(const SolverType &solver) const
Definition: hierarchical-iterative.hh:79
#define hppDout(channel, data)
Vec3f b
FCL_REAL r
Vec3f c
Status
Definition: hierarchical-iterative.hh:171
assert(d.lhs()._blocks()==d.rhs()._blocks())
bool operator()(const SolverType &solver, vectorOut_t arg, vectorOut_t darg)
Definition: hierarchical-iterative.hh:93
bool operator()(const SolverType &solver, vectorOut_t arg, vectorOut_t darg)
Definition: hierarchical-iterative.hh:27
Status solve(vectorOut_t arg, LineSearchType ls=LineSearchType()) const
bool operator()(const SolverType &solver, vectorOut_t arg, vectorOut_t darg)
Definition: hierarchical-iterative.hh:102
Vec3f a
FCL_REAL d
bool operator()(const SolverType &solver, vectorOut_t arg, vectorOut_t darg)
Definition: hierarchical-iterative.hh:34
pinocchio::vectorOut_t vectorOut_t
Definition: fwd.hh:47
pinocchio::size_type size_type
Definition: fwd.hh:35
Transform3f t
pinocchio::value_type value_type
Definition: fwd.hh:36