- 3.0.2 optimal control module.
DiscreteConstraintBase.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 <Eigen/Core>
9 
10 namespace ct {
11 namespace optcon {
12 namespace tpl {
13 
20 template <typename SCALAR>
22 {
23 public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25  using VectorXs = Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>;
26 
30  DiscreteConstraintBase() = default;
34  virtual ~DiscreteConstraintBase() = default;
40  virtual VectorXs eval() = 0;
41 
48  virtual VectorXs evalSparseJacobian() = 0;
49 
56  virtual size_t getConstraintSize() = 0;
57 
64  virtual size_t getNumNonZerosJacobian() = 0;
65 
74  virtual void genSparsityPattern(Eigen::VectorXi& iRow_vec, Eigen::VectorXi& jCol_vec) = 0;
75 
76 
85  virtual void genSparsityPatternHessian(Eigen::VectorXi& iRow_vec, Eigen::VectorXi& jCol_vec)
86  {
87  throw std::runtime_error(
88  "genSparsityPatternHessian() for DiscreteConstraintBase not implemented. Use Hessian approximation.");
89  }
90 
100  virtual void sparseHessianValues(const Eigen::VectorXd& optVec,
101  const Eigen::VectorXd& lambda,
102  Eigen::VectorXd& sparseHes)
103  {
104  throw std::runtime_error(
105  "sparseHessianValues() for DiscreteConstraintBase not implemented. Use Hessian approximation.");
106  }
107 
108 
114  virtual VectorXs getLowerBound() = 0;
115 
121  virtual VectorXs getUpperBound() = 0;
122 
123  Eigen::VectorXi& iRowHessian() { return iRowHessian_; }
124  Eigen::VectorXi& jColHessian() { return jColHessian_; }
125 protected:
139  size_t genDiagonalIndices(const size_t col_start,
140  const size_t num_elements,
141  Eigen::VectorXi& iRow_vec,
142  Eigen::VectorXi& jCol_vec,
143  const size_t indexNumber);
144 
160  size_t genBlockIndices(const size_t col_start,
161  const size_t num_rows,
162  const size_t num_cols,
163  Eigen::VectorXi& iRow_vec,
164  Eigen::VectorXi& jCol_vec,
165  const size_t indexNumber);
166 
167  Eigen::VectorXi iRowHessian_;
168  Eigen::VectorXi jColHessian_;
169 };
170 
171 template <typename SCALAR>
172 inline size_t DiscreteConstraintBase<SCALAR>::genDiagonalIndices(const size_t col_start,
173  const size_t num_elements,
174  Eigen::VectorXi& iRow_vec,
175  Eigen::VectorXi& jCol_vec,
176  const size_t indexNumber)
177 {
178  Eigen::VectorXi new_row_indices;
179  Eigen::VectorXi new_col_indices;
180  new_row_indices.resize(num_elements);
181  new_col_indices.resize(num_elements);
182 
183  size_t count = 0;
184 
185  for (size_t i = 0; i < num_elements; ++i)
186  {
187  new_row_indices(count) = i;
188  new_col_indices(count) = col_start + i;
189  count++;
190  }
191 
192  assert(count == num_elements);
193 
194  iRow_vec.segment(indexNumber, num_elements) = new_row_indices;
195  jCol_vec.segment(indexNumber, num_elements) = new_col_indices;
196 
197  return count;
198 }
199 
200 template <typename SCALAR>
201 inline size_t DiscreteConstraintBase<SCALAR>::genBlockIndices(const size_t col_start,
202  const size_t num_rows,
203  const size_t num_cols,
204  Eigen::VectorXi& iRow_vec,
205  Eigen::VectorXi& jCol_vec,
206  const size_t indexNumber)
207 {
208  size_t num_gen_indices = num_rows * num_cols;
209 
210  Eigen::VectorXi new_row_indices;
211  Eigen::VectorXi new_col_indices;
212  new_row_indices.resize(num_gen_indices);
213  new_col_indices.resize(num_gen_indices);
214 
215  size_t count = 0;
216 
217  for (size_t row = 0; row < num_rows; ++row)
218  {
219  for (size_t col = col_start; col < col_start + num_cols; ++col)
220  {
221  new_row_indices(count) = row;
222  new_col_indices(count) = col;
223  count++;
224  }
225  }
226 
227  assert(count == num_gen_indices);
228 
229  iRow_vec.segment(indexNumber, num_gen_indices) = new_row_indices;
230  jCol_vec.segment(indexNumber, num_gen_indices) = new_col_indices;
231 
232  return num_gen_indices;
233 }
234 }
235 
237 
238 } // namespace optcon
239 } // namespace ct
virtual size_t getNumNonZerosJacobian()=0
Returns the number of non zero elements of the jacobian.
virtual VectorXs eval()=0
Evaluates the constraint violation.
virtual ~DiscreteConstraintBase()=default
Destructor.
virtual VectorXs evalSparseJacobian()=0
Returns the non zero elements of the eval method with respect to the optimization variables...
virtual void sparseHessianValues(const Eigen::VectorXd &optVec, const Eigen::VectorXd &lambda, Eigen::VectorXd &sparseHes)
Returns the non zero elements (values) of the Hessian matrix of this constraint.
Definition: DiscreteConstraintBase.h:100
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: DiscreteConstraintBase.h:25
Eigen::VectorXi & iRowHessian()
Definition: DiscreteConstraintBase.h:123
size_t genBlockIndices(const size_t col_start, const size_t num_rows, const size_t num_cols, Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec, const size_t indexNumber)
This method generates Row and Column vectors which indicate the sparsity pattern of the constraint ja...
Definition: DiscreteConstraintBase.h:201
Implements an abstract base class from which all the discrete custom NLP constraints should derive...
Definition: DiscreteConstraintBase.h:21
Eigen::VectorXi & jColHessian()
Definition: DiscreteConstraintBase.h:124
virtual void genSparsityPatternHessian(Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec)
Returns the sparsity structure of the constraint hessian.
Definition: DiscreteConstraintBase.h:85
virtual VectorXs getLowerBound()=0
Returns the lower bound of the constraint.
Eigen::VectorXi iRowHessian_
Definition: DiscreteConstraintBase.h:167
DiscreteConstraintBase()=default
Default constructor.
for i
Definition: mpc_unittest_plotting.m:14
virtual size_t getConstraintSize()=0
Returns size of the constraint vector.
virtual VectorXs getUpperBound()=0
Returns the upper bound of the constraint.
size_t genDiagonalIndices(const size_t col_start, const size_t num_elements, Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec, const size_t indexNumber)
This method generates Row and Column vectors which indicate the sparsity pattern of the constraint ja...
Definition: DiscreteConstraintBase.h:172
virtual void genSparsityPattern(Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec)=0
Returns the sparsity structure of the constraint jacobian.
Eigen::VectorXi jColHessian_
Definition: DiscreteConstraintBase.h:168