31 #ifndef COSTFUNCTIONBOLZA_HPP_
32 #define COSTFUNCTIONBOLZA_HPP_
34 #include <Eigen/Dense>
37 namespace DirectTrajectoryOptimization {
43 template <
class DIMENSIONS>
47 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
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;
61 int getNumberOfDependentVariables(){
64 int size_of_jacobian = this->state_size + this->control_size + 1;
65 return(size_of_jacobian);
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);
78 Eigen::VectorXd evaluate_gradient();
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;
87 virtual double terminalCost(
const state_vector_t &x_f) = 0;
88 virtual state_vector_t terminalCostDerivative(
const state_vector_t & x) = 0;
91 template <
class DIMENSIONS>
94 double total_trajectory_cost = 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));
103 total_trajectory_cost = total_trajectory_cost/inc;
105 total_trajectory_cost += this->terminalCost(this->x_trajectory.back());
107 return(total_trajectory_cost);
110 template <
class DIMENSIONS>
111 Eigen::VectorXd CostFunctionBolza<DIMENSIONS>::evaluate_gradient() {
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;
116 Eigen::VectorXd local(this->node_jacobian_size);
117 Eigen::VectorXd jacobian_vector(this->cost_function_jacobian_size);
121 for (
int k = 0 ; k < this->trajectory_size-1 ; k++ ) {
123 if (!(this->left_index.array()==k).any()) {
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;
131 jacobian_vector = jacobian_vector/inc;
133 jacobian_vector.segment(j_index,this->state_size) = this->terminalCostDerivative(this->x_trajectory.back());
136 return(jacobian_vector);
139 template <
class DIMENSIONS>
144 double increment_cost = phi(x,u);
145 return(increment_cost);
148 template <
class DIMENSIONS>
150 const double & h , Eigen::VectorXd & f0_gradient) {
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);
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