25 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
29 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>
VectorXs;
35 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 61 ct::core::ADCGScalar
t)
override 63 return A_.template cast<ct::core::ADCGScalar>() * x;
71 void setA(
const Eigen::Matrix<SCALAR, STATE_DIM, STATE_DIM>& A) { A_ = A; }
74 Eigen::Matrix<SCALAR, STATE_DIM, STATE_DIM> A_;
78 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
82 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
87 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>
VectorXs;
88 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic>
MatrixXs;
116 return A_ * x + B_ *
u;
121 ct::core::ADCGScalar
t)
override 123 return (A_.template cast<ct::core::ADCGScalar>() * x + B_.template cast<ct::core::ADCGScalar>() * u);
136 void setAB(
const Eigen::Matrix<SCALAR, CONTROL_DIM, STATE_DIM>& A,
137 const Eigen::Matrix<SCALAR, CONTROL_DIM, CONTROL_DIM> B)
144 Eigen::Matrix<SCALAR, CONTROL_DIM, STATE_DIM> A_;
145 Eigen::Matrix<SCALAR, CONTROL_DIM, CONTROL_DIM> B_;
149 TEST(pureStateConstraintTest, pureStateConstraintTest)
151 std::shared_ptr<ct::optcon::ConstraintContainerAD<state_dim, control_dim>> constraintAD(
152 new ct::optcon::ConstraintContainerAD<state_dim, control_dim>());
154 std::shared_ptr<ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>> constraintAN(
157 Eigen::Matrix<double, state_dim, state_dim> A;
160 std::shared_ptr<PureStateConstraint_Example<state_dim, control_dim>> term1_ad(
162 term1_ad->setName(
"term1_ad");
165 std::shared_ptr<PureStateConstraint_Example<state_dim, control_dim, double>> term1_an(
167 term1_an->setName(
"term1_an");
170 constraintAD->addIntermediateConstraint(term1_ad, verbose);
171 constraintAD->initialize();
172 constraintAN->addIntermediateConstraint(term1_an, verbose);
173 constraintAN->initialize();
175 std::shared_ptr<ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>> constraintAN_cloned(
176 constraintAN->clone());
177 std::shared_ptr<ct::optcon::ConstraintContainerAD<state_dim, control_dim>> constraintAD_cloned(
178 constraintAD->clone());
182 for (
size_t i = 0;
i < nRuns;
i++)
185 Eigen::VectorXd g1_ad, g1_an, g1_ad_cl, g1_an_cl;
187 Eigen::Matrix<double, state_dim, 1> state;
189 Eigen::Matrix<double, control_dim, 1> input;
192 std::random_device rd;
193 std::mt19937 gen(rd());
194 std::uniform_real_distribution<> time_distr(0, 100);
195 double time = time_distr(gen);
197 constraintAN->setCurrentStateAndControl(state, input, time);
198 constraintAD->setCurrentStateAndControl(state, input, time);
199 constraintAD_cloned->setCurrentStateAndControl(state, input, time);
200 constraintAN_cloned->setCurrentStateAndControl(state, input, time);
202 g1_an = constraintAN->evaluateIntermediate();
203 g1_ad = constraintAD->evaluateIntermediate();
204 g1_an_cl = constraintAN_cloned->evaluateIntermediate();
205 g1_ad_cl = constraintAD_cloned->evaluateIntermediate();
208 ASSERT_TRUE(g1_an.isApprox(g1_ad));
209 ASSERT_TRUE(g1_an.isApprox(g1_ad_cl));
210 ASSERT_TRUE(g1_an.isApprox(g1_an_cl));
212 Eigen::MatrixXd F_an, F_ad, F_cloned, F_cloned_an;
216 F_cloned_an.setZero();
218 F_an = constraintAN->jacobianStateIntermediate();
219 F_ad = constraintAD->jacobianStateIntermediate();
220 F_cloned_an = constraintAN_cloned->jacobianStateIntermediate();
221 F_cloned = constraintAD_cloned->jacobianStateIntermediate();
225 ASSERT_TRUE(F_an.isApprox(F_ad));
226 ASSERT_TRUE(F_an.isApprox(F_cloned));
227 ASSERT_TRUE(F_an.isApprox(F_cloned_an));
232 TEST(stateInputConstraintTest, stateInputConstraintTest)
234 std::shared_ptr<ct::optcon::ConstraintContainerAD<state_dim, control_dim>> constraintAD(
235 new ct::optcon::ConstraintContainerAD<state_dim, control_dim>());
236 std::shared_ptr<ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>> constraintAN(
239 Eigen::Matrix<double, control_dim, state_dim> A;
241 Eigen::Matrix<double, control_dim, control_dim> B;
244 std::shared_ptr<StateInputConstraint_Example<state_dim, control_dim>> term1_ad(
246 term1_ad->setName(
"term1_ad");
247 term1_ad->setAB(A, B);
249 std::shared_ptr<StateInputConstraint_Example<state_dim, control_dim, double>> term1_an(
251 term1_an->setName(
"term1_an");
252 term1_an->setAB(A, B);
256 constraintAD->addIntermediateConstraint(term1_ad, verbose);
257 constraintAD->initialize();
258 constraintAN->addIntermediateConstraint(term1_an, verbose);
259 constraintAN->initialize();
262 Eigen::VectorXd g1_ad, g1_an;
264 Eigen::Matrix<double, state_dim, 1> state;
266 Eigen::Matrix<double, control_dim, 1> input;
271 constraintAN->setCurrentStateAndControl(state, input, time);
272 constraintAD->setCurrentStateAndControl(state, input, time);
275 g1_an = constraintAN->evaluateIntermediate();
276 g1_ad = constraintAD->evaluateIntermediate();
279 ASSERT_TRUE(g1_an.isApprox(g1_ad));
281 Eigen::MatrixXd C_an, C_ad, C_cloned, C_cloned_an;
282 Eigen::MatrixXd D_an, D_ad, D_cloned, D_cloned_an;
284 C_an = constraintAN->jacobianStateIntermediate();
285 C_ad = constraintAD->jacobianStateIntermediate();
286 D_an = constraintAN->jacobianInputIntermediate();
287 D_ad = constraintAD->jacobianInputIntermediate();
290 std::shared_ptr<ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>> constraintAN_cloned(
291 constraintAN->clone());
293 std::shared_ptr<ct::optcon::ConstraintContainerAD<state_dim, control_dim>> constraintAD_cloned(
294 constraintAD->clone());
296 C_cloned_an = constraintAN_cloned->jacobianStateIntermediate();
297 C_cloned = constraintAD_cloned->jacobianStateIntermediate();
298 D_cloned_an = constraintAN_cloned->jacobianInputIntermediate();
299 D_cloned = constraintAD_cloned->jacobianInputIntermediate();
302 ASSERT_TRUE(C_an.isApprox(C_ad));
303 ASSERT_TRUE(C_an.isApprox(C_cloned));
304 ASSERT_TRUE(C_an.isApprox(C_cloned_an));
306 ASSERT_TRUE(D_an.isApprox(D_ad));
307 ASSERT_TRUE(D_an.isApprox(D_cloned));
308 ASSERT_TRUE(D_an.isApprox(D_cloned_an));
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: ConstraintTest.h:53
core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: ConstraintTest.h:32
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
virtual ~PureStateConstraint_Example()
Definition: ConstraintTest.h:46
PureStateConstraint_Example()
Definition: ConstraintTest.h:37
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: ConstraintTest.h:58
const size_t state_dim
Definition: ConstraintComparison.h:20
virtual size_t getConstraintSize() const override
The evaluate method used for jit compilation in constraint container ad.
Definition: ConstraintTest.h:52
void setA(const Eigen::Matrix< SCALAR, STATE_DIM, STATE_DIM > &A)
Definition: ConstraintTest.h:71
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: ConstraintTest.h:66
CppAD::AD< CppAD::cg::CG< double > > SCALAR
TEST(ConstraintComparison, comparisonAnalyticAD)
Definition: ConstraintComparison.h:158
for i
Definition: mpc_unittest_plotting.m:14
VectorXs ub_
lower bound on the constraints
Definition: ConstraintBase.h:203
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef ct::core::tpl::TraitSelector< SCALAR >::Trait Trait
Definition: ConstraintTest.h:30
PureStateConstraint_Example(const PureStateConstraint_Example &arg)
Definition: ConstraintTest.h:45
Contains all the constraints using analytically calculated jacobians.
Definition: ConstraintContainerAnalytical.h:27
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: ConstraintTest.h:35
ct::optcon::ConstraintBase< state_dim, control_dim, SCALAR > Base
Definition: ConstraintTest.h:31
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ConstraintTest.h:34
VectorXs lb_
Definition: ConstraintBase.h:202
A pure state constraint term.
Definition: ConstraintTest.h:26
core::ControlVector< CONTROL_DIM, SCALAR > control_vector_t
Definition: ConstraintTest.h:33
const bool verbose
Definition: ConstraintComparison.h:18
const size_t control_dim
Definition: ConstraintComparison.h:21
Base class for the constraints used in this toolbox.
Definition: ConstraintBase.h:21
virtual PureStateConstraint_Example< STATE_DIM, CONTROL_DIM, SCALAR > * clone() const override
Creates a new instance of the object with same properties than original.
Definition: ConstraintTest.h:47