- 3.0.2 optimal control module.
TermQuadratic-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>
13  const control_matrix_t& R)
14  : Q_(Q), R_(R)
15 {
16  x_ref_.setZero(); // default values
17  u_ref_.setZero();
18 }
19 
20 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
22 {
23  Q_.setConstant(9999); // default values
24  R_.setConstant(9999);
25  x_ref_.setZero();
26  u_ref_.setZero();
27 }
28 
29 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
31  const control_matrix_t& R,
34  : Q_(Q), R_(R), x_ref_(x_ref), u_ref_(u_ref)
35 {
36 }
37 
38 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
40  const std::string& termName,
41  bool verbose)
42 {
43  loadConfigFile(configFile, termName, verbose);
44 }
45 
46 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
50  Q_(arg.Q_),
51  R_(arg.R_),
52  x_ref_(arg.x_ref_),
53  u_ref_(arg.u_ref_)
54 {
55 }
56 
57 
58 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
60 {
61 }
62 
63 
64 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
67 {
68  return new TermQuadratic(*this);
69 }
70 
71 
72 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
74  const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, STATE_DIM>& Q,
75  const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM>& R)
76 {
77  Q_ = Q;
78  R_ = R;
79 }
80 
81 
82 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
85 {
86  return Q_;
87 }
88 
89 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
92 {
93  return Q_;
94 }
95 
96 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
99 {
100  return R_;
101 }
102 
103 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
106 {
107  return R_;
108 }
109 
110 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
114 {
115  x_ref_ = x_ref;
116  u_ref_ = u_ref;
117 }
118 
119 
120 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
122  const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
123  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
124  const SCALAR& t)
125 {
126  return evalLocal<SCALAR>(x, u, t);
127 }
128 
129 #ifdef CPPADCG
130 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
134  ct::core::ADCGScalar t)
135 {
136  return evalLocal<ct::core::ADCGScalar>(x, u, t);
137 }
138 #endif
139 
140 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
145  const SCALAR_EVAL& t)
146 {
148 
149  return xDiff.transpose() * Q_.transpose() + xDiff.transpose() * Q_;
150 }
151 
152 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
157  const SCALAR_EVAL& t)
158 {
159  return Q_ + Q_.transpose();
160 }
161 
162 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
167  const SCALAR_EVAL& t)
168 {
170 
171  return uDiff.transpose() * R_.transpose() + uDiff.transpose() * R_;
172 }
173 
174 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
179  const SCALAR_EVAL& t)
180 {
181  return R_ + R_.transpose();
182 }
183 
184 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
189  const SCALAR_EVAL& t)
190 {
191  return control_state_matrix_t::Zero();
192 }
193 
194 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
196  const std::string& termName,
197  bool verbose)
198 {
199  boost::property_tree::ptree pt;
200  try
201  {
202  boost::property_tree::read_info(filename, pt);
203  } catch (...)
204  {
205  }
206  this->name_ = pt.get<std::string>(termName + ".name.", termName);
207 
208  loadMatrixCF(filename, "Q", Q_, termName);
209  loadMatrixCF(filename, "R", R_, termName);
210  loadMatrixCF(filename, "x_des", x_ref_, termName);
211  loadMatrixCF(filename, "u_des", u_ref_, termName);
212  if (verbose)
213  {
214  std::cout << "Read Q as Q = \n" << Q_ << std::endl;
215  std::cout << "Read R as R = \n" << R_ << std::endl;
216  std::cout << "Read x_des as x_des = \n" << x_ref_.transpose() << std::endl;
217  std::cout << "Read u_des as u_des = \n" << u_ref_.transpose() << std::endl;
218  }
219 }
220 
221 
222 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
224  const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& newRefState)
225 {
226  x_ref_ = newRefState;
227 }
228 
229 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
231  const Eigen::Matrix<SCALAR_EVAL, CONTROL_DIM, 1>& newRefControl)
232 {
233  u_ref_ = newRefControl;
234 }
235 
236 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
238  const
239 {
240  return x_ref_;
241 }
242 } // namespace optcon
243 } // 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
control_matrix_t R_
Definition: TermQuadratic.hpp:110
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
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
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
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
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermBase.hpp:39
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
void loadMatrixCF(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &termName="")
Definition: utilities.hpp:46
std::string name_
a name identifier for this term
Definition: TermBase.hpp:34
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
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: TermBase.hpp:40