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, 2018
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_HIERARCHICAL_ITERATIVE_HH
18 #define HPP_CONSTRAINTS_SOLVER_HIERARCHICAL_ITERATIVE_HH
19 
20 #include <boost/function.hpp>
21 
23 
24 #include <hpp/constraints/fwd.hh>
25 #include <hpp/constraints/config.hh>
26 #include <hpp/constraints/deprecated.hh>
27 
30 
31 namespace hpp {
32  namespace constraints {
33  namespace solver {
36  namespace lineSearch {
38  struct Constant {
39  template <typename SolverType>
40  bool operator() (const SolverType& solver, vectorOut_t arg,
41  vectorOut_t darg);
42  };
43 
46  struct Backtracking {
47  Backtracking ();
48 
49  template <typename SolverType>
50  bool operator() (const SolverType& solver, vectorOut_t arg,
51  vectorOut_t darg);
52 
53  template <typename SolverType>
54  inline value_type computeLocalSlope(const SolverType& solver) const;
55 
56  value_type c, tau, smallAlpha; // 0.8 ^ 7 = 0.209, 0.8 ^ 8 = 0.1677
57  mutable vector_t arg_darg, df, darg;
58  };
59 
63  struct FixedSequence {
64  FixedSequence();
65 
66  template <typename SolverType>
67  bool operator() (const SolverType& solver, vectorOut_t arg,
68  vectorOut_t darg);
69 
71  value_type alphaMax, K;
72  };
73 
79  struct ErrorNormBased {
81  ErrorNormBased(value_type alphaMin = 0.2);
82 
83  template <typename SolverType>
84  bool operator() (const SolverType& solver, vectorOut_t arg,
85  vectorOut_t darg);
86 
87  value_type C, K, a, b;
88  };
89  } // namespace lineSearch
90 
165  class HPP_CONSTRAINTS_DLLAPI HierarchicalIterative
166  {
167  public:
170 
171  enum Status {
175  SUCCESS
176  };
188  typedef boost::function<bool (vectorIn_t q, vectorOut_t qSat,
189  Eigen::VectorXi& saturation)>
191 
192  HierarchicalIterative (const LiegroupSpacePtr_t& configSpace);
193 
195 
197 
200 
203  {
204  return configSpace_;
205  }
213  virtual bool contains (const ImplicitPtr_t& numericalConstraint) const;
214 
223  void add (const DifferentiableFunctionPtr_t& f, const std::size_t& priority) HPP_CONSTRAINTS_DEPRECATED;
224 
232  void add (const DifferentiableFunctionPtr_t& f, const std::size_t& priority,
234 
240  void add (const ImplicitPtr_t& constraint, const std::size_t& priority);
241 
247  virtual void merge (const HierarchicalIterative& other);
248 
250  void saturation (const Saturation_t& saturate)
251  {
252  saturate_ = saturate;
253  }
254 
256  const Saturation_t& saturation () const
257  {
258  return saturate_;
259  }
260 
262 
265 
280  template <typename LineSearchType>
281  Status solve (vectorOut_t arg, LineSearchType ls = LineSearchType()) const;
282 
297  inline Status solve (vectorOut_t arg) const
298  {
299  return solve (arg, DefaultLineSearch());
300  }
301 
302  bool isSatisfied (vectorIn_t arg) const
303  {
304  computeValue<false>(arg);
305  computeError();
306  return squaredNorm_ < squaredErrorThreshold_;
307  }
308 
318  bool isConstraintSatisfied (const ImplicitPtr_t& constraint,
319  vectorIn_t arg, vectorOut_t error,
320  bool& constraintFound) const;
321 
326  const value_type& sigma () const
327  {
328  return sigma_;
329  }
330 
332 
335 
342  void freeVariables (const segments_t intervals)
343  {
344  freeVariables_ = Indices_t();
345  for (std::size_t i = 0; i < intervals.size(); ++i)
346  freeVariables_.addRow (intervals[i].first, intervals[i].second);
347  freeVariables_.updateIndices<true, true, true>();
348  update ();
349  }
350 
355  void freeVariables (const Indices_t& indices)
356  {
357  freeVariables_ = indices;
358  update ();
359  }
360 
362  const Indices_t& freeVariables () const
363  {
364  return freeVariables_;
365  }
366 
368  void maxIterations (size_type iterations)
369  {
370  maxIterations_ = iterations;
371  }
374  {
375  return maxIterations_;
376  }
377 
379  void errorThreshold (const value_type& threshold)
380  {
381  squaredErrorThreshold_ = threshold * threshold;
382  }
385  {
386  return sqrt (squaredErrorThreshold_);
387  }
390  {
391  return squaredErrorThreshold_;
392  }
393 
396  {
397  return inequalityThreshold_;
398  }
401  {
402  inequalityThreshold_ = it;
403  }
404 
405  void lastIsOptional (bool optional)
406  {
407  lastIsOptional_ = optional;
408  }
409 
410  bool lastIsOptional () const
411  {
412  return lastIsOptional_;
413  }
414 
416 
419 
421  const ImplicitConstraintSet& constraints (const std::size_t priority)
422  {
423  assert(priority < stacks_.size());
424  return stacks_[priority];
425  }
426 
429  {
430  return constraints_;
431  }
432 
433  std::size_t numberStacks() const
434  {
435  return stacks_.size();
436  }
437 
438  const size_type& dimension () const
439  {
440  return dimension_;
441  }
442 
445  const size_type& reducedDimension () const
446  {
447  return reducedDimension_;
448  }
449 
451  ArrayXb activeParameters () const;
452 
454  ArrayXb activeDerivativeParameters () const;
455 
457 
460  {
461  return squaredNorm_;
462  }
463 
465  void residualError(vectorOut_t error) const;
466 
475  bool definesSubmanifoldOf (const HierarchicalIterative& solver) const;
476 
479 
486  vector_t rightHandSideFromConfig (ConfigurationIn_t config);
487 
494  virtual bool rightHandSideFromConfig (const ImplicitPtr_t& constraint,
495  ConfigurationIn_t config);
501  virtual bool rightHandSide (const ImplicitPtr_t& constraint,
502  vectorIn_t rhs);
503 
505  virtual bool getRightHandSide (const ImplicitPtr_t& constraint,vectorOut_t rhs) const;
506 
511  virtual void rightHandSide (vectorIn_t rhs);
512 
515  void rightHandSideAt (const value_type& s);
516 
521  vector_t rightHandSide () const;
522 
526  size_type rightHandSideSize () const;
527 
529 
533 
535  template <bool ComputeJac> void computeValue (vectorIn_t arg) const;
536  void computeSaturation (vectorIn_t arg) const;
537  void getValue (vectorOut_t v) const;
538  void getReducedJacobian (matrixOut_t J) const;
541  void computeError () const;
542 
544  const vector_t& lastStep () const
545  {
546  return dq_;
547  }
548 
549  virtual bool integrate(vectorIn_t from, vectorIn_t velocity,
550  vectorOut_t result) const;
552 
553  virtual std::ostream& print (std::ostream& os) const;
554 
555  protected:
556  typedef Eigen::JacobiSVD <matrix_t> SVD_t;
557 
558  struct Data {
560  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
564  matrix_t jacobian, reducedJ;
565 
566  SVD_t svd;
568 
570 
572  std::vector<std::size_t> inequalityIndices;
575  };
576 
579  void update ();
580 
584  virtual void computeActiveRowsOfJ (std::size_t iStack);
585 
593  void computeDescentDirection () const;
594  void expandDqSmall () const;
595  void saturate (vectorOut_t arg) const;
596 
597 
598  value_type squaredErrorThreshold_, inequalityThreshold_;
600 
601  std::vector<ImplicitConstraintSet> stacks_;
606  Indices_t freeVariables_;
611  std::map <DifferentiableFunctionPtr_t, size_type> iq_;
613  std::map <DifferentiableFunctionPtr_t, size_type> iv_;
615  std::map <DifferentiableFunctionPtr_t, std::size_t> priority_;
616 
619 
620  mutable vector_t dq_, dqSmall_;
622  mutable Eigen::VectorXi saturation_, reducedSaturation_;
624  mutable ArrayXb tmpSat_;
626  mutable std::vector<Data> datas_;
627  mutable SVD_t svd_;
628  mutable vector_t OM_;
629  mutable vector_t OP_;
630 
631 
632  mutable ::hpp::statistics::SuccessStatistics statistics_;
633 
635  }; // class HierarchicalIterative
637  } // namespace solver
638  namespace lineSearch {
639  typedef ::hpp::constraints::solver::lineSearch::Constant Constant
641  typedef ::hpp::constraints::solver::lineSearch::Backtracking Backtracking
643  typedef ::hpp::constraints::solver::lineSearch::FixedSequence
644  FixedSequence HPP_CONSTRAINTS_DEPRECATED;
645  typedef ::hpp::constraints::solver::lineSearch::ErrorNormBased
646  ErrorNormBased HPP_CONSTRAINTS_DEPRECATED;
647  } // namespace lineSearch
648  } // namespace constraints
649 } // namespace hpp
650 
651 #endif // HPP_CONSTRAINTS_SOLVER_HIERARCHICAL_ITERATIVE_HH
pinocchio::vector_t vector_t
Definition: fwd.hh:45
pinocchio::vectorIn_t vectorIn_t
Definition: fwd.hh:46
Definition: implicit-constraint-set.hh:34
NumericalConstraints_t constraints_
Members moved from core::ConfigProjector.
Definition: hierarchical-iterative.hh:609
Definition: hierarchical-iterative.hh:46
void inequalityThreshold(const value_type &it)
set the inequality threshold
Definition: hierarchical-iterative.hh:400
bool lastIsOptional_
Definition: hierarchical-iterative.hh:604
std::map< DifferentiableFunctionPtr_t, std::size_t > priority_
Priority level of constraint.
Definition: hierarchical-iterative.hh:615
size_type maxIterations_
Definition: hierarchical-iterative.hh:599
void errorThreshold(const value_type &threshold)
Set error threshold.
Definition: hierarchical-iterative.hh:379
Definition: hierarchical-iterative.hh:79
std::vector< std::size_t > inequalityIndices
Definition: hierarchical-iterative.hh:572
Vec3f b
Eigen::RowBlockIndices equalityIndices
Definition: hierarchical-iterative.hh:573
pinocchio::LiegroupSpacePtr_t LiegroupSpacePtr_t
Definition: fwd.hh:54
value_type K
Definition: hierarchical-iterative.hh:87
ArrayXb tmpSat_
Definition: hierarchical-iterative.hh:624
matrix_t reducedJ
Definition: hierarchical-iterative.hh:564
lineSearch::FixedSequence DefaultLineSearch
Definition: hierarchical-iterative.hh:169
LiegroupSpacePtr_t configSpace_
Definition: hierarchical-iterative.hh:602
SVD_t svd
Definition: hierarchical-iterative.hh:566
std::vector< ImplicitConstraintSet > stacks_
Definition: hierarchical-iterative.hh:601
std::vector< segment_t > segments_t
Definition: fwd.hh:69
pinocchio::matrix_t matrix_t
Definition: fwd.hh:42
Eigen::RowBlockIndices Indices_t
Definition: hierarchical-iterative.hh:168
Vec3f c
pinocchio::ArrayXb ArrayXb
Definition: fwd.hh:65
value_type K
Definition: hierarchical-iterative.hh:71
size_type maxRank
Definition: hierarchical-iterative.hh:569
const LiegroupSpacePtr_t & configSpace() const
Get configuration space on which constraints are defined.
Definition: hierarchical-iterative.hh:202
const Saturation_t & saturation() const
Get the saturation function.
Definition: hierarchical-iterative.hh:256
vector_t dqSmall_
Definition: hierarchical-iterative.hh:620
No line search. Use .
Definition: hierarchical-iterative.hh:38
pinocchio::Configuration_t Configuration_t
Definition: fwd.hh:87
Status
Definition: hierarchical-iterative.hh:171
Definition: hierarchical-iterative.hh:63
vector_t OM_
Definition: hierarchical-iterative.hh:628
pinocchio::ConfigurationIn_t ConfigurationIn_t
Definition: fwd.hh:88
std::vector< ComparisonType > ComparisonTypes_t
Definition: fwd.hh:170
const size_type & reducedDimension() const
Definition: hierarchical-iterative.hh:445
value_type alpha
Definition: hierarchical-iterative.hh:70
assert(d.lhs()._blocks()==d.rhs()._blocks())
const Indices_t & freeVariables() const
Get free velocity variables.
Definition: hierarchical-iterative.hh:362
Status solve(vectorOut_t arg) const
Definition: hierarchical-iterative.hh:297
void freeVariables(const Indices_t &indices)
Definition: hierarchical-iterative.hh:355
size_type reducedDimension_
Definition: hierarchical-iterative.hh:603
void saturate(const DevicePtr_t &robot, ConfigurationOut_t configuration)
bool operator()(const SolverType &solver, vectorOut_t arg, vectorOut_t darg)
Definition: hierarchical-iterative.hh:27
void freeVariables(const segments_t intervals)
Definition: hierarchical-iterative.hh:342
bool isSatisfied(vectorIn_t arg) const
Definition: hierarchical-iterative.hh:302
const NumericalConstraints_t & constraints() const
Get constraints (implicit and explicit)
Definition: hierarchical-iterative.hh:428
boost::shared_ptr< DifferentiableFunction > DifferentiableFunctionPtr_t
Definition: fwd.hh:95
const ImplicitConstraintSet & constraints(const std::size_t priority)
Get set of constraints for a give priority level.
Definition: hierarchical-iterative.hh:421
Configuration_t qSat_
Definition: hierarchical-iterative.hh:623
matrix_t reducedJ_
Definition: hierarchical-iterative.hh:621
value_type errorThreshold() const
Get error threshold.
Definition: hierarchical-iterative.hh:384
Eigen::VectorXi saturation_
Definition: hierarchical-iterative.hh:622
Saturation_t saturate_
Definition: hierarchical-iterative.hh:607
bool lastIsOptional() const
Definition: hierarchical-iterative.hh:410
std::map< DifferentiableFunctionPtr_t, size_type > iq_
Value rank of constraint in its priority level.
Definition: hierarchical-iterative.hh:611
Vec3f a
Eigen::JacobiSVD< matrix_t > SVD_t
Definition: hierarchical-iterative.hh:556
value_type squaredNorm_
Definition: hierarchical-iterative.hh:625
value_type tau
Definition: hierarchical-iterative.hh:56
value_type inequalityThreshold() const
Get the inequality threshold.
Definition: hierarchical-iterative.hh:395
ComparisonTypes_t comparison
Definition: hierarchical-iterative.hh:571
vector_t df
Definition: hierarchical-iterative.hh:57
void lastIsOptional(bool optional)
Definition: hierarchical-iterative.hh:405
virtual ~HierarchicalIterative()
Definition: hierarchical-iterative.hh:196
Definition: hierarchical-iterative.hh:558
pinocchio::vectorOut_t vectorOut_t
Definition: fwd.hh:47
Indices_t freeVariables_
Unknown of the set of implicit constraints.
Definition: hierarchical-iterative.hh:606
value_type sigma_
The smallest non-zero singular value.
Definition: hierarchical-iterative.hh:618
mutable ::hpp::statistics::SuccessStatistics statistics_
Definition: hierarchical-iterative.hh:632
std::vector< Data > datas_
Definition: hierarchical-iterative.hh:626
void maxIterations(size_type iterations)
Set maximal number of iterations.
Definition: hierarchical-iterative.hh:368
void saturation(const Saturation_t &saturate)
Set the saturation function.
Definition: hierarchical-iterative.hh:250
pinocchio::size_type size_type
Definition: fwd.hh:35
Eigen::Ref< matrix_t > matrixOut_t
Definition: fwd.hh:44
vector_t error
Definition: hierarchical-iterative.hh:563
Definition: hierarchical-iterative.hh:174
LiegroupElement rightHandSide
Definition: hierarchical-iterative.hh:562
SVD_t svd_
Definition: hierarchical-iterative.hh:627
vector_t OP_
Definition: hierarchical-iterative.hh:629
boost::function< bool(vectorIn_t q, vectorOut_t qSat, Eigen::VectorXi &saturation)> Saturation_t
Definition: hierarchical-iterative.hh:190
pinocchio::value_type value_type
Definition: fwd.hh:36
value_type residualError() const
Returns the squared norm of the error vector.
Definition: hierarchical-iterative.hh:459
const size_type & dimension() const
Definition: hierarchical-iterative.hh:438
Eigen::MatrixBlocks< false, false > activeRowsOfJ
Definition: hierarchical-iterative.hh:574
matrix_t PK
Definition: hierarchical-iterative.hh:567
value_type squaredErrorThreshold_
Definition: hierarchical-iterative.hh:598
boost::shared_ptr< Implicit > ImplicitPtr_t
Definition: fwd.hh:157
std::size_t numberStacks() const
Definition: hierarchical-iterative.hh:433
Definition: hierarchical-iterative.hh:165
size_type maxIterations() const
Get maximal number of iterations in config projector.
Definition: hierarchical-iterative.hh:373
std::map< DifferentiableFunctionPtr_t, size_type > iv_
Derivative rank of constraint in its priority level.
Definition: hierarchical-iterative.hh:613
DifferentiableFunctionSet DifferentiableFunctionStack HPP_CONSTRAINTS_DEPRECATED
Definition: fwd.hh:99
const vector_t & lastStep() const
Accessor to the last step done.
Definition: hierarchical-iterative.hh:544
std::vector< constraints::ImplicitPtr_t > NumericalConstraints_t
Definition: fwd.hh:159
value_type squaredErrorThreshold() const
Get error threshold.
Definition: hierarchical-iterative.hh:389
const value_type & sigma() const
Definition: hierarchical-iterative.hh:326