- 3.0.2 optimal control module.
TermSmoothAbs-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 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
17  const SCALAR_EVAL alpha)
18  : a_(a), x_ref_(x_ref), b_(b), u_ref_(u_ref), alphaSquared_(alpha * alpha)
19 {
20 }
21 
22 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
24  : TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>(arg),
25  a_(arg.a_),
26  x_ref_(arg.x_ref_),
27  b_(arg.b_),
28  u_ref_(arg.u_ref_),
30 {
31 }
32 
33 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
36 {
38 }
39 
40 
41 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
43 {
44 }
45 
46 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
48  const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
49  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
50  const SCALAR& t)
51 {
52  return evalLocal<SCALAR>(x, u, t);
53 }
54 
55 #ifdef CPPADCG
56 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
60  ct::core::ADCGScalar t)
61 {
62  return evalLocal<ct::core::ADCGScalar>(x, u, t);
63 }
64 #endif
65 
66 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
70  const SCALAR_EVAL& t)
71 {
72  return (a_.array() * (x - x_ref_).array() *
73  ((x - x_ref_).array().square() + Eigen::Array<SCALAR_EVAL, STATE_DIM, 1>::Ones() * alphaSquared_)
74  .sqrt()
75  .inverse())
76  .matrix();
77 }
78 
79 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
84  const SCALAR_EVAL& t)
85 {
86  // return as diagonal matrix
87  return (a_.array() * alphaSquared_ *
88  (Eigen::Array<SCALAR_EVAL, STATE_DIM, 1>::Ones() * alphaSquared_ + (x - x_ref_).array().square())
89  .pow(-3.0 / 2.0))
90  .matrix()
91  .asDiagonal();
92 }
93 
94 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
99  const SCALAR_EVAL& t)
100 {
101  return (b_.array() * (u - u_ref_).array() *
102  ((u - u_ref_).array().square() + Eigen::Array<SCALAR_EVAL, CONTROL_DIM, 1>::Ones() * alphaSquared_)
103  .sqrt()
104  .inverse())
105  .matrix();
106 }
107 
108 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
113  const SCALAR_EVAL& t)
114 {
115  // return as diagonal matrix
116  return (b_.array() * alphaSquared_ *
117  (Eigen::Array<SCALAR_EVAL, CONTROL_DIM, 1>::Ones() * alphaSquared_ + (u - u_ref_).array().square())
118  .pow(-3.0 / 2.0))
119  .matrix()
120  .asDiagonal();
121 }
122 
123 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
128  const SCALAR_EVAL& t)
129 {
130  return control_state_matrix_t::Zero();
131 }
132 
133 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
135  const std::string& termName,
136  bool verbose)
137 {
138  // read in the file and put the valus in a_ and b_
139  a_.setZero();
140  x_ref_.setZero();
141  b_.setZero();
142  u_ref_.setZero();
143 
144  loadMatrixCF(filename, "a", a_, termName);
145  loadMatrixCF(filename, "x_ref", x_ref_, termName);
146  loadMatrixCF(filename, "b", b_, termName);
147  loadMatrixCF(filename, "u_ref", u_ref_, termName);
148  loadScalarCF(filename, "alpha", alphaSquared_, termName);
150 
151  if (verbose)
152  {
153  std::cout << "Reading " << termName;
154  std::cout << "\nRead a as a= " << a_.transpose();
155  std::cout << "\nRead x_ref as x_ref= " << x_ref_.transpose();
156  std::cout << "\nRead b as b= " << b_.transpose();
157  std::cout << "\nRead u_ref as u_ref= " << u_ref_.transpose();
158  std::cout << "\nRead alpha^2 as " << alphaSquared_ << std::endl;
159  }
160 }
161 } // namespace optcon
162 } // namespace ct
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > b_
Definition: TermSmoothAbs.hpp:89
SCALAR_EVAL alphaSquared_
Definition: TermSmoothAbs.hpp:91
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
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
void loadScalarCF(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const std::string &termName="")
Definition: utilities.hpp:21
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
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, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermBase.hpp:39
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermSmoothAbs.hpp:29
const bool verbose
Definition: ConstraintComparison.h:18
void loadMatrixCF(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &termName="")
Definition: utilities.hpp:46
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