DirectTrajectoryOptimization  v0.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
direct_trajectory_optimization_problem.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 
82 // system
83 #include <Eigen/Core>
84 #include <unistd.h>
85 #include <vector>
86 #include <memory>
87 // dynamical_systems
88 #include <dynamical_systems/base/DynamicsBase.hpp>
89 #include <dynamical_systems/base/DerivativesBaseDS.hpp>
94 
95 #ifdef COMPILE_IPOPT
97 #endif
98 
99 #ifdef COMPILE_SNOPT
101 #endif
102 
103 #ifndef INCLUDE_DIRECT_TRAJECTORY_OPTIMIZATION_PROBLEM_HPP_
104 #define INCLUDE_DIRECT_TRAJECTORY_OPTIMIZATION_PROBLEM_HPP_
105 
109 namespace DirectTrajectoryOptimization {
110 
115 namespace Methods {
117 typedef enum Method {
120 } method_t;
121 }
122 
126 namespace Solvers {
128 typedef enum Solver {
131 } solver_t;
132 }
133 
139 template <typename DIMENSIONS>
141 
142 public:
143  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
144 
145  typedef typename DIMENSIONS::state_vector_t state_vector_t;
146  typedef typename DIMENSIONS::control_vector_t control_vector_t;
147  typedef typename DIMENSIONS::state_vector_array_t state_vector_array_t;
148  typedef typename DIMENSIONS::control_vector_array_t control_vector_array_t;
149 
165  static std::unique_ptr<DirectTrajectoryOptimizationProblem<DIMENSIONS> > CreateWithMultipleDynamics(
166  std::vector<std::shared_ptr<DynamicsBase<DIMENSIONS> > > dynamics,
167  std::vector<std::shared_ptr<DerivativesBaseDS<DIMENSIONS> > > derivatives,
168  std::shared_ptr<BaseClass::CostFunctionBase<DIMENSIONS> > costFunction,
169  std::vector<std::shared_ptr<BaseClass::ConstraintsBase<DIMENSIONS> > > constraints,
170  Eigen::Vector2d duration, int numberOfNodes,
171  Solvers::solver_t solverType, Methods::method_t methodType,
172  bool solverVerbose, bool methodVerbose, bool feasiblePointOnly=false) {
173 
174  // generate the class instance
175  std::unique_ptr<DirectTrajectoryOptimizationProblem<DIMENSIONS> > problem( new DirectTrajectoryOptimizationProblem<DIMENSIONS>() );
176 
177  // generate the method interface
178  switch(methodType) {
179 
181  problem->_method = std::shared_ptr<DTOMethodInterface<DIMENSIONS> >( new DTODirectTranscription<DIMENSIONS>(
182  dynamics, derivatives, costFunction, constraints,
183  duration, numberOfNodes, methodVerbose)); // ToDo add back dircol selection
184  break;
185 
187 
188  problem->_method = std::shared_ptr<DTOMethodInterface<DIMENSIONS> >( new DTOMultipleShooting<DIMENSIONS>(
189  dynamics, derivatives, costFunction, constraints,
190  duration, numberOfNodes, methodVerbose));
191  break;
192 
193  default:
194  std::cout << "METHOD NOT Implemented" << std::endl;
195  exit(EXIT_FAILURE);
196  }
197 
198  // generate the solver interface
199  switch(solverType) {
200 
201 #ifdef COMPILE_SNOPT
203  problem->_solver = std::shared_ptr<DTOSolverInterface<DIMENSIONS> > (new DTOSnoptInterface<DIMENSIONS>(
204  problem->_method, solverVerbose,feasiblePointOnly));
205  break;
206 #endif
207 
208 #ifdef COMPILE_IPOPT
210  problem->_solver = std::shared_ptr<DTOSolverInterface<DIMENSIONS> > (new DTOIpoptInterface<DIMENSIONS>(
211  problem->_method, solverVerbose));
212  break;
213 #endif
214 
215  default:
216  std::cout << "SOLVER NOT Available" << std::endl;
217  exit(EXIT_FAILURE);
218  }
219 
220  return problem;
221  }
222 
238  static std::unique_ptr<DirectTrajectoryOptimizationProblem<DIMENSIONS> > Create(
239  std::shared_ptr<DynamicsBase<DIMENSIONS> > dynamics,
240  std::shared_ptr<DerivativesBaseDS<DIMENSIONS> > derivatives,
241  std::shared_ptr<BaseClass::CostFunctionBase<DIMENSIONS> > costFunction,
242  std::shared_ptr<BaseClass::ConstraintsBase<DIMENSIONS> > constraints,
243  Eigen::Vector2d duration, int numberOfNodes,
244  Solvers::solver_t solverType, Methods::method_t methodType,
245  bool solverVerbose, bool methodVerbose, bool feasiblePointOnly=false) {
246  std::vector<std::shared_ptr<DynamicsBase<DIMENSIONS> > > dynamicsVector;
247  std::vector<std::shared_ptr<DerivativesBaseDS<DIMENSIONS> > > derivativesVector;
248  std::vector<std::shared_ptr<BaseClass::ConstraintsBase<DIMENSIONS> > > constraintsVector;
249 
250  dynamicsVector.push_back(dynamics);
251  derivativesVector.push_back(derivatives);
252  constraintsVector.push_back(constraints);
253 
254  return CreateWithMultipleDynamics(dynamicsVector, derivativesVector, costFunction, constraintsVector, duration,
255  numberOfNodes, solverType, methodType, solverVerbose, methodVerbose,feasiblePointOnly);
256  }
257 
262 
271  const std::string & problemName,
272  const std::string & outputFile,
273  const std::string & paramFile /*= "" */,
274  const bool &computeJacobian) {
275  _solver->InitializeSolver(problemName, outputFile, paramFile, computeJacobian);
276  }
277 
278  void InitializeNLPvector(Eigen::VectorXd & x,
279  const state_vector_array_t & state_initial,
280  const control_vector_array_t & control_initial,
281  const Eigen::VectorXd & h_initial) {
282 
283  _method->InitializeNLPvector(x, state_initial, control_initial, h_initial);
284  }
285 
286 
290  void SetInitialState(const state_vector_t & state){
291  _method->SetInitialState(state);
292  }
293 
297  void SetFinalState(const state_vector_t & state) {
298  _method->SetFinalState(state);
299  }
300 
304  void SetFinalVelocityZero(const Eigen::VectorXi & vel_indexes)
305  {
306  _method->SetFinalVelocityZero(vel_indexes);
307  }
308 
312  void SetInitialControl(const control_vector_t & control) {
313  _method->SetInitialControl(control);
314  }
315 
319  void SetFinalControl(const control_vector_t & control) {
320  _method->SetFinalControl(control);
321  }
322 
326  void SetEvenTimeIncrements(const bool & opt) {
327  _method->SetEvenTimeIncrements(opt);
328  }
329 
334  void SetPercentageNodesPerDynamics(Eigen::VectorXd nodeSplit) {
335  _method->SetPercentageNodesPerDynamics(nodeSplit);
336  }
337 
342  void SetGuardFunction(std::vector<double (*)(state_vector_t&)> fcnVector) {
343  _method->SetGuardFunction(fcnVector);
344  }
345 
350  void Solve(bool warminit = false) {
351  _solver->Solve(warminit);
352  }
353 
358  void SetVerbosity(bool verbose) {
359  _solver->SetVerbosity(verbose);
360  _method->SetVerbosity(verbose);
361  }
362 
370  state_vector_array_t &yTraj,
371  control_vector_array_t &uTraj,
372  Eigen::VectorXd &hTraj) {
373  _solver->GetDTOSolution(yTraj, uTraj, hTraj);
374  }
375 
384  state_vector_array_t &yTraj,
385  control_vector_array_t &uTraj,
386  Eigen::VectorXd &hTraj,
387  std::vector<int> &phaseId){
388  _solver->GetDTOSolution(yTraj, uTraj, hTraj, phaseId);
389  }
390 
391  void GetDTOSolution(
392  state_vector_array_t &yTraj,
393  state_vector_array_t &ydotTraj,
394  control_vector_array_t &uTraj,
395  Eigen::VectorXd & hTraj,
396  std::vector<int> &phaseId) {
397 
398  _solver->GetDTOSolution(yTraj, ydotTraj, uTraj, hTraj,phaseId);
399 
400  }
401 
406  void GetFVector(Eigen::VectorXd &fVec) {
407  _solver->GetFVector(fVec);
408  }
409 
414  return(_method->ChangeNumberOfpoints());
415  }
416 
422  void SetStateBounds(const state_vector_t & lb, const state_vector_t & ub) {
423  _method->SetStateBounds(lb,ub);
424  }
425 
431  void SetStateBounds(const state_vector_array_t & lb, const state_vector_array_t & ub){
432  _method->SetStateBounds(lb,ub);
433  }
434 
440  void SetControlBounds(const control_vector_t & lb, const control_vector_t & ub) {
441  _method->SetControlBounds(lb,ub);
442  }
443 
449  void SetControlBounds(const control_vector_array_t & lb, const control_vector_array_t & ub) {
450  _method->SetControlBounds(lb,ub);
451  }
452 
457  void SetFreeFinalState(int state_number) {
458  _method->SetFreeFinalState(state_number);
459  }
460 
466  void SetTimeIncrementsBounds(const double & h_min, const double & h_max) {
467  _method->setTimeIncrementsBounds(h_min,h_max);
468  }
469 
474  void SetDircolMethod(const int & dcm)
475  {
476  _method->SetDircolMethod(dcm);
477  }
478 
479  std::shared_ptr<DTOMethodInterface<DIMENSIONS> > _method;
480  std::shared_ptr<DTOSolverInterface<DIMENSIONS> > _solver;
483 private:
484 
489 };
490 
491 } // namespace DirectTrajectoryOptimization
492 #endif /* INCLUDE_DIRECT_TRAJECTORY_OPTIMIZATION_PROBLEM_HPP_ */
This class implements Direct transcription.
Definition: direct_transcription.hpp:55
void SetControlBounds(const control_vector_array_t &lb, const control_vector_array_t &ub)
Set trajectory of control bounds.
Definition: direct_trajectory_optimization_problem.hpp:449
void GetDTOSolution(state_vector_array_t &yTraj, control_vector_array_t &uTraj, Eigen::VectorXd &hTraj)
Get state control and time solution.
Definition: direct_trajectory_optimization_problem.hpp:369
std::shared_ptr< DTOSolverInterface< DIMENSIONS > > _solver
Definition: direct_trajectory_optimization_problem.hpp:480
void GetFVector(Eigen::VectorXd &fVec)
Get final value of constraint vector.
Definition: direct_trajectory_optimization_problem.hpp:406
void SetFinalVelocityZero(const Eigen::VectorXi &vel_indexes)
Set final velocity zero as hard constraint.
Definition: direct_trajectory_optimization_problem.hpp:304
~DirectTrajectoryOptimizationProblem()
Destruct the given DTOSolver instance.
Definition: direct_trajectory_optimization_problem.hpp:261
std::shared_ptr< DTOMethodInterface< DIMENSIONS > > _method
Definition: direct_trajectory_optimization_problem.hpp:479
Method
Definition: direct_trajectory_optimization_problem.hpp:117
Definition: direct_trajectory_optimization_problem.hpp:129
void SetDircolMethod(const int &dcm)
SetDircolMethod.
Definition: direct_trajectory_optimization_problem.hpp:474
Base class for NLP Solvers interface.
Definition: direct_trajectory_optimization_problem.hpp:118
void Solve(bool warminit=false)
Solves the DTO problem.
Definition: direct_trajectory_optimization_problem.hpp:350
Class interfacing Ipopt NLP with a DirecTrajectoryOptimization problem.
void SetStateBounds(const state_vector_t &lb, const state_vector_t &ub)
Set constant state trajectory bounds.
Definition: direct_trajectory_optimization_problem.hpp:422
enum DirectTrajectoryOptimization::Solvers::Solver solver_t
void SetInitialControl(const control_vector_t &control)
Set initial control.
Definition: direct_trajectory_optimization_problem.hpp:312
void SetStateBounds(const state_vector_array_t &lb, const state_vector_array_t &ub)
Set trajectory of state bounds.
Definition: direct_trajectory_optimization_problem.hpp:431
Solver
Definition: direct_trajectory_optimization_problem.hpp:128
Base class for TO constraints.
Definition: ConstraintsBase.hpp:46
Class interfacing SNOPT NLP with DTO Problem.
Definition: snopt.hpp:49
This class implements Direct Multiple Shooting.
Definition: multiple_shooting.hpp:44
void SetControlBounds(const control_vector_t &lb, const control_vector_t &ub)
Set constant control trajectory bounds.
Definition: direct_trajectory_optimization_problem.hpp:440
void SetEvenTimeIncrements(const bool &opt)
Set even time increments as hard constraint.
Definition: direct_trajectory_optimization_problem.hpp:326
void SetTimeIncrementsBounds(const double &h_min, const double &h_max)
Set bounds for the time increments between nodes.
Definition: direct_trajectory_optimization_problem.hpp:466
This class allows users to solve direct trajectory optimization problems. using different solvers (ip...
Definition: direct_trajectory_optimization_problem.hpp:140
void SetFinalControl(const control_vector_t &control)
Set final control.
Definition: direct_trajectory_optimization_problem.hpp:319
static std::unique_ptr< DirectTrajectoryOptimizationProblem< DIMENSIONS > > CreateWithMultipleDynamics(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 numberOfNodes, Solvers::solver_t solverType, Methods::method_t methodType, bool solverVerbose, bool methodVerbose, bool feasiblePointOnly=false)
Create a new DTOSolver instance.
Definition: direct_trajectory_optimization_problem.hpp:165
Direct transcription class for trajectory optimization.
Class interfacing SNOPT with a DTO Problem.
static std::unique_ptr< DirectTrajectoryOptimizationProblem< DIMENSIONS > > Create(std::shared_ptr< DynamicsBase< DIMENSIONS > > dynamics, std::shared_ptr< DerivativesBaseDS< DIMENSIONS > > derivatives, std::shared_ptr< BaseClass::CostFunctionBase< DIMENSIONS > > costFunction, std::shared_ptr< BaseClass::ConstraintsBase< DIMENSIONS > > constraints, Eigen::Vector2d duration, int numberOfNodes, Solvers::solver_t solverType, Methods::method_t methodType, bool solverVerbose, bool methodVerbose, bool feasiblePointOnly=false)
Create a new DTOSolver instance for single phase problem.
Definition: direct_trajectory_optimization_problem.hpp:238
void SetInitialState(const state_vector_t &state)
Set initial state as hard constraint.
Definition: direct_trajectory_optimization_problem.hpp:290
void SetPercentageNodesPerDynamics(Eigen::VectorXd nodeSplit)
Set percentages of nodes per phase.
Definition: direct_trajectory_optimization_problem.hpp:334
void GetDTOSolution(state_vector_array_t &yTraj, control_vector_array_t &uTraj, Eigen::VectorXd &hTraj, std::vector< int > &phaseId)
Get state control , time and phaseId solution.
Definition: direct_trajectory_optimization_problem.hpp:383
void SetVerbosity(bool verbose)
Change Verbosity level for solver and method.
Definition: direct_trajectory_optimization_problem.hpp:358
enum DirectTrajectoryOptimization::Methods::Method method_t
int ChangeNumberOfPoints()
Change the number of points after construction.
Definition: direct_trajectory_optimization_problem.hpp:413
void InitializeSolver(const std::string &problemName, const std::string &outputFile, const std::string &paramFile, const bool &computeJacobian)
Initialize the DTOSolver problem.
Definition: direct_trajectory_optimization_problem.hpp:270
Class interfacing Ipopt NLP with a DirectTrajectoryOptimization problem.
Definition: ipopt.hpp:50
void SetGuardFunction(std::vector< double(*)(state_vector_t &)> fcnVector)
Set pointer to function evaluating guard function.
Definition: direct_trajectory_optimization_problem.hpp:342
Definition: direct_trajectory_optimization_problem.hpp:119
void SetFinalState(const state_vector_t &state)
Set final state as hard constraint.
Definition: direct_trajectory_optimization_problem.hpp:297
Base class for direct methods.
void SetFreeFinalState(int state_number)
Set final state free of bounds.
Definition: direct_trajectory_optimization_problem.hpp:457
Class implementing multiple shooting for direct trajectory optimizaion.
Definition: direct_trajectory_optimization_problem.hpp:130