- 3.0.2 optimal control module.
ShotContainer.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 #pragma once
7 
8 #include <cmath>
9 #include <functional>
10 
12 
14 
18 
19 namespace ct {
20 namespace optcon {
21 
31 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
33 {
34 public:
35  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
36 
38 
39  typedef typename DIMENSIONS::state_vector_t state_vector_t;
40  typedef typename DIMENSIONS::control_vector_t control_vector_t;
41  typedef typename DIMENSIONS::state_vector_array_t state_vector_array_t;
42  typedef typename DIMENSIONS::control_vector_array_t control_vector_array_t;
43  typedef typename DIMENSIONS::time_array_t time_array_t;
44 
45  typedef typename DIMENSIONS::state_matrix_t state_matrix_t;
46  typedef typename DIMENSIONS::state_control_matrix_t state_control_matrix_t;
47 
48  typedef typename DIMENSIONS::state_matrix_array_t state_matrix_array_t;
49  typedef typename DIMENSIONS::state_control_matrix_array_t state_control_matrix_array_t;
50 
51  ShotContainer() = delete;
52 
66  std::shared_ptr<ct::core::LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>> linearSystem,
69  std::shared_ptr<SplinerBase<control_vector_t, SCALAR>> controlSpliner,
70  std::shared_ptr<tpl::TimeGrid<SCALAR>> timeGrid,
71  size_t shotNr,
72  DmsSettings settings,
73  size_t nIntegrationSteps)
74  : controlledSystem_(controlledSystem),
75  linearSystem_(linearSystem),
76  costFct_(costFct),
77  w_(w),
78  controlSpliner_(controlSpliner),
79  timeGrid_(timeGrid),
80  shotNr_(shotNr),
81  settings_(settings),
82  integrationCount_(0),
83  costIntegrationCount_(0),
84  sensIntegrationCount_(0),
85  costSensIntegrationCount_(0),
86  cost_(SCALAR(0.0)),
87  discreteQ_(state_vector_t::Zero()),
88  discreteR_(control_vector_t::Zero()),
89  discreteRNext_(control_vector_t::Zero())
90  {
91  if (shotNr_ >= settings.N_)
92  throw std::runtime_error("Dms Shot Integrator: shot index >= settings.N_ - check your settings.");
93 
94  switch (settings_.integrationType_)
95  {
96  case DmsSettings::EULER:
97  {
98  integratorCT_ = std::allocate_shared<SensitivityIntegratorCT<STATE_DIM, CONTROL_DIM, SCALAR>,
99  Eigen::aligned_allocator<SensitivityIntegratorCT<STATE_DIM, CONTROL_DIM, SCALAR>>>(
100  Eigen::aligned_allocator<SensitivityIntegratorCT<STATE_DIM, CONTROL_DIM, SCALAR>>(),
101  controlledSystem_, core::EULERCT);
102  break;
103  }
104  case DmsSettings::RK4:
105  {
106  integratorCT_ = std::allocate_shared<SensitivityIntegratorCT<STATE_DIM, CONTROL_DIM, SCALAR>,
107  Eigen::aligned_allocator<SensitivityIntegratorCT<STATE_DIM, CONTROL_DIM, SCALAR>>>(
108  Eigen::aligned_allocator<SensitivityIntegratorCT<STATE_DIM, CONTROL_DIM, SCALAR>>(),
109  controlledSystem_, core::RK4CT);
110  break;
111  }
112  case DmsSettings::RK5:
113  {
114  throw std::runtime_error("Currently we do not support adaptive integrators in dms");
115  }
116 
117  default:
118  {
119  std::cerr << "... ERROR: unknown integration type. Exiting" << std::endl;
120  exit(0);
121  }
122  }
123 
124  tStart_ = timeGrid_->getShotStartTime(shotNr_);
125  // SCALAR t_shot_end = timeGrid_->getShotEndTime(shotNr_);
126 
127  // +0.5 needed to avoid rounding errors from double to size_t
128  nSteps_ = nIntegrationSteps;
129  // std::cout << "shotNr_: " << shotNr_ << "\t nSteps: " << nSteps_ << std::endl;
130 
131  integratorCT_->setLinearSystem(linearSystem_);
132 
133  if (settings_.costEvaluationType_ == DmsSettings::FULL)
134  integratorCT_->setCostFunction(costFct_);
135  }
136 
141  {
142  if ((w_->getUpdateCount() != integrationCount_))
143  {
144  integrationCount_ = w_->getUpdateCount();
145  state_vector_t initState = w_->getOptimizedState(shotNr_);
146  integratorCT_->integrate(
147  initState, tStart_, nSteps_, SCALAR(settings_.dt_sim_), stateSubsteps_, timeSubsteps_);
148  }
149  }
150 
152  {
153  if ((w_->getUpdateCount() != costIntegrationCount_))
154  {
155  costIntegrationCount_ = w_->getUpdateCount();
156  integrateShot();
157  cost_ = SCALAR(0.0);
158  integratorCT_->integrateCost(cost_, tStart_, nSteps_, SCALAR(settings_.dt_sim_));
159  }
160  }
161 
166  {
167  if ((w_->getUpdateCount() != sensIntegrationCount_))
168  {
169  sensIntegrationCount_ = w_->getUpdateCount();
170  integrateShot();
171  discreteA_.setIdentity();
172  discreteB_.setZero();
173  integratorCT_->linearize();
174  integratorCT_->integrateSensitivityDX0(discreteA_, tStart_, nSteps_, SCALAR(settings_.dt_sim_));
175  integratorCT_->integrateSensitivityDU0(discreteB_, tStart_, nSteps_, SCALAR(settings_.dt_sim_));
176 
178  {
179  discreteBNext_.setZero();
180  integratorCT_->integrateSensitivityDUf(discreteBNext_, tStart_, nSteps_, SCALAR(settings_.dt_sim_));
181  }
182  }
183  }
184 
186  {
187  if ((w_->getUpdateCount() != costSensIntegrationCount_))
188  {
189  costSensIntegrationCount_ = w_->getUpdateCount();
191  discreteQ_.setZero();
192  discreteR_.setZero();
193  integratorCT_->integrateCostSensitivityDX0(discreteQ_, tStart_, nSteps_, SCALAR(settings_.dt_sim_));
194  integratorCT_->integrateCostSensitivityDU0(discreteR_, tStart_, nSteps_, SCALAR(settings_.dt_sim_));
195 
197  {
198  discreteRNext_.setZero();
199  integratorCT_->integrateCostSensitivityDUf(discreteRNext_, tStart_, nSteps_, SCALAR(settings_.dt_sim_));
200  }
201  }
202  }
203 
204  void reset()
205  {
206  integratorCT_->clearStates();
207  integratorCT_->clearSensitivities();
208  integratorCT_->clearLinearization();
209  }
210 
216  const state_vector_t& getStateIntegrated() { return stateSubsteps_.back(); }
222  const SCALAR getIntegrationTimeFinal() { return timeSubsteps_.back(); }
229  const state_matrix_t& getdXdSiIntegrated() { return discreteA_; }
236  const state_control_matrix_t& getdXdQiIntegrated() { return discreteB_; }
243  const state_control_matrix_t& getdXdQip1Integrated() { return discreteBNext_; }
250  // const state_vector_t getdXdHiIntegrated()
251  // {
252  // return dXdHi_history_.back();
253  // }
254 
260  const state_vector_array_t& getXHistory() const { return stateSubsteps_; }
266  const control_vector_array_t& getUHistory()
267  {
268  inputSubsteps_.clear();
269  for (size_t t = 0; t < timeSubsteps_.size(); ++t)
270  {
271  inputSubsteps_.push_back(controlSpliner_->evalSpline(timeSubsteps_[t], shotNr_));
272  }
273  return inputSubsteps_;
274  }
275 
281  const time_array_t& getTHistory() const { return timeSubsteps_; }
287  const SCALAR getCostIntegrated() const { return cost_; }
294  const state_vector_t& getdLdSiIntegrated() const { return discreteQ_; }
301  const control_vector_t& getdLdQiIntegrated() const { return discreteR_; }
308  const control_vector_t& getdLdQip1Integrated() const { return discreteRNext_; }
315  // const double getdLdHiIntegrated() const
316  // {
317  // return costGradientHi_;
318  // }
319 
320 
321 private:
322  std::shared_ptr<ct::core::ControlledSystem<STATE_DIM, CONTROL_DIM, SCALAR>> controlledSystem_;
323  std::shared_ptr<ct::core::LinearSystem<STATE_DIM, CONTROL_DIM, SCALAR>> linearSystem_;
324  std::shared_ptr<ct::optcon::CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>> costFct_;
325  std::shared_ptr<OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>> w_;
326  std::shared_ptr<SplinerBase<control_vector_t, SCALAR>> controlSpliner_;
327  std::shared_ptr<tpl::TimeGrid<SCALAR>> timeGrid_;
328 
329  const size_t shotNr_;
330  const DmsSettings settings_;
331 
332  size_t integrationCount_;
333  size_t costIntegrationCount_;
334  size_t sensIntegrationCount_;
335  size_t costSensIntegrationCount_;
336 
337  // Integrated trajectories
338  state_vector_array_t stateSubsteps_;
339  control_vector_array_t inputSubsteps_;
340  time_array_t timeSubsteps_;
341 
342  //Sensitivity Trajectories
343  state_matrix_t discreteA_;
344  state_control_matrix_t discreteB_;
345  state_control_matrix_t discreteBNext_;
346 
347  //Cost and cost gradient
348  SCALAR cost_;
349  state_vector_t discreteQ_;
350  control_vector_t discreteR_;
351  control_vector_t discreteRNext_;
352 
353  std::shared_ptr<SensitivityIntegratorCT<STATE_DIM, CONTROL_DIM, SCALAR>> integratorCT_;
354  size_t nSteps_;
355  SCALAR tStart_;
356 };
357 
358 } // namespace optcon
359 } // namespace ct
const state_matrix_t & getdXdSiIntegrated()
Returns the integrated ODE sensitivity with respect to the discretized state s_i. ...
Definition: ShotContainer.h:229
Definition: DmsSettings.h:26
Definition: DmsSettings.h:28
const control_vector_t & getdLdQip1Integrated() const
Returns to cost gradient with respect to q_{i+1} integrated over the shot.
Definition: ShotContainer.h:308
const state_vector_array_t & getXHistory() const
Returns the integrated ODE sensitivity with respect to the time segments h_i.
Definition: ShotContainer.h:260
DIMENSIONS::control_vector_t control_vector_t
Definition: ShotContainer.h:40
double dt_sim_
Definition: DmsSettings.h:59
const control_vector_t & getdLdQiIntegrated() const
Returns the cost gradient with respect to q_i integrated over the shot.
Definition: ShotContainer.h:301
Abstract base class for the control input splining between the DMS shots.
Definition: SplinerBase.h:20
void integrateCostSensitivities()
Definition: ShotContainer.h:185
const state_vector_t & getStateIntegrated()
Returns the integrated state.
Definition: ShotContainer.h:216
Definition: DmsSettings.h:29
DIMENSIONS::time_array_t time_array_t
Definition: ShotContainer.h:43
This class is a wrapper around the NLP Optvector. It wraps the Vectors from the NLP solvers into stat...
Definition: OptVectorDms.h:37
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
Definition: DmsSettings.h:28
This class performs the state and the sensitivity integration on a shot.
Definition: ShotContainer.h:32
const SCALAR getCostIntegrated() const
Gets the cost integrated.
Definition: ShotContainer.h:287
size_t N_
Definition: DmsSettings.h:51
void integrateShot()
Performs the state integration between the shots.
Definition: ShotContainer.h:140
Describes a cost function with a quadratic approximation, i.e. one that can compute first and second ...
Definition: CostFunctionQuadratic.hpp:29
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void integrateSensitivities()
Performs the state and the sensitivity integration between the shots.
Definition: ShotContainer.h:165
DIMENSIONS::state_vector_t state_vector_t
Definition: ShotContainer.h:39
Defines basic types used in the DMS algorithm.
Definition: DmsDimensions.h:18
ShotContainer(std::shared_ptr< ct::core::ControlledSystem< STATE_DIM, CONTROL_DIM, SCALAR >> controlledSystem, std::shared_ptr< ct::core::LinearSystem< STATE_DIM, CONTROL_DIM, SCALAR >> linearSystem, std::shared_ptr< ct::optcon::CostFunctionQuadratic< STATE_DIM, CONTROL_DIM, SCALAR >> costFct, std::shared_ptr< OptVectorDms< STATE_DIM, CONTROL_DIM, SCALAR >> w, std::shared_ptr< SplinerBase< control_vector_t, SCALAR >> controlSpliner, std::shared_ptr< tpl::TimeGrid< SCALAR >> timeGrid, size_t shotNr, DmsSettings settings, size_t nIntegrationSteps)
Custom constructor.
Definition: ShotContainer.h:65
Definition: TimeGrid.h:27
const state_control_matrix_t & getdXdQip1Integrated()
Returns the integrated ODE sensitivity with respect to the discretized inputs q_{i+1}.
Definition: ShotContainer.h:243
DIMENSIONS::control_vector_array_t control_vector_array_t
Definition: ShotContainer.h:42
SplineType_t splineType_
Definition: DmsSettings.h:54
DIMENSIONS::state_control_matrix_array_t state_control_matrix_array_t
Definition: ShotContainer.h:49
void integrateCost()
Definition: ShotContainer.h:151
const SCALAR getIntegrationTimeFinal()
Returns the end time of the integration.
Definition: ShotContainer.h:222
DIMENSIONS::state_control_matrix_t state_control_matrix_t
Definition: ShotContainer.h:46
Defines the DMS settings.
Definition: DmsSettings.h:23
const state_control_matrix_t & getdXdQiIntegrated()
Returns the integrated ODE sensitivity with respect to the discretized inputs q_i.
Definition: ShotContainer.h:236
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef DmsDimensions< STATE_DIM, CONTROL_DIM, SCALAR > DIMENSIONS
Definition: ShotContainer.h:37
DIMENSIONS::state_matrix_t state_matrix_t
Definition: ShotContainer.h:45
const state_vector_t & getdLdSiIntegrated() const
Returns the cost gradient with respect to s_i integrated over the shot.
Definition: ShotContainer.h:294
CostEvaluationType_t costEvaluationType_
Definition: DmsSettings.h:55
const time_array_t & getTHistory() const
Returns the time trajectory used during the integration.
Definition: ShotContainer.h:281
DIMENSIONS::state_matrix_array_t state_matrix_array_t
Definition: ShotContainer.h:48
void reset()
Definition: ShotContainer.h:204
IntegrationType_t integrationType_
Definition: DmsSettings.h:58
DIMENSIONS::state_vector_array_t state_vector_array_t
Definition: ShotContainer.h:41
const control_vector_array_t & getUHistory()
Returns the control input trajectory used during the state integration.
Definition: ShotContainer.h:266
Definition: DmsSettings.h:28