11 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
16 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
23 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
27 constraintsIntermediate_(arg.constraintsIntermediate_),
28 constraintsTerminal_(arg.constraintsTerminal_),
29 evalIntermediate_(arg.evalIntermediate_),
30 evalJacSparseStateIntermediate_(arg.evalJacSparseStateIntermediate_),
31 evalJacSparseInputIntermediate_(arg.evalJacSparseInputIntermediate_),
32 evalJacDenseStateIntermediate_(arg.evalJacDenseStateIntermediate_),
33 evalJacDenseInputIntermediate_(arg.evalJacDenseInputIntermediate_),
34 evalTerminal_(arg.evalTerminal_),
35 evalJacSparseStateTerminal_(arg.evalJacSparseStateTerminal_),
36 evalJacSparseInputTerminal_(arg.evalJacSparseInputTerminal_),
37 evalJacDenseStateTerminal_(arg.evalJacDenseStateTerminal_),
38 evalJacDenseInputTerminal_(arg.evalJacDenseInputTerminal_)
41 constraintsIntermediate_.resize(arg.constraintsIntermediate_.size());
42 constraintsTerminal_.resize(arg.constraintsTerminal_.size());
44 for (
size_t i = 0;
i < constraintsIntermediate_.size(); ++
i)
45 constraintsIntermediate_[
i] =
48 for (
size_t i = 0;
i < constraintsTerminal_.size(); ++
i)
49 constraintsTerminal_[
i] =
54 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
61 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
66 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
71 constraintsIntermediate_.push_back(constraint);
75 constraint->getName(name);
76 std::cout <<
"''" << name <<
"'' added as Analytical intermediate constraint " << std::endl;
81 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
86 constraintsTerminal_.push_back(constraint);
90 constraint->getName(name);
91 std::cout <<
"''" << name <<
"'' added as Analytical terminal constraint " << std::endl;
96 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
100 checkIntermediateConstraints();
103 for (
auto constraint : constraintsIntermediate_)
105 size_t constraint_dim = constraint->getConstraintSize();
106 evalIntermediate_.segment(count, constraint_dim) = constraint->evaluate(this->
x_, this->
u_, this->
t_);
107 count += constraint_dim;
109 return evalIntermediate_;
112 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
116 checkTerminalConstraints();
119 for (
auto constraint : constraintsTerminal_)
121 size_t constraint_dim = constraint->getConstraintSize();
122 evalTerminal_.segment(count, constraint_dim) = constraint->evaluate(this->
x_, this->
u_, this->
t_);
123 count += constraint_dim;
125 return evalTerminal_;
128 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
133 for (
auto constraint : constraintsIntermediate_)
134 count += constraint->getConstraintSize();
139 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
143 for (
auto constraint : constraintsTerminal_)
144 count += constraint->getConstraintSize();
149 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
153 checkIntermediateConstraints();
156 for (
auto constraint : constraintsIntermediate_)
158 size_t nonZerosState = constraint->getNumNonZerosJacobianState();
160 if (nonZerosState != 0)
162 evalJacSparseStateIntermediate_.segment(count, nonZerosState) =
163 constraint->jacobianStateSparse(this->
x_, this->
u_, this->
t_);
164 count += nonZerosState;
167 return evalJacSparseStateIntermediate_;
170 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
174 checkIntermediateConstraints();
178 for (
auto constraint : constraintsIntermediate_)
180 size_t constraint_dim = constraint->getConstraintSize();
181 evalJacDenseStateIntermediate_.block(count, 0, constraint_dim, STATE_DIM) =
182 constraint->jacobianState(this->
x_, this->
u_, this->
t_);
183 count += constraint_dim;
186 return evalJacDenseStateIntermediate_;
189 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
193 checkIntermediateConstraints();
196 for (
auto constraint : constraintsTerminal_)
198 size_t nonZerosState = constraint->getNumNonZerosJacobianState();
200 if (nonZerosState != 0)
202 evalJacSparseStateTerminal_.segment(count, nonZerosState) =
203 constraint->jacobianStateSparse(this->
x_, this->
u_, this->
t_);
204 count += nonZerosState;
207 return evalJacSparseStateTerminal_;
210 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
214 checkIntermediateConstraints();
217 for (
auto constraint : constraintsTerminal_)
219 size_t constraint_dim = constraint->getConstraintSize();
220 evalJacDenseStateTerminal_.block(count, 0, constraint_dim, STATE_DIM) =
221 constraint->jacobianState(this->
x_, this->
u_, this->
t_);
222 count += constraint_dim;
225 return evalJacDenseStateTerminal_;
228 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
232 checkIntermediateConstraints();
235 for (
auto constraint : constraintsIntermediate_)
237 size_t nonZerosInput = constraint->getNumNonZerosJacobianInput();
239 if (nonZerosInput != 0)
241 evalJacSparseInputIntermediate_.segment(count, nonZerosInput) =
242 constraint->jacobianInputSparse(this->
x_, this->
u_, this->
t_);
243 count += nonZerosInput;
246 return evalJacSparseInputIntermediate_;
249 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
253 checkIntermediateConstraints();
256 for (
auto constraint : constraintsIntermediate_)
258 size_t constraint_dim = constraint->getConstraintSize();
259 evalJacDenseInputIntermediate_.block(count, 0, constraint_dim, CONTROL_DIM) =
260 constraint->jacobianInput(this->
x_, this->
u_, this->
t_);
261 count += constraint_dim;
264 return evalJacDenseInputIntermediate_;
267 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
271 checkTerminalConstraints();
274 for (
auto constraint : constraintsTerminal_)
276 size_t nonZerosInput = constraint->getNumNonZerosJacobianInput();
278 if (nonZerosInput != 0)
280 evalJacSparseInputTerminal_.segment(count, nonZerosInput) =
281 constraint->jacobianInputSparse(this->
x_, this->
u_, this->
t_);
282 count += nonZerosInput;
285 return evalJacSparseInputTerminal_;
288 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
292 checkTerminalConstraints();
295 for (
auto constraint : constraintsTerminal_)
297 size_t constraint_dim = constraint->getConstraintSize();
298 evalJacDenseInputTerminal_.block(count, 0, constraint_dim, CONTROL_DIM) =
299 constraint->jacobianInput(this->
x_, this->
u_, this->
t_);
300 count += constraint_dim;
303 return evalJacDenseInputTerminal_;
307 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
309 Eigen::VectorXi& iRows,
310 Eigen::VectorXi& jCols)
312 checkIntermediateConstraints();
314 Eigen::VectorXi iRowLocal;
315 Eigen::VectorXi jColLocal;
316 Eigen::VectorXi iRowTot;
317 Eigen::VectorXi jColTot;
320 size_t constraintCount = 0;
322 for (
auto constraint : constraintsIntermediate_)
324 size_t nonZerosState = constraint->getNumNonZerosJacobianState();
325 if (nonZerosState > 0)
327 constraint->sparsityPatternState(iRowLocal, jColLocal);
329 iRowTot.conservativeResize(count + nonZerosState);
330 iRowTot.segment(count, nonZerosState) = iRowLocal.array() + constraintCount;
332 jColTot.conservativeResize(count + nonZerosState);
333 jColTot.segment(count, nonZerosState) = jColLocal;
335 count += nonZerosState;
337 constraintCount += constraint->getConstraintSize();
345 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
347 Eigen::VectorXi& jCols)
350 throw std::runtime_error(
351 "sparsityPatternStateTerminalConstraints not initialized yet. Call 'initialize()' before");
353 Eigen::VectorXi iRowLocal;
354 Eigen::VectorXi jColLocal;
355 Eigen::VectorXi iRowTot;
356 Eigen::VectorXi jColTot;
359 size_t constraintCount = 0;
361 for (
auto constraint : constraintsTerminal_)
363 size_t nonZerosState = constraint->getNumNonZerosJacobianState();
364 if (nonZerosState > 0)
366 constraint->sparsityPatternState(iRowLocal, jColLocal);
368 iRowTot.conservativeResize(count + nonZerosState);
369 iRowTot.segment(count, nonZerosState) = iRowLocal.array() + constraintCount;
371 jColTot.conservativeResize(count + nonZerosState);
372 jColTot.segment(count, nonZerosState) = jColLocal;
374 count += nonZerosState;
376 constraintCount += constraint->getConstraintSize();
383 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
385 Eigen::VectorXi& iRows,
386 Eigen::VectorXi& jCols)
388 checkIntermediateConstraints();
390 Eigen::VectorXi iRowLocal;
391 Eigen::VectorXi jColLocal;
392 Eigen::VectorXi iRowTot;
393 Eigen::VectorXi jColTot;
396 size_t constraintCount = 0;
398 for (
auto constraint : constraintsIntermediate_)
400 size_t nonZerosInput = constraint->getNumNonZerosJacobianInput();
401 if (nonZerosInput > 0)
403 constraint->sparsityPatternInput(iRowLocal, jColLocal);
405 iRowTot.conservativeResize(count + nonZerosInput);
406 iRowTot.segment(count, nonZerosInput) = iRowLocal.array() + constraintCount;
408 jColTot.conservativeResize(count + nonZerosInput);
409 jColTot.segment(count, nonZerosInput) = jColLocal;
411 count += nonZerosInput;
413 constraintCount += constraint->getConstraintSize();
420 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
422 Eigen::VectorXi& jCols)
425 throw std::runtime_error(
426 "sparsityPatternInputTerminalConstraints not initialized yet. Call 'initialize()' before");
428 Eigen::VectorXi iRowLocal;
429 Eigen::VectorXi jColLocal;
430 Eigen::VectorXi iRowTot;
431 Eigen::VectorXi jColTot;
434 size_t constraintCount = 0;
436 for (
auto constraint : constraintsTerminal_)
438 size_t nonZerosInput = constraint->getNumNonZerosJacobianInput();
439 if (nonZerosInput > 0)
441 constraint->sparsityPatternInput(iRowLocal, jColLocal);
443 iRowTot.conservativeResize(count + nonZerosInput);
444 iRowTot.segment(count, nonZerosInput) = iRowLocal.array() + constraintCount;
446 jColTot.conservativeResize(count + nonZerosInput);
447 jColTot.segment(count, nonZerosInput) = jColLocal;
449 count += nonZerosInput;
451 constraintCount += constraint->getConstraintSize();
458 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
462 for (
auto constraint : constraintsIntermediate_)
463 count += constraint->getNumNonZerosJacobianState();
468 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
472 for (
auto constraint : constraintsTerminal_)
473 count += constraint->getNumNonZerosJacobianState();
478 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
482 for (
auto constraint : constraintsIntermediate_)
483 count += constraint->getNumNonZerosJacobianInput();
488 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
492 for (
auto constraint : constraintsTerminal_)
493 count += constraint->getNumNonZerosJacobianInput();
498 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
504 evalIntermediate_.setZero();
506 evalJacSparseStateIntermediate_.setZero();
508 evalJacSparseInputIntermediate_.setZero();
510 evalJacDenseStateIntermediate_.setZero();
512 evalJacDenseInputIntermediate_.setZero();
521 for (
auto constraint : constraintsIntermediate_)
523 size_t constraintSize = constraint->getConstraintSize();
526 count += constraintSize;
533 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
539 evalTerminal_.setZero();
541 evalJacSparseStateTerminal_.setZero();
543 evalJacSparseInputTerminal_.setZero();
545 evalJacDenseStateTerminal_.setZero();
547 evalJacDenseInputTerminal_.setZero();
556 for (
auto constraint : constraintsTerminal_)
558 size_t constraintSize = constraint->getConstraintSize();
561 count += constraintSize;
569 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
574 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
578 throw std::runtime_error(
"Error: Intermediate constraints are or not initialized yet. ");
581 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
585 throw std::runtime_error(
"Error: Terminal constraints are either not initialized yet. ");
virtual void sparsityPatternInputTerminal(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt control.
Definition: ConstraintContainerAnalytical-impl.h:421
virtual ConstraintContainerAnalytical_Raw_Ptr_t clone() const override
Deep-cloning of Constraint.
Definition: ConstraintContainerAnalytical-impl.h:56
virtual MatrixXs jacobianStateIntermediate() override
Evaluates the constraint jacobian wrt the state.
Definition: ConstraintContainerAnalytical-impl.h:172
virtual size_t getJacobianInputNonZeroCountIntermediate() override
Returns the number of non zero elements in the constraint jacobian wrt input.
Definition: ConstraintContainerAnalytical-impl.h:479
virtual VectorXs jacobianStateSparseTerminal() override
Evaluates the constraint jacobian wrt the state using sparse representation.
Definition: ConstraintContainerAnalytical-impl.h:191
virtual void sparsityPatternStateTerminal(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt state.
Definition: ConstraintContainerAnalytical-impl.h:346
A base function for linear constraint functions which have a first derivative.
Definition: LinearConstraintContainer.h:27
input_vector_t u_
Definition: ConstraintContainerBase.h:198
void addTerminalConstraint(std::shared_ptr< ConstraintBase< STATE_DIM, CONTROL_DIM, SCALAR >> constraint, bool verbose)
Adds a terminal constraint.
Definition: ConstraintContainerAnalytical-impl.h:82
virtual size_t getJacobianInputNonZeroCountTerminal() override
Returns the number of non zero elements in the constraint jacobian wrt input.
Definition: ConstraintContainerAnalytical-impl.h:489
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
virtual bool initializeTerminal() override
Initializes the terminal constraints.
Definition: ConstraintContainerAnalytical-impl.h:534
bool initializedIntermediate_
Definition: LinearConstraintContainer.h:238
virtual MatrixXs jacobianStateTerminal() override
Evaluates the constraint jacobian wrt the state.
Definition: ConstraintContainerAnalytical-impl.h:212
virtual VectorXs jacobianInputSparseIntermediate() override
Evaluates the constraint jacobian wrt the control input using sparse representation.
Definition: ConstraintContainerAnalytical-impl.h:230
VectorXs lowerBoundsTerminal_
Definition: ConstraintContainerBase.h:202
virtual size_t getIntermediateConstraintsCount() override
Retrieves the number of intermediate constraints.
Definition: ConstraintContainerAnalytical-impl.h:129
virtual size_t getTerminalConstraintsCount() override
Retrieves the number of final constraints.
Definition: ConstraintContainerAnalytical-impl.h:140
virtual VectorXs evaluateTerminal() override
Evaluates the terminal constraints.
Definition: ConstraintContainerAnalytical-impl.h:114
virtual VectorXs jacobianStateSparseIntermediate() override
Evaluates the constraint jacobian wrt the state using sparse representation.
Definition: ConstraintContainerAnalytical-impl.h:151
virtual void sparsityPatternInputIntermediate(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt control.
Definition: ConstraintContainerAnalytical-impl.h:384
void addIntermediateConstraint(std::shared_ptr< ConstraintBase< STATE_DIM, CONTROL_DIM, SCALAR >> constraint, bool verbose)
Adds an intermedaite constraint.
Definition: ConstraintContainerAnalytical-impl.h:67
VectorXs upperBoundsTerminal_
Definition: ConstraintContainerBase.h:204
virtual VectorXs evaluateIntermediate() override
Evaluates the intermediate constraints.
Definition: ConstraintContainerAnalytical-impl.h:98
VectorXs lowerBoundsIntermediate_
Definition: ConstraintContainerBase.h:201
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
virtual void sparsityPatternStateIntermediate(Eigen::VectorXi &iRows, Eigen::VectorXi &jCols) override
Returns the sparsity pattern for the jacobian wrt state.
Definition: ConstraintContainerAnalytical-impl.h:308
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ConstraintContainerAnalytical.h:36
CppAD::AD< CppAD::cg::CG< double > > SCALAR
bool initializedTerminal_
Definition: LinearConstraintContainer.h:239
virtual VectorXs jacobianInputSparseTerminal() override
Evaluates the constraint jacobian wrt the control input using sparse representation.
Definition: ConstraintContainerAnalytical-impl.h:269
virtual size_t getJacobianStateNonZeroCountIntermediate() override
Returns the number of non zero elements in the constraint jacobian wrt state.
Definition: ConstraintContainerAnalytical-impl.h:459
Eigen::Matrix< SCALAR, Eigen::Dynamic, Eigen::Dynamic > MatrixXs
Definition: ConstraintContainerAnalytical.h:37
ConstraintContainerAnalytical()
Definition: ConstraintContainerAnalytical-impl.h:12
for i
Definition: mpc_unittest_plotting.m:14
SCALAR t_
Definition: ConstraintContainerBase.h:199
Contains all the constraints using analytically calculated jacobians.
Definition: ConstraintContainerAnalytical.h:27
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
virtual MatrixXs jacobianInputIntermediate() override
Evaluates the constraint jacobian wrt the control input.
Definition: ConstraintContainerAnalytical-impl.h:251
virtual bool initializeIntermediate() override
Initializes the intermediate constraints.
Definition: ConstraintContainerAnalytical-impl.h:499
VectorXs upperBoundsIntermediate_
Definition: ConstraintContainerBase.h:203
state_vector_t x_
Definition: ConstraintContainerBase.h:197
virtual size_t getJacobianStateNonZeroCountTerminal() override
Returns the number of non zero elements in the constraint jacobian wrt state.
Definition: ConstraintContainerAnalytical-impl.h:469
virtual ~ConstraintContainerAnalytical()
Destructor.
Definition: ConstraintContainerAnalytical-impl.h:62
virtual MatrixXs jacobianInputTerminal() override
Evaluates the constraint jacobian wrt the control input.
Definition: ConstraintContainerAnalytical-impl.h:290
Eigen::Matrix< double, nStates, 1 > state_vector_t
const bool verbose
Definition: ConstraintComparison.h:18
Base class for the constraints used in this toolbox.
Definition: ConstraintBase.h:21