30 #ifndef INCLUDE_METHOD_INTERFACES_BASE_METHOD_INTERFACE_HPP_
31 #define INCLUDE_METHOD_INTERFACES_BASE_METHOD_INTERFACE_HPP_
42 #include <method_interfaces/nonlinear_program/nonlinear_program.hpp>
44 #include <dynamical_systems/base/DynamicsBase.hpp>
45 #include <dynamical_systems/base/DerivativesBaseDS.hpp>
48 #include <optimization/constraints/InterPhaseBaseConstraint.hpp>
49 #include <optimization/constraints/GuardBase.hpp>
51 namespace DirectTrajectoryOptimization {
59 template<
typename DIMENSIONS>
63 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
65 typedef typename DIMENSIONS::state_vector_t state_vector_t;
66 typedef typename DIMENSIONS::control_vector_t control_vector_t;
67 typedef typename DIMENSIONS::state_vector_array_t state_vector_array_t;
68 typedef typename DIMENSIONS::control_vector_array_t control_vector_array_t;
80 std::vector<std::shared_ptr<DynamicsBase<DIMENSIONS> > > dynamics,
81 std::vector<std::shared_ptr<DerivativesBaseDS<DIMENSIONS> > > derivatives,
82 std::shared_ptr<BaseClass::CostFunctionBase<DIMENSIONS> > costFunction,
84 Eigen::Vector2d duration,
int number_of_nodes,
bool methodVerbose);
88 virtual void SetDircolMethod(
const int &dcm) {}
89 void SetInitialState(
const state_vector_t & y0);
90 void SetFinalState(
const state_vector_t & yF);
92 void SetInitialControl(
const control_vector_t & u0);
93 void SetFinalControl(
const control_vector_t & uf);
95 void SetFinalVelocityZero(
const Eigen::VectorXi & indexes) {
96 vel_state_to_zero.resize(indexes.rows());
97 vel_state_to_zero = indexes;
98 _finalVelocityZero =
true;
101 void SetEvenTimeIncrements(
bool flag) {
102 _evenTimeIncr = flag;
104 void SetVerbosity(
bool verbose) {
108 void SetGuardFunction(std::vector<
double (*)(state_vector_t&)> usrFct) {
110 if (usrFct.size() != num_phases - 1) {
111 throw std::invalid_argument(
"Vector of guard functions are of the wrong size.");
115 _guardVector = usrFct;
116 _usingGuardFunction =
true;
119 void SetInterPhaseFunctions(std::vector<std::shared_ptr<BaseClass::GuardBase<DIMENSIONS> > > gconst,
120 std::vector<std::shared_ptr<BaseClass::InterPhaseBase<DIMENSIONS>>> intph) {
122 if (gconst.size() != num_phases - 1) {
123 throw std::invalid_argument(
"Vector of guard functions are of the wrong size.");
126 if (intph.size() != num_phases - 1) {
127 throw std::invalid_argument(
"Vector of interPhase functions are of the wrong size.");
131 _interphase_constraint = intph;
136 void SetPercentageNodesPerDynamics(Eigen::VectorXd nodeSplit);
137 void SetNodesPerPhase(
const Eigen::VectorXi & nodes);
138 void SetStateBounds(
const state_vector_t & lb,
const state_vector_t & ub);
139 void SetStateBounds(
const state_vector_array_t & lb,
const state_vector_array_t & ub);
140 void SetControlBounds(
const control_vector_t & lb,
const control_vector_t & ub);
141 void SetControlBounds(
const control_vector_array_t & lb,
const control_vector_array_t & ub);
142 void SetFreeFinalState(
int state_number);
145 void InitializeNLPvector(Eigen::VectorXd & x,
146 const state_vector_array_t & state_initial,
147 const control_vector_array_t & control_initial,
148 const Eigen::VectorXd & h_initial);
150 void setTimeIncrementsBounds(
const double & h_min,
const double & h_max);
153 inline int getNumDefects() {
158 inline int getNumStates() {
161 inline int getNumActions() {
164 inline int getNumIncrements() {
170 void getTrajectoryIndexes(Eigen::VectorXi &hs, Eigen::MatrixXi &ys,
171 Eigen::MatrixXi &us);
180 void UpdateDecisionVariables(
const double* x);
181 void evalObjective(
double *f);
182 void evalConstraintFct(
double* g,
const int& m);
184 void evalSparseJacobianCost(
double* val);
185 virtual void evalSparseJacobianConstraint(
double* val) = 0;
190 void SetSizeOfVariables();
191 void SetSizeOfConstraints();
192 void SetTOConstraintsBounds();
193 void SetTOVariablesBounds();
194 void SetupJacobianVariables();
195 virtual void Method_specific_initialization() {};
198 void setNLPnnzConstraintJacobian();
199 void setSparsityJacobianCost();
200 void setSparsityJacobianConstraint();
203 int getNumNonZeroUserJacobian();
204 int getNumNonZeroGuardFunctionJacobian();
205 int getNumNonZeroInterphaseJacobian();
208 void setNLPnnzCostFunctionJacobian();
211 bool ValidateDuration();
212 bool ValidateStateControlBounds();
215 virtual void evaluateDefects(Eigen::VectorXd & defect_vector) = 0;
216 void evaluateParametricConstraints(Eigen::VectorXd & parametric_vector);
217 void evaluateUserConstraints(Eigen::VectorXd & constraints_vector);
218 void evaluateGuardConstraints(Eigen::VectorXd & constraints_vector);
219 void evaluateInterphaseConstraints(Eigen::VectorXd & constraints_vector);
224 void getStateControlIncrementsTrajectoriesFromDecisionVariables(
225 const Eigen::Map<Eigen::VectorXd> & x_local,
226 state_vector_array_t & y_trajectory,
227 control_vector_array_t & u_trajectory,
228 Eigen::VectorXd & h_trajectory);
229 void getStateControlIncrementsTrajectoriesFromDecisionVariables(
230 const Eigen::VectorXd & x_local,
231 state_vector_array_t & y_trajectory,
232 control_vector_array_t & u_trajectory,
233 Eigen::VectorXd & h_trajectory);
235 void UpdateDTOTrajectoriesFromDecisionVariables(
236 const Eigen::Map<Eigen::VectorXd> & x_local);
238 void getCompleteSolution(state_vector_array_t & yTraj,
239 state_vector_array_t & ydotTraj,
240 control_vector_array_t & uTraj,
241 Eigen::VectorXd & hTraj,
242 std::vector<int> & phaseId)
const;
244 void SetDecisionVariablesFromStateControlIncrementsTrajectories(
245 Eigen::VectorXd & x_vector,
246 const state_vector_array_t & y_sol,
247 const control_vector_array_t & u_sol,
248 const Eigen::VectorXd & h_sol);
250 void getProtectedVariables(state_vector_array_t & y_t , state_vector_array_t & yd_t,
251 control_vector_array_t & u_t, Eigen::VectorXd & h_t) {
253 yd_t = _ydot_trajectory;
262 int _minNodesPerPhase = 2;
263 double min_traj_time = 0.01;
264 double minimum_increment_size = 0.005;
265 double maximum_increment_size = 0.1;
266 bool _initialControlZero =
false;
267 bool _finalVelocityZero =
false;
268 bool _evenTimeIncr =
false;
269 bool _nodeSplitSet =
false;
270 bool _nodeDistribution =
false;
271 bool _usingGuardFunction =
false;
272 bool _multiphaseproblem =
false;
274 Eigen::Vector2d _trajTimeRange;
275 Eigen::VectorXd _nodeSplitPercentage;
277 const int _numStates;
278 const int _numControls;
285 int num_parametric_constraints = 0;
286 int num_user_constraints = 0;
288 int num_interphase_constraints = 0;
290 int nnz_user_constraints = 0;
292 std::vector<int> num_user_constraints_per_node_in_phase;
293 std::vector<int> num_nonzero_jacobian_user_per_node_in_phase;
295 std::vector<int> size_interphase_constraint;
296 std::vector<int> size_guard;
299 state_vector_t y_0, y_f;
300 state_vector_array_t y_lb, y_ub;
301 control_vector_t u_0, u_f;
302 control_vector_array_t u_lb, u_ub;
305 Eigen::VectorXi vel_state_to_zero;
309 Eigen::VectorXi _flagInitialState;
310 Eigen::VectorXi _flagFinalState;
312 Eigen::VectorXi _flagInitialControl;
313 Eigen::VectorXi _flagFinalControl;
316 Eigen::VectorXi h_inds;
317 Eigen::MatrixXi y_inds;
318 Eigen::MatrixXi u_inds;
321 Eigen::VectorXi leftphase_nodes;
322 Eigen::VectorXi rightphase_nodes;
325 Eigen::VectorXd defects_ub;
326 Eigen::VectorXd defects_lb;
329 Eigen::VectorXd parametricConstraints_lb;
330 Eigen::VectorXd parametricConstraints_ub;
333 Eigen::VectorXd userConstraints_lb;
334 Eigen::VectorXd userConstraints_ub;
337 Eigen::VectorXd guard_lb;
338 Eigen::VectorXd guard_ub;
341 Eigen::VectorXd interphase_lb;
342 Eigen::VectorXd interphase_ub;
345 Eigen::VectorXd defect_out_;
346 Eigen::VectorXd parametric_out_;
347 Eigen::VectorXd user_constraints_out_;
350 Eigen::VectorXd guard_vector_;
351 Eigen::VectorXd interface_vector_;
355 Eigen::VectorXd increments_solution;
356 Eigen::VectorXd time_solution;
357 Eigen::MatrixXd states_solution;
358 Eigen::MatrixXd controls_solution;
359 bool problem_is_infeasible =
false;
366 std::vector<int> node_to_phase_map;
367 std::vector<int> number_nodes_phase;
368 std::vector<int> user_non_zero_G_phase;
369 std::vector<double (*)(state_vector_t &)> _guardVector;
370 std::vector<void (*)(state_vector_t &x, state_vector_t & J) > _guardVectorDerivative;
374 state_vector_array_t _y_trajectory;
375 control_vector_array_t _u_trajectory;
376 Eigen::VectorXd _h_trajectory;
377 state_vector_array_t _ydot_trajectory;
381 std::vector<std::shared_ptr<DynamicsBase<DIMENSIONS> > > _dynamics;
382 std::vector<std::shared_ptr<DerivativesBaseDS<DIMENSIONS> > > _derivatives;
383 std::shared_ptr<BaseClass::CostFunctionBase<DIMENSIONS> > _costFunction;
384 std::vector<std::shared_ptr<BaseClass::ConstraintsBase<DIMENSIONS> > > _constraints;
385 std::vector<std::shared_ptr<BaseClass::InterPhaseBase<DIMENSIONS> > > _interphase_constraint;
386 std::vector<std::shared_ptr<BaseClass::GuardBase<DIMENSIONS> > > _guards;
396 #include <method_interfaces/bmi_implementation.hpp>
This class serves as a base interface for inheriting classes.
Definition: base_method_interface.hpp:60
This class contains all the information related to the NLP.
Definition: nonlinear_program.hpp:44
Base class for constraints.
Base class for TO constraints.
Definition: ConstraintsBase.hpp:46
Base class to derive cost functions.
DTOMethodInterface(std::vector< std::shared_ptr< DynamicsBase< DIMENSIONS > > > dynamics, std::vector< std::shared_ptr< DerivativesBaseDS< DIMENSIONS > > > derivatives, std::shared_ptr< BaseClass::CostFunctionBase< DIMENSIONS > > costFunction, std::vector< std::shared_ptr< BaseClass::ConstraintsBase< DIMENSIONS > > > constraints, Eigen::Vector2d duration, int number_of_nodes, bool methodVerbose)
Construct an instance of the DTOMethodInterface class.
Definition: bmi_implementation.hpp:35