- 3.0.2 optimal control module.
CostFunctionAD.hpp
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 
10 #include <ct/core/core.h>
11 #include <memory>
12 
13 #include <boost/property_tree/ptree.hpp>
14 #include <boost/property_tree/info_parser.hpp>
15 #include <boost/algorithm/string.hpp>
16 
18 #include "utility/utilities.hpp"
19 
20 #include "term/TermLoadMacros.hpp"
21 
22 namespace ct {
23 namespace optcon {
24 
36 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
37 class CostFunctionAD : public CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>
38 {
39 public:
40  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
41 
42  typedef core::DerivativesCppadJIT<STATE_DIM + CONTROL_DIM + 1, 1> JacCG;
43  typedef typename JacCG::CG_SCALAR CGScalar;
44  typedef Eigen::Matrix<CGScalar, 1, 1> MatrixCg;
45 
46  typedef Eigen::Matrix<SCALAR, STATE_DIM, STATE_DIM> state_matrix_t;
47  typedef Eigen::Matrix<SCALAR, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
48  typedef Eigen::Matrix<SCALAR, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
49 
50  typedef core::StateVector<STATE_DIM, SCALAR> state_vector_t;
51  typedef core::ControlVector<CONTROL_DIM, SCALAR> control_vector_t;
52  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> VectorXs;
53  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> MatrixXs;
54 
58  CostFunctionAD();
59 
65  CostFunctionAD(const std::string& filename, bool verbose = false);
66 
71  CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>* clone() const override;
72 
77  CostFunctionAD(const CostFunctionAD& arg);
78 
79 
83  virtual ~CostFunctionAD();
84 
85 
90  virtual void initialize() override;
91 
101  void addIntermediateADTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> term,
102  bool verbose = false) override;
103 
113  void addFinalADTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> term,
114  bool verbose = false) override;
115 
116  void setCurrentStateAndControl(const state_vector_t& x, const control_vector_t& u, const SCALAR& t = 0.0) override;
117 
118  void loadFromConfigFile(const std::string& filename, bool verbose = false) override;
119 
120  SCALAR evaluateIntermediate() override;
121  SCALAR evaluateTerminal() override;
122 
123  state_vector_t stateDerivativeIntermediate() override;
124  state_vector_t stateDerivativeTerminal() override;
125 
126  control_vector_t controlDerivativeIntermediate() override;
127  control_vector_t controlDerivativeTerminal() override;
128 
129  state_matrix_t stateSecondDerivativeIntermediate() override;
130  state_matrix_t stateSecondDerivativeTerminal() override;
131 
132  control_matrix_t controlSecondDerivativeIntermediate() override;
133  control_matrix_t controlSecondDerivativeTerminal() override;
134 
135  control_state_matrix_t stateControlDerivativeIntermediate() override;
136  control_state_matrix_t stateControlDerivativeTerminal() override;
137 
138  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> getIntermediateADTermById(const size_t id);
139 
140  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> getFinalADTermById(const size_t id);
141 
142  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> getIntermediateADTermByName(
143  const std::string& name);
144 
145  std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> getFinalADTermByName(const std::string& name);
146 
147 
148 private:
149  MatrixCg evaluateIntermediateCg(const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime);
150  MatrixCg evaluateTerminalCg(const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime);
151 
153  Eigen::Matrix<SCALAR, STATE_DIM + CONTROL_DIM + 1, 1> stateControlTime_;
154 
156  std::vector<std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>>> intermediateTerms_;
158  std::vector<std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>>> finalTerms_;
159 
161  std::shared_ptr<JacCG> intermediateCostCodegen_;
162  std::shared_ptr<JacCG> finalCostCodegen_;
163 
165  typename JacCG::FUN_TYPE_CG intermediateFun_;
166  typename JacCG::FUN_TYPE_CG finalFun_;
167 };
168 
169 } // namespace optcon
170 } // namespace ct
171 
172 #endif
Eigen::Matrix< double, nControls, nControls > control_matrix_t
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
Eigen::Matrix< double, nControls, 1 > control_vector_t
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, nStates > state_matrix_t
Eigen::Matrix< double, nStates, 1 > state_vector_t
const bool verbose
Definition: ConstraintComparison.h:18