DirectTrajectoryOptimization  v0.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
GenericConstraintsBase.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 _GENERICCONSTRAINTSBASE_H
32 #define _GENERICCONSTRAINTSBASE_H
33 
34 #include <Eigen/Dense>
35 #include <memory>
37 
38 namespace DirectTrajectoryOptimization {
39 namespace BaseClass{
40 
44 class GenericConstraintsBase : public NumDiffDerivativesBase , public std::enable_shared_from_this<GenericConstraintsBase> {
45 
46 public:
47 
48  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
49 
55  GenericConstraintsBase(int num_eq , int num_var):g_size_(num_eq),input_size_(num_var){};
56 
57 
62  virtual Eigen::VectorXd Evalfx(const Eigen::VectorXd & input) = 0;
63 
64  virtual void fx(const Eigen::VectorXd & in , Eigen::VectorXd & out) override;
65 
70  virtual Eigen::MatrixXd EvalFxJacobian(const Eigen::VectorXd & input);
71 
76  Eigen::VectorXd GetConstraintsLowerBound() {return g_low_;}
77 
82  Eigen::VectorXd GetConstraintsUpperBound() {return g_up_;}
83 
88  void SetConstraintsLowerBound(const Eigen::VectorXd & g_lb_candidate);
89 
94  void setConstraintsUpperBound(const Eigen::VectorXd & g_ub_candidate);
95 
96  int GetNumConstraints() {return g_size_;}
97 
98  int GetNnzJac() {return nnz_jac_;}
99 
100  Eigen::VectorXi GetSparseRow() {return iRow_;}
101  Eigen::VectorXi GetSparseCol() {return jCol_;}
102 
103  void initialize_num_diff();
104 
105  virtual ~GenericConstraintsBase() {};
106 
107 
108 protected:
109 
110  std::shared_ptr<GenericConstraintsBase> self_pointer;
111 
112  int input_size_ = 0;
113  int g_size_ = 0;
114 
115  int nnz_jac_=0;
116 
117  Eigen::VectorXd g_low_;
118  Eigen::VectorXd g_up_;
119 
120  Eigen::VectorXi iRow_;
121  Eigen::VectorXi jCol_;
122 
123 };
124 
125 void GenericConstraintsBase::fx(const Eigen::VectorXd & input, Eigen::VectorXd & output){
126 
127  output = this->Evalfx(input);
128 
129 }
130 
131 Eigen::MatrixXd GenericConstraintsBase::EvalFxJacobian(const Eigen::VectorXd & input){
132 
133  // NumDiff Base Jacobian of the constraint. User can override this method in the
134  // derived constraint. But this class provides a default Jacobian
135 
136  mJacobian.setZero();
137  this->numDiff->df(input,this->mJacobian);
138 
139  return(mJacobian);
140 
141 }
142 
143 void GenericConstraintsBase::SetConstraintsLowerBound(const Eigen::VectorXd & g_lb_candidate){
144  this->g_low_ = g_lb_candidate;
145 }
146 
147 void GenericConstraintsBase::setConstraintsUpperBound(const Eigen::VectorXd & g_ub_candidate){
148  this->g_up_ = g_ub_candidate;
149 }
150 
152 
153  mJacobian.resize(g_size_,input_size_);
154 
155  this->self_pointer = std::enable_shared_from_this<GenericConstraintsBase>::shared_from_this();
156 
157  this->numdifoperator = std::shared_ptr<FunctionOperator>(new FunctionOperator(input_size_, g_size_ , this->self_pointer));
158 
159  this->numDiff = std::shared_ptr<Eigen::NumericalDiff<FunctionOperator> >( new Eigen::NumericalDiff<FunctionOperator>(*this->numdifoperator , std::numeric_limits<double>::epsilon()));
160 
161 }
162 }
163 }
164 #endif /* _GENERICCONSTRAINTSBASE_H */
std::shared_ptr< Eigen::NumericalDiff< FunctionOperator > > numDiff
Definition: NumDiffDerivativesBase.hpp:108
void initialize_num_diff()
Overload this method to define the local pointers.
Definition: GenericConstraintsBase.hpp:151
Eigen::VectorXd GetConstraintsLowerBound()
Method to get the lower bound of the constraint.
Definition: GenericConstraintsBase.hpp:76
Eigen::VectorXd GetConstraintsUpperBound()
Method to get the upper bound of the constraint.
Definition: GenericConstraintsBase.hpp:82
Base class for numerical differentiation of vector functions.
std::shared_ptr< FunctionOperator > numdifoperator
Definition: NumDiffDerivativesBase.hpp:107
virtual Eigen::MatrixXd EvalFxJacobian(const Eigen::VectorXd &input)
To be overloaded by the derived class in case NumDiff is not required.
Definition: GenericConstraintsBase.hpp:131
void setConstraintsUpperBound(const Eigen::VectorXd &g_ub_candidate)
Method to set the upper bound of the constraint.
Definition: GenericConstraintsBase.hpp:147
virtual Eigen::VectorXd Evalfx(const Eigen::VectorXd &input)=0
To be overloaded by the derived class.
Base Class to define vector of constraints and its derivatives.
Definition: GenericConstraintsBase.hpp:44
void SetConstraintsLowerBound(const Eigen::VectorXd &g_lb_candidate)
Method to set the lower bound of the constraint.
Definition: GenericConstraintsBase.hpp:143
EIGEN_MAKE_ALIGNED_OPERATOR_NEW GenericConstraintsBase(int num_eq, int num_var)
Generic Constraint Constructor.
Definition: GenericConstraintsBase.hpp:55
virtual void fx(const Eigen::VectorXd &in, Eigen::VectorXd &out) override
Overload this method in the derived class to obtain numerical approximation of the gradient...
Definition: GenericConstraintsBase.hpp:125
This base class provides functionality for numerical differentiation of vector functions.
Definition: NumDiffDerivativesBase.hpp:44