9 #include <Eigen/Sparse> 31 template <
typename SCALAR>
35 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
37 using VectorXs = Eigen::Matrix<SCALAR, Eigen::Dynamic, 1>;
38 using VectorXi = Eigen::Matrix<int, Eigen::Dynamic, 1>;
50 virtual ~Nlp() =
default;
66 throw std::runtime_error(
"Error in evaluateCostFun. Costevaluator not initialized");
81 throw std::runtime_error(
"Error in evaluateCostGradient. Costevaluator not initialized");
95 throw std::runtime_error(
"Error in evaluateConstraints. Constraints not initialized");
109 throw std::runtime_error(
"Error in evaluateConstraintJacobian. Constraints not initialized");
126 #if EIGEN_VERSION_AT_LEAST(3, 3, 0) 129 Eigen::Matrix<double, 1, 1> omega;
133 Eigen::VectorXd hessianCostValues, hessianConstraintsValues;
142 std::vector<Eigen::Triplet<SCALAR>, Eigen::aligned_allocator<Eigen::Triplet<SCALAR>>> triplets;
151 triplets.push_back(Eigen::Triplet<SCALAR>(
156 Hessian_eval_.setFromTriplets(triplets.begin(), triplets.end());
161 Hessian_eval_ = Hessian_eval_.template triangularView<Eigen::Lower>();
163 hes = Eigen::Map<Eigen::VectorXd>(Hessian_eval_.valuePtr(), nele_hes, 1);
166 throw std::runtime_error(
167 "Nlp::evaluateHessian() only available for Eigen 3.3 and newer. Please change solver settings to NOT use " 185 throw std::runtime_error(
"Error in getSparsityPatternJacobian. Constraints not initialized");
215 throw std::runtime_error(
"Error in getConstraintsCount. Constraints not initialized");
230 throw std::runtime_error(
"Error in getNonZeroJacobianCount. Constraints not initialized");
244 #if EIGEN_VERSION_AT_LEAST(3, 3, 0) 265 std::vector<Eigen::Triplet<SCALAR>, Eigen::aligned_allocator<Eigen::Triplet<SCALAR>>> triplets;
276 Hessian_sparsity_.setFromTriplets(triplets.begin(), triplets.end());
281 Hessian_sparsity_ = Hessian_sparsity_.template triangularView<Eigen::Lower>();
284 std::vector<int> iRowHessianStdVec;
285 std::vector<int> jColHessianStdVec;
286 for (
int k = 0; k < Hessian_sparsity_.outerSize(); ++k)
288 for (
typename Eigen::SparseMatrix<SCALAR>::InnerIterator it(Hessian_sparsity_, k); it; ++it)
290 iRowHessianStdVec.push_back(it.row());
291 jColHessianStdVec.push_back(it.col());
294 iRowHessian_ = Eigen::Map<Eigen::VectorXi>(iRowHessianStdVec.data(), iRowHessianStdVec.size(), 1);
295 jColHessian_ = Eigen::Map<Eigen::VectorXi>(jColHessianStdVec.data(), jColHessianStdVec.size(), 1);
299 return nonZerosHessian;
301 throw std::runtime_error(
302 "Nlp::getNonZeroHessianCount() only available for Eigen 3.3 and newer. Please change solver settings to " 304 "exact Hessians or upgrade Eigen version.");
319 throw std::runtime_error(
"Error in getConstraintBounds. Constraints not initialized");
332 throw std::runtime_error(
"Error in getVarCount. Optvariables not initialized");
347 throw std::runtime_error(
"Error in getVariableBounds. Optvariables not initialized");
363 throw std::runtime_error(
"Error in extractOptimizationVars. Optvariables not initialized");
381 throw std::runtime_error(
"Error in getOptimizationVars. Optvariables not initialized");
398 throw std::runtime_error(
"Error in getOptimizationMultState. Optvariables not initialized");
414 throw std::runtime_error(
"Error in getConstraintsMultState. Optvariables not initialized");
429 throw std::runtime_error(
"Error in getBoundMultipliers. Optvariables not initialized");
443 throw std::runtime_error(
"Error in getLambdaVars. Optvariables not initialized");
462 throw std::runtime_error(
"Error in extractIpoptSolution. Optvariables not initialized");
483 throw std::runtime_error(
"Error in extractSnoptSolution. Optvariables not initialized");
485 optVariables_->setNewSnoptSolution(x, xMul, xState, fMul, fState);
499 #if EIGEN_VERSION_AT_LEAST(3, 3, 0) 500 Eigen::SparseMatrix<SCALAR> Hessian_eval_;
501 Eigen::SparseMatrix<SCALAR> Hessian_sparsity_;
void getVariableBounds(MapVecXs &lowerBound, MapVecXs &upperBound, const size_t n) const
Reads the bounds on the Optimization optimization variables.
Definition: Nlp.h:344
Eigen::VectorXi jColHessianConstraints_
Definition: Nlp.h:505
void getConstraintsMultState(const size_t m, MapVecXs &zMul, MapVecXi &zState) const
Gets the constraint multiplier and state, used in the NLP solver SNOPT.
Definition: Nlp.h:411
void getConstraintBounds(MapVecXs &lowerBound, MapVecXs &upperBound, const size_t m) const
Reads the bounds of the constraints.
Definition: Nlp.h:316
void evaluateHessian(const int nele_hes, MapVecXs &hes, const SCALAR obj_fac, MapConstVecXs &lambda)
Evaluates the hessian of the lagrangian.
Definition: Nlp.h:124
void extractSnoptSolution(const MapVecXs &x, const MapVecXs &xMul, const MapVecXi &xState, const MapVecXs &fMul, const MapVecXi &fState)
{ Extracts the solution values from SNOPT }
Definition: Nlp.h:476
The NLP base class. This class serves as abstract base class to use as an interface to the NLP solver...
Definition: Nlp.h:32
void evaluateConstraints(MapVecXs &values)
{ Evaluates the constraints }
Definition: Nlp.h:92
size_t getConstraintsCount() const
Returns the number of constraints in the NLP.
Definition: Nlp.h:212
void extractIpoptSolution(const MapConstVecXs &x, const MapConstVecXs &zL, const MapConstVecXs &zU, const MapConstVecXs &lambda)
{ Extracts the solution values from IPOPT }
Definition: Nlp.h:456
Eigen::Matrix< SCALAR, Eigen::Dynamic, 1 > VectorXs
Definition: Nlp.h:37
void getSparsityPatternHessian(const int nele_hes, MapVecXi &iRow, MapVecXi &jCol) const
Gets the sparsity pattern of the Hessian of the Lagrangian.
Definition: Nlp.h:200
void getBoundMultipliers(size_t n, MapVecXs &zLow, MapVecXs &zUp) const
Gets the bound multipliers used in the NLP solver IPOPT.
Definition: Nlp.h:426
virtual ~Nlp()=default
Destructor.
void getLambdaVars(size_t m, MapVecXs &lambda) const
Gets the values of the constraint multipliers.
Definition: Nlp.h:440
std::shared_ptr< DiscreteCostEvaluatorBase< SCALAR > > costEvaluator_
Ptr to cost evaluator, which approximates the cost evaluation for the discrete problem.
Definition: Nlp.h:491
size_t getVarCount() const
Returns the number of Optimization optimization variables.
Definition: Nlp.h:329
Eigen::VectorXi jColHessianCost_
Definition: Nlp.h:505
std::shared_ptr< OptVector< SCALAR > > optVariables_
Ptr to optimization variable container, which holds the optimization variables used in the NLP solver...
Definition: Nlp.h:494
void extractOptimizationVars(const MapConstVecXs &x, bool isNew)
{Extracts the Optimization optimization variables from the nlp solvers between nlp iterations} ...
Definition: Nlp.h:360
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void evaluateCostGradient(const size_t n, MapVecXs &grad)
{ Evaluates the gradient of the costfunction}
Definition: Nlp.h:78
virtual void updateProblem()=0
{ This method gets called at each update of the Optimization variables. This can be used to distribut...
for i
Definition: mpc_unittest_plotting.m:14
void getSparsityPatternJacobian(const int nele_jac, MapVecXi &iRow, MapVecXi &jCol) const
Gets the sparsity pattern.
Definition: Nlp.h:182
size_t getNonZeroJacobianCount() const
Returns the number of the non zero elements of the constraint jacobian.
Definition: Nlp.h:227
size_t getNonZeroHessianCount()
Returns the number of non zeros in the Hessian.
Definition: Nlp.h:242
Eigen::VectorXi iRowHessianCost_
helper containers for calculating the sparsity patterns
Definition: Nlp.h:505
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
Eigen::VectorXi jColHessian_
Definition: Nlp.h:508
void getOptimizationMultState(const size_t n, MapVecXs &xMul, MapVecXi &xState) const
Gets the variable multiplier and the variable state, used in the NLP solver SNOPT. See the snopt documentation for further explanations.
Definition: Nlp.h:395
Nlp()=default
Default constructor.
Eigen::Map< const VectorXs > MapConstVecXs
Definition: Nlp.h:41
void evaluateConstraintJacobian(const int nele_jac, MapVecXs &jac)
{ Evaluates the constraint jacobian }
Definition: Nlp.h:106
Eigen::Map< VectorXi > MapVecXi
Definition: Nlp.h:40
std::shared_ptr< DiscreteConstraintContainerBase< SCALAR > > constraints_
Ptr to constraint container, which contains the discretized constraints for the problem.
Definition: Nlp.h:497
Eigen::VectorXi iRowHessianConstraints_
Definition: Nlp.h:505
Eigen::Map< VectorXs > MapVecXs
Definition: Nlp.h:39
Eigen::Matrix< int, Eigen::Dynamic, 1 > VectorXi
Definition: Nlp.h:38
void getInitialGuess(const size_t n, MapVecXs &x) const
Gets the Optimization variables.
Definition: Nlp.h:378
SCALAR evaluateCostFun()
{ Evaluates the costfunction at the current nlp iteration }
Definition: Nlp.h:63
Eigen::VectorXi iRowHessian_
combined Hessian sparsity pattern gets stored here
Definition: Nlp.h:508