33 #ifndef _INTERPHASEBASE_FREECONTROL_HPP_
34 #define _INTERPHASEBASE_FREECONTROL_HPP_
39 namespace DirectTrajectoryOptimization{
42 template <
class DIMENSIONS>
43 class InterPhaseBaseFreeControl :
public NumDiffDerivativesBase,
44 public std::enable_shared_from_this<InterPhaseBaseFreeControl<DIMENSIONS> > {
48 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
50 typedef typename DIMENSIONS::state_vector_t state_vector_t;
51 typedef typename DIMENSIONS::control_vector_t control_vector_t;
53 int s_size =
static_cast<int>(DIMENSIONS::DimensionsSize::STATE_SIZE);
54 int c_size =
static_cast<int>(DIMENSIONS::DimensionsSize::CONTROL_SIZE);
56 Eigen::VectorXd ipc_lb;
57 Eigen::VectorXd ipc_ub;
58 int complete_vector_size = 0;
59 int size_of_jacobian = 0;
60 int total_inputs_jacobian=0;
62 std::shared_ptr<InterPhaseBaseFreeControl<DIMENSIONS> > my_pointer;
65 InterPhaseBaseFreeControl(
const int & n_constraints ,
const Eigen::VectorXd & lb ,
const Eigen::VectorXd & ub):complete_vector_size(n_constraints),ipc_lb(lb),ipc_ub(ub){
67 if(ipc_ub.size()!=ipc_lb.size() || ipc_ub.size() != n_constraints) {
68 std::cout <<
"Wrong size of InterPhase constraints! " << std::endl;
69 std::exit(EXIT_FAILURE);
71 complete_vector_size = n_constraints;
74 InterPhaseBaseFreeControl() {
77 complete_vector_size = s_size;
78 ipc_lb = Eigen::VectorXd::Zero(complete_vector_size);
79 ipc_ub = Eigen::VectorXd::Zero(complete_vector_size);
82 int GetSize() {
return complete_vector_size;}
83 int GetNnzJacobian() {
return size_of_jacobian;}
84 Eigen::VectorXd getLowerBound(){
return ipc_lb;}
85 Eigen::VectorXd getUpperBound(){
return ipc_ub;}
87 virtual ~InterPhaseBaseFreeControl(){ };
90 void initialize_num_diff();
91 void fx(
const Eigen::VectorXd & in, Eigen::VectorXd & out);
96 virtual Eigen::VectorXd evalConstraintsFct(
const state_vector_t& x1,
const state_vector_t & x2,
97 const control_vector_t & u1,
const control_vector_t & u2);
99 Eigen::VectorXd evalConstraintsFctDerivatives(
const state_vector_t & x1,
const state_vector_t & x2,
100 const control_vector_t & u1,
const control_vector_t & u2);
103 Eigen::VectorXd temp_jacobian_vector;
106 template <
class DIMENSIONS>
107 void InterPhaseBaseFreeControl<DIMENSIONS>::initialize() {
110 total_inputs_jacobian = 2*s_size + 2*c_size;
111 size_of_jacobian = complete_vector_size*total_inputs_jacobian;
113 temp_jacobian_vector.resize(size_of_jacobian);
115 initialize_num_diff();
118 template <
class DIMENSIONS>
119 Eigen::VectorXd InterPhaseBaseFreeControl<DIMENSIONS>::evalConstraintsFct(
const state_vector_t& x1,
const state_vector_t & x2,
120 const control_vector_t & u1,
const control_vector_t & u2) {
127 template <
class DIMENSIONS>
128 Eigen::VectorXd InterPhaseBaseFreeControl<DIMENSIONS>::evalConstraintsFctDerivatives(
129 const state_vector_t & x1,
const state_vector_t & x2 ,
130 const control_vector_t &u1,
const control_vector_t &u2) {
132 Eigen::VectorXd in_vector(total_inputs_jacobian);
134 in_vector.segment(0,s_size) = x1;
135 in_vector.segment(s_size,c_size) = u1;
136 in_vector.segment(s_size+c_size,s_size) = x2;
137 in_vector.segment(2*s_size+c_size,c_size) = u2;
139 this->numDiff->df(in_vector,this->mJacobian);
141 Eigen::MatrixXd temp = this->mJacobian.transpose();
144 return (Eigen::Map<Eigen::VectorXd>(temp.data(),size_of_jacobian));
147 template <
class DIMENSIONS>
148 void InterPhaseBaseFreeControl<DIMENSIONS>::fx(
const Eigen::VectorXd & inputs, Eigen::VectorXd & outputs) {
150 state_vector_t x1 = inputs.segment(0,s_size);
151 control_vector_t u1 = inputs.segment(s_size,c_size);
152 state_vector_t x2 = inputs.segment(s_size+c_size,s_size);
153 control_vector_t u2 = inputs.segment(2*s_size+c_size,c_size);
155 outputs = evalConstraintsFct(x1,x2,u1,u2);
158 template <
class DIMENSIONS>
159 void InterPhaseBaseFreeControl<DIMENSIONS>::initialize_num_diff() {
161 this->my_pointer = std::enable_shared_from_this<InterPhaseBaseFreeControl<DIMENSIONS>>::shared_from_this();
163 mJacobian.resize(complete_vector_size,total_inputs_jacobian);
165 this->numdifoperator = std::shared_ptr<FunctionOperator>(
new FunctionOperator(total_inputs_jacobian, complete_vector_size , this->my_pointer));
167 this->numDiff = std::shared_ptr<Eigen::NumericalDiff<FunctionOperator> >(
new Eigen::NumericalDiff<FunctionOperator>(*this->numdifoperator,std::sqrt(std::numeric_limits<double>::epsilon())));
Base class for vector constraints and numdiff derivatives.