DirectTrajectoryOptimization  v0.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
shooting_constraint.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 SHOOTINGCONSTRAINT_HPP_
32 #define SHOOTINGCONSTRAINT_HPP_
33 
34 #include <Eigen/Dense>
37 #include <boost/numeric/odeint.hpp>
38 #include <dynamical_systems/tools/eigenIntegration.h>
39 
40 namespace DirectTrajectoryOptimization {
41 template <typename DIMENSIONS>
42 class DTOMultipleShooting;
43 
48 template<class DIMENSIONS>
50 
51 public:
52 
53  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
54 
55  typedef typename DIMENSIONS::state_vector_t state_vector_t;
56  typedef typename DIMENSIONS::control_vector_t control_vector_t;
57  typedef typename DIMENSIONS::state_vector_array_t state_vector_array_t;
58  typedef typename DIMENSIONS::control_vector_array_t control_vector_array_t;
59 
66  ShootingConstraint(const int & n_func, const int & n_vars, std::shared_ptr<DTOMultipleShooting<DIMENSIONS>> my_owner):
67  _dms_(my_owner),GenericConstraintsBase(n_func, n_vars) { }
68 
69  virtual Eigen::VectorXd Evalfx(const Eigen::VectorXd & in_vect) override;
70 
71  void forwardIntegrateForTimeRange(const state_vector_t & curr_state, const control_vector_t & curr_control,
72  const control_vector_t & next_control, const double & total_t, state_vector_t & integrated_state);
73 
74  void evalDefectGradient(const Eigen::VectorXd & inputspace, Eigen::MatrixXd &jacobianMatrix);
75 
76  Eigen::VectorXd evalDefectVector(const state_vector_array_t & y_trajectory,
77  const control_vector_array_t & u_trajectory, const Eigen::VectorXd & h_trajectory);
78 
79  ~ShootingConstraint() {}
80 
81  void initialize() {
82  this->initialize_num_diff();
83  _ms_defect_vector.resize(_dms_->num_defects);
84  }
85 
86  double ms_dt_ = 0.001;
88  Eigen::VectorXd _ms_defect_vector;
89 
90  std::shared_ptr<DTOMultipleShooting<DIMENSIONS> > _dms_;
91  std::shared_ptr<DynamicsBase<DIMENSIONS> > _local_dynamics_;
92 
93  //ToDo : Try with different steppers!
94  boost::numeric::odeint::runge_kutta4<state_vector_t, double,
95  state_vector_t, double,
96  boost::numeric::odeint::vector_space_algebra> ms_stepper_;
97 };
98 
99 template<class DIMENSIONS>
100 typename Eigen::VectorXd ShootingConstraint<DIMENSIONS>::Evalfx(const Eigen::VectorXd & in_vector) {
101 
102  state_vector_array_t y_trajectory;
103  control_vector_array_t u_trajectory;
104  Eigen::VectorXd h_trajectory;
105 
106  _dms_->getStateControlIncrementsTrajectoriesFromDecisionVariables(in_vector,y_trajectory,u_trajectory,h_trajectory);
107 
108  Eigen::VectorXd defect = evalDefectVector(y_trajectory,u_trajectory,h_trajectory);
109 
110  return(defect);
111 }
112 
113 template<class DIMENSIONS>
114 Eigen::VectorXd ShootingConstraint<DIMENSIONS>::evalDefectVector(const state_vector_array_t & y_trajectory,
115  const control_vector_array_t & u_trajectory , const Eigen::VectorXd & h_trajectory) {
116 
117  state_vector_t integrated_state;
118  _ms_defect_vector.setZero();
119 
120  int current_defect_index = 0;
121 
122  for (int k = 0; k < _dms_->n_inc ; k++) {
123 
124  _local_dynamics_ = _dms_->_dynamics[_dms_->node_to_phase_map[k]];
125 
126  forwardIntegrateForTimeRange(y_trajectory[k], u_trajectory[k],
127  u_trajectory[k+1], h_trajectory[k],integrated_state);
128 
129  state_vector_t defect = (y_trajectory[k+1] - integrated_state);
130 
131  _ms_defect_vector.segment<DIMENSIONS::kTotalStates>(current_defect_index) = defect;
132 
133  current_defect_index += DIMENSIONS::kTotalStates;
134  }
135 
136  return(_ms_defect_vector);
137 }
138 
139 template<class DIMENSIONS>
140 void ShootingConstraint<DIMENSIONS>::evalDefectGradient(const Eigen::VectorXd& input, Eigen::MatrixXd &defect_jacobian) {
141  // User can still override the function EVALFXJACOBIAN in this class and write an analytical version
142 
143  defect_jacobian = this->EvalFxJacobian(input);
144 }
145 
146 template<class DIMENSIONS>
147 void ShootingConstraint<DIMENSIONS>::forwardIntegrateForTimeRange(
148  const state_vector_t & curr_state, const control_vector_t & curr_control,
149  const control_vector_t & next_control, const double & total_t, state_vector_t & integrated_state) {
150 
151  // generate stepper and lambda for forward integration
152  integrated_state = curr_state;
153 
154  auto f = [=](const state_vector_t &x, state_vector_t &dxdt, const double t) {
155 
156  double t_percentage = t/total_t;
157  control_vector_t control =
158  next_control * t_percentage +
159  curr_control * (1-t_percentage);
160 
161  dxdt = _local_dynamics_->systemDynamics(x, control);
162  };
163 
164  // run the integration
165  boost::numeric::odeint::integrate_const(ms_stepper_, f, integrated_state,
166  0.0,total_t,ms_dt_);
167 }
168 
169 } // namespace DirectTrajectoryOptimization
170 #endif /* SHOOTINGCONSTRAINT_HPP_ */
void initialize_num_diff()
Overload this method to define the local pointers.
Definition: GenericConstraintsBase.hpp:151
double ms_dt_
Definition: shooting_constraint.hpp:86
virtual Eigen::VectorXd Evalfx(const Eigen::VectorXd &in_vect) override
To be overloaded by the derived class.
Definition: shooting_constraint.hpp:100
This class implements Direct Multiple Shooting.
Definition: multiple_shooting.hpp:44
Base Class to define vector of constraints and its derivatives.
Definition: GenericConstraintsBase.hpp:44
EIGEN_MAKE_ALIGNED_OPERATOR_NEW GenericConstraintsBase(int num_eq, int num_var)
Generic Constraint Constructor.
Definition: GenericConstraintsBase.hpp:55
Class handling the defects of a multiple shooting method.
Definition: shooting_constraint.hpp:49
Base class for vector constraints and numdiff derivatives.
ShootingConstraint(const int &n_func, const int &n_vars, std::shared_ptr< DTOMultipleShooting< DIMENSIONS >> my_owner)
Constructor of ShootingConstraint.
Definition: shooting_constraint.hpp:66
Class implementing multiple shooting for direct trajectory optimizaion.