- 3.0.2 optimal control module.
DmsProblem.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 
7 #pragma once
8 
9 
16 
20 
21 #include <ct/optcon/nlp/Nlp.h>
22 
23 namespace ct {
24 namespace optcon {
25 
26 
33 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
34 class DmsProblem : public tpl::Nlp<SCALAR>
35 {
36 public:
37  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
38 
40  typedef typename DIMENSIONS::state_vector_t state_vector_t;
41  typedef typename DIMENSIONS::control_vector_array_t control_vector_array_t;
42  typedef typename DIMENSIONS::control_vector_t control_vector_t;
43  typedef typename DIMENSIONS::state_vector_array_t state_vector_array_t;
44  typedef typename DIMENSIONS::time_array_t time_array_t;
45 
47 
48 
63  std::vector<typename OptConProblem_t::DynamicsPtr_t> systemPtrs,
64  std::vector<typename OptConProblem_t::LinearPtr_t> linearPtrs,
65  std::vector<typename OptConProblem_t::CostFunctionPtr_t> costPtrs,
66  std::vector<typename OptConProblem_t::ConstraintPtr_t> inputBoxConstraints,
67  std::vector<typename OptConProblem_t::ConstraintPtr_t> stateBoxConstraints,
68  std::vector<typename OptConProblem_t::ConstraintPtr_t> generalConstraints,
69  const state_vector_t& x0)
70  : settings_(settings)
71  {
72  assert(systemPtrs.size() == settings_.N_);
73  assert(linearPtrs.size() == settings_.N_);
74  assert(costPtrs.size() == settings_.N_);
75  settings_.parametersOk();
76 
77  timeGrid_ = std::shared_ptr<tpl::TimeGrid<SCALAR>>(new tpl::TimeGrid<SCALAR>(settings.N_, settings.T_));
78 
79  switch (settings_.splineType_)
80  {
82  {
83  controlSpliner_ = std::shared_ptr<ZeroOrderHoldSpliner<control_vector_t, SCALAR>>(
85  break;
86  }
88  {
89  controlSpliner_ = std::shared_ptr<LinearSpliner<control_vector_t, SCALAR>>(
91  break;
92  }
93  default:
94  throw(std::runtime_error("Unknown spline type"));
95  }
96 
97 
98  size_t wLength = (settings.N_ + 1) * (STATE_DIM + CONTROL_DIM);
100  {
101  throw std::runtime_error("We do not support adaptive time gridding");
102  }
103 
104  this->optVariables_ = std::shared_ptr<OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>>(
105  new OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>(wLength, settings));
106 
107  optVariablesDms_ = std::static_pointer_cast<OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>>(this->optVariables_);
108 
109  if (inputBoxConstraints.size() > 0 || stateBoxConstraints.size() > 0 || generalConstraints.size() > 0)
110  discretizedConstraints_ = std::shared_ptr<ConstraintDiscretizer<STATE_DIM, CONTROL_DIM, SCALAR>>(
112  optVariablesDms_, controlSpliner_, timeGrid_, settings_.N_));
113 
114  if (inputBoxConstraints.size() > 0)
115  discretizedConstraints_->setBoxConstraints(
116  inputBoxConstraints
117  .front()); // at this point we do not distinguish between state and input box constraints
118 
119  if (stateBoxConstraints.size() > 0)
120  discretizedConstraints_->setBoxConstraints(
121  stateBoxConstraints
122  .front()); // at this point we do not distinguish between state and input box constraints
123 
124  if (generalConstraints.size() > 0)
125  discretizedConstraints_->setGeneralConstraints(generalConstraints.front());
126 
127 
128  for (size_t shotIdx = 0; shotIdx < settings_.N_; shotIdx++)
129  {
130  std::shared_ptr<ControllerDms<STATE_DIM, CONTROL_DIM, SCALAR>> newController(
131  new ControllerDms<STATE_DIM, CONTROL_DIM, SCALAR>(controlSpliner_, shotIdx));
132  systemPtrs[shotIdx]->setController(newController);
133  linearPtrs[shotIdx]->setController(newController);
134 
135  size_t nIntegrationSteps =
136  (timeGrid_->getShotEndTime(shotIdx) - timeGrid_->getShotStartTime(shotIdx)) / settings_.dt_sim_ + 0.5;
137 
138  shotContainers_.push_back(std::shared_ptr<ShotContainer<STATE_DIM, CONTROL_DIM, SCALAR>>(
139  new ShotContainer<STATE_DIM, CONTROL_DIM, SCALAR>(systemPtrs[shotIdx], linearPtrs[shotIdx],
140  costPtrs[shotIdx], optVariablesDms_, controlSpliner_, timeGrid_, shotIdx, settings_,
141  nIntegrationSteps)));
142  }
143 
144  switch (settings_.costEvaluationType_)
145  {
146  case DmsSettings::SIMPLE:
147  {
148  this->costEvaluator_ = std::shared_ptr<CostEvaluatorSimple<STATE_DIM, CONTROL_DIM, SCALAR>>(
150  costPtrs.front(), optVariablesDms_, timeGrid_, settings_));
151  break;
152  }
153  case DmsSettings::FULL:
154  {
155  this->costEvaluator_ = std::shared_ptr<CostEvaluatorFull<STATE_DIM, CONTROL_DIM, SCALAR>>(
157  costPtrs.front(), optVariablesDms_, controlSpliner_, shotContainers_, settings_));
158  break;
159  }
160  default:
161  throw(std::runtime_error("Unknown cost evaluation type"));
162  }
163 
164  this->constraints_ = std::shared_ptr<ConstraintsContainerDms<STATE_DIM, CONTROL_DIM, SCALAR>>(
166  optVariablesDms_, timeGrid_, shotContainers_, discretizedConstraints_, x0, settings_));
167 
168  this->optVariables_->resizeConstraintVars(this->getConstraintsCount());
169  }
170 
171 
175  ~DmsProblem() override = default;
176 
177  void updateProblem() override
178  {
179  controlSpliner_->computeSpline(optVariablesDms_->getOptimizedInputs().toImplementation());
180  for (auto shotContainer : shotContainers_)
181  shotContainer->reset();
182  }
183 
189  void configure(const DmsSettings& settings) { settings_ = settings; }
195  const state_vector_array_t& getStateSolution() { return optVariablesDms_->getOptimizedStates(); }
201  const control_vector_array_t& getInputSolution() { return optVariablesDms_->getOptimizedInputs(); }
207  const time_array_t& getTimeSolution() { return timeGrid_->toImplementation(); }
213  const state_vector_array_t& getStateTrajectory() { return optVariablesDms_->getOptimizedStates(); }
219  const control_vector_array_t& getInputTrajectory() { return optVariablesDms_->getOptimizedInputs(); }
225  const time_array_t& getTimeArray() { return timeGrid_->toImplementation(); }
233  void setInitialGuess(const state_vector_array_t& x_init_guess,
234  const control_vector_array_t& u_init_guess,
235  const time_array_t& t_init_guess = time_array_t(0.0))
236  {
237  optVariablesDms_->setInitGuess(x_init_guess, u_init_guess);
238  }
239 
245  const core::Time getTimeHorizon() const { return timeGrid_->getTimeHorizon(); }
251  void changeTimeHorizon(const SCALAR tf) { timeGrid_->changeTimeHorizon(tf); }
257  void changeInitialState(const state_vector_t& x0)
258  {
259  // constraints_->changeInitialConstraint(x0);
260  optVariablesDms_->changeInitialState(x0);
261  }
262 
266  void printSolution() { optVariablesDms_->printoutSolution(); }
267 
268 private:
269  DmsSettings settings_;
270 
271  std::shared_ptr<ConstraintDiscretizer<STATE_DIM, CONTROL_DIM, SCALAR>> discretizedConstraints_;
272 
273  std::vector<std::shared_ptr<ShotContainer<STATE_DIM, CONTROL_DIM, SCALAR>>> shotContainers_;
274  std::shared_ptr<OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>> optVariablesDms_;
275  std::shared_ptr<SplinerBase<control_vector_t, SCALAR>> controlSpliner_;
276  std::shared_ptr<tpl::TimeGrid<SCALAR>> timeGrid_;
277 
278  state_vector_array_t stateSolutionDense_;
279  control_vector_array_t inputSolutionDense_;
280  time_array_t timeSolutionDense_;
281 };
282 
283 
284 } // namespace optcon
285 } // namespace ct
ObjectiveType_t objectiveType_
Definition: DmsSettings.h:56
Definition: DmsSettings.h:26
Definition: DmsSettings.h:27
void changeTimeHorizon(const SCALAR tf)
Updates the timehorizon.
Definition: DmsProblem.h:251
The NLP base class. This class serves as abstract base class to use as an interface to the NLP solver...
Definition: Nlp.h:32
size_t getConstraintsCount() const
Returns the number of constraints in the NLP.
Definition: Nlp.h:212
const control_vector_array_t & getInputTrajectory()
Retrieves a dense input solution trajectory.
Definition: DmsProblem.h:219
void setInitialGuess(const state_vector_array_t &x_init_guess, const control_vector_array_t &u_init_guess, const time_array_t &t_init_guess=time_array_t(0.0))
Sets the initial guess of the optimization.
Definition: DmsProblem.h:233
Performs the full cost integration over the shots.
Definition: CostEvaluatorFull.h:32
DMS controller class.
Definition: ControllerDms.h:30
double dt_sim_
Definition: DmsSettings.h:59
Definition: DmsSettings.h:29
The spline implementation for the zero order hold spliner.
Definition: ZeroOrderHoldSpliner.h:22
const time_array_t & getTimeSolution()
Retrieves the solution time trajectory at every shot.
Definition: DmsProblem.h:207
DIMENSIONS::state_vector_t state_vector_t
Definition: DmsProblem.h:40
double T_
Definition: DmsSettings.h:52
Definition: DmsSettings.h:29
The linear spline implementation.
Definition: LinearSpliner.h:22
This class is a wrapper around the NLP Optvector. It wraps the Vectors from the NLP solvers into stat...
Definition: OptVectorDms.h:37
This class sets up the DMS problem.
Definition: DmsProblem.h:34
std::shared_ptr< DiscreteCostEvaluatorBase< SCALAR > > costEvaluator_
Ptr to cost evaluator, which approximates the cost evaluation for the discrete problem.
Definition: Nlp.h:491
This class performs the state and the sensitivity integration on a shot.
Definition: ShotContainer.h:32
size_t N_
Definition: DmsSettings.h:51
Definition: DmsSettings.h:26
std::shared_ptr< OptVector< SCALAR > > optVariables_
Ptr to optimization variable container, which holds the optimization variables used in the NLP solver...
Definition: Nlp.h:494
const state_vector_array_t & getStateTrajectory()
Retrieves a dense state solution trajectory.
Definition: DmsProblem.h:213
DIMENSIONS::time_array_t time_array_t
Definition: DmsProblem.h:44
const state_vector_array_t & getStateSolution()
Retrieves the solution state trajectory at every shot.
Definition: DmsProblem.h:195
CppAD::AD< CppAD::cg::CG< double > > SCALAR
bool parametersOk() const
Definition: DmsSettings.h:86
const time_array_t & getTimeArray()
Retrieves a dense time solution trajectory.
Definition: DmsProblem.h:225
const control_vector_array_t & getInputSolution()
Retrieves the solution control trajectory at every shot.
Definition: DmsProblem.h:201
ContinuousOptConProblem< STATE_DIM, CONTROL_DIM, SCALAR > OptConProblem_t
Definition: DmsProblem.h:46
Defines basic types used in the DMS algorithm.
Definition: DmsDimensions.h:18
Definition: TimeGrid.h:27
Container class for the constraints used in DMS.
Definition: ConstraintsContainerDms.h:35
DIMENSIONS::control_vector_t control_vector_t
Definition: DmsProblem.h:42
void updateProblem() override
{ This method gets called at each update of the Optimization variables. This can be used to distribut...
Definition: DmsProblem.h:177
void printSolution()
Prints the solution trajectories.
Definition: DmsProblem.h:266
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef DmsDimensions< STATE_DIM, CONTROL_DIM, SCALAR > DIMENSIONS
Definition: DmsProblem.h:39
void configure(const DmsSettings &settings)
Updates the settings.
Definition: DmsProblem.h:189
SplineType_t splineType_
Definition: DmsSettings.h:54
void changeInitialState(const state_vector_t &x0)
Updates the initial state.
Definition: DmsProblem.h:257
Defines the DMS settings.
Definition: DmsSettings.h:23
Evaluates the cost at the shots and performs some interpolation in between.
Definition: CostEvaluatorSimple.h:33
The class takes continuous constraints defined with the constraint toolbox and discretizes them over ...
Definition: ConstraintDiscretizer.h:30
DmsProblem(DmsSettings settings, std::vector< typename OptConProblem_t::DynamicsPtr_t > systemPtrs, std::vector< typename OptConProblem_t::LinearPtr_t > linearPtrs, std::vector< typename OptConProblem_t::CostFunctionPtr_t > costPtrs, std::vector< typename OptConProblem_t::ConstraintPtr_t > inputBoxConstraints, std::vector< typename OptConProblem_t::ConstraintPtr_t > stateBoxConstraints, std::vector< typename OptConProblem_t::ConstraintPtr_t > generalConstraints, const state_vector_t &x0)
Custom constructor, sets up the objects needed for the Dms algorithm depending on the settings...
Definition: DmsProblem.h:62
const core::Time getTimeHorizon() const
Return the timehorizon of the problem.
Definition: DmsProblem.h:245
DIMENSIONS::control_vector_array_t control_vector_array_t
Definition: DmsProblem.h:41
DIMENSIONS::state_vector_array_t state_vector_array_t
Definition: DmsProblem.h:43
StateVector< state_dim > x0
Definition: ConstrainedNLOCTest.cpp:14
~DmsProblem() override=default
Destructor.
std::shared_ptr< DiscreteConstraintContainerBase< SCALAR > > constraints_
Ptr to constraint container, which contains the discretized constraints for the problem.
Definition: Nlp.h:497
CostEvaluationType_t costEvaluationType_
Definition: DmsSettings.h:55
Definition: OptConProblemBase.h:40
double Time