DirectTrajectoryOptimization  v0.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
NumDiffDerivativesBase.hpp
Go to the documentation of this file.
1 /***********************************************************************************
2 Copyright (c) 2017, Diego Pardo. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright notice,
7  this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice,
9  this list of conditions and the following disclaimer in the documentation
10  and/or other materials provided with the distribution.
11  * Neither the name of ETH ZURICH nor the names of its contributors may be used
12  to endorse or promote products derived from this software without specific
13  prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
16 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
18 SHALL ETH ZURICH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
19 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
23 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 ***************************************************************************************/
25 
31 #ifndef NUMDIFFDERIVATIVESBASE_HPP_
32 #define NUMDIFFDERIVATIVESBASE_HPP_
33 
34 #include <Eigen/Dense>
35 #include <unsupported/Eigen/NumericalDiff>
36 
37 namespace DirectTrajectoryOptimization {
38 namespace BaseClass {
39 
45 
46  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
47 
48 protected:
50 
51 public:
52 
53  virtual ~NumDiffDerivativesBase(){};
54 
55  template<typename _Scalar, int NX = Eigen::Dynamic, int NY = Eigen::Dynamic>
56  struct DynamicSizedFunctor {
57 
58  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
59 
60  typedef _Scalar Scalar;
61  enum {
62  InputsAtCompileTime = NX, ValuesAtCompileTime = NY
63  };
64  typedef Eigen::Matrix<Scalar, InputsAtCompileTime, 1> InputType;
65  typedef Eigen::Matrix<Scalar, ValuesAtCompileTime, 1> ValueType;
66  typedef Eigen::Matrix<Scalar, ValuesAtCompileTime, InputsAtCompileTime> JacobianType;
67 
68  int m_inputs, m_values;
69 
70  DynamicSizedFunctor() :
71  m_inputs(InputsAtCompileTime), m_values(ValuesAtCompileTime) {
72  }
73  DynamicSizedFunctor(int inputs, int values) :
74  m_inputs(inputs), m_values(values) {
75  }
76 
77  int inputs() const {
78  return m_inputs;
79  }
80  int values() const {
81  return m_values;
82  }
83 
84  };
85 
86  struct FunctionOperator:DynamicSizedFunctor<double> {
87 
88  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
89 
90  //y = f(x);
91  FunctionOperator(int in_size, int out_size, std::shared_ptr<NumDiffDerivativesBase> my_owner_pointer) :
92  DynamicSizedFunctor<double>(in_size, out_size),owner_pointer(my_owner_pointer)
93  { }
94 
95  int operator()(const Eigen::VectorXd &in_vect,
96  Eigen::VectorXd &fvec) const {
97 
98  fvec.setZero();
99  owner_pointer->fx(in_vect, fvec);
100  return 0;
101  }
102  private :
103  std::shared_ptr<NumDiffDerivativesBase> owner_pointer;
104 
105  };
106 
107  std::shared_ptr<FunctionOperator> numdifoperator;
108  std::shared_ptr<Eigen::NumericalDiff<FunctionOperator> > numDiff;
109  Eigen::MatrixXd mJacobian;
110 
114  virtual void fx(const Eigen::VectorXd &, Eigen::VectorXd &) = 0;
115 
120  virtual void initialize_num_diff() = 0;
121 };
122 }
123 }
124 #endif /* NUMDIFFDERIVATIVESBASE_HPP_ */
std::shared_ptr< Eigen::NumericalDiff< FunctionOperator > > numDiff
Definition: NumDiffDerivativesBase.hpp:108
virtual void initialize_num_diff()=0
Overload this method to define the local pointers.
virtual void fx(const Eigen::VectorXd &, Eigen::VectorXd &)=0
Overload this method in the derived class to obtain numerical approximation of the gradient...
std::shared_ptr< FunctionOperator > numdifoperator
Definition: NumDiffDerivativesBase.hpp:107
This base class provides functionality for numerical differentiation of vector functions.
Definition: NumDiffDerivativesBase.hpp:44