- 3.0.2 optimal control module.
TermBase.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 
7 #pragma once
8 
9 #include <boost/algorithm/string.hpp>
10 
12 
13 namespace ct {
14 namespace optcon {
15 
29 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL = double, typename SCALAR = SCALAR_EVAL>
30 class TermBase
31 {
32 protected:
34  std::string name_;
36  std::shared_ptr<ct::core::tpl::ActivationBase<SCALAR_EVAL>> c_i_;
37 
38 public:
39  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_t;
40  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
41  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
42  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_double_t;
43  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_double_t;
44  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_double_t;
45 
50  TermBase(std::string name = "Unnamed");
51 
56  TermBase(const TermBase& arg);
57 
63 
67  virtual ~TermBase();
68 
78  virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
79  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
80  const SCALAR& t) = 0;
81 
82 #ifdef CPPADCG
83 
92  virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
94  ct::core::ADCGScalar t);
95 #endif
96 
107  SCALAR_EVAL eval(const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& x,
108  const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, 1>& u,
109  const SCALAR_EVAL& t);
110 
118  virtual bool isActiveAtTime(SCALAR_EVAL t);
119 
121  SCALAR_EVAL computeActivation(SCALAR_EVAL t);
122 
127  const SCALAR_EVAL& t);
128 
132  const SCALAR_EVAL& t);
133 
138  const SCALAR_EVAL& t);
139 
143  const SCALAR_EVAL& t);
144 
146  virtual control_state_matrix_t stateControlDerivative(const core::StateVector<STATE_DIM, SCALAR_EVAL>& x,
148  const SCALAR_EVAL& t);
149 
151  virtual void loadConfigFile(const std::string& filename, const std::string& termName, bool verbose = false);
152 
154  void setTimeActivation(std::shared_ptr<ct::core::tpl::ActivationBase<SCALAR_EVAL>> c_i, bool verbose = false);
155 
157  void loadTimeActivation(const std::string& filename, const std::string& termName, bool verbose = false);
158 
163  const std::string& getName() const;
164 
169  void setName(const std::string& termName);
170 
172  virtual void updateReferenceState(const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& newRefState);
173 
175  virtual void updateReferenceControl(const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, 1>& newRefControl);
176 
178  virtual Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1> getReferenceState() const;
179 };
180 
181 } // namespace optcon
182 } // namespace ct
virtual Eigen::Matrix< SCALAR_EVAL, STATE_DIM, 1 > getReferenceState() const
retrieve this term&#39;s current reference state
Definition: TermBase-impl.hpp:207
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermBase.hpp:42
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: TermBase.hpp:44
virtual SCALAR evaluate(const Eigen::Matrix< SCALAR, STATE_DIM, 1 > &x, const Eigen::Matrix< SCALAR, CONTROL_DIM, 1 > &u, const SCALAR &t)=0
Evaluates the term at x, u, t.
void setName(const std::string &termName)
Sets the name of the term.
Definition: TermBase-impl.hpp:189
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
SCALAR_EVAL eval(const Eigen::Matrix< SCALAR_EVAL, STATE_DIM, 1 > &x, const Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, 1 > &u, const SCALAR_EVAL &t)
Gets called by the analytical costfunction. Adds time dependent activations on top of the term...
Definition: TermBase-impl.hpp:32
virtual ~TermBase()
Destructor.
Definition: TermBase-impl.hpp:26
virtual TermBase< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const =0
Deep-copy term.
virtual void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false)
load this term from a configuration file
Definition: TermBase-impl.hpp:127
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: TermBase.hpp:41
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
TermBase(std::string name="Unnamed")
Default constructor.
Definition: TermBase-impl.hpp:13
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual void updateReferenceState(const Eigen::Matrix< SCALAR_EVAL, STATE_DIM, 1 > &newRefState)
updates the reference state for this term
Definition: TermBase-impl.hpp:195
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
const std::string & getName() const
Returns the name of the term.
Definition: TermBase-impl.hpp:183
virtual control_state_matrix_t stateControlDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t)
compute the cross-term derivative (state-control) of this cost function term
Definition: TermBase-impl.hpp:115
std::shared_ptr< ct::core::tpl::ActivationBase< SCALAR_EVAL > > c_i_
time activations for this term
Definition: TermBase.hpp:36
SCALAR_EVAL computeActivation(SCALAR_EVAL t)
compute time activation
Definition: TermBase-impl.hpp:58
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermBase.hpp:39
void setTimeActivation(std::shared_ptr< ct::core::tpl::ActivationBase< SCALAR_EVAL >> c_i, bool verbose=false)
set the time activation functions for this term
Definition: TermBase-impl.hpp:137
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: TermBase.hpp:43
virtual void updateReferenceControl(const Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, 1 > &newRefControl)
updates the reference control for this term
Definition: TermBase-impl.hpp:201
const bool verbose
Definition: ConstraintComparison.h:18
virtual core::StateVector< STATE_DIM, SCALAR_EVAL > stateDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t)
compute derivative of this cost term w.r.t. the state
Definition: TermBase-impl.hpp:64
virtual state_matrix_t stateSecondDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t)
compute second order derivative of this cost term w.r.t. the state
Definition: TermBase-impl.hpp:77
std::string name_
a name identifier for this term
Definition: TermBase.hpp:34
void loadTimeActivation(const std::string &filename, const std::string &termName, bool verbose=false)
load the time activation functions for this term from file
Definition: TermBase-impl.hpp:147
virtual core::ControlVector< CONTROL_DIM, SCALAR_EVAL > controlDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t)
compute derivative of this cost term w.r.t. the control input
Definition: TermBase-impl.hpp:89
virtual control_matrix_t controlSecondDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t)
compute second order derivative of this cost term w.r.t. the control input
Definition: TermBase-impl.hpp:102
virtual bool isActiveAtTime(SCALAR_EVAL t)
Returns if term is non-zero at a specific time By default, all terms are evaluated at all times...
Definition: TermBase-impl.hpp:52
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermBase.hpp:40