- 3.0.2 optimal control module.
TermQuadMult.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 
23 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL = double, typename SCALAR = SCALAR_EVAL>
24 class TermQuadMult : public TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>
25 {
26 public:
27  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
28 
29  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_t;
30  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_t;
31  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_t;
32  typedef Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM> state_matrix_double_t;
33  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM> control_matrix_double_t;
34  typedef Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, STATE_DIM> control_state_matrix_double_t;
35 
36  TermQuadMult();
37 
38  TermQuadMult(const state_matrix_t& Q, const control_matrix_t& R);
39 
40  TermQuadMult(const state_matrix_t& Q,
41  const control_matrix_t& R,
44 
45  TermQuadMult(const TermQuadMult& arg);
46 
47  virtual ~TermQuadMult();
48 
50 
51  void setWeights(const state_matrix_double_t& Q, const control_matrix_double_t& R);
52 
55 
56  virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
57  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
58  const SCALAR& t) override;
59 
60 #ifdef CPPADCG
61  virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
63  ct::core::ADCGScalar t) override;
64 #endif
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 
84  const SCALAR_EVAL& t) override;
85 
86  void loadConfigFile(const std::string& filename, const std::string& termName, bool verbose = false) override;
87 
88 
89 protected:
90  template <typename SC>
91  SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, const SC& t);
92 
93  state_matrix_t Q_;
94  control_matrix_t R_;
95 
98 };
99 
100 
101 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
102 template <typename SC>
104  const Eigen::Matrix<SC, CONTROL_DIM, 1>& u,
105  const SC& t)
106 {
107  Eigen::Matrix<SC, STATE_DIM, 1> xDiff = (x - x_ref_.template cast<SC>());
108  Eigen::Matrix<SC, CONTROL_DIM, 1> uDiff = (u - u_ref_.template cast<SC>());
109 
110  return (xDiff.transpose() * Q_.template cast<SC>() * xDiff)(0, 0) *
111  (uDiff.transpose() * R_.template cast<SC>() * uDiff)(0, 0);
112 }
113 
114 } // namespace optcon
115 } // namespace ct
virtual ~TermQuadMult()
Definition: TermQuadMult-impl.hpp:57
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: TermQuadMult-impl.hpp:81
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: TermQuadMult-impl.hpp:115
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: TermQuadMult-impl.hpp:144
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: TermQuadMult.hpp:31
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
TermQuadMult()
Definition: TermQuadMult-impl.hpp:22
void loadConfigFile(const std::string &filename, const std::string &termName, bool verbose=false) override
load this term from a configuration file
Definition: TermQuadMult-impl.hpp:171
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermQuadMult.hpp:32
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermQuadMult.hpp:30
void setWeights(const state_matrix_double_t &Q, const control_matrix_double_t &R)
Definition: TermQuadMult-impl.hpp:62
CppAD::AD< CppAD::cg::CG< double > > SCALAR
A multiplicative term of type .
Definition: TermQuadMult.hpp:24
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: TermQuadMult.hpp:33
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > u_ref_
Definition: TermQuadMult.hpp:97
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
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: TermQuadMult-impl.hpp:158
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: TermQuadMult-impl.hpp:129
void setStateAndControlReference(const core::StateVector< STATE_DIM > &x_ref, core::ControlVector< CONTROL_DIM > &u_ref)
Definition: TermQuadMult-impl.hpp:70
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermQuadMult.hpp:29
state_matrix_t Q_
Definition: TermQuadMult.hpp:93
control_matrix_t R_
Definition: TermQuadMult.hpp:94
SC evalLocal(const Eigen::Matrix< SC, STATE_DIM, 1 > &x, const Eigen::Matrix< SC, CONTROL_DIM, 1 > &u, const SC &t)
Definition: TermQuadMult.hpp:103
core::StateVector< STATE_DIM, SCALAR_EVAL > x_ref_
Definition: TermQuadMult.hpp:96
const bool verbose
Definition: ConstraintComparison.h:18
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: TermQuadMult.hpp:34
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: TermQuadMult-impl.hpp:100
TermQuadMult< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const override
Deep-copy term.
Definition: TermQuadMult-impl.hpp:51