- 3.0.2 optimal control module.
TermMixed-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 {
14  x_ref_.setZero(); // default values
15  u_ref_.setZero();
16 }
17 
18 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
20 {
21  P_.setZero(); // default values
22  x_ref_.setZero();
23  u_ref_.setZero();
24 }
25 
26 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
30  : P_(P), x_ref_(x_ref), u_ref_(u_ref)
31 {
32 }
33 
34 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
38 {
39 }
40 
41 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
43  const
44 {
45  return new TermMixed(*this);
46 }
47 
48 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
50 {
51 }
52 
53 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
55 {
56  P_ = P.template cast<SCALAR_EVAL>();
57 }
58 
59 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
61  const core::StateVector<STATE_DIM>& x_ref,
63 {
64  x_ref_ = x_ref.template cast<SCALAR_EVAL>();
65  u_ref_ = u_ref.template cast<SCALAR_EVAL>();
66 }
67 
68 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
70  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
71  const SCALAR& t)
72 {
73  return evalLocal<SCALAR>(x, u, t);
74 }
75 
76 #ifdef CPPADCG
77 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
81  ct::core::ADCGScalar t)
82 {
83  return evalLocal<ct::core::ADCGScalar>(x, u, t);
84 }
85 #endif
86 
87 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
91  const SCALAR_EVAL& t)
92 {
94 
95  return P_.transpose() * uDiff;
96 }
97 
98 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
103  const SCALAR_EVAL& t)
104 {
105  return state_matrix_t::Zero();
106 }
107 
108 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
112  const SCALAR_EVAL& t)
113 {
115 
116  return P_ * xDiff;
117 }
118 
119 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
124  const SCALAR_EVAL& t)
125 {
126  return control_matrix_t::Zero();
127 }
128 
129 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
134  const SCALAR_EVAL& t)
135 {
136  return P_;
137 }
138 
139 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
141  const std::string& termName,
142  bool verbose)
143 {
144  boost::property_tree::ptree pt;
145  try
146  {
147  boost::property_tree::read_info(filename, pt);
148  } catch (...)
149  {
150  }
151  this->name_ = pt.get<std::string>(termName + ".name.", termName);
152 
153  loadMatrixCF(filename, "P", P_, termName);
154  loadMatrixCF(filename, "x_des", x_ref_, termName);
155  loadMatrixCF(filename, "u_des", u_ref_, termName);
156  if (verbose)
157  {
158  std::cout << "Read P as P = \n" << P_ << std::endl;
159  std::cout << "Read x_ref as x_ref = \n" << x_ref_.transpose() << std::endl;
160  std::cout << "Read u_ref as u_ref = \n" << u_ref_.transpose() << std::endl;
161  }
162 }
163 
164 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
166  const Eigen::Matrix<SCALAR_EVAL, STATE_DIM, 1>& newRefState)
167 {
168  x_ref_ = newRefState;
169 }
170 } // namespace optcon
171 } // namespace ct
TermMixed()
Definition: TermMixed-impl.hpp:19
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: TermBase.hpp:44
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
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: TermBase.hpp:41
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
control_state_matrix_t P_
Definition: TermMixed.hpp:92
CppAD::AD< CppAD::cg::CG< double > > SCALAR
core::StateVector< STATE_DIM, SCALAR_EVAL > x_ref_
Definition: TermMixed.hpp:94
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
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
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
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: TermBase.hpp:39
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
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
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