8 #define EIGEN_INITIALIZE_MATRICES_BY_NAN 22 template <
size_t state_dim,
size_t control_dim>
26 std::cout <<
"eval intermediate " << std::endl;
30 std::cout <<
"eval terminal " << std::endl;
34 std::cout <<
"eval stateDerivativeIntermediate " << std::endl;
38 std::cout <<
"eval stateDerivativeIntermediate " << std::endl;
42 std::cout <<
"eval stateSecondDerivativeIntermediate " << std::endl;
46 std::cout <<
"eval stateSecondDerivativeTerminal " << std::endl;
50 std::cout <<
"eval controlDerivativeIntermediate " << std::endl;
54 std::cout <<
"eval controlDerivativeTerminal " << std::endl;
58 std::cout <<
"eval controlSecondDerivativeIntermediate " << std::endl;
62 std::cout <<
"eval controlSecondDerivativeIntermediate " << std::endl;
66 std::cout <<
"eval stateControlDerivativeIntermediate " << std::endl;
70 std::cout <<
"eval stateControlDerivativeTerminal " << std::endl;
74 std::cout <<
"eval stateSecondDerivativeIntermediate " << std::endl;
78 std::cout <<
"eval controlSecondDerivativeIntermediate " << std::endl;
84 TEST(CostFunctionTest, ADQuadraticTest)
86 const size_t nWeights = 2;
87 const size_t nTests = 10;
90 CostFunctionAD<state_dim, control_dim> costFunctionAD;
93 std::shared_ptr<TermQuadratic<state_dim, control_dim, double>> termQuadratic_interm(
95 std::shared_ptr<TermQuadratic<state_dim, control_dim, double, ct::core::ADCGScalar>> termQuadraticAD_interm(
99 std::shared_ptr<TermQuadratic<state_dim, control_dim, double>> termQuadratic_final(
101 std::shared_ptr<TermQuadratic<state_dim, control_dim, double, ct::core::ADCGScalar>> termQuadraticAD_final(
105 costFunctionAD.addIntermediateADTerm(termQuadraticAD_interm,
true);
107 costFunctionAD.addFinalADTerm(termQuadraticAD_final,
true);
109 Eigen::Matrix<double, state_dim, state_dim> Q_interm;
110 Eigen::Matrix<double, state_dim, state_dim> Q_final;
111 Eigen::Matrix<double, control_dim, control_dim> R;
116 for (
size_t i = 0;
i < nWeights;
i++)
120 Q_interm.setRandom();
135 Q_interm += Q_interm.transpose().eval();
136 R += R.transpose().eval();
137 Q_final += Q_final.transpose().eval();
139 termQuadratic_interm->setWeights(Q_interm, R);
140 termQuadraticAD_interm->setWeights(Q_interm, R);
141 termQuadratic_interm->setStateAndControlReference(x_ref, u_ref);
142 termQuadraticAD_interm->setStateAndControlReference(x_ref, u_ref);
144 termQuadratic_final->setWeights(Q_final, R);
145 termQuadraticAD_final->setWeights(Q_final, R);
146 termQuadratic_final->setStateAndControlReference(x_ref, u_ref);
147 termQuadraticAD_final->setStateAndControlReference(x_ref, u_ref);
149 costFunctionAD.initialize();
152 std::shared_ptr<CostFunctionAD<state_dim, control_dim>> costFunctionAD_clone(costFunctionAD.clone());
154 for (
size_t j = 0; j < nTests; j++)
168 costFunctionAD.setCurrentStateAndControl(x, u, 1.0);
169 costFunctionAD_clone->setCurrentStateAndControl(x, u, 1.0);
186 }
catch (std::exception& e)
194 TEST(CostFunctionTest, ADQuadMultTest)
196 const size_t nWeights = 3;
197 const size_t nTests = 10;
200 CostFunctionAD<state_dim, control_dim> costFunctionAD;
202 std::shared_ptr<TermQuadMult<state_dim, control_dim, double>> termQuadMult(
204 std::shared_ptr<TermQuadMult<state_dim, control_dim, double, ct::core::ADCGScalar>> termQuadMultAD(
208 std::shared_ptr<TermMixed<state_dim, control_dim, double, ct::core::ADCGScalar>> termMixedAD(
212 costFunctionAD.addIntermediateADTerm(termQuadMultAD);
213 costFunctionAD.initialize();
215 Eigen::Matrix<double, state_dim, state_dim> Q;
216 Eigen::Matrix<double, control_dim, control_dim> R;
217 Eigen::Matrix<double, control_dim, state_dim> P;
222 for (
size_t i = 0;
i < nWeights;
i++)
246 x_ref.setConstant(10.0);
247 u_ref.setConstant(10.0);
250 Q += Q.transpose().eval();
251 R += R.transpose().eval();
253 termQuadMult->setWeights(Q, R);
254 termQuadMultAD->setWeights(Q, R);
255 termQuadMult->setStateAndControlReference(x_ref, u_ref);
256 termQuadMultAD->setStateAndControlReference(x_ref, u_ref);
258 costFunctionAD.initialize();
261 std::shared_ptr<CostFunctionAD<state_dim, control_dim>> costFunctionAD_clone(costFunctionAD.clone());
263 for (
size_t j = 0; j < nTests; j++)
278 costFunctionAD.setCurrentStateAndControl(x, u, t);
279 costFunctionAD_clone->setCurrentStateAndControl(x, u, t);
284 }
catch (std::exception& e)
298 TEST(CostFunctionTest, TrackingTermTest)
304 std::shared_ptr<CostFunctionAnalytical<state_dim, control_dim>> costFunction(
307 Eigen::Matrix<double, state_dim, state_dim> Q;
308 Eigen::Matrix<double, control_dim, control_dim> R;
315 size_t trajSize = 50;
316 bool timeIsAbsolute =
true;
317 for (
size_t i = 0;
i < trajSize; ++
i)
324 Q, R, core::InterpolationType::LIN, core::InterpolationType::ZOH,
true));
326 trackingTerm->setStateAndControlReference(stateTraj, controlTraj);
327 costFunction->addIntermediateTerm(trackingTerm);
335 costFunction->setCurrentStateAndControl(x, u, t);
337 ASSERT_TRUE(costFunction->stateDerivativeIntermediateTest());
338 ASSERT_TRUE(costFunction->controlDerivativeIntermediateTest());
344 TEST(CostFunctionTest, TermSmoothAbsTest)
350 std::shared_ptr<CostFunctionAnalytical<state_dim, control_dim>> costFunction(
354 using CGScalar =
typename CostFunctionAD<state_dim, control_dim>::CGScalar;
355 std::shared_ptr<CostFunctionAD<state_dim, control_dim>> costFunctionAD(
356 new CostFunctionAD<state_dim, control_dim>());
358 Eigen::Matrix<double, state_dim, 1> a, x_ref;
361 Eigen::Matrix<double, control_dim, 1> b, u_ref;
366 std::shared_ptr<TermSmoothAbs<state_dim, control_dim>> smoothAbsTerm(
369 std::shared_ptr<TermSmoothAbs<state_dim, control_dim, double, CGScalar>> smoothAbsTermAD(
372 costFunction->addIntermediateTerm(smoothAbsTerm);
373 costFunctionAD->addIntermediateADTerm(smoothAbsTermAD);
374 costFunctionAD->initialize();
382 costFunction->setCurrentStateAndControl(x, u, t);
383 ASSERT_TRUE(costFunction->stateDerivativeIntermediateTest());
384 ASSERT_TRUE(costFunction->controlDerivativeIntermediateTest());
386 for (
int i = 0;
i < 100;
i++)
391 costFunction->setCurrentStateAndControl(x, u, t);
392 costFunctionAD->setCurrentStateAndControl(x, u, t);
control_vector_t controlDerivativeIntermediate() override
Computes intermediate-cost first-order derivative with respect to control.
Definition: CostFunctionAnalytical-impl.hpp:136
A basic quadratic term of type .
Definition: TermQuadratic.hpp:22
virtual state_vector_t stateDerivativeIntermediate()=0
Computes intermediate-cost first-order derivative with respect to state.
virtual SCALAR evaluateIntermediate()=0
evaluate intermediate costs
virtual void setCurrentStateAndControl(const state_vector_t &x, const control_vector_t &u, const SCALAR &t=SCALAR(0.0))
Definition: CostFunction-impl.hpp:30
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
state_vector_t stateDerivativeTerminal() override
Definition: CostFunctionAnalytical-impl.hpp:115
const size_t state_dim
Definition: ConstraintComparison.h:20
A basic quadratic term of type .
Definition: TermMixed.hpp:21
virtual control_state_matrix_t stateControlDerivativeTerminal()
Computes final-cost derivative with respect to state and control.
Definition: CostFunctionQuadratic-impl.hpp:85
state_matrix_t stateSecondDerivativeTerminal() override
Computes final-cost second-order derivative with respect to state.
Definition: CostFunctionAnalytical-impl.hpp:129
A quadratic tracking term of type .
Definition: TermQuadTracking.hpp:20
A cost function which contains only terms that have analytical derivatives.
Definition: CostFunctionAnalytical.hpp:31
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
virtual control_matrix_t controlSecondDerivativeIntermediate()=0
Computes intermediate-cost second-order derivative with respect to input.
Describes a cost function with a quadratic approximation, i.e. one that can compute first and second ...
Definition: CostFunctionQuadratic.hpp:29
void printCostFunctionOutput(CostFunctionQuadratic< state_dim, control_dim > &costFunction, CostFunctionQuadratic< state_dim, control_dim > &costFunction2)
Definition: CostFunctionTest.h:23
A multiplicative term of type .
Definition: TermQuadMult.hpp:24
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
virtual size_t addIntermediateTerm(std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR >> term, bool verbose=false)
Adds an intermediate term.
Definition: CostFunctionQuadratic-impl.hpp:279
const size_t control_dim
Definition: CostFunctionTest.h:15
for i
Definition: mpc_unittest_plotting.m:14
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
virtual size_t addFinalTerm(std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR >> term, bool verbose=false)
Adds a final term.
Definition: CostFunctionQuadratic-impl.hpp:294
state_matrix_t stateSecondDerivativeIntermediate() override
Computes intermediate-cost second-order derivative with respect to state.
Definition: CostFunctionAnalytical-impl.hpp:122
control_matrix_t controlSecondDerivativeIntermediate() override
Computes intermediate-cost second-order derivative with respect to input.
Definition: CostFunctionAnalytical-impl.hpp:150
virtual state_vector_t stateDerivativeTerminal()=0
virtual state_matrix_t stateSecondDerivativeIntermediate()=0
Computes intermediate-cost second-order derivative with respect to state.
void push_back(const T &data, const SCALAR &time, const bool timeIsAbsolute)
virtual control_vector_t controlDerivativeIntermediate()=0
Computes intermediate-cost first-order derivative with respect to control.
virtual control_vector_t controlDerivativeTerminal()
Computes terminal-cost first-order derivative with respect to control.
Definition: CostFunctionQuadratic-impl.hpp:71
virtual control_state_matrix_t stateControlDerivativeIntermediate()=0
Computes intermediate-cost derivative with respect to state and control.
virtual SCALAR evaluateTerminal()=0
evaluate terminal costs
void compareCostFunctionOutput(CostFunctionQuadratic< state_dim, control_dim > &costFunction, CostFunctionQuadratic< state_dim, control_dim > &costFunction2)
This method is called from different unit tests in order to compare the cost, first and second order ...
Definition: compareCostFunctions.h:18
const size_t state_dim
Definition: CostFunctionTest.h:14
const size_t control_dim
Definition: ConstraintComparison.h:21
state_vector_t stateDerivativeIntermediate() override
Computes intermediate-cost first-order derivative with respect to state.
Definition: CostFunctionAnalytical-impl.hpp:108
virtual state_matrix_t stateSecondDerivativeTerminal()=0
Computes final-cost second-order derivative with respect to state.
A smooth absolute term of type where this calculation is performed component-wise and summed with in...
Definition: TermSmoothAbs.hpp:23
virtual control_matrix_t controlSecondDerivativeTerminal()
Computes final-cost second-order derivative with respect to input.
Definition: CostFunctionQuadratic-impl.hpp:78