22 template <
typename SCALAR>
26 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
28 using VectorXs = Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>;
29 using VectorXi = Eigen::Matrix<int, Eigen::Dynamic, 1>;
69 size_t cSize = constraint->getConstraintSize();
70 c_nlp.segment(ind, cSize) = constraint->eval();
74 assert(ind == static_cast<size_t>(c_nlp.rows()));
84 size_t cSize = constraint->getConstraintSize();
85 c_nlp.segment(ind, cSize) = constraint->eval();
89 assert(static_cast<int>(ind) == c_nlp.rows());
106 size_t nnEle = constraint->getNumNonZerosJacobian();
107 jac_nlp.segment(ind, nnEle) = constraint->evalSparseJacobian();
111 assert(static_cast<int>(ind) == nzz_jac_g);
125 Eigen::Map<Eigen::VectorXi>& jCol_nlp,
129 size_t constraintCount = 0;
133 size_t nnEle = constraint->getNumNonZerosJacobian();
134 size_t cSize = constraint->getConstraintSize();
135 Eigen::VectorXi iRow, jCol;
138 constraint->genSparsityPattern(iRow, jCol);
139 iRow_nlp.segment(ind, nnEle) = iRow.array() + constraintCount;
140 jCol_nlp.segment(ind, nnEle) = jCol;
141 constraintCount += cSize;
145 assert(static_cast<int>(ind) == nnz_jac_g);
157 count += constraint->getConstraintSize();
170 count += constraint->getNumNonZerosJacobian();
182 #if EIGEN_VERSION_AT_LEAST(3, 3, 0) 185 constraintHessianTot_.resize(numOptVar, numOptVar);
186 constraintHessianSparsity_.resize(numOptVar, numOptVar);
188 std::vector<Eigen::Triplet<SCALAR>, Eigen::aligned_allocator<Eigen::Triplet<SCALAR>>> triplets;
195 triplets.push_back(Eigen::Triplet<SCALAR>(
200 constraintHessianSparsity_.setFromTriplets(triplets.begin(), triplets.end());
205 for (
int k = 0; k < constraintHessianSparsity_.outerSize(); k++)
207 for (
typename Eigen::SparseMatrix<SCALAR>::InnerIterator it(constraintHessianSparsity_, k); it; ++it)
217 throw std::runtime_error(
218 "getSparsityPatternHessian only available for Eigen 3.3 and newer. Please change solver settings to NOT " 219 "use exact Hessians or upgrade Eigen version.");
233 #if EIGEN_VERSION_AT_LEAST(3, 3, 0) 235 std::vector<Eigen::Triplet<SCALAR>, Eigen::aligned_allocator<Eigen::Triplet<SCALAR>>> triplets;
242 Eigen::VectorXd hessianSubValues;
243 constraints_[c]->sparseHessianValues(optVec, lambda.segment(count, c_nel), hessianSubValues);
247 for (
int i = 0;
i < hessianSubValues.rows();
i++)
248 triplets.push_back(Eigen::Triplet<SCALAR>(
253 constraintHessianTot_.setFromTriplets(triplets.begin(), triplets.end());
256 constraintHessianTot_ = constraintHessianTot_.template triangularView<Eigen::Lower>();
259 return Eigen::VectorXd(Eigen::Map<Eigen::VectorXd>(constraintHessianTot_.valuePtr(), nele_constraint_hes, 1));
261 throw std::runtime_error(
262 "sparseHessianValues only available for Eigen 3.3 and newer. Please use BFGS Hessian approx or upgrade " 264 return Eigen::VectorXd::Zero();
280 size_t cSize = constraint->getConstraintSize();
281 lowerBound.segment(ind, cSize) = constraint->getLowerBound();
282 upperBound.segment(ind, cSize) = constraint->getUpperBound();
289 std::vector<std::shared_ptr<DiscreteConstraintBase<SCALAR>>>
constraints_;
294 #if EIGEN_VERSION_AT_LEAST(3, 3, 0) 295 Eigen::SparseMatrix<SCALAR> constraintHessianTot_;
296 Eigen::SparseMatrix<SCALAR>
297 constraintHessianSparsity_;
void getBounds(MapVecXs &lowerBound, MapVecXs &upperBound)
Retrieves the constraint bounds and writes them into the vectors used in the NLP. ...
Definition: DiscreteConstraintContainerBase.h:275
DiscreteConstraintContainerBase()=default
Default constructor.
void evalConstraints(MapVecXs &c_nlp)
Writes the constraint evaluations into the large constraint optimization vector.
Definition: DiscreteConstraintContainerBase.h:62
size_t getConstraintsCount() const
Returns the number of constraints in the NLP.
Definition: DiscreteConstraintContainerBase.h:153
virtual void prepareJacobianEvaluation()=0
Gets called before the constraint jacobian evaluation. This method should contain all the calculation...
virtual void prepareEvaluation()=0
Gets called before the constraint evaluation. This method should contain all the calculations needed ...
An abstract base class which serves as a container for all the discrete constraints used in the NLP...
Definition: DiscreteConstraintContainerBase.h:23
size_t getNonZerosJacobianCount() const
Returns the number of non zeros in the constraint jacobian.
Definition: DiscreteConstraintContainerBase.h:166
CppAD::AD< CppAD::cg::CG< double > > SCALAR
for i
Definition: mpc_unittest_plotting.m:14
void evalSparseJacobian(MapVecXs &jac_nlp, const int nzz_jac_g)
Evaluates the jacobian of the constraints and writes them into the nlp vector.
Definition: DiscreteConstraintContainerBase.h:99
void getSparsityPatternHessian(Eigen::VectorXi &iRow, Eigen::VectorXi &jCol, size_t numOptVar)
creates the combined hessian sparsity pattern from a number of constraint terms
Definition: DiscreteConstraintContainerBase.h:180
std::vector< int > iRowHessianStdVec_
Definition: DiscreteConstraintContainerBase.h:291
virtual ~DiscreteConstraintContainerBase()=default
Destructor.
std::vector< std::shared_ptr< DiscreteConstraintBase< SCALAR > > > constraints_
Container which holds all the constraints of the NLP.
Definition: DiscreteConstraintContainerBase.h:289
Eigen::VectorXd sparseHessianValues(const Eigen::VectorXd &optVec, const Eigen::VectorXd &lambda)
Evaluates the constraint Hessian.
Definition: DiscreteConstraintContainerBase.h:231
std::vector< int > jColHessianStdVec_
Definition: DiscreteConstraintContainerBase.h:292
void getSparsityPattern(Eigen::Map< Eigen::VectorXi > &iRow_nlp, Eigen::Map< Eigen::VectorXi > &jCol_nlp, const int nnz_jac_g)
Retrieves the sparsity pattern of the constraint Jacobian and writes them into the nlp vectors...
Definition: DiscreteConstraintContainerBase.h:124
Eigen::Matrix< int, Eigen::Dynamic, 1 > VectorXi
Definition: DiscreteConstraintContainerBase.h:29
Eigen::Map< VectorXs > MapVecXs
Definition: DiscreteConstraintContainerBase.h:30
void evalConstraints(VectorXs &c_nlp)
Definition: DiscreteConstraintContainerBase.h:77
Eigen::Map< VectorXi > MapVecXi
Definition: DiscreteConstraintContainerBase.h:31
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: DiscreteConstraintContainerBase.h:28