- 3.0.2 optimal control module.
ConstraintBase.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 
20 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
22 {
23 public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
27 
30 
31  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> VectorXs;
32  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> MatrixXs;
33 
39  ConstraintBase(std::string name = "Unnamed");
40 
46  ConstraintBase(const ConstraintBase& arg);
47 
54 
58  virtual ~ConstraintBase();
59 
70  virtual VectorXs evaluate(const state_vector_t& x, const control_vector_t& u, const SCALAR t) = 0;
71 
82 #ifdef CPPADCG
83  virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
86  ct::core::ADCGScalar t);
87 #endif
88 
94  virtual size_t getConstraintSize() const = 0;
95 
96 
102  virtual MatrixXs jacobianState(const state_vector_t& x, const control_vector_t& u, const SCALAR t);
103 
109  virtual MatrixXs jacobianInput(const state_vector_t& x, const control_vector_t& u, const SCALAR t);
110 
116  virtual VectorXs getLowerBound() const;
117 
123  virtual VectorXs getUpperBound() const;
124 
130  void getName(std::string& constraintName) const;
131 
137  void setName(const std::string constraintName);
138 
146  virtual size_t getNumNonZerosJacobianState() const;
147 
155  virtual size_t getNumNonZerosJacobianInput() const;
156 
164  virtual VectorXs jacobianStateSparse(const state_vector_t& x, const control_vector_t& u, const SCALAR t);
165 
173  virtual VectorXs jacobianInputSparse(const state_vector_t& x, const control_vector_t& u, const SCALAR t);
174 
175 
186  virtual void sparsityPatternState(Eigen::VectorXi& rows, Eigen::VectorXi& cols);
187 
198  virtual void sparsityPatternInput(Eigen::VectorXi& rows, Eigen::VectorXi& cols);
199 
200 
201 protected:
202  VectorXs lb_;
203  VectorXs ub_;
204 
212  static void genDiagonalIndices(const size_t num_elements, Eigen::VectorXi& iRow_vec, Eigen::VectorXi& jCol_vec);
213 
221  static void genSparseDiagonalIndices(const Eigen::VectorXi& diag_sparsity,
222  Eigen::VectorXi& iRow_vec,
223  Eigen::VectorXi& jCol_vec);
224 
233  static void genBlockIndices(const size_t num_rows,
234  const size_t num_cols,
235  Eigen::VectorXi& iRow_vec,
236  Eigen::VectorXi& jCol_vec);
237 
238 private:
239  std::string name_;
240 };
241 
242 } // namespace optcon
243 } // namespace ct
virtual VectorXs jacobianStateSparse(const state_vector_t &x, const control_vector_t &u, const SCALAR t)
Returns the constraint jacobian wrt state in sparse structure. The default implementation maps the Ja...
Definition: ConstraintBase-impl.h:100
core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: ConstraintBase.h:28
virtual VectorXs getLowerBound() const
Returns the lower constraint bound.
Definition: ConstraintBase-impl.h:62
void getName(std::string &constraintName) const
Returns the constraint name.
Definition: ConstraintBase-impl.h:75
virtual ConstraintBase< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const =0
Creates a new instance of the object with same properties than original.
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
virtual MatrixXs jacobianState(const state_vector_t &x, const control_vector_t &u, const SCALAR t)
Returns the constraint jacobian wrt state.
Definition: ConstraintBase-impl.h:40
static void genDiagonalIndices(const size_t num_elements, Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec)
upper bound on the constraints
Definition: ConstraintBase-impl.h:136
virtual VectorXs jacobianInputSparse(const state_vector_t &x, const control_vector_t &u, const SCALAR t)
Returns the constraint jacobian wrt control input in sparse structure. The default implementation map...
Definition: ConstraintBase-impl.h:113
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: ConstraintBase.h:32
core::ControlVector< CONTROL_DIM, SCALAR > control_vector_t
Definition: ConstraintBase.h:29
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
static void genSparseDiagonalIndices(const Eigen::VectorXi &diag_sparsity, Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec)
Generates indices of a sparse diagonal square matrix.
Definition: ConstraintBase-impl.h:155
void setName(const std::string constraintName)
Sets the constraint name.
Definition: ConstraintBase-impl.h:81
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual void sparsityPatternInput(Eigen::VectorXi &rows, Eigen::VectorXi &cols)
Generates the sparsity pattern of the jacobian wrt control input. The default implementation returns ...
Definition: ConstraintBase-impl.h:130
virtual VectorXs evaluate(const state_vector_t &x, const control_vector_t &u, const SCALAR t)=0
The evaluation of the constraint violation. Note this method is SCALAR typed.
virtual size_t getConstraintSize() const =0
The evaluate method used for jit compilation in constraint container ad.
VectorXs ub_
lower bound on the constraints
Definition: ConstraintBase.h:203
virtual size_t getNumNonZerosJacobianState() const
Returns the number of nonzeros in the jacobian wrt state. The default implementation assumes a dense ...
Definition: ConstraintBase-impl.h:87
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
virtual VectorXs getUpperBound() const
Returns the upper constraint bound.
Definition: ConstraintBase-impl.h:69
virtual ~ConstraintBase()
Destructor.
Definition: ConstraintBase-impl.h:23
ConstraintBase(std::string name="Unnamed")
Custom constructor.
Definition: ConstraintBase-impl.h:12
VectorXs lb_
Definition: ConstraintBase.h:202
virtual void sparsityPatternState(Eigen::VectorXi &rows, Eigen::VectorXi &cols)
Generates the sparsity pattern of the jacobian wrt state. The default implementation returns a vector...
Definition: ConstraintBase-impl.h:124
virtual MatrixXs jacobianInput(const state_vector_t &x, const control_vector_t &u, const SCALAR t)
Returns the constraint jacobian wrt input.
Definition: ConstraintBase-impl.h:51
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef ct::core::tpl::TraitSelector< SCALAR >::Trait Trait
Definition: ConstraintBase.h:26
virtual size_t getNumNonZerosJacobianInput() const
Returns the number of nonzeros in the jacobian wrt control input. The default implementation assumes ...
Definition: ConstraintBase-impl.h:93
static void genBlockIndices(const size_t num_rows, const size_t num_cols, Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec)
Generates indices of a full matrix.
Definition: ConstraintBase-impl.h:182
Base class for the constraints used in this toolbox.
Definition: ConstraintBase.h:21