33 #ifndef _GuardBase_HPP_
34 #define _GuardBase_HPP_
36 #include <Eigen/Dense>
40 namespace DirectTrajectoryOptimization{
43 template <
class DIMENSIONS>
44 class GuardBase :
public NumDiffDerivativesBase,
45 public std::enable_shared_from_this<GuardBase<DIMENSIONS> > {
49 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
51 typedef typename DIMENSIONS::state_vector_t state_vector_t;
52 typedef typename DIMENSIONS::control_vector_t control_vector_t;
53 static const int s_size =
static_cast<int>(DIMENSIONS::DimensionsSize::STATE_SIZE);
58 GuardBase(
const bool & no_guard) {
60 SetBounds(Eigen::VectorXd::Constant(1,-1e20),Eigen::VectorXd::Constant(1,1e20));
64 virtual ~GuardBase(){ };
66 int GetSize() {
return complete_vector_size;}
67 Eigen::VectorXd GetLowerBound() {
return guard_lb; }
68 Eigen::VectorXd GetUpperBound() {
return guard_ub; }
69 void SetSize(
const int & sog);
70 void SetBounds(
const Eigen::VectorXd & lb,
const Eigen::VectorXd & ub);
72 void initialize_num_diff();
73 void fx(
const Eigen::VectorXd & in, Eigen::VectorXd & out);
75 virtual Eigen::VectorXd evalConstraintsFct(
const state_vector_t& x1);
76 Eigen::VectorXd evalConstraintsFctDerivatives(
const state_vector_t & x1);
78 std::shared_ptr<GuardBase<DIMENSIONS> > my_pointer;
80 int complete_vector_size = 0;
81 int size_of_jacobian = 0;
82 int total_inputs_jacobian=0;
86 Eigen::VectorXd guard_lb;
87 Eigen::VectorXd guard_ub;
89 bool sog_ready_ =
false;
90 bool bounds_ready_ =
false;
94 template <
class DIMENSIONS>
95 void GuardBase<DIMENSIONS>::SetSize(
const int & sog) {
97 std::cout <<
"Guard size must be greater than 0" << std::endl;
98 std::exit(EXIT_FAILURE);
100 complete_vector_size = sog;
104 template <
class DIMENSIONS>
105 void GuardBase<DIMENSIONS>::SetBounds(
const Eigen::VectorXd & lb,
const Eigen::VectorXd & ub) {
106 if(!(lb.size() == complete_vector_size) || !(ub.size() == complete_vector_size)) {
107 std::cout <<
"Guard bounds size must be equal to SoG" << std::endl;
108 std::exit(EXIT_FAILURE);
112 bounds_ready_ =
true;
115 template <
class DIMENSIONS>
116 void GuardBase<DIMENSIONS>::initialize() {
118 if(!sog_ready_ || !bounds_ready_) {
119 std::cout <<
"guard is not ready!" << std::endl;
120 std::exit(EXIT_FAILURE);
124 total_inputs_jacobian = s_size;
127 size_of_jacobian = complete_vector_size*total_inputs_jacobian;
129 initialize_num_diff();
132 template <
class DIMENSIONS>
133 Eigen::VectorXd GuardBase<DIMENSIONS>::evalConstraintsFct(
const state_vector_t& x1) {
136 return (Eigen::VectorXd::Constant(1,0.0));
139 template <
class DIMENSIONS>
140 Eigen::VectorXd GuardBase<DIMENSIONS>::evalConstraintsFctDerivatives(
const state_vector_t & x1) {
142 this->numDiff->df(x1,this->mJacobian);
146 Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> temp(this->mJacobian);
147 return (Eigen::Map<Eigen::VectorXd>(temp.data(),size_of_jacobian));
150 template <
class DIMENSIONS>
151 void GuardBase<DIMENSIONS>::fx(
const Eigen::VectorXd & inputs, Eigen::VectorXd & outputs) {
152 outputs = evalConstraintsFct(inputs);
155 template <
class DIMENSIONS>
156 void GuardBase<DIMENSIONS>::initialize_num_diff() {
158 this->my_pointer = std::enable_shared_from_this<GuardBase<DIMENSIONS>>::shared_from_this();
159 this->mJacobian.resize(complete_vector_size,total_inputs_jacobian);
160 this->numdifoperator = std::shared_ptr<FunctionOperator>(
new FunctionOperator(total_inputs_jacobian, complete_vector_size , this->my_pointer));
161 this->numDiff = std::shared_ptr<Eigen::NumericalDiff<FunctionOperator> >(
new Eigen::NumericalDiff<FunctionOperator>(*this->numdifoperator,std::numeric_limits<double>::epsilon()));
164 template <
class DIMENSIONS>
165 const int GuardBase<DIMENSIONS>::s_size;
Base class for vector constraints and numdiff derivatives.