29 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR =
double>
33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
43 typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>
VectorXs;
66 controlSpliner_(controlSpliner),
70 constraintsIntermediateCount_(0),
71 constraintsTerminalCount_(0),
73 nonZeroJacCountIntermediate_(0),
74 nonZeroJacCountTerminal_(0)
81 constraints_.push_back(boxConstraints);
82 constraintsIntermediateCount_ += (N_ + 1) * boxConstraints->getIntermediateConstraintsCount();
83 constraintsTerminalCount_ += boxConstraints->getTerminalConstraintsCount();
84 constraintsCount_ = constraintsIntermediateCount_ + constraintsTerminalCount_;
86 discreteConstraints_.resize(constraintsCount_);
87 discreteLowerBound_.resize(constraintsCount_);
88 discreteUpperBound_.resize(constraintsCount_);
90 nonZeroJacCountIntermediate_ += (N_ + 1) * (boxConstraints->getJacobianStateNonZeroCountIntermediate() +
91 boxConstraints->getJacobianInputNonZeroCountIntermediate());
92 nonZeroJacCountTerminal_ += boxConstraints->getJacobianStateNonZeroCountTerminal() +
93 boxConstraints->getJacobianInputNonZeroCountTerminal();
94 nonZeroJacCount_ = nonZeroJacCountIntermediate_ + nonZeroJacCountTerminal_;
96 discreteJac_.resize(nonZeroJacCount_);
97 discreteIRow_.resize(nonZeroJacCount_);
98 discreteJCol_.resize(nonZeroJacCount_);
104 constraints_.push_back(generalConstraints);
105 constraintsIntermediateCount_ += (N_ + 1) * generalConstraints->getIntermediateConstraintsCount();
106 constraintsTerminalCount_ += generalConstraints->getTerminalConstraintsCount();
107 constraintsCount_ = constraintsIntermediateCount_ + constraintsTerminalCount_;
109 discreteConstraints_.resize(constraintsCount_);
110 discreteLowerBound_.resize(constraintsCount_);
111 discreteUpperBound_.resize(constraintsCount_);
113 nonZeroJacCountIntermediate_ += (N_ + 1) * generalConstraints->getJacobianStateNonZeroCountIntermediate();
114 nonZeroJacCountTerminal_ += generalConstraints->getJacobianStateNonZeroCountTerminal();
115 nonZeroJacCount_ = nonZeroJacCountIntermediate_ + nonZeroJacCountTerminal_;
117 discreteJac_.resize(nonZeroJacCount_);
118 discreteIRow_.resize(nonZeroJacCount_);
119 discreteJCol_.resize(nonZeroJacCount_);
124 size_t constraintSize = 0;
125 size_t discreteInd = 0;
127 for (
size_t n = 0;
n < N_ + 1; ++
n)
129 SCALAR tShot = timeGrid_->getShotStartTime(
n);
130 for (
auto constraint : constraints_)
132 constraint->setCurrentStateAndControl(
133 w_->getOptimizedState(
n), controlSpliner_->evalSpline(tShot,
n), tShot);
134 constraintSize = constraint->getIntermediateConstraintsCount();
135 if (constraintSize > 0)
137 discreteConstraints_.segment(discreteInd, constraintSize) = constraint->evaluateIntermediate();
138 discreteInd += constraintSize;
143 for (
auto constraint : constraints_)
145 constraintSize = constraint->getTerminalConstraintsCount();
146 if (constraintSize > 0)
148 discreteConstraints_.segment(discreteInd, constraintSize) = constraint->evaluateTerminal();
149 discreteInd += constraintSize;
153 return discreteConstraints_;
159 size_t discreteInd = 0;
161 for (
size_t n = 0;
n < N_ + 1; ++
n)
163 SCALAR tShot = timeGrid_->getShotStartTime(
n);
165 for (
auto constraint : constraints_)
167 constraint->setCurrentStateAndControl(
168 w_->getOptimizedState(
n), controlSpliner_->evalSpline(tShot,
n), tShot);
169 jacSize = constraint->getJacobianStateNonZeroCountIntermediate();
172 discreteJac_.segment(discreteInd, jacSize) = constraint->jacobianStateSparseIntermediate();
173 discreteInd += jacSize;
176 jacSize = constraint->getJacobianInputNonZeroCountIntermediate();
179 discreteJac_.segment(discreteInd, jacSize) = constraint->jacobianInputSparseIntermediate();
180 discreteInd += jacSize;
185 for (
auto constraint : constraints_)
187 jacSize = constraint->getJacobianStateNonZeroCountTerminal();
190 discreteJac_.segment(discreteInd, jacSize) = constraint->jacobianStateSparseTerminal();
191 discreteInd += jacSize;
193 jacSize = constraint->getJacobianInputNonZeroCountTerminal();
196 discreteJac_.segment(discreteInd, jacSize) = constraint->jacobianInputSparseTerminal();
197 discreteInd += jacSize;
207 size_t discreteInd = 0;
208 size_t rowOffset = 0;
211 for (
size_t n = 0;
n < N_ + 1; ++
n)
213 for (
auto constraint : constraints_)
215 nnEle = constraint->getJacobianStateNonZeroCountIntermediate();
218 Eigen::VectorXi iRowStateIntermediate(constraint->getJacobianStateNonZeroCountIntermediate());
219 Eigen::VectorXi jColStateIntermediate(constraint->getJacobianStateNonZeroCountIntermediate());
220 constraint->sparsityPatternStateIntermediate(iRowStateIntermediate, jColStateIntermediate);
221 discreteIRow_.segment(discreteInd, nnEle) = iRowStateIntermediate.array() + rowOffset;
222 discreteJCol_.segment(discreteInd, nnEle) = jColStateIntermediate.array() + w_->getStateIndex(
n);
223 discreteInd += nnEle;
226 nnEle = constraint->getJacobianInputNonZeroCountIntermediate();
229 Eigen::VectorXi iRowInputIntermediate(constraint->getJacobianInputNonZeroCountIntermediate());
230 Eigen::VectorXi jColInputIntermediate(constraint->getJacobianInputNonZeroCountIntermediate());
231 constraint->sparsityPatternInputIntermediate(iRowInputIntermediate, jColInputIntermediate);
232 discreteIRow_.segment(discreteInd, nnEle) = iRowInputIntermediate.array() + rowOffset;
233 discreteJCol_.segment(discreteInd, nnEle) = jColInputIntermediate.array() + w_->getControlIndex(
n);
234 discreteInd += nnEle;
237 rowOffset += constraint->getIntermediateConstraintsCount();
241 for (
auto constraint : constraints_)
243 nnEle = constraint->getJacobianStateNonZeroCountTerminal();
246 Eigen::VectorXi iRowStateTerminal(constraint->getJacobianStateNonZeroCountTerminal());
247 Eigen::VectorXi jColStateTerminal(constraint->getJacobianStateNonZeroCountTerminal());
248 constraint->sparsityPatternStateTerminal(iRowStateTerminal, jColStateTerminal);
249 discreteIRow_.segment(discreteInd, nnEle) = iRowStateTerminal.array() + rowOffset;
250 discreteJCol_.segment(discreteInd, nnEle) = jColStateTerminal.array() + w_->getStateIndex(N_);
251 discreteInd += nnEle;
254 nnEle = constraint->getJacobianInputNonZeroCountTerminal();
257 Eigen::VectorXi iRowInputTerminal(constraint->getJacobianInputNonZeroCountTerminal());
258 Eigen::VectorXi jColInputTerminal(constraint->getJacobianInputNonZeroCountTerminal());
259 constraint->sparsityPatternInputTerminal(iRowInputTerminal, jColInputTerminal);
260 discreteIRow_.segment(discreteInd, nnEle) = iRowInputTerminal.array() + rowOffset;
261 discreteJCol_.segment(discreteInd, nnEle) = jColInputTerminal.array() + w_->getControlIndex(N_);
262 discreteInd += nnEle;
264 rowOffset += constraint->getTerminalConstraintsCount();
267 iRow_vec = discreteIRow_;
268 jCol_vec = discreteJCol_;
273 size_t discreteInd = 0;
274 size_t constraintSize = 0;
276 for (
size_t n = 0;
n < N_ + 1; ++
n)
278 for (
auto constraint : constraints_)
280 constraintSize = constraint->getIntermediateConstraintsCount();
281 if (constraintSize > 0)
283 discreteLowerBound_.segment(discreteInd, constraintSize) = constraint->getLowerBoundsIntermediate();
284 discreteInd += constraintSize;
289 for (
auto constraint : constraints_)
291 constraintSize = constraint->getTerminalConstraintsCount();
292 if (constraintSize > 0)
294 discreteLowerBound_.segment(discreteInd, constraintSize) = constraint->getLowerBoundsTerminal();
295 discreteInd += constraintSize;
299 return discreteLowerBound_;
304 size_t discreteInd = 0;
305 size_t constraintSize = 0;
307 for (
size_t n = 0;
n < N_ + 1; ++
n)
309 for (
auto constraint : constraints_)
311 constraintSize = constraint->getIntermediateConstraintsCount();
312 if (constraintSize > 0)
314 discreteUpperBound_.segment(discreteInd, constraintSize) = constraint->getUpperBoundsIntermediate();
315 discreteInd += constraintSize;
320 for (
auto constraint : constraints_)
322 constraintSize = constraint->getTerminalConstraintsCount();
323 if (constraintSize > 0)
325 discreteUpperBound_.segment(discreteInd, constraintSize) = constraint->getUpperBoundsTerminal();
326 discreteInd += constraintSize;
330 return discreteUpperBound_;
335 std::shared_ptr<OptVectorDms<STATE_DIM, CONTROL_DIM, SCALAR>> w_;
336 std::shared_ptr<SplinerBase<control_vector_t, SCALAR>> controlSpliner_;
337 std::shared_ptr<tpl::TimeGrid<SCALAR>> timeGrid_;
340 std::vector<std::shared_ptr<LinearConstraintContainer<STATE_DIM, CONTROL_DIM, SCALAR>>> constraints_;
342 size_t constraintsCount_;
343 size_t constraintsIntermediateCount_;
344 size_t constraintsTerminalCount_;
346 VectorXs discreteConstraints_;
347 VectorXs discreteLowerBound_;
348 VectorXs discreteUpperBound_;
350 VectorXs discreteJac_;
351 Eigen::VectorXi discreteIRow_;
352 Eigen::VectorXi discreteJCol_;
354 size_t nonZeroJacCount_;
355 size_t nonZeroJacCountIntermediate_;
356 size_t nonZeroJacCountTerminal_;
VectorXs getUpperBound() override
Returns the upper bound of the constraint.
Definition: ConstraintDiscretizer.h:302
VectorXs getLowerBound() override
Returns the lower bound of the constraint.
Definition: ConstraintDiscretizer.h:271
size_t getNumNonZerosJacobian() override
Returns the number of non zero elements of the jacobian.
Definition: ConstraintDiscretizer.h:204
A base function for linear constraint functions which have a first derivative.
Definition: LinearConstraintContainer.h:27
ct::core::ControlVectorArray< CONTROL_DIM, SCALAR > control_vector_array_t
Definition: ConstraintDiscretizer.h:40
void setGeneralConstraints(std::shared_ptr< LinearConstraintContainer< STATE_DIM, CONTROL_DIM, SCALAR >> generalConstraints)
Definition: ConstraintDiscretizer.h:101
ct::core::StateVector< STATE_DIM, SCALAR > state_vector_t
Definition: ConstraintDiscretizer.h:36
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef tpl::DiscreteConstraintBase< SCALAR > BASE
Definition: ConstraintDiscretizer.h:35
Abstract base class for the control input splining between the DMS shots.
Definition: SplinerBase.h:20
ConstraintDiscretizer()=default
Default constructor.
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: ConstraintDiscretizer.h:43
Implements an abstract base class from which all the discrete custom NLP constraints should derive...
Definition: DiscreteConstraintBase.h:21
This class is a wrapper around the NLP Optvector. It wraps the Vectors from the NLP solvers into stat...
Definition: OptVectorDms.h:37
~ConstraintDiscretizer() override=default
Destructor.
void genSparsityPattern(Eigen::VectorXi &iRow_vec, Eigen::VectorXi &jCol_vec) override
Returns the sparsity structure of the constraint jacobian.
Definition: ConstraintDiscretizer.h:205
CppAD::AD< CppAD::cg::CG< double > > SCALAR
ConstraintDiscretizer(std::shared_ptr< OptVectorDms< STATE_DIM, CONTROL_DIM, SCALAR >> w, std::shared_ptr< SplinerBase< control_vector_t, SCALAR >> controlSpliner, std::shared_ptr< tpl::TimeGrid< SCALAR >> timeGrid, size_t N)
Custom constructor.
Definition: ConstraintDiscretizer.h:61
ct::core::StateVectorArray< STATE_DIM, SCALAR > state_vector_array_t
Definition: ConstraintDiscretizer.h:37
void setBoxConstraints(std::shared_ptr< LinearConstraintContainer< STATE_DIM, CONTROL_DIM, SCALAR >> boxConstraints)
Definition: ConstraintDiscretizer.h:79
Definition: TimeGrid.h:27
VectorXs evalSparseJacobian() override
Returns the non zero elements of the eval method with respect to the optimization variables...
Definition: ConstraintDiscretizer.h:156
ct::core::tpl::TimeArray< SCALAR > time_array_t
Definition: ConstraintDiscretizer.h:42
size_t getConstraintSize() override
Returns size of the constraint vector.
Definition: ConstraintDiscretizer.h:333
The class takes continuous constraints defined with the constraint toolbox and discretizes them over ...
Definition: ConstraintDiscretizer.h:30
ct::core::ControlVector< CONTROL_DIM, SCALAR > control_vector_t
Definition: ConstraintDiscretizer.h:39
VectorXs eval() override
Evaluates the constraint violation.
Definition: ConstraintDiscretizer.h:122