24 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
28 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
35 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>
VectorXs;
36 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic>
MatrixXs;
53 virtual VectorXs
evaluate(
const state_vector_t&
x,
const control_vector_t&
u,
const SCALAR t)
override 55 Eigen::Matrix<SCALAR, term_dim, 1> constr_violation;
56 constr_violation.template segment<1>(0) << (
u(1) * Trait::cos(
x(2)) -
u(0) * Trait::sin(
x(2)) -
u(2));
57 return constr_violation;
63 ct::core::ADCGScalar
t)
override 65 Eigen::Matrix<ct::core::ADCGScalar, term_dim, 1> constr_violation;
66 constr_violation.template segment<1>(0) << (
u(1) * TraitCG::cos(
x(2)) -
u(0) * TraitCG::sin(
x(2)) -
u(2));
67 return constr_violation;
72 Eigen::Matrix<SCALAR, term_dim, STATE_DIM> jac;
80 Eigen::Matrix<SCALAR, term_dim, CONTROL_DIM> jac;
82 jac << -sin(
x(2)), cos(
x(2)),
SCALAR(-1.0);
89 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
93 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
100 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>
VectorXs;
101 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic>
MatrixXs;
118 virtual VectorXs
evaluate(
const state_vector_t&
x,
const control_vector_t&
u,
const SCALAR t)
override 120 Eigen::Matrix<SCALAR, term_dim, 1> constr_violation;
121 constr_violation(0) = (
u(1) * Trait::cos(
x(2)) -
u(0) * Trait::sin(
x(2)) -
u(2));
122 constr_violation(1) = (
u(2) * Trait::cos(
x(1)) -
u(2) * Trait::sin(
x(1)) -
u(1));
123 return constr_violation;
129 ct::core::ADCGScalar
t)
override 131 Eigen::Matrix<ct::core::ADCGScalar, term_dim, 1> constr_violation;
132 constr_violation(0) = (
u(1) * TraitCG::cos(
x(2)) -
u(0) * TraitCG::sin(
x(2)) -
u(2));
133 constr_violation(1) = (
u(2) * TraitCG::cos(
x(1)) -
u(2) * TraitCG::sin(
x(1)) -
u(1));
134 return constr_violation;
139 Eigen::Matrix<SCALAR, term_dim, STATE_DIM> jac;
141 jac.row(0) << 0.0, 0.0, -
u(1) * sin(
x(2)) -
u(0) * cos(
x(2));
142 jac.row(1) << 0.0, -(
u(2)) * sin(
x(1)) -
u(2) * cos(
x(1)), 0.0;
149 Eigen::Matrix<SCALAR, term_dim, CONTROL_DIM> jac;
151 jac.row(0) << -sin(
x(2)), cos(
x(2)), -1.0;
152 jac.row(1) << 0.0, -1.0, cos(
x(1)) - sin(
x(1));
158 TEST(ConstraintComparison, comparisonAnalyticAD)
160 std::shared_ptr<ct::optcon::ConstraintContainerAD<state_dim, control_dim>> constraintAD(
161 new ct::optcon::ConstraintContainerAD<state_dim, control_dim>());
163 std::shared_ptr<ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>> constraintAN(
167 term1_ad->setName(
"term1_ad");
169 term2_ad->setName(
"term2_ad");
171 std::shared_ptr<ConstraintTerm1D<state_dim, control_dim, double>> term1_an(
173 term1_an->setName(
"term1_an");
174 std::shared_ptr<ConstraintTerm2D<state_dim, control_dim, double>> term2_an(
176 term2_an->setName(
"term2_an");
179 std::cout <<
"Adding terms to constraint_analytic" << std::endl;
180 constraintAD->addIntermediateConstraint(term1_ad, verbose);
181 constraintAD->addIntermediateConstraint(term2_ad, verbose);
182 constraintAN->addIntermediateConstraint(term1_an, verbose);
183 constraintAN->addIntermediateConstraint(term2_an, verbose);
185 constraintAD->initialize();
186 constraintAN->initialize();
189 Eigen::VectorXd g1_ad, g1_an;
191 Eigen::Matrix<double, state_dim, 1> state = Eigen::Matrix<double, state_dim, 1>::Random();
192 Eigen::Matrix<double, control_dim, 1> control = Eigen::Matrix<double, control_dim, 1>::Random();
195 constraintAN->setCurrentStateAndControl(state, control, time);
196 constraintAD->setCurrentStateAndControl(state, control, time);
198 g1_an = constraintAN->evaluateIntermediate();
199 g1_ad = constraintAD->evaluateIntermediate();
201 std::cout <<
"g1_an: " << g1_an.transpose() << std::endl;
202 std::cout <<
"g1_ad: " << g1_ad.transpose() << std::endl;
205 ASSERT_TRUE(g1_an.isApprox(g1_ad));
207 Eigen::MatrixXd C_an, C_ad, D_an, D_ad;
208 C_an = constraintAN->jacobianStateIntermediate();
209 D_an = constraintAN->jacobianInputIntermediate();
210 C_ad = constraintAD->jacobianStateIntermediate();
211 D_ad = constraintAD->jacobianInputIntermediate();
213 ASSERT_TRUE(C_an.isApprox(C_ad));
215 ASSERT_TRUE(D_an.isApprox(D_ad));
ct::core::tpl::TraitSelector< ct::core::ADCGScalar >::Trait TraitCG
Definition: ConstraintComparison.h:96
virtual Eigen::Matrix< ct::core::ADCGScalar, Eigen::Dynamic, 1 > 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: ConstraintComparison.h:60
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: ConstraintComparison.h:101
ct::optcon::ConstraintBase< STATE_DIM, CONTROL_DIM, SCALAR > Base
Definition: ConstraintComparison.h:32
virtual VectorXs evaluate(const state_vector_t &x, const control_vector_t &u, const SCALAR t) override
The evaluation of the constraint violation. Note this method is SCALAR typed.
Definition: ConstraintComparison.h:118
virtual MatrixXs jacobianInput(const state_vector_t &x, const control_vector_t &u, const SCALAR t) override
Returns the constraint jacobian wrt input.
Definition: ConstraintComparison.h:147
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
core::ControlVector< CONTROL_DIM, SCALAR > control_vector_t
Definition: ConstraintComparison.h:99
const size_t state_dim
Definition: ConstraintComparison.h:20
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
virtual MatrixXs jacobianState(const state_vector_t &x, const control_vector_t &u, const SCALAR t) override
Returns the constraint jacobian wrt state.
Definition: ConstraintComparison.h:137
ct::core::tpl::TraitSelector< SCALAR >::Trait Trait
Definition: ConstraintComparison.h:95
virtual Eigen::Matrix< ct::core::ADCGScalar, Eigen::Dynamic, 1 > 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: ConstraintComparison.h:126
virtual ~ConstraintTerm1D()
Definition: ConstraintComparison.h:46
virtual size_t getConstraintSize() const override
The evaluate method used for jit compilation in constraint container ad.
Definition: ConstraintComparison.h:52
CppAD::AD< CppAD::cg::CG< double > > SCALAR
virtual ConstraintTerm1D< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override
Creates a new instance of the object with same properties than original.
Definition: ConstraintComparison.h:47
ConstraintTerm1D()
Definition: ConstraintComparison.h:38
core::ControlVector< CONTROL_DIM, SCALAR > control_vector_t
Definition: ConstraintComparison.h:34
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
virtual MatrixXs jacobianInput(const state_vector_t &x, const control_vector_t &u, const SCALAR t) override
Returns the constraint jacobian wrt input.
Definition: ConstraintComparison.h:78
VectorXs ub_
lower bound on the constraints
Definition: ConstraintBase.h:203
Contains all the constraints using analytically calculated jacobians.
Definition: ConstraintContainerAnalytical.h:27
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
virtual MatrixXs jacobianState(const state_vector_t &x, const control_vector_t &u, const SCALAR t) override
Returns the constraint jacobian wrt state.
Definition: ConstraintComparison.h:70
virtual ~ConstraintTerm2D()
Definition: ConstraintComparison.h:111
ct::core::tpl::TraitSelector< ct::core::ADCGScalar >::Trait TraitCG
Definition: ConstraintComparison.h:31
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ConstraintComparison.h:35
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ConstraintComparison.h:100
A simple example with an 1d constraint.
Definition: ConstraintComparison.h:25
ConstraintTerm2D< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override
Creates a new instance of the object with same properties than original.
Definition: ConstraintComparison.h:112
EIGEN_MAKE_ALIGNED_OPERATOR_NEW static const size_t term_dim
Definition: ConstraintComparison.h:29
ct::optcon::ConstraintBase< STATE_DIM, CONTROL_DIM, SCALAR > Base
Definition: ConstraintComparison.h:97
VectorXs lb_
Definition: ConstraintBase.h:202
core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: ConstraintComparison.h:33
virtual size_t getConstraintSize() const override
The evaluate method used for jit compilation in constraint container ad.
Definition: ConstraintComparison.h:117
ConstraintTerm2D()
Definition: ConstraintComparison.h:103
const bool verbose
Definition: ConstraintComparison.h:18
core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: ConstraintComparison.h:98
const size_t control_dim
Definition: ConstraintComparison.h:21
virtual VectorXs evaluate(const state_vector_t &x, const control_vector_t &u, const SCALAR t) override
The evaluation of the constraint violation. Note this method is SCALAR typed.
Definition: ConstraintComparison.h:53
Base class for the constraints used in this toolbox.
Definition: ConstraintBase.h:21
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: ConstraintComparison.h:36
ct::core::tpl::TraitSelector< SCALAR >::Trait Trait
Definition: ConstraintComparison.h:30
A simple example with a 2d constraint.
Definition: ConstraintComparison.h:90