- 3.0.2 optimal control module.
ConstraintBase-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 STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
13 {
14 }
15 
16 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
18  : lb_(arg.lb_), ub_(arg.ub_), name_(arg.name_)
19 {
20 }
21 
22 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
24 {
25 }
26 
27 #ifdef CPPADCG
28 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
29 Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateCppadCg(
32  ct::core::ADCGScalar t)
33 {
34  throw std::runtime_error("Term " + name_ + " has no Implementation of evaluateCppaCg.");
35 }
36 #endif
37 
38 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
41  const control_vector_t& u,
42  const SCALAR t)
43 {
44  throw std::runtime_error(
45  "This constraint function element is not implemented for the given term."
46  "Please use either auto-diff cost function or implement the analytical derivatives manually.");
47 }
48 
49 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
52  const control_vector_t& u,
53  const SCALAR t)
54 {
55  throw std::runtime_error(
56  "This constraint function element is not implemented for the given term."
57  "Please use either auto-diff cost function or implement the analytical derivatives manually.");
58 }
59 
60 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
63 {
64  return lb_;
65 }
66 
67 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
70 {
71  return ub_;
72 }
73 
74 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
75 void ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::getName(std::string& constraintName) const
76 {
77  constraintName = name_;
78 }
79 
80 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
81 void ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::setName(const std::string constraintName)
82 {
83  name_ = constraintName;
84 }
85 
86 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
88 {
89  return STATE_DIM * getConstraintSize();
90 }
91 
92 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
94 {
95  return CONTROL_DIM * getConstraintSize();
96 }
97 
98 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
101  const control_vector_t& u,
102  const SCALAR t)
103 {
104  MatrixXs jacState = jacobianState(x, u, t);
105 
106  VectorXs jac(Eigen::Map<VectorXs>(jacState.data(), jacState.rows() * jacState.cols()));
107 
108  return jac;
109 }
110 
111 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
114  const control_vector_t& u,
115  const SCALAR t)
116 {
117  MatrixXs jacInput = jacobianInput(x, u, t);
118 
119  VectorXs jac(Eigen::Map<VectorXs>(jacInput.data(), jacInput.rows() * jacInput.cols()));
120  return jac;
121 }
122 
123 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
124 void ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::sparsityPatternState(Eigen::VectorXi& rows, Eigen::VectorXi& cols)
125 {
126  genBlockIndices(getConstraintSize(), STATE_DIM, rows, cols);
127 }
128 
129 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
130 void ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::sparsityPatternInput(Eigen::VectorXi& rows, Eigen::VectorXi& cols)
131 {
132  genBlockIndices(getConstraintSize(), CONTROL_DIM, rows, cols);
133 }
134 
135 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
137  Eigen::VectorXi& iRow_vec,
138  Eigen::VectorXi& jCol_vec)
139 {
140  iRow_vec.resize(num_elements);
141  jCol_vec.resize(num_elements);
142 
143  size_t count = 0;
144 
145  for (size_t i = 0; i < num_elements; ++i)
146  {
147  iRow_vec(count) = i;
148  jCol_vec(count) = i;
149  count++;
150  }
151 }
152 
153 
154 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
156  Eigen::VectorXi& iRow_vec,
157  Eigen::VectorXi& jCol_vec)
158 {
159  // make sure the sparsity pattern is correct and consists only of ones and zeros
160  assert(diag_sparsity.maxCoeff() <= 1);
161  assert(diag_sparsity.minCoeff() >= 0);
162 
163  const int num_elements = diag_sparsity.sum();
164 
165  iRow_vec.resize(num_elements);
166  jCol_vec.resize(num_elements);
167 
168  size_t count = 0;
169 
170  for (int i = 0; i < diag_sparsity.rows(); ++i)
171  {
172  if (diag_sparsity(i) == 1)
173  {
174  iRow_vec(count) = i;
175  jCol_vec(count) = i;
176  count++;
177  }
178  }
179 }
180 
181 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
183  const size_t num_cols,
184  Eigen::VectorXi& iRow_vec,
185  Eigen::VectorXi& jCol_vec)
186 {
187  size_t num_gen_indices = num_rows * num_cols;
188 
189  iRow_vec.resize(num_gen_indices);
190  jCol_vec.resize(num_gen_indices);
191 
192  size_t count = 0;
193 
194  for (size_t row = 0; row < num_rows; ++row)
195  {
196  for (size_t col = 0; col < num_cols; ++col)
197  {
198  iRow_vec(count) = row;
199  jCol_vec(count) = col;
200  count++;
201  }
202  }
203 }
204 
205 
206 } // namespace optcon
207 } // namespace ct
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: ConstraintBase.h:32
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ConstraintBase.h:31
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
CppAD::AD< CppAD::cg::CG< double > > SCALAR
for i
Definition: mpc_unittest_plotting.m:14
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
ConstraintBase(std::string name="Unnamed")
Custom constructor.
Definition: ConstraintBase-impl.h:12
Base class for the constraints used in this toolbox.
Definition: ConstraintBase.h:21