DirectTrajectoryOptimization  v0.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
CostFunctionBolza.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 COSTFUNCTIONBOLZA_HPP_
32 #define COSTFUNCTIONBOLZA_HPP_
33 
34 #include <Eigen/Dense>
36 
37 namespace DirectTrajectoryOptimization {
38 namespace BaseClass {
39 
43 template <class DIMENSIONS>
44 class CostFunctionBolza : public CostFunctionBase<DIMENSIONS> {
45 public:
46 
47  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
48 
49  typedef typename DIMENSIONS::state_vector_t state_vector_t;
50  typedef typename DIMENSIONS::control_vector_t control_vector_t;
51  typedef typename DIMENSIONS::state_vector_array_t state_vector_array_t;
52  typedef typename DIMENSIONS::control_vector_array_t control_vector_array_t;
53  typedef typename DIMENSIONS::state_matrix_t state_matrix_t;
54 
56 
57  }
58 
59  virtual ~CostFunctionBolza() {}
60 
61  int getNumberOfDependentVariables(){
62 
63  // This applies for all the cost functions derived from CostFunction Bolza
64  int size_of_jacobian = this->state_size + this->control_size + 1;
65  return(size_of_jacobian);
66  }
67 
75  double IntermediateCost(const state_vector_t &x , const control_vector_t & u , const double & h);
76  void gradient_xuh(const state_vector_t &x , const control_vector_t & u , const double & h , Eigen::VectorXd & f0_gradient);
77  double evaluate();
78  Eigen::VectorXd evaluate_gradient();
79 
80 private :
81 
82  // phi can only depend on x and u
83  virtual double phi(const state_vector_t & x, const control_vector_t & u) = 0;
84  virtual state_vector_t phi_x(const state_vector_t &x, const control_vector_t & u) = 0;
85  virtual control_vector_t phi_u(const state_vector_t &x, const control_vector_t & u) = 0;
86  // There is no implementation of phi_h as it is always phi(x,u)
87  virtual double terminalCost(const state_vector_t &x_f) = 0;
88  virtual state_vector_t terminalCostDerivative(const state_vector_t & x) = 0;
89 };
90 
91 template <class DIMENSIONS>
93 
94  double total_trajectory_cost = 0;
95  int inc=0;
96  for (int i = 0 ; i < this->trajectory_size-1 ; i++ ) {
97  if(!(this->left_index.array()==i).any()) {
98  total_trajectory_cost+=IntermediateCost(this->x_trajectory[i],this->u_trajectory[i],this->h_trajectory(inc));
99  inc++;
100  }
101  }
102 
103  total_trajectory_cost = total_trajectory_cost/inc;
104 
105  total_trajectory_cost += this->terminalCost(this->x_trajectory.back());
106 
107  return(total_trajectory_cost);
108 }
109 
110 template <class DIMENSIONS>
111 Eigen::VectorXd CostFunctionBolza<DIMENSIONS>::evaluate_gradient() {
112 
113  this->cost_function_jacobian_size = (this->trajectory_size - 1 - this->left_index.size()) * (this->state_size + this->control_size) + this->h_trajectory.size() + this->state_size;
114  this->node_jacobian_size = this->state_size + this->control_size + 1;
115 
116  Eigen::VectorXd local(this->node_jacobian_size);
117  Eigen::VectorXd jacobian_vector(this->cost_function_jacobian_size);
118 
119  int j_index = 0;
120  int inc=0;
121  for (int k = 0 ; k < this->trajectory_size-1 ; k++ ) {
122 
123  if (!(this->left_index.array()==k).any()) {
124 
125  gradient_xuh(this->x_trajectory[k],this->u_trajectory[k],this->h_trajectory(inc),local);
126  jacobian_vector.segment(j_index,this->node_jacobian_size)=local;
127  j_index += this->node_jacobian_size;
128  inc++;
129  }
130  }
131  jacobian_vector = jacobian_vector/inc;
132 
133  jacobian_vector.segment(j_index,this->state_size) = this->terminalCostDerivative(this->x_trajectory.back());
134  //jacobian_vector.segment(j_index+this->state_size,this->control_size) = control_vector_t::Zero();
135 
136  return(jacobian_vector);
137 }
138 
139 template <class DIMENSIONS>
140 double CostFunctionBolza<DIMENSIONS>::IntermediateCost(const state_vector_t &x , const control_vector_t & u ,
141  const double & h) {
142 
143  //Intermediate cost is NOT necessarily the same as g(x,u)
144  double increment_cost = phi(x,u);
145  return(increment_cost);
146 }
147 
148 template <class DIMENSIONS>
149 void CostFunctionBolza<DIMENSIONS>::gradient_xuh(const state_vector_t &x , const control_vector_t & u ,
150  const double & h , Eigen::VectorXd & f0_gradient) {
151 
152  //f0 already has the right size
153  f0_gradient(0) = 0;
154  f0_gradient.segment(1,this->state_size) = phi_x(x,u);
155  f0_gradient.segment(1+this->state_size,this->control_size) = phi_u(x,u);
156 }
157 
158 } //BaseClass
159 } //DirectTrajectoryOptimization
160 
161 #endif /*COSTFUNCTIONBOLZA_HPP_*/
double IntermediateCost(const state_vector_t &x, const control_vector_t &u, const double &h)
Intermediate cost function.
Definition: CostFunctionBolza.hpp:140
Base class to derive cost functions.
Base class to derive cost function of the type J = sum_k( phi_k(x_k,u_k,k) ) + H(X_K) ...
Definition: CostFunctionBolza.hpp:44