DirectTrajectoryOptimization  v0.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
GuardBase.hpp
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 
26 /*
27  * GuardBase.hpp
28  *
29  * Created on: Sep 5, 2016
30  * Author: depardo
31  */
32 
33 #ifndef _GuardBase_HPP_
34 #define _GuardBase_HPP_
35 
36 #include <Eigen/Dense>
37 #include <memory>
39 
40 namespace DirectTrajectoryOptimization{
41 namespace BaseClass{
42 
43 template <class DIMENSIONS>
44 class GuardBase : public NumDiffDerivativesBase,
45  public std::enable_shared_from_this<GuardBase<DIMENSIONS> > {
46 
47 public:
48 
49  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50 
51  typedef typename DIMENSIONS::state_vector_t state_vector_t;
52  typedef typename DIMENSIONS::control_vector_t control_vector_t;
53  static const int s_size = static_cast<int>(DIMENSIONS::DimensionsSize::STATE_SIZE);
54 
58  GuardBase(const bool & no_guard) {
59  SetSize(1);
60  SetBounds(Eigen::VectorXd::Constant(1,-1e20),Eigen::VectorXd::Constant(1,1e20));
61  }
62  GuardBase(){}
63 
64  virtual ~GuardBase(){ };
65 
66  int GetSize() {return complete_vector_size;}
67  Eigen::VectorXd GetLowerBound() {return guard_lb; }
68  Eigen::VectorXd GetUpperBound() {return guard_ub; }
69  void SetSize(const int & sog);
70  void SetBounds(const Eigen::VectorXd & lb, const Eigen::VectorXd & ub);
71  void initialize();
72  void initialize_num_diff();
73  void fx(const Eigen::VectorXd & in, Eigen::VectorXd & out);
74 
75  virtual Eigen::VectorXd evalConstraintsFct(const state_vector_t& x1);
76  Eigen::VectorXd evalConstraintsFctDerivatives(const state_vector_t & x1);
77 
78  std::shared_ptr<GuardBase<DIMENSIONS> > my_pointer;
79 
80  int complete_vector_size = 0;
81  int size_of_jacobian = 0;
82  int total_inputs_jacobian=0;
83 
84 private:
85 
86  Eigen::VectorXd guard_lb;
87  Eigen::VectorXd guard_ub;
88 
89  bool sog_ready_ = false;
90  bool bounds_ready_ = false;
91 
92 };
93 
94 template <class DIMENSIONS>
95 void GuardBase<DIMENSIONS>::SetSize(const int & sog) {
96  if(sog < 1) {
97  std::cout << "Guard size must be greater than 0" << std::endl;
98  std::exit(EXIT_FAILURE);
99  }
100  complete_vector_size = sog;
101  sog_ready_ = true;
102 }
103 
104 template <class DIMENSIONS>
105 void GuardBase<DIMENSIONS>::SetBounds(const Eigen::VectorXd & lb, const Eigen::VectorXd & ub) {
106  if(!(lb.size() == complete_vector_size) || !(ub.size() == complete_vector_size)) {
107  std::cout << "Guard bounds size must be equal to SoG" << std::endl;
108  std::exit(EXIT_FAILURE);
109  }
110  guard_lb = lb;
111  guard_ub = ub;
112  bounds_ready_ = true;
113 }
114 
115 template <class DIMENSIONS>
116 void GuardBase<DIMENSIONS>::initialize() {
117 
118  if(!sog_ready_ || !bounds_ready_) {
119  std::cout << "guard is not ready!" << std::endl;
120  std::exit(EXIT_FAILURE);
121  }
122 
123  // This cannot change
124  total_inputs_jacobian = s_size;
125 
126  // This cannot change
127  size_of_jacobian = complete_vector_size*total_inputs_jacobian;
128 
129  initialize_num_diff();
130 }
131 
132 template <class DIMENSIONS>
133 Eigen::VectorXd GuardBase<DIMENSIONS>::evalConstraintsFct(const state_vector_t& x1) {
134 
135  // always possible!
136  return (Eigen::VectorXd::Constant(1,0.0));
137 }
138 
139 template <class DIMENSIONS>
140 Eigen::VectorXd GuardBase<DIMENSIONS>::evalConstraintsFctDerivatives(const state_vector_t & x1) {
141 
142  this->numDiff->df(x1,this->mJacobian);
143 
144  // The Jacobian of the guards is defined as row_major: repeating the constraints!
145  //returning ROW-wise the constraint as a vector
146  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> temp(this->mJacobian);
147  return (Eigen::Map<Eigen::VectorXd>(temp.data(),size_of_jacobian));
148 }
149 
150 template <class DIMENSIONS>
151 void GuardBase<DIMENSIONS>::fx(const Eigen::VectorXd & inputs, Eigen::VectorXd & outputs) {
152  outputs = evalConstraintsFct(inputs);
153 }
154 
155 template <class DIMENSIONS>
156 void GuardBase<DIMENSIONS>::initialize_num_diff() {
157 
158  this->my_pointer = std::enable_shared_from_this<GuardBase<DIMENSIONS>>::shared_from_this();
159  this->mJacobian.resize(complete_vector_size,total_inputs_jacobian);
160  this->numdifoperator = std::shared_ptr<FunctionOperator>(new FunctionOperator(total_inputs_jacobian, complete_vector_size , this->my_pointer));
161  this->numDiff = std::shared_ptr<Eigen::NumericalDiff<FunctionOperator> >( new Eigen::NumericalDiff<FunctionOperator>(*this->numdifoperator,std::numeric_limits<double>::epsilon()));
162 }
163 
164 template <class DIMENSIONS>
165 const int GuardBase<DIMENSIONS>::s_size;
166 
167 } // BaseClass
168 } // DirectTrajectoryOptimization
169 #endif /* _GuardBase_HPP_ */
Base class for vector constraints and numdiff derivatives.