- 3.0.2 optimal control module.
ContinuityConstraint.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 
10 
14 
15 namespace ct {
16 namespace optcon {
17 
18 
27 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
29 {
30 public:
31  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
32 
35 
40 
44 
45  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> VectorXs;
46  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> MatrixXs;
47 
51  ContinuityConstraint() = default;
62  size_t shotIndex,
63  const DmsSettings settings)
64  : shotContainer_(shotContainer), w_(w), shotIndex_(shotIndex), settings_(settings)
65  {
66  lb_.setConstant(SCALAR(0.0));
67  ub_.setConstant(SCALAR(0.0));
68 
69  size_t nr = 0;
70 
71  switch (settings_.splineType_)
72  {
74  {
75  nr = STATE_DIM * STATE_DIM + STATE_DIM * CONTROL_DIM + STATE_DIM;
76  break;
77  }
79  {
80  nr = STATE_DIM * STATE_DIM + STATE_DIM * CONTROL_DIM + STATE_DIM + STATE_DIM * CONTROL_DIM;
81  break;
82  }
83  default:
84  {
85  throw(std::runtime_error("specified invalid spliner type in ContinuityConstraint-class"));
86  }
87  }
88 
89  // if(settings_.objectiveType_ == DmsSettings::OPTIMIZE_GRID)
90  // nr += STATE_DIM;
91  jacLocal_.resize(nr);
92  }
93 
94 
95  VectorXs eval() override { return w_->getOptimizedState(shotIndex_ + 1) - shotContainer_->getStateIntegrated(); }
96  VectorXs evalSparseJacobian() override
97  {
98  count_local_ = 0;
99  switch (settings_.splineType_)
100  {
102  {
103  computeXblock(); // add the big block (derivative w.r.t. state s_i)
104  computeUblock(); // add the smaller block (derivative w.r.t. control q_i)
105  computeIblock(); // add the diagonal (derivative w.r.t. state s_(i+1))
106  // if(settings_.objectiveType_ == DmsSettings::OPTIMIZE_GRID)
107  // computeHblock();
108  break;
109  }
111  {
112  computeXblock(); // add the big block (derivative w.r.t. state s_i)
113  computeUblock(); // add the smaller block (derivative w.r.t. control q_i)
114  computeIblock(); // add the diagonal (derivative w.r.t. state s_(i+1))
115  computeUblock_2(); // add the smaller block (derivative w.r.t. control q_(i+1))
116  // if(settings_.objectiveType_ == DmsSettings::OPTIMIZE_GRID)
117  // computeHblock();
118  break;
119  }
120  default:
121  {
122  throw(std::runtime_error("specified invalid spliner type in ContinuityConstraint-class"));
123  }
124  }
125  return jacLocal_;
126  }
127 
128  size_t getNumNonZerosJacobian() override
129  {
130  size_t no = 0;
131  switch (settings_.splineType_)
132  {
134  {
135  no = STATE_DIM * STATE_DIM + STATE_DIM * CONTROL_DIM + STATE_DIM * 1;
136  break;
137  }
139  {
140  no = STATE_DIM * STATE_DIM + STATE_DIM * CONTROL_DIM + STATE_DIM * 1 + STATE_DIM * CONTROL_DIM;
141  break;
142  }
143  default:
144  {
145  throw(std::runtime_error("specified invalid spliner type in ContinuityConstraint-class"));
146  }
147  }
148 
149  /* the derivatives w.r.t. the time optimization variable (h_i) */
150  // if(settings_.objectiveType_ == DmsSettings::OPTIMIZE_GRID)
151  // {
152  // no += STATE_DIM;
153  // }
154 
155  return no;
156  }
157 
158 
159  void genSparsityPattern(Eigen::VectorXi& iRow_vec, Eigen::VectorXi& jCol_vec) override
160  {
161  size_t indexNumber = 0;
162 
163  switch (settings_.splineType_)
164  {
166  {
167  // add the big block (derivative w.r.t. state)
168  indexNumber += BASE::genBlockIndices(
169  w_->getStateIndex(shotIndex_), STATE_DIM, STATE_DIM, iRow_vec, jCol_vec, indexNumber);
170 
171  // add the smaller block (derivative w.r.t. control)
172  indexNumber += BASE::genBlockIndices(
173  w_->getControlIndex(shotIndex_), STATE_DIM, CONTROL_DIM, iRow_vec, jCol_vec, indexNumber);
174 
175  // add the diagonal
176  indexNumber += BASE::genDiagonalIndices(
177  w_->getStateIndex(shotIndex_ + 1), STATE_DIM, iRow_vec, jCol_vec, indexNumber);
178  break;
179  }
181  {
182  // add the big block (derivative w.r.t. state)
183  indexNumber += BASE::genBlockIndices(
184  w_->getStateIndex(shotIndex_), STATE_DIM, STATE_DIM, iRow_vec, jCol_vec, indexNumber);
185 
186  // add the smaller block (derivative w.r.t. control)
187  indexNumber += BASE::genBlockIndices(
188  w_->getControlIndex(shotIndex_), STATE_DIM, CONTROL_DIM, iRow_vec, jCol_vec, indexNumber);
189 
190  // add the diagonal
191  indexNumber += BASE::genDiagonalIndices(
192  w_->getStateIndex(shotIndex_ + 1), STATE_DIM, iRow_vec, jCol_vec, indexNumber);
193 
194  // add the fourth block (derivative w.r.t. control)
195  indexNumber += BASE::genBlockIndices(
196  w_->getControlIndex(shotIndex_ + 1), STATE_DIM, CONTROL_DIM, iRow_vec, jCol_vec, indexNumber);
197  break;
198  }
199  default:
200  {
201  throw(std::runtime_error("specified invalid spliner type in ContinuityConstraint-class"));
202  }
203  }
204 
205  /* for the derivatives w.r.t. the time optimization variables (t_i) */
206  // if(settings_.objectiveType_ == DmsSettings::OPTIMIZE_GRID)
207  // {
208  // indexNumber += BASE::genBlockIndices(w_->getTimeSegmentIndex(shotIndex_),
209  // STATE_DIM, 1, iRow_vec, jCol_vec, indexNumber);
210  // }
211  }
212 
213  VectorXs getLowerBound() override { return lb_; }
214  VectorXs getUpperBound() override { return ub_; }
215  size_t getConstraintSize() override { return STATE_DIM; }
216 private:
221  void computeXblock();
222 
227  void computeUblock();
228 
233  void computeUblock_2();
234 
238  void computeIblock();
239 
244  void computeHblock();
245 
246  std::shared_ptr<ShotContainer<STATE_DIM, CONTROL_DIM, SCALAR>> shotContainer_;
247  std::shared_ptr<OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>> w_;
248  size_t shotIndex_;
249  const DmsSettings settings_;
250 
251  VectorXs jacLocal_;
252  size_t count_local_;
253 
254  state_vector_t lb_;
255  state_vector_t ub_;
256 };
257 
258 
259 // template <size_t STATE_DIM, size_t CONTROL_DIM>
260 // void ContinuityConstraint<STATE_DIM, CONTROL_DIM>::computeHblock()
261 // {
262 // // take last elements of state and time
263 // state_vector_t state = shotContainer_->getStateIntegrated();
264 // ct::core::Time time = shotContainer_->getIntegrationTimeFinal();
265 
266 // // compute derivative
267 // state_vector_t mat;
268 // state_vector_t dynamics;
269 // shotContainer_->getControlledSystemPtr()->computeDynamics(state, time, dynamics);
270 
271 // switch (settings_.splineType_)
272 // {
273 // case DmsSettings::ZERO_ORDER_HOLD:
274 // {
275 // mat = - dynamics;
276 // break;
277 // }
278 // case DmsSettings::PIECEWISE_LINEAR:
279 // {
280 // mat = -dynamics - shotContainer_->getdXdHiIntegrated();
281 // break;
282 // }
283 // default:
284 // {
285 // throw(std::runtime_error("specified invalid spliner type in ContinuityConstraint-class"));
286 // }
287 // }
288 
289 // jacLocal_.segment(count_local_, STATE_DIM) = mat;
290 // count_local_ += STATE_DIM;
291 // }
292 
293 
294 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
296 {
297  state_matrix_t mat = -shotContainer_->getdXdSiIntegrated();
298  mat.transposeInPlace();
299  VectorXs dXdSiVec = (Eigen::Map<VectorXs>(mat.data(), STATE_DIM * STATE_DIM));
300 
301  // fill into value vector with correct indexing
302  jacLocal_.segment(count_local_, STATE_DIM * STATE_DIM) = dXdSiVec;
303 
304  count_local_ += STATE_DIM * STATE_DIM;
305 }
306 
307 
308 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
310 {
311  MatrixXs mat = -shotContainer_->getdXdQiIntegrated();
312  mat.transposeInPlace();
313  VectorXs dXdQiVec = Eigen::Map<VectorXs>(mat.data(), STATE_DIM * CONTROL_DIM);
314 
315  // // fill into value vector with correct indexing
316  jacLocal_.segment(count_local_, STATE_DIM * CONTROL_DIM) = dXdQiVec;
317  count_local_ += STATE_DIM * CONTROL_DIM;
318 }
319 
320 
321 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
323 {
324  MatrixXs mat = -shotContainer_->getdXdQip1Integrated();
325  mat.transposeInPlace();
326  VectorXs dXdU1Vec = Eigen::Map<VectorXs>(mat.data(), STATE_DIM * CONTROL_DIM);
327 
328  // fill into value vector with correct indexing
329  jacLocal_.segment(count_local_, STATE_DIM * CONTROL_DIM) = dXdU1Vec;
330  count_local_ += STATE_DIM * CONTROL_DIM;
331 }
332 
333 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
335 {
336  // fill into value vector with correct indexing
337  jacLocal_.segment(count_local_, STATE_DIM) = VectorXs::Ones(STATE_DIM);
338  count_local_ += STATE_DIM;
339 }
340 
341 } // namespace optcon
342 } // namespace ct
VectorXs getLowerBound() override
Returns the lower bound of the constraint.
Definition: ContinuityConstraint.h:213
VectorXs getUpperBound() override
Returns the upper bound of the constraint.
Definition: ContinuityConstraint.h:214
VectorXs eval() override
Evaluates the constraint violation.
Definition: ContinuityConstraint.h:95
Definition: DmsSettings.h:26
Implementation of the DMS continuity constraints.
Definition: ContinuityConstraint.h:28
DIMENSIONS::state_control_matrix_array_t state_control_matrix_array_t
Definition: ContinuityConstraint.h:43
DIMENSIONS::control_vector_array_t control_vector_array_t
Definition: ContinuityConstraint.h:38
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef ct::core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: DmsDimensions.h:23
VectorXs evalSparseJacobian() override
Returns the non zero elements of the eval method with respect to the optimization variables...
Definition: ContinuityConstraint.h:96
Implements an abstract base class from which all the discrete custom NLP constraints should derive...
Definition: DiscreteConstraintBase.h:21
This class is a wrapper around the NLP Optvector. It wraps the Vectors from the NLP solvers into stat...
Definition: OptVectorDms.h:37
DIMENSIONS::control_vector_t control_vector_t
Definition: ContinuityConstraint.h:37
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ContinuityConstraint.h:45
This class performs the state and the sensitivity integration on a shot.
Definition: ShotContainer.h:32
Definition: DmsSettings.h:26
DIMENSIONS::state_matrix_array_t state_matrix_array_t
Definition: ContinuityConstraint.h:42
CppAD::AD< CppAD::cg::CG< double > > SCALAR
size_t getNumNonZerosJacobian() override
Returns the number of non zero elements of the jacobian.
Definition: ContinuityConstraint.h:128
Defines basic types used in the DMS algorithm.
Definition: DmsDimensions.h:18
DmsDimensions< STATE_DIM, CONTROL_DIM, SCALAR > DIMENSIONS
Definition: ContinuityConstraint.h:34
DIMENSIONS::state_vector_t state_vector_t
Definition: ContinuityConstraint.h:36
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef tpl::DiscreteConstraintBase< SCALAR > BASE
Definition: ContinuityConstraint.h:33
Eigen::Matrix< SCALAR, STATE_DIM, STATE_DIM > state_matrix_t
Definition: DmsDimensions.h:26
DIMENSIONS::state_matrix_t state_matrix_t
Definition: ContinuityConstraint.h:41
SplineType_t splineType_
Definition: DmsSettings.h:54
ContinuityConstraint(std::shared_ptr< ShotContainer< STATE_DIM, CONTROL_DIM, SCALAR >> shotContainer, std::shared_ptr< OptVectorDms< STATE_DIM, CONTROL_DIM, SCALAR >> w, size_t shotIndex, const DmsSettings settings)
Custom constructor.
Definition: ContinuityConstraint.h:60
DIMENSIONS::time_array_t time_array_t
Definition: ContinuityConstraint.h:39
Defines the DMS settings.
Definition: DmsSettings.h:23
void genSparsityPattern(Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec) override
Returns the sparsity structure of the constraint jacobian.
Definition: ContinuityConstraint.h:159
size_t getConstraintSize() override
Returns size of the constraint vector.
Definition: ContinuityConstraint.h:215
ContinuityConstraint()=default
Default constructor.
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: ContinuityConstraint.h:46