- 3.0.2 optimal control module.
BoxConstraintBase-impl.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 namespace ct {
9 namespace optcon {
10 
11 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
13  const decision_vector_t& ub)
14 {
15  // check if box constraints are meaningful
16  sanityCheck(DERIVED_DIM, lb, ub);
17 
18  // this box constraint is 'dense' (one dense jacobian diagonal, one zero jacobian)
19  constrSize_ = DERIVED_DIM;
20  Base::lb_.resize(DERIVED_DIM);
21  Base::ub_.resize(DERIVED_DIM);
22  sparsity_ = Eigen::Matrix<int, DERIVED_DIM, 1>::Ones();
23  sparsity_J_.resize(DERIVED_DIM, DERIVED_DIM);
24  sparsity_J_.setIdentity();
25  Base::lb_ = lb.template cast<SCALAR>();
26  Base::ub_ = ub.template cast<SCALAR>();
27 }
28 
29 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
31  const VectorXs& ub,
32  const Eigen::VectorXi& sparsity_vec)
33 {
34  // make sure the provided sparsity pattern is correct and consists only of ones and zeros
35  assert(sparsity_vec.maxCoeff() <= 1);
36  assert(sparsity_vec.minCoeff() >= 0);
37 
38  constrSize_ = (size_t)sparsity_vec.sum();
39 
40  // make sure the provided bounds are consistent
41  sanityCheck(constrSize_, lb, ub);
42 
43  Base::lb_.resize(constrSize_);
44  Base::ub_.resize(constrSize_);
45  sparsity_ = sparsity_vec;
46  sparsity_J_.resize(constrSize_, DERIVED_DIM);
47  sparsity_J_ = diagSparsityVecToSparsityMat(sparsity_vec, constrSize_);
48  Base::lb_ = lb.template cast<SCALAR>();
49  Base::ub_ = ub.template cast<SCALAR>();
50 }
51 
52 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
54  : Base(arg), sparsity_(arg.sparsity_), sparsity_J_(arg.sparsity_J_), constrSize_(arg.constrSize_)
55 {
56 }
57 
58 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
60 {
61 }
62 
63 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
65 {
66  return constrSize_;
67 }
68 
69 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
72  const size_t& nConstr)
73 {
74  // set up sparsity matrix all zero
75  sparsity_matrix_t mat(nConstr, DERIVED_DIM);
76  mat.setZero();
77 
78  // set selected elements to one
79  size_t count = 0;
80  assert(spVec.rows() == DERIVED_DIM);
81  for (size_t i = 0; i < DERIVED_DIM; i++)
82  {
83  if (spVec(i) == 1)
84  {
85  mat(count, i) = 1;
86  count++;
87  }
88  }
89  return mat;
90 }
91 
92 
93 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
95  const VectorXs& lb,
96  const VectorXs& ub) const
97 {
98  // assert that the size of constraint vectors is equal to the computed/given number of constraints
99  if ((lb.rows() != static_cast<int>(nCon)) | (ub.rows() != static_cast<int>(nCon)))
100  {
101  std::cout << "no. Constraints: " << nCon << std::endl;
102  std::cout << "BoxConstraintBase: lb " << lb.transpose() << std::endl;
103  std::cout << "BoxConstraintBase: ub " << ub.transpose() << std::endl;
104  throw std::runtime_error("BoxConstraintBase: wrong constraint sizes in StateConstraint");
105  }
106 
107  // assert that the boundaries are meaningful
108  for (size_t i = 0; i < nCon; i++)
109  {
110  if (lb(i) > ub(i))
111  {
112  std::cout << "BoxConstraintBase: lb " << lb.transpose() << std::endl;
113  std::cout << "BoxConstraintBase: ub " << ub.transpose() << std::endl;
114  throw std::runtime_error("BoxConstraintBase: wrong boundaries: lb > ub");
115  }
116  }
117 }
118 
119 
120 template <size_t DERIVED_DIM, size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
122  const VectorXi& sparsity_vec,
123  const size_t& constrSize,
124  VectorXi& rows,
125  VectorXi& cols)
126 {
127  Base::genSparseDiagonalIndices(sparsity_vec, rows, cols);
128  for (size_t i = 0; i < constrSize; i++)
129  rows(i) = i;
130 }
131 
132 
133 } // namespace optcon
134 } // namespace ct
BoxConstraintBase(const decision_vector_t &vLow, const decision_vector_t &vHigh)
Constructor taking lower and upper state bounds directly. Assumes the box constraint is dense...
Definition: BoxConstraintBase-impl.h:12
Eigen::Matrix< SCALAR, Eigen::Dynamic, DERIVED_DIM > sparsity_matrix_t
Definition: BoxConstraintBase.h:36
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ConstraintBase.h:31
for i
Definition: mpc_unittest_plotting.m:14
Eigen::Matrix< int, Eigen::Dynamic, 1 > VectorXi
Definition: BoxConstraintBase.h:32
Base for box constraint, templated on dimension of the decision vector of the derived class...
Definition: BoxConstraintBase.h:20
Base class for the constraints used in this toolbox.
Definition: ConstraintBase.h:21