- 3.0.2 optimal control module.
ConstraintContainerAD.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 #ifdef CPPADCG
9 
11 #include "term/ConstraintBase.h"
12 
13 namespace ct {
14 namespace optcon {
15 
24 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
25 class ConstraintContainerAD : public LinearConstraintContainer<STATE_DIM, CONTROL_DIM, SCALAR>
26 {
27 public:
28  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29 
30  typedef core::DerivativesCppadJIT<STATE_DIM + CONTROL_DIM, -1> JacCG;
31  typedef typename JacCG::CG_SCALAR CGScalar;
32 
33  typedef core::StateVector<STATE_DIM, SCALAR> state_vector_t;
34  typedef core::ControlVector<CONTROL_DIM, SCALAR> input_vector_t;
35 
36  typedef ConstraintContainerAD<STATE_DIM, CONTROL_DIM, SCALAR>* ConstraintContainerAD_Raw_Ptr_t;
37  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> VectorXs;
38  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> MatrixXs;
39 
40 
44  ConstraintContainerAD();
45 
52  ConstraintContainerAD(const state_vector_t& x, const input_vector_t& u, const SCALAR& t = 0.0);
53 
54 
59  ConstraintContainerAD(const ConstraintContainerAD& arg);
60 
66  virtual ConstraintContainerAD_Raw_Ptr_t clone() const override;
67 
71  virtual ~ConstraintContainerAD();
72 
79  void addIntermediateConstraint(std::shared_ptr<ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>> constraint,
80  bool verbose);
81 
88  void addTerminalConstraint(std::shared_ptr<ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>> constraint,
89  bool verbose);
90 
91  virtual VectorXs evaluateIntermediate() override;
92 
93  virtual VectorXs evaluateTerminal() override;
94 
95  virtual size_t getIntermediateConstraintsCount() override;
96 
97  virtual size_t getTerminalConstraintsCount() override;
98 
99  virtual VectorXs jacobianStateSparseIntermediate() override;
100 
101  virtual MatrixXs jacobianStateIntermediate() override;
102 
103  virtual VectorXs jacobianStateSparseTerminal() override;
104 
105  virtual MatrixXs jacobianStateTerminal() override;
106 
107  virtual VectorXs jacobianInputSparseIntermediate() override;
108 
109  virtual MatrixXs jacobianInputIntermediate() override;
110 
111  virtual VectorXs jacobianInputSparseTerminal() override;
112 
113  virtual MatrixXs jacobianInputTerminal() override;
114 
115  virtual void sparsityPatternStateIntermediate(Eigen::VectorXi& iRows, Eigen::VectorXi& jCols) override;
116 
117  virtual void sparsityPatternStateTerminal(Eigen::VectorXi& iRows, Eigen::VectorXi& jCols) override;
118 
119  virtual void sparsityPatternInputIntermediate(Eigen::VectorXi& iRows, Eigen::VectorXi& jCols) override;
120 
121  virtual void sparsityPatternInputTerminal(Eigen::VectorXi& iRows, Eigen::VectorXi& jCols) override;
122 
123  virtual size_t getJacobianStateNonZeroCountIntermediate() override;
124 
125  virtual size_t getJacobianStateNonZeroCountTerminal() override;
126 
127  virtual size_t getJacobianInputNonZeroCountIntermediate() override;
128 
129  virtual size_t getJacobianInputNonZeroCountTerminal() override;
130 
131  virtual bool initializeIntermediate() override;
132 
133  virtual bool initializeTerminal() override;
134 
135 private:
136  // cache the matrix here, no need to call it at every eval matrix
137  virtual void update() override;
138 
147  Eigen::Matrix<CGScalar, Eigen::Dynamic, 1> evaluateIntermediateCodegen(
148  const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM, 1>& stateinput);
149 
158  Eigen::Matrix<CGScalar, Eigen::Dynamic, 1> evaluateTerminalCodegen(
159  const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM, 1>& stateinput);
160 
161  //containers
162  std::vector<std::shared_ptr<ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>>> constraintsIntermediate_;
163  std::vector<std::shared_ptr<ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>>> constraintsTerminal_;
164 
165  std::shared_ptr<JacCG> intermediateCodegen_;
166  std::shared_ptr<JacCG> terminalCodegen_;
167 
168  typename JacCG::FUN_TYPE_CG fIntermediate_;
169  typename JacCG::FUN_TYPE_CG fTerminal_;
170 
171  Eigen::VectorXi sparsityIntermediateRows_;
172  Eigen::VectorXi sparsityStateIntermediateRows_;
173  Eigen::VectorXi sparsityStateIntermediateCols_;
174  Eigen::VectorXi sparsityInputIntermediateRows_;
175  Eigen::VectorXi sparsityInputIntermediateCols_;
176 
177  Eigen::VectorXi sparsityTerminalRows_;
178  Eigen::VectorXi sparsityStateTerminalRows_;
179  Eigen::VectorXi sparsityStateTerminalCols_;
180  Eigen::VectorXi sparsityInputTerminalRows_;
181  Eigen::VectorXi sparsityInputTerminalCols_;
182 
183 
184  Eigen::Matrix<SCALAR, STATE_DIM + CONTROL_DIM, 1> stateControlD_;
185 };
186 
187 
188 } // namespace optcon
189 } // namespace ct
190 
191 #endif
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
CppAD::AD< CppAD::cg::CG< double > > SCALAR
ct::core::DerivativesCppadCG< state_dim, control_dim > JacCG
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Eigen::Matrix< double, nStates, 1 > state_vector_t
const bool verbose
Definition: ConstraintComparison.h:18