- 3.0.2 optimal control module.
TermMixed.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 
20 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL = double, typename SCALAR = SCALAR_EVAL>
21 class TermMixed : public TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>
22 {
23 public:
24  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
25 
26  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_t;
27  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
28  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
29  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_double_t;
30  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_double_t;
31  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_double_t;
32 
33  TermMixed();
34 
35  TermMixed(const control_state_matrix_t& P);
36 
37  TermMixed(const control_state_matrix_t& P,
40 
41  TermMixed(const TermMixed& arg);
42 
43  virtual ~TermMixed();
44 
46 
47  void setWeights(const control_state_matrix_double_t& P);
48 
51 
52  virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
53  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
54  const SCALAR& t) override;
55 
56 #ifdef CPPADCG
57  virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
59  ct::core::ADCGScalar t) override;
60 #endif
61 
64  const SCALAR_EVAL& t) override;
65 
68  const SCALAR_EVAL& t) override;
69 
72  const SCALAR_EVAL& t) override;
73 
76  const SCALAR_EVAL& t) override;
77 
80  const SCALAR_EVAL& t) override;
81 
82  virtual void loadConfigFile(const std::string& filename,
83  const std::string& termName,
84  bool verbose = false) override;
85 
86  void updateReferenceState(const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& newRefState) override;
87 
88 protected:
89  template <typename SC>
90  SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, const SC& t);
91 
92  control_state_matrix_t P_;
93 
96 };
97 
98 
99 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
100 template <typename SC>
102  const Eigen::Matrix<SC, CONTROL_DIM, 1>& u,
103  const SC& t)
104 {
105  Eigen::Matrix<SC, STATE_DIM, 1> xDiff = (x - x_ref_.template cast<SC>());
106  Eigen::Matrix<SC, CONTROL_DIM, 1> uDiff = (u - u_ref_.template cast<SC>());
107 
108  return (uDiff.transpose() * P_.template cast<SC>() * xDiff)(0, 0);
109 }
110 
111 
112 } // namespace optcon
113 } // namespace ct
TermMixed()
Definition: TermMixed-impl.hpp:19
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: TermMixed.hpp:28
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
A basic quadratic term of type .
Definition: TermMixed.hpp:21
TermMixed< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const override
Deep-copy term.
Definition: TermMixed-impl.hpp:42
void setWeights(const control_state_matrix_double_t &P)
Definition: TermMixed-impl.hpp:54
virtual 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: TermMixed-impl.hpp:69
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
void setStateAndControlReference(const core::StateVector< STATE_DIM > &x_ref, const core::ControlVector< CONTROL_DIM > &u_ref)
Definition: TermMixed-impl.hpp:60
SC evalLocal(const Eigen::Matrix< SC, STATE_DIM, 1 > &x, const Eigen::Matrix< SC, CONTROL_DIM, 1 > &u, const SC &t)
Definition: TermMixed.hpp:101
control_state_matrix_t P_
Definition: TermMixed.hpp:92
CppAD::AD< CppAD::cg::CG< double > > SCALAR
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: TermMixed.hpp:30
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermMixed.hpp:26
core::StateVector< STATE_DIM, SCALAR_EVAL > x_ref_
Definition: TermMixed.hpp:94
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermMixed.hpp:29
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: TermMixed-impl.hpp:100
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: TermMixed.hpp:31
virtual ~TermMixed()
Definition: TermMixed-impl.hpp:49
void updateReferenceState(const Eigen::Matrix< SCALAR_EVAL, STATE_DIM, 1 > &newRefState) override
updates the reference state for this term
Definition: TermMixed-impl.hpp:165
virtual void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false) override
load this term from a configuration file
Definition: TermMixed-impl.hpp:140
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: TermMixed-impl.hpp:88
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: TermMixed-impl.hpp:109
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > u_ref_
Definition: TermMixed.hpp:95
const bool verbose
Definition: ConstraintComparison.h:18
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: TermMixed-impl.hpp:121
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermMixed.hpp:27
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: TermMixed-impl.hpp:131