- 3.0.2 optimal control module.
ADTest_timeDependent.h
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 #define EIGEN_INITIALIZE_MATRICES_BY_NAN
9 
10 namespace ct {
11 namespace optcon {
12 namespace example {
13 
14 /*
15  * Define a cost function term of form cost = c*t^2 (x'Qx + u'Ru)
16  * analytical derivatives:
17  * state Derivative: 2*c*t^2 Qx
18  * control Derivative: 2*c*t^2 Rx
19  * state second Derivative: 2*c*t^2 Q
20  * control second Derivative: 2*c*t^2 R
21  * */
22 
23 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL = double, typename SCALAR = SCALAR_EVAL>
24 class TestTerm : 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 
37  TestTerm();
38 
39  TestTerm(const state_matrix_t& Q, const control_matrix_t& R, const SCALAR_EVAL& c) : Q_(Q), R_(R), c_(c)
40  {
41  x_ref_.setZero(); // default values
42  u_ref_.setZero();
43  }
44 
45  TestTerm(const TestTerm& arg)
46  : TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>(arg),
47  Q_(arg.Q_),
48  R_(arg.R_),
49  c_(arg.c_),
50  x_ref_(arg.x_ref_),
51  u_ref_(arg.u_ref_)
52  {
53  }
54 
55  virtual ~TestTerm() {}
56  TestTerm<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>* clone() const override { return new TestTerm(*this); }
57  void setWeights(const state_matrix_t& Q, const control_matrix_t& R, const double& c)
58  {
59  Q_ = Q;
60  R_ = R;
61  c_ = c;
62  }
63 
66  {
67  x_ref_ = x_ref;
68  u_ref_ = u_ref;
69  }
70 
71  virtual SCALAR evaluate(const Eigen::Matrix<SCALAR, STATE_DIM, 1>& x,
72  const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
73  const SCALAR& t) override
74  {
75  return evalLocal<SCALAR>(x, u, t);
76  }
77 
80  ct::core::ADCGScalar t) override
81  {
82  return evalLocal<ct::core::ADCGScalar>(x, u, t);
83  }
84 
85 
86  // todo: this is quite ugly
89  const double t)
90  {
91  return (2 * c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t * Q_ * x.template cast<SCALAR_EVAL>());
92  }
93 
94  // todo: this is very ugly
97  double t)
98  {
99  return (2 * c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t * R_ * u.template cast<SCALAR_EVAL>());
100  }
101 
102  // todo: this is quite ugly
105  const double t)
106  {
107  return (2 * c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t * Q_);
108  }
109 
110  // todo: this is very ugly
113  double t)
114  {
115  return (2 * c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t * R_);
116  }
117 
118 
119 protected:
120  template <typename SC>
121  SC evalLocal(const Eigen::Matrix<SC, STATE_DIM, 1>& x, const Eigen::Matrix<SC, CONTROL_DIM, 1>& u, 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) * SC(c_) * t * t +
127  (uDiff.transpose() * R_.template cast<SC>() * uDiff) * SC(c_) * t * t)(0, 0);
128  }
129 
130  state_matrix_t Q_;
131  control_matrix_t R_;
132  SCALAR_EVAL c_;
133 
136 };
137 
138 
139 TEST(AD_TEST_TIME_VAR, AD_TEST_TIME_VAR)
140 {
141  using namespace ct;
142 
143  Eigen::Matrix<double, 3, 3> Q;
144  Q.setIdentity();
145  Eigen::Matrix<double, 3, 3> R;
146  R.setIdentity();
147 
148  Eigen::Matrix<double, 3, 3> Q_f = 10 * Q;
149  Eigen::Matrix<double, 3, 3> R_f = 10 * R;
150 
151  double c = 1.0;
152  double c_f = 2.0;
153 
154  std::shared_ptr<TestTerm<3, 3, double, ct::core::ADCGScalar>> term_intermediate(
156  std::shared_ptr<TestTerm<3, 3, double, ct::core::ADCGScalar>> term_final(
158 
159  // autodiff costfunction
160  std::shared_ptr<ct::optcon::CostFunctionAD<3, 3>> ADcf(new ct::optcon::CostFunctionAD<3, 3>());
161  ADcf->addIntermediateADTerm(term_intermediate);
162  ADcf->addFinalADTerm(term_final);
163 
164 
166  x.setRandom();
168  u.setRandom();
169 
170  double t_final = 4.0;
171 
172  ADcf->initialize();
173 
174  for (double t = 0.0; t <= t_final; t = t + 1)
175  {
176  ADcf->setCurrentStateAndControl(x, u, t);
177 
178  Eigen::Matrix<double, 3, 1> diff1 = (ADcf->stateDerivativeTerminal()).template cast<double>() -
179  (term_final->stateDerivativeAnalytical(x, u, t));
180  ASSERT_TRUE(diff1.maxCoeff() - diff1.minCoeff() < 1e-9);
181 
182  Eigen::Matrix<double, 3, 1> diff2 = (ADcf->stateDerivativeIntermediate()).template cast<double>() -
183  (term_intermediate->stateDerivativeAnalytical(x, u, t));
184  ASSERT_TRUE(diff2.maxCoeff() - diff2.minCoeff() < 1e-9);
185 
186  Eigen::Matrix<double, 3, 1> diff3 = (ADcf->controlDerivativeIntermediate()).template cast<double>() -
187  (term_intermediate->controlDerivativeAnalytical(x, u, t));
188  ASSERT_TRUE(diff3.maxCoeff() - diff3.minCoeff() < 1e-9);
189 
190  Eigen::Matrix<double, 3, 1> diff4 = (ADcf->controlDerivativeTerminal()).template cast<double>() -
191  (term_final->controlDerivativeAnalytical(x, u, t));
192  ASSERT_TRUE(diff4.maxCoeff() - diff4.minCoeff() < 1e-9);
193 
194  Eigen::Matrix<double, 3, 3> diff5 = (ADcf->controlSecondDerivativeIntermediate()).template cast<double>() -
195  (term_intermediate->controlSecondDerivativeAnalytical(x, u, t));
196  ASSERT_TRUE(diff5.maxCoeff() - diff5.minCoeff() < 1e-9);
197 
198  Eigen::Matrix<double, 3, 3> diff6 = (ADcf->controlSecondDerivativeTerminal()).template cast<double>() -
199  (term_final->controlSecondDerivativeAnalytical(x, u, t));
200  ASSERT_TRUE(diff6.maxCoeff() - diff6.minCoeff() < 1e-9);
201 
202  Eigen::Matrix<double, 3, 3> diff7 = (ADcf->stateSecondDerivativeIntermediate()).template cast<double>() -
203  (term_intermediate->stateSecondDerivativeAnalytical(x, u, t));
204  ASSERT_TRUE(diff7.maxCoeff() - diff7.minCoeff() < 1e-9);
205 
206  Eigen::Matrix<double, 3, 3> diff8 = (ADcf->stateSecondDerivativeTerminal()).template cast<double>() -
207  (term_final->stateSecondDerivativeAnalytical(x, u, t));
208  ASSERT_TRUE(diff8.maxCoeff() - diff8.minCoeff() < 1e-9);
209  }
210 }
211 
212 } // namespace example
213 } // namespace optcon
214 } // namespace ct
TestTerm(const TestTerm &arg)
Definition: ADTest_timeDependent.h:45
virtual ~TestTerm()
Definition: ADTest_timeDependent.h:55
virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector< STATE_DIM, ct::core::ADCGScalar > &x, const core::ControlVector< CONTROL_DIM, ct::core::ADCGScalar > &u, ct::core::ADCGScalar t) override
Definition: ADTest_timeDependent.h:78
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > controlDerivativeAnalytical(const core::StateVector< STATE_DIM > &x, const core::ControlVector< CONTROL_DIM > &u, double t)
Definition: ADTest_timeDependent.h:95
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: ADTest_timeDependent.h:30
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
core::StateVector< STATE_DIM, SCALAR_EVAL > stateDerivativeAnalytical(const core::StateVector< STATE_DIM > &x, const core::ControlVector< CONTROL_DIM > &u, const double t)
Definition: ADTest_timeDependent.h:87
Cored::Vector3 Vector3d
Definition: ADTest_timeDependent.h:24
core::ControlVector< CONTROL_DIM, SCALAR_EVAL > u_ref_
Definition: ADTest_timeDependent.h:135
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: ADTest_timeDependent.h:31
core::StateVector< STATE_DIM, SCALAR_EVAL > x_ref_
Definition: ADTest_timeDependent.h:134
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
SCALAR_EVAL c_
Definition: ADTest_timeDependent.h:132
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
CppAD::AD< CppAD::cg::CG< double > > SCALAR
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_t
Definition: ADTest_timeDependent.h:29
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
TestTerm(const state_matrix_t &Q, const control_matrix_t &R, const SCALAR_EVAL &c)
Definition: ADTest_timeDependent.h:39
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, STATE_DIM > control_state_matrix_double_t
Definition: ADTest_timeDependent.h:34
Eigen::Matrix< SCALAR_EVAL, STATE_DIM, STATE_DIM > state_matrix_double_t
Definition: ADTest_timeDependent.h:32
control_matrix_t R_
Definition: ADTest_timeDependent.h:131
state_matrix_t stateSecondDerivativeAnalytical(const core::StateVector< STATE_DIM > &x, const core::ControlVector< CONTROL_DIM > &u, const double t)
Definition: ADTest_timeDependent.h:103
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: ADTest_timeDependent.h:71
state_matrix_t Q_
Definition: ADTest_timeDependent.h:130
control_matrix_t controlSecondDerivativeAnalytical(const core::StateVector< STATE_DIM > &x, const core::ControlVector< CONTROL_DIM > &u, double t)
Definition: ADTest_timeDependent.h:111
TestTerm< STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR > * clone() const override
Deep-copy term.
Definition: ADTest_timeDependent.h:56
Eigen::Matrix< SCALAR_EVAL, CONTROL_DIM, CONTROL_DIM > control_matrix_double_t
Definition: ADTest_timeDependent.h:33
void setStateAndControlReference(const core::StateVector< STATE_DIM, SCALAR_EVAL > &x_ref, core::ControlVector< CONTROL_DIM, SCALAR_EVAL > &u_ref)
Definition: ADTest_timeDependent.h:64
SC evalLocal(const Eigen::Matrix< SC, STATE_DIM, 1 > &x, const Eigen::Matrix< SC, CONTROL_DIM, 1 > &u, const SC &t)
Definition: ADTest_timeDependent.h:121
void setWeights(const state_matrix_t &Q, const control_matrix_t &R, const double &c)
Definition: ADTest_timeDependent.h:57