- 3.0.2 optimal control module.
TermBase-impl.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 namespace ct {
9 namespace optcon {
10 
11 
12 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
14  : name_(name),
15  c_i_(
16  std::shared_ptr<ct::core::tpl::ActivationBase<SCALAR_EVAL>>(new ct::core::tpl::ActivationBase<SCALAR_EVAL>()))
17 {
18 }
19 
20 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
22 {
23 }
24 
25 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
27 {
28 }
29 
30 
31 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
33  const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& x,
34  const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, 1>& u,
35  const SCALAR_EVAL& t)
36 {
37  return computeActivation(t) * evaluate(x, u, t);
38 }
39 
40 #ifdef CPPADCG
41 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
45  ct::core::ADCGScalar t)
46 {
47  throw std::runtime_error("The cost function term term " + name_ + " does not implement evaluate CppadCg.");
48 }
49 #endif
50 
51 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
53 {
54  return c_i_->isActive(t);
55 }
56 
57 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
59 {
60  return c_i_->computeActivation(t);
61 }
62 
63 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
67  const SCALAR_EVAL& t)
68 {
69  throw std::runtime_error(
70  "This cost function element is not implemented "
71  "for the given term. Please use either auto-diff cost function "
72  "or implement the analytical derivatives manually.");
73 }
74 
75 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
80  const SCALAR_EVAL& t)
81 {
82  throw std::runtime_error(
83  "This cost function element is not implemented "
84  "for the given term. Please use either auto-diff cost function or "
85  "implement the analytical derivatives manually.");
86 }
87 
88 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
92  const SCALAR_EVAL& t)
93 {
94  throw std::runtime_error(
95  "This cost function element is not implemented "
96  "for the given term. Please use either auto-diff cost function "
97  "or implement the analytical derivatives manually.");
98 }
99 
100 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
105  const SCALAR_EVAL& t)
106 {
107  throw std::runtime_error(
108  "This cost function element is not implemented "
109  "for the given term. Please use either auto-diff cost function "
110  "or implement the analytical derivatives manually.");
111 }
112 
113 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
118  const SCALAR_EVAL& t)
119 {
120  throw std::runtime_error(
121  "This cost function element is not implemented "
122  "for the given term. Please use either auto-diff cost function "
123  "or implement the analytical derivatives manually.");
124 }
125 
126 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
128  const std::string& termName,
129  bool verbose)
130 {
131  throw std::runtime_error(
132  "This cost function element is not implemented for the given term. Please use either auto-diff cost function "
133  "or implement the analytical derivatives manually.");
134 }
135 
136 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
138  std::shared_ptr<ct::core::tpl::ActivationBase<SCALAR_EVAL>> c_i,
139  bool verbose)
140 {
141  c_i_ = c_i;
142  if (verbose)
143  c_i_->printInfo();
144 }
145 
146 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
148  const std::string& termName,
149  bool verbose)
150 {
151  if (verbose)
152  std::cout << "TermBase: loading TimeActivation ..." << std::endl;
153 
154  boost::property_tree::ptree pt;
155  boost::property_tree::read_info(filename, pt);
156 
157  try
158  {
159  std::string activationKind = pt.get<std::string>(termName + ".time_activation" + ".kind");
160  boost::algorithm::to_lower(activationKind);
161  std::shared_ptr<ct::core::tpl::ActivationBase<SCALAR_EVAL>> c_i;
162  CT_LOADABLE_ACTIVATIONS(SCALAR_EVAL);
163  c_i->loadConfigFile(filename, termName + ".time_activation", verbose);
164  if (!c_i)
165  {
166  throw std::runtime_error("Activation type \"" + activationKind + "\" not supported");
167  }
168  else
169  {
170  c_i_ = c_i;
171  if (verbose)
172  c_i_->printInfo();
173  }
174  } catch (std::exception& e)
175  {
176  if (verbose)
177  std::cout << "TermBase: encountered exception while loading TimeActivation." << std::endl;
178  return;
179  }
180 }
181 
182 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
184 {
185  return name_;
186 }
187 
188 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
190 {
191  name_ = termName;
192 }
193 
194 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
196  const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& newRefState)
197 {
198 }
199 
200 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
202  const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, 1>& newRefControl)
203 {
204 }
205 
206 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
208  const
209 {
210  throw std::runtime_error("getReferenceState is not implemented for the current term!");
211 }
212 
213 
214 } // namespace optcon
215 } // 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
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 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
tpl::ActivationBase< double > ActivationBase
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
#define CT_LOADABLE_ACTIVATIONS(SCALAR)
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
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