- 3.0.2 optimal control module.
TermQuadratic.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 
21 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL = double, typename SCALAR = SCALAR_EVAL>
22 class TermQuadratic : public TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>
23 {
24 public:
25  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 
27  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_t;
28  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
29  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
30  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_double_t;
31  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_double_t;
32  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_double_t;
33 
34  TermQuadratic();
35 
36  TermQuadratic(const state_matrix_t& Q, const control_matrix_t& R);
37 
38  TermQuadratic(const state_matrix_t& Q,
39  const control_matrix_t& R,
42 
43  TermQuadratic(const std::string& configFile, const std::string& termName, bool verbose = false);
44 
45  TermQuadratic(const TermQuadratic& arg);
46 
47  virtual ~TermQuadratic();
48 
50 
51  void setWeights(const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM>& Q,
52  const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM>& R);
53 
54  const state_matrix_t& getStateWeight() const;
55 
56  state_matrix_t& getStateWeight();
57 
58  const control_matrix_t& getControlWeight() const;
59 
60  control_matrix_t& getControlWeight();
61 
64 
65  virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
66  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
67  const SCALAR& t) override;
68 
69 #ifdef CPPADCG
70  virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
72  ct::core::ADCGScalar t) override;
73 #endif
74 
77  const SCALAR_EVAL& t) override;
78 
81  const SCALAR_EVAL& t) override;
82 
85  const SCALAR_EVAL& t) override;
86 
89  const SCALAR_EVAL& t) override;
90 
93  const SCALAR_EVAL& t) override;
94 
95  virtual void loadConfigFile(const std::string& filename,
96  const std::string& termName,
97  bool verbose = false) override;
98 
99  virtual void updateReferenceState(const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& newRefState) override;
100 
101  virtual void updateReferenceControl(const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, 1>& newRefControl) override;
102 
103  virtual Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1> getReferenceState() const override;
104 
105 protected:
106  template <typename SC>
107  SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, const SC& t);
108 
109  state_matrix_t Q_;
110  control_matrix_t R_;
111 
114 };
115 
116 
117 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
118 template <typename SC>
120  const Eigen::Matrix<SC, CONTROL_DIM, 1>& u,
121  const SC& t)
122 {
123  Eigen::Matrix<SC, STATE_DIM, 1> xDiff = (x - x_ref_.template cast<SC>());
124  Eigen::Matrix<SC, CONTROL_DIM, 1> uDiff = (u - u_ref_.template cast<SC>());
125 
126  return (xDiff.transpose() * Q_.template cast<SC>() * xDiff + uDiff.transpose() * R_.template cast<SC>() * uDiff)(
127  0, 0);
128 }
129 
130 } // namespace optcon
131 } // namespace ct
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: TermQuadratic-impl.hpp:186
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: TermQuadratic-impl.hpp:121
A basic quadratic term of type .
Definition: TermQuadratic.hpp:22
virtual void updateReferenceState(const Eigen::Matrix< SCALAR_EVAL, STATE_DIM, 1 > &newRefState) override
updates the reference state for this term
Definition: TermQuadratic-impl.hpp:223
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: TermQuadratic-impl.hpp:176
virtual void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false) override
load this term from a configuration file
Definition: TermQuadratic-impl.hpp:195
void setStateAndControlReference(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x_ref, const core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u_ref)
Definition: TermQuadratic-impl.hpp:111
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
SC evalLocal(const Eigen::Matrix< SC, STATE_DIM, 1 > &x, const Eigen::Matrix< SC, CONTROL_DIM, 1 > &u, const SC &t)
Definition: TermQuadratic.hpp:119
control_matrix_t R_
Definition: TermQuadratic.hpp:110
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermQuadratic.hpp:30
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > u_ref_
Definition: TermQuadratic.hpp:113
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: TermQuadratic-impl.hpp:142
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermQuadratic.hpp:27
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
TermQuadratic()
Definition: TermQuadratic-impl.hpp: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: TermQuadratic-impl.hpp:164
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: TermQuadratic.hpp:31
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual TermQuadratic< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const override
Deep-copy term.
Definition: TermQuadratic-impl.hpp:66
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: TermQuadratic.hpp:32
virtual void updateReferenceControl(const Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, 1 > &newRefControl) override
updates the reference control for this term
Definition: TermQuadratic-impl.hpp:230
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
core::StateVector< STATE_DIM, SCALAR_EVAL > x_ref_
Definition: TermQuadratic.hpp:112
virtual Eigen::Matrix< SCALAR_EVAL, STATE_DIM, 1 > getReferenceState() const override
retrieve this term&#39;s current reference state
Definition: TermQuadratic-impl.hpp:237
state_matrix_t Q_
Definition: TermQuadratic.hpp:109
virtual ~TermQuadratic()
Definition: TermQuadratic-impl.hpp:59
const state_matrix_t & getStateWeight() const
Definition: TermQuadratic-impl.hpp:84
void setWeights(const Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > &Q, const Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > &R)
Definition: TermQuadratic-impl.hpp:73
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: TermQuadratic.hpp:29
const bool verbose
Definition: ConstraintComparison.h:18
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: TermQuadratic-impl.hpp:154
const control_matrix_t & getControlWeight() const
Definition: TermQuadratic-impl.hpp:98
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermQuadratic.hpp:28