DirectTrajectoryOptimization  v0.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
InterPhaseBaseConstraint.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  * InterPhaseBase.hpp
28  *
29  * Created on: Sep 3, 2016
30  * Author: depardo
31  */
32 
33 #ifndef _InterPhaseBase_HPP_
34 #define _InterPhaseBase_HPP_
35 
36 #include <memory>
38 
39 namespace DirectTrajectoryOptimization{
40 namespace BaseClass{
41 
42 template <class DIMENSIONS>
43 class InterPhaseBase : public NumDiffDerivativesBase,
44  public std::enable_shared_from_this<InterPhaseBase<DIMENSIONS> > {
45 
46 public:
47 
48  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
49 
50  typedef typename DIMENSIONS::state_vector_t state_vector_t;
51  typedef typename DIMENSIONS::control_vector_t control_vector_t;
52 
53  static const int s_size = static_cast<int>(DIMENSIONS::DimensionsSize::STATE_SIZE);
54  static const int c_size = static_cast<int>(DIMENSIONS::DimensionsSize::CONTROL_SIZE);
55 
56  int complete_vector_size = 0;
57  int size_of_jacobian = 0;
58  int total_inputs_jacobian=0;
59 
60  std::shared_ptr<InterPhaseBase<DIMENSIONS> > my_pointer;
61 
62  InterPhaseBase() { }
63  InterPhaseBase(const bool & no_ipc) {
64 
65  //Initialization for the default case
66  complete_vector_size = s_size;
67  ipc_lb = Eigen::VectorXd::Zero(complete_vector_size);
68  ipc_ub = Eigen::VectorXd::Zero(complete_vector_size);
69 
70  soc_ready_= true;
71  bounds_ready_ = true;
72  }
73  virtual ~InterPhaseBase(){ };
74  int GetSize() { return complete_vector_size;}
75  int GetNnzJacobian() { return size_of_jacobian;}
76  Eigen::VectorXd getLowerBound(){return ipc_lb;}
77  Eigen::VectorXd getUpperBound(){return ipc_ub;}
78 
79  void setSize(const int & soc);
80  void setBounds(const Eigen::VectorXd & lb , const Eigen::VectorXd & ub);
81 
82  void initialize();
83  void initialize_num_diff();
84  void fx(const Eigen::VectorXd & in, Eigen::VectorXd & out);
85 
86  // Implement a default constraint where the positions and controls must be the same
87  virtual Eigen::VectorXd evalConstraintsFct(const state_vector_t& x1, const state_vector_t & x2,
88  const control_vector_t & u1, const control_vector_t & u2);
89 
90  Eigen::VectorXd evalConstraintsFctDerivatives(const state_vector_t & x1, const state_vector_t & x2,
91  const control_vector_t & u1, const control_vector_t & u2);
92 
93 private:
94  Eigen::VectorXd ipc_lb;
95  Eigen::VectorXd ipc_ub;
96 
97  bool soc_ready_ = false;
98  bool bounds_ready_ = false;
99 
100 };
101 
102 template <class DIMENSIONS>
103 void InterPhaseBase<DIMENSIONS>::setSize(const int & soc) {
104  if(soc < 1) {
105  std::cout << "Interphase size must be greater than 0" << std::endl;
106  std::exit(EXIT_FAILURE);
107  }
108  complete_vector_size = soc;
109  soc_ready_ = true;
110 }
111 
112 template <class DIMENSIONS>
113 void InterPhaseBase<DIMENSIONS>::setBounds(const Eigen::VectorXd & lb , const Eigen::VectorXd & ub) {
114  if(!(lb.size() == complete_vector_size) || !(ub.size() == complete_vector_size)) {
115  std::cout << "Interphase bounds size must be equal to SoG" << std::endl;
116  std::exit(EXIT_FAILURE);
117  }
118  ipc_lb = lb;
119  ipc_ub = ub;
120  bounds_ready_ = true;
121 }
122 
123 template <class DIMENSIONS>
124 void InterPhaseBase<DIMENSIONS>::initialize() {
125 
126  if(!soc_ready_ || !bounds_ready_) {
127  std::cout << "Interphase is not ready!" << std::endl;
128  std::exit(EXIT_FAILURE);
129  }
130 
131  // this is always like this!
132  total_inputs_jacobian = 2*s_size + 2*c_size;
133  size_of_jacobian = complete_vector_size*total_inputs_jacobian;
134  initialize_num_diff();
135 }
136 
137 template <class DIMENSIONS>
138 Eigen::VectorXd InterPhaseBase<DIMENSIONS>::evalConstraintsFct(const state_vector_t& x1, const state_vector_t & x2,
139  const control_vector_t & u1, const control_vector_t & u2) {
140 
141  return (x1-x2);
142 }
143 
144 template <class DIMENSIONS>
145 Eigen::VectorXd InterPhaseBase<DIMENSIONS>::evalConstraintsFctDerivatives(
146  const state_vector_t & x1, const state_vector_t & x2 ,
147  const control_vector_t &u1,const control_vector_t &u2) {
148 
149  Eigen::VectorXd in_vector(total_inputs_jacobian);
150 
151  in_vector.segment<s_size>(0) = x1;
152  in_vector.segment<c_size>(s_size) = u1;
153  in_vector.segment<s_size>(s_size+c_size)= x2;
154  in_vector.segment<c_size>(s_size+s_size+c_size) = u2;
155 
156  this->numDiff->df(in_vector,this->mJacobian);
157 
158  //returning ROW-wise the constraint as a vector
159  Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> temp(this->mJacobian);
160  return (Eigen::Map<Eigen::VectorXd>(temp.data(),size_of_jacobian));
161 }
162 
163 template <class DIMENSIONS>
164 void InterPhaseBase<DIMENSIONS>::fx(const Eigen::VectorXd & inputs, Eigen::VectorXd & outputs) {
165 
166  state_vector_t x1 = inputs.segment<s_size>(0);
167  control_vector_t u1 = inputs.segment<c_size>(s_size);
168  state_vector_t x2 = inputs.segment<s_size>(s_size+c_size);
169  control_vector_t u2 = inputs.segment<c_size>(s_size+s_size+c_size);
170 
171  outputs = evalConstraintsFct(x1,x2,u1,u2);
172 }
173 
174 template <class DIMENSIONS>
175 void InterPhaseBase<DIMENSIONS>::initialize_num_diff() {
176 
177  this->my_pointer = std::enable_shared_from_this<InterPhaseBase<DIMENSIONS>>::shared_from_this();
178  this->mJacobian.resize(complete_vector_size,total_inputs_jacobian);
179  this->numdifoperator = std::shared_ptr<FunctionOperator>(new FunctionOperator(total_inputs_jacobian, complete_vector_size , this->my_pointer));
180  this->numDiff = std::shared_ptr<Eigen::NumericalDiff<FunctionOperator> >( new Eigen::NumericalDiff<FunctionOperator>(*this->numdifoperator,std::numeric_limits<double>::epsilon()));
181 
182 }
183 
184 } // BaseClass
185 } // DirectTrajectoryOptimization
186 #endif /* _InterPhaseBase_HPP_ */
Base class for vector constraints and numdiff derivatives.