- 3.0.2 optimal control module.
ConstraintComparison.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 
12 #pragma once
13 
14 namespace ct {
15 namespace optcon {
16 namespace example {
17 
18 const bool verbose = true;
19 
20 const size_t state_dim = 3;
21 const size_t control_dim = 3;
22 
24 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
25 class ConstraintTerm1D : public ct::optcon::ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>
26 {
27 public:
28  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29  const static size_t term_dim = 1;
35  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> VectorXs;
36  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> MatrixXs;
37 
39  {
40  Base::lb_.resize(term_dim);
41  Base::ub_.resize(term_dim);
42  Base::lb_.setZero();
43  Base::ub_.setZero();
44  }
45 
46  virtual ~ConstraintTerm1D() {}
48  {
50  }
51 
52  virtual size_t getConstraintSize() const override { return term_dim; }
53  virtual VectorXs evaluate(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override
54  {
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;
58  }
59 
60  virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
63  ct::core::ADCGScalar t) override
64  {
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;
68  }
69 
70  virtual MatrixXs jacobianState(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override
71  {
72  Eigen::Matrix<SCALAR, term_dim, STATE_DIM> jac;
73  jac.setZero();
74  jac << SCALAR(0.0), SCALAR(0.0), -u(1) * sin(x(2)) - u(0) * cos(x(2));
75  return jac;
76  }
77 
78  virtual MatrixXs jacobianInput(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override
79  {
80  Eigen::Matrix<SCALAR, term_dim, CONTROL_DIM> jac;
81  jac.setZero();
82  jac << -sin(x(2)), cos(x(2)), SCALAR(-1.0);
83  return jac;
84  }
85 };
86 
87 
89 template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR = double>
90 class ConstraintTerm2D : public ct::optcon::ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>
91 {
92 public:
93  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
94  const static size_t term_dim = 2;
100  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> VectorXs;
101  typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> MatrixXs;
102 
104  {
105  Base::lb_.resize(term_dim);
106  Base::ub_.resize(term_dim);
107  Base::lb_.setZero();
108  Base::ub_.setZero();
109  }
110 
111  virtual ~ConstraintTerm2D() {}
113  {
115  }
116 
117  virtual size_t getConstraintSize() const override { return term_dim; }
118  virtual VectorXs evaluate(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override
119  {
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;
124  }
125 
126  virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
129  ct::core::ADCGScalar t) override
130  {
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;
135  }
136 
137  virtual MatrixXs jacobianState(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override
138  {
139  Eigen::Matrix<SCALAR, term_dim, STATE_DIM> jac;
140  jac.setZero();
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;
143 
144  return jac;
145  }
146 
147  virtual MatrixXs jacobianInput(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override
148  {
149  Eigen::Matrix<SCALAR, term_dim, CONTROL_DIM> jac;
150  jac.setZero();
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));
153  return jac;
154  }
155 };
156 
157 
158 TEST(ConstraintComparison, comparisonAnalyticAD)
159 {
160  std::shared_ptr<ct::optcon::ConstraintContainerAD<state_dim, control_dim>> constraintAD(
161  new ct::optcon::ConstraintContainerAD<state_dim, control_dim>());
162 
163  std::shared_ptr<ct::optcon::ConstraintContainerAnalytical<state_dim, control_dim>> constraintAN(
165 
166  std::shared_ptr<ConstraintTerm1D<state_dim, control_dim>> term1_ad(new ConstraintTerm1D<state_dim, control_dim>());
167  term1_ad->setName("term1_ad");
168  std::shared_ptr<ConstraintTerm2D<state_dim, control_dim>> term2_ad(new ConstraintTerm2D<state_dim, control_dim>());
169  term2_ad->setName("term2_ad");
170 
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");
177 
178 
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);
184 
185  constraintAD->initialize();
186  constraintAN->initialize();
187 
188  /* evaluate constraint */
189  Eigen::VectorXd g1_ad, g1_an;
190 
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();
193  double time = 0.5;
194 
195  constraintAN->setCurrentStateAndControl(state, control, time);
196  constraintAD->setCurrentStateAndControl(state, control, time);
197 
198  g1_an = constraintAN->evaluateIntermediate();
199  g1_ad = constraintAD->evaluateIntermediate();
200 
201  std::cout << "g1_an: " << g1_an.transpose() << std::endl;
202  std::cout << "g1_ad: " << g1_ad.transpose() << std::endl;
203 
204  // test if constraint violations are the same
205  ASSERT_TRUE(g1_an.isApprox(g1_ad));
206 
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();
212 
213  ASSERT_TRUE(C_an.isApprox(C_ad));
214 
215  ASSERT_TRUE(D_an.isApprox(D_ad));
216 
217  ASSERT_TRUE(1.0);
218 }
219 
220 } // namespace example
221 } // namespace optcon
222 } // namespace ct
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