8 #define EIGEN_INITIALIZE_MATRICES_BY_NAN 23 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR_EVAL =
double,
typename SCALAR = SCALAR_EVAL>
27 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
39 TestTerm(
const state_matrix_t& Q,
const control_matrix_t& R,
const SCALAR_EVAL& c) :
Q_(Q),
R_(R),
c_(c)
57 void setWeights(
const state_matrix_t& Q,
const control_matrix_t& R,
const double& c)
72 const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>&
u,
75 return evalLocal<SCALAR>(
x,
u,
t);
80 ct::core::ADCGScalar
t)
override 82 return evalLocal<ct::core::ADCGScalar>(
x,
u,
t);
91 return (2 *
c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t *
Q_ * x.template cast<SCALAR_EVAL>());
99 return (2 *
c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t *
R_ * u.template cast<SCALAR_EVAL>());
107 return (2 *
c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t *
Q_);
115 return (2 *
c_ * (SCALAR_EVAL)t * (SCALAR_EVAL)t *
R_);
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)
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>());
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);
139 TEST(AD_TEST_TIME_VAR, AD_TEST_TIME_VAR)
143 Eigen::Matrix<double, 3, 3> Q;
145 Eigen::Matrix<double, 3, 3> R;
148 Eigen::Matrix<double, 3, 3> Q_f = 10 * Q;
149 Eigen::Matrix<double, 3, 3> R_f = 10 * R;
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(
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);
170 double t_final = 4.0;
174 for (
double t = 0.0;
t <= t_final;
t =
t + 1)
176 ADcf->setCurrentStateAndControl(x, u,
t);
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);
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);
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);
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);
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);
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);
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);
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);
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
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