- 3.0.2 optimal control module.
TermSmoothAbs.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 #include "TermBase.hpp"
9 
10 namespace ct {
11 namespace optcon {
12 
22 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL = double, typename SCALAR = SCALAR_EVAL>
23 class TermSmoothAbs : public TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>
24 {
25 public:
26  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
27 
28  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_t;
29  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
30  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
31  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_double_t;
32  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_double_t;
33  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_double_t;
34 
39  const SCALAR_EVAL alpha = 0.2);
40 
41  TermSmoothAbs() = default;
42 
43  TermSmoothAbs(const TermSmoothAbs& arg);
44 
46 
47  virtual ~TermSmoothAbs();
48 
49  SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
50  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
51  const SCALAR& t) override;
52 
53 #ifdef CPPADCG
54  virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
56  ct::core::ADCGScalar t) override;
57 #endif
58 
61  const SCALAR_EVAL& t) override;
62 
65  const SCALAR_EVAL& t) override;
66 
69  const SCALAR_EVAL& t) override;
70 
73  const SCALAR_EVAL& t) override;
74 
77  const SCALAR_EVAL& t) override;
78 
79  void loadConfigFile(const std::string& filename,
80  const std::string& termName,
81  bool verbose = false) override; // virtual function for data loading
82 
83 protected:
84  template <typename SC>
85  SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, const SC& t);
86 
91  SCALAR_EVAL alphaSquared_;
92 };
93 
94 
95 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
96 template <typename SC>
98  const Eigen::Matrix<SC, CONTROL_DIM, 1>& u,
99  const SC& t)
100 {
101  const Eigen::Matrix<SC, STATE_DIM, 1> xerr2 = (x - x_ref_.template cast<SC>()).array().square();
102  const Eigen::Matrix<SC, CONTROL_DIM, 1> uerr2 = (u - u_ref_.template cast<SC>()).array().square();
103 
104  const Eigen::Matrix<SC, STATE_DIM, 1> xerrAbs =
105  (xerr2 + core::StateVector<STATE_DIM, SC>::Ones() * static_cast<SC>(alphaSquared_)).cwiseSqrt();
106  const Eigen::Matrix<SC, CONTROL_DIM, 1> uerrAbs =
107  (uerr2 + core::ControlVector<CONTROL_DIM, SC>::Ones() * static_cast<SC>(alphaSquared_)).cwiseSqrt();
108 
109  return a_.template cast<SC>().dot(xerrAbs) + b_.template cast<SC>().dot(uerrAbs);
110 }
111 
112 
113 } // namespace optcon
114 } // namespace ct
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > b_
Definition: TermSmoothAbs.hpp:89
SC evalLocal(const Eigen::Matrix< SC, STATE_DIM, 1 > &x, const Eigen::Matrix< SC, CONTROL_DIM, 1 > &u, const SC &t)
Definition: TermSmoothAbs.hpp:97
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermSmoothAbs.hpp:28
SCALAR_EVAL alphaSquared_
Definition: TermSmoothAbs.hpp:91
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermSmoothAbs.hpp:31
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) override
compute derivative of this cost term w.r.t. the control input
Definition: TermSmoothAbs-impl.hpp:96
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
core::StateVector< STATE_DIM, SCALAR_EVAL > a_
Definition: TermSmoothAbs.hpp:87
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: TermSmoothAbs.hpp:30
state_matrix_t stateSecondDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t) override
compute second order derivative of this cost term w.r.t. the state
Definition: TermSmoothAbs-impl.hpp:81
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
control_matrix_t controlSecondDerivative(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u, const SCALAR_EVAL &t) override
compute second order derivative of this cost term w.r.t. the control input
Definition: TermSmoothAbs-impl.hpp:110
CppAD::AD< CppAD::cg::CG< double > > SCALAR
TermSmoothAbs< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const override
Deep-copy term.
Definition: TermSmoothAbs-impl.hpp:35
SCALAR evaluate(const Eigen::Matrix< SCALAR, STATE_DIM, 1 > &x, const Eigen::Matrix< SCALAR, CONTROL_DIM, 1 > &u, const SCALAR &t) override
Evaluates the term at x, u, t.
Definition: TermSmoothAbs-impl.hpp:47
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: TermSmoothAbs.hpp:32
void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false) override
load this term from a configuration file
Definition: TermSmoothAbs-impl.hpp:134
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) override
compute the cross-term derivative (state-control) of this cost function term
Definition: TermSmoothAbs-impl.hpp:125
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > u_ref_
Definition: TermSmoothAbs.hpp:90
core::StateVector< STATE_DIM, SCALAR_EVAL > x_ref_
Definition: TermSmoothAbs.hpp:88
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermSmoothAbs.hpp:29
const bool verbose
Definition: ConstraintComparison.h:18
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: TermSmoothAbs.hpp:33
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) override
compute derivative of this cost term w.r.t. the state
Definition: TermSmoothAbs-impl.hpp:67
A smooth absolute term of type where this calculation is performed component-wise and summed with in...
Definition: TermSmoothAbs.hpp:23
virtual ~TermSmoothAbs()
Definition: TermSmoothAbs-impl.hpp:42