- 3.0.2 optimal control module.
TermQuadMult-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 
12 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
14  const control_matrix_t& R)
15  : Q_(Q), R_(R)
16 {
17  x_ref_.setZero(); // default values
18  u_ref_.setZero();
19 }
20 
21 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
23 {
24  Q_.setIdentity(); // default values
25  R_.setIdentity();
26  x_ref_.setZero();
27  u_ref_.setZero();
28 }
29 
30 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
32  const control_matrix_t& R,
35  : Q_(Q), R_(R), x_ref_(x_ref), u_ref_(u_ref)
36 {
37 }
38 
39 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
41  : TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>(arg),
42  Q_(arg.Q_),
43  R_(arg.R_),
44  x_ref_(arg.x_ref_),
45  u_ref_(arg.u_ref_)
46 {
47 }
48 
49 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
52 {
53  return new TermQuadMult(*this);
54 }
55 
56 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
58 {
59 }
60 
61 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
63  const control_matrix_double_t& R)
64 {
65  Q_ = Q.template cast<SCALAR_EVAL>();
66  R_ = R.template cast<SCALAR_EVAL>();
67 }
68 
69 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
71  const core::StateVector<STATE_DIM>& x_ref,
73 {
74  x_ref_ = x_ref.template cast<SCALAR_EVAL>();
75  ;
76  u_ref_ = u_ref.template cast<SCALAR_EVAL>();
77  ;
78 }
79 
80 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
82  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
83  const SCALAR& t)
84 {
85  return evalLocal<SCALAR>(x, u, t);
86 }
87 
88 #ifdef CPPADCG
89 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
93  ct::core::ADCGScalar t)
94 {
95  return evalLocal<ct::core::ADCGScalar>(x, u, t);
96 }
97 #endif
98 
99 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
103  const SCALAR_EVAL& t)
104 {
107 
108  SCALAR_EVAL r = (uDiff.transpose() * R_ * uDiff)(0, 0);
109 
110  return (xDiff.transpose() * Q_.transpose() + xDiff.transpose() * Q_) * r;
111 }
112 
113 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
118  const SCALAR_EVAL& t)
119 {
121 
122  SCALAR_EVAL r = (uDiff.transpose() * R_ * uDiff)(0, 0);
123 
124  return (Q_ + Q_.transpose()) * r;
125 }
126 
127 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
132  const SCALAR_EVAL& t)
133 {
136 
137  SCALAR_EVAL q = (xDiff.transpose() * Q_ * xDiff)(0, 0);
138 
139  return (uDiff.transpose() * R_.transpose() + uDiff.transpose() * R_) * q;
140 }
141 
142 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
147  const SCALAR_EVAL& t)
148 {
150 
151  SCALAR_EVAL q = (xDiff.transpose() * Q_ * xDiff)(0, 0);
152 
153  return (R_ + R_.transpose()) * q;
154 }
155 
156 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
161  const SCALAR_EVAL& t)
162 {
165 
166  return (uDiff.transpose() * R_.transpose() + uDiff.transpose() * R_).transpose() *
167  (xDiff.transpose() * Q_.transpose() + xDiff.transpose() * Q_);
168 }
169 
170 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
172  const std::string& termName,
173  bool verbose)
174 {
175  loadMatrixCF(filename, "Q", Q_, termName);
176  loadMatrixCF(filename, "R", R_, termName);
177  loadMatrixCF(filename, "x_des", x_ref_, termName);
178  loadMatrixCF(filename, "u_des", u_ref_, termName);
179  if (verbose)
180  {
181  std::cout << "Read Q as Q = \n" << Q_ << std::endl;
182  std::cout << "Read R as R = \n" << R_ << std::endl;
183  std::cout << "Read x_ref as x_ref = \n" << x_ref_.transpose() << std::endl;
184  std::cout << "Read u_ref as u_ref = \n" << u_ref_.transpose() << std::endl;
185  }
186 }
187 
188 } // namespace optcon
189 } // namespace ct
virtual ~TermQuadMult()
Definition: TermQuadMult-impl.hpp:57
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: TermBase.hpp:42
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
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
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
state_matrix_t Q_
Definition: TermQuadMult.hpp:93
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_double_t
Definition: TermBase.hpp:43
control_matrix_t R_
Definition: TermQuadMult.hpp:94
core::StateVector< STATE_DIM, SCALAR_EVAL > x_ref_
Definition: TermQuadMult.hpp:96
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: TermQuadMult-impl.hpp:100
TermQuadMult< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const override
Deep-copy term.
Definition: TermQuadMult-impl.hpp:51
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermBase.hpp:40