42 const std::string& _commonHeaderName
44 cholObjS(_userInteraction, _commonHeaderName),
45 cholSAC(_userInteraction, _commonHeaderName),
46 acSolver(userInteraction, _commonHeaderName)
65 if ( _levenbergMarquardt < 0.0 )
80 int discretizationType;
177 retInit.
setDoc(
"=0: OK, otherwise an error code of a QP solver.");
179 initialize.
doc(
"Solver initialization. Must be called once before any other function call." );
182 initialize.
addComment(
"This is a function which must be called once before any other function call!" );
187 initialize <<
"memset(&" << moduleName <<
"Workspace, 0, sizeof( " << moduleName <<
"Workspace ));" <<
"\n";
205 int hessianApproximation;
222 x.
setDoc(
string(
"Matrix containing ") +
toString(
getN() + 1) +
" differential variable vectors." );
224 z.
setDoc(
string(
"Matrix containing ") +
toString(
N ) +
" algebraic variable vectors." );
226 u.
setDoc(
string(
"Matrix containing ") +
toString(
N ) +
" control variable vectors." );
237 bool gradientUpdate = (bool) gradientUp;
255 if( secondOrder && !gradientUpdate ) {
258 else if( secondOrder && gradientUpdate ) {
265 unsigned indexZ = NX +
NXA;
266 if( secondOrder || adjoint ) indexZ = indexZ +
NX;
267 unsigned indexGxx = indexZ + NX *
NX;
268 unsigned indexGzx = indexGxx + NXA *
NX;
269 unsigned indexGxu = indexGzx + NX *
NU;
270 unsigned indexGzu = indexGxu + NXA *
NU;
271 unsigned indexH = indexGzu;
272 if( secondOrder ) indexH = indexGzu + symH;
273 unsigned indexG = indexH;
274 if( (secondOrder && gradientUpdate) || adjoint ) indexG = indexH + NX+
NU;
275 unsigned indexU = indexG +
NU;
276 unsigned indexOD = indexU +
NOD;
309 if( secondOrder || adjoint ) {
331 loop << retSim.
getFullName() <<
" = " << moduleName <<
"_integrate" 340 << moduleName <<
"_integrate" 344 << moduleName <<
"_integrate" 385 for(
uint i = 0; i < NX+
NU; i++ ) {
386 for(
uint j = 0; j <= i; j++ ) {
395 for(
uint i = 0; i < NX+
NU; i++ ) {
396 for(
uint j = 0; j < NX+
NU; j++ ) {
402 for(
uint i = 0; i <
NX; i++ ) {
403 for(
uint j = 0; j <
NX; j++ ) {
407 for(
uint i = 0; i <
NU; i++ ) {
408 for(
uint j = 0; j <
NX; j++ ) {
413 for(
uint i = 0; i <
NU; i++ ) {
414 for(
uint j = 0; j <
NU; j++ ) {
422 if( secondOrder && gradientUpdate ) {
423 for(
uint i = 0; i < NX+
NU; i++ ) {
428 for(
uint i = 0; i <
NX; i++ ) {
431 for(
uint i = 0; i <
NU; i++ ) {
476 bool gradientUpdate = (bool) gradientUp;
477 int hessianApproximation;
483 if( secondOrder && gradientUpdate ) {
509 Expression expFx, expFu, expDF, expDDF,
S, lambda, arg, dl;
510 S = eye<double>(
NX+
NU);
631 if (objFEndTerm.
getNU() > 0)
641 Expression expFEndTermX, expFEndTermXX,
S, lambda, dl;
660 objFEndTerm << expFEndTermX;
668 FEndTermXX << expFEndTermXX;
681 objFEndTerm << expFEndTermXX;
705 objSlx.
setDoc(
"Linear term weighting vector for states.");
706 objSlu.setDoc(
"Linear term weighting vector for controls.");
722 int hessianApproximation;
740 if (lsqExternElements.size() > 0 || lsqExternEndTermElements.size() > 0)
742 if (lsqExternElements.size() != 1 || lsqExternEndTermElements.size() != 1)
744 if (lsqExternElements[ 0 ].W.isSquare() ==
false || lsqExternElements[ 0 ].W.isSquare() ==
false)
747 setNY( lsqExternElements[ 0 ].W.getNumRows() );
748 setNYN( lsqExternEndTermElements[ 0 ].W.getNumRows() );
750 if (variableObjS ==
YES)
754 else if (lsqExternElements[ 0 ].givenW ==
false)
766 int forceDiagHessian;
814 if( lsqElements.size() == 0 )
816 if (lsqElements.size() > 1 || lsqEndTermElements.size() > 1)
818 "Current implementation of code generation module\n" 819 "supports only one LSQ term definition per one OCP." );
820 if (lsqElements[0].h.getDim() == 0)
823 if (lsqElements[ 0 ].W.isSquare() ==
false)
825 if (lsqElements[ 0 ].W.getNumRows() != (unsigned)lsqElements[ 0 ].h.getDim())
828 if ( lsqEndTermElements.size() == 0 )
830 if (lsqEndTermElements[0].h.getDim() == 0)
833 if (lsqEndTermElements[ 0 ].W.isSquare() ==
false)
835 if (lsqEndTermElements[ 0 ].W.getNumRows() != (unsigned)lsqEndTermElements[ 0 ].h.getDim())
838 objF = lsqElements[ 0 ].h;
856 if (lsqElements[ 0 ].givenW ==
false)
858 if ( variableObjS ==
YES )
872 if (lsqElements[ 0 ].W.isPositiveSemiDefinite() ==
false)
973 Q1 = zeros<double>(
NX,
NX);
974 Q2 = zeros<double>(
NX,
NY);
1001 R1 = zeros<double>(
NU,
NU);
1002 R2 = zeros<double>(
NU,
NY);
1016 DMatrix depQ = depFx.transpose() * lsqElements[ 0 ].W * depFx;
1017 DMatrix depR = depFu.transpose() * lsqElements[ 0 ].W * depFu;
1018 DMatrix depS = depFx.transpose() * lsqElements[ 0 ].W * depFu;
1020 if (depQ.isDiagonal() && depR.isDiagonal() && depS.isZero())
1025 if (depS.isZero() ==
true)
1027 S1 = zeros<double>(
NX,
NU);
1044 objFEndTerm = lsqEndTermElements[ 0 ].h;
1046 if (objFEndTerm.
getNU() > 0)
1052 if (lsqEndTermElements[ 0 ].givenW ==
false)
1058 if (lsqEndTermElements[ 0 ].W.isPositiveDefinite() ==
false)
1072 FEndTermX << expFEndTermX;
1084 objFEndTerm << expFEndTermX;
1134 DMatrix depQN = depFxEnd.transpose() * lsqEndTermElements[ 0 ].W * depFxEnd;
1135 diagonalHN = depQN.isDiagonal() ?
true :
false;
1164 if (lsqLinearElements.size() > 0)
1168 if (variableObjS ==
YES)
1178 if (lsqLinearElements[ 0 ].givenW ==
false)
1198 bool gradientUpdate = (bool) gradientUp;
1200 int sensitivityProp;
1208 objSlx.
setDoc(
"Linear term weighting vector for states.");
1209 objSlu.
setDoc(
"Linear term weighting vector for controls.");
1218 " reference/measurement vectors of size " +
toString(
NY ) +
" for first " +
toString(
N ) +
" nodes." );
1220 yN.
setDoc(
string(
"Reference/measurement vector for the ") +
toString(
N + 1) +
". node." );
1245 tmp.
init(&xgrid, 0, 0, &ugrid, 0);
1249 bool boxConIsFinite =
false;
1261 if ((ubTmp >= lbTmp) ==
false)
1265 boxConIsFinite =
true;
1268 if (boxConIsFinite ==
true)
1276 boxConIsFinite =
false;
1282 if ((ubTmp >= lbTmp) ==
false)
1286 boxConIsFinite =
true;
1289 if ( boxConIsFinite ==
true )
1308 int hessianApproximation;
1323 DMatrix pacLBMatrix, pacUBMatrix;
1336 Expression expPacH, expPacHx, expPacHu, expPacDH, expPacDDH;
1341 S = eye<double>(
NX+
NU);
1361 pacDDH << expPacDDH;
1372 if (v.isZero() ==
false)
1390 if (v.isZero() ==
false)
1409 if (v.isZero() ==
false)
1414 else if( secondOrder )
1444 unsigned dimPocHMax = 0;
1450 for (
unsigned i = 0; i <
N + 1; ++i)
1453 Expression expPocH, expPocH2, expPocHx, expPocHu;
1454 DMatrix pocLBMatrix, pocUBMatrix, pocLBMatrix2, pocUBMatrix2;
1463 if (pocH2.
getNU() > 0 && i ==
N)
1473 for (
unsigned j = 0; j < (
uint)pocH2.
getDim(); ++j) {
1478 expPocH << expPocH2;
1484 if( expPocH.
getDim() == 0 )
continue;
1501 std::string pocFName;
1503 pocFName =
"evaluatePointConstraint" +
toString( i );
1515 if ( dimPocHMax < (
unsigned)pocH.
getDim() )
1517 dimPocHMax = pocH.
getDim();
1537 unsigned pocAuxVarDim = 0;
1548 pocAuxVarDim = pocAuxVarDim < pocAuxVarTemp.
getDim() ? pocAuxVarTemp.
getDim() : pocAuxVarDim;
1559 unsigned conValueOutDim =
1591 int fixInitialState;
1594 return (
bool)fixInitialState;
1616 uEnd.
setDoc(
"Value for the u vector on the second to last node. If =0 the old value is used." );
1637 xEnd.
setDoc(
"Value for the x vector on the last node. If =0 the old value is used." );
1639 strategy.
setDoc(
string(
"Shifting strategy: 1. Initialize node ") +
toString(
N + 1) +
" with xEnd." \
1640 " 2. Initialize node " +
toString(
N + 1) +
" by forward simulation." );
1645 shiftStates.
doc(
"Shift differential variables vector by one interval." );
1647 shiftStates.
doc(
"Shift differential variables vector and algebraic variables vector by one interval." );
1668 bool gradientUpdate = (bool) gradientUp;
1670 int hessianApproximation;
1672 int sensitivityProp;
1683 unsigned indexZ =
NX +
NXA;
1684 if( secondOrder || adjoint ) indexZ = indexZ +
NX;
1685 unsigned indexGxx = indexZ + NX *
NX;
1686 unsigned indexGzx = indexGxx + NXA *
NX;
1687 unsigned indexGxu = indexGzx + NX *
NU;
1688 unsigned indexGzu = indexGxu + NXA *
NU;
1689 unsigned indexH = indexGzu;
1690 if( secondOrder ) indexH = indexGzu + symH;
1691 unsigned indexG = indexH;
1692 if( (secondOrder && gradientUpdate) || adjoint ) indexG = indexH + NX+NU;
1693 unsigned indexU = indexG +
NU;
1694 unsigned indexOD = indexU +
NOD;
1734 initializeNodes.
doc(
"Initialize shooting nodes by a forward simulation starting from the first node." );
1742 iLoop << std::string(
"if (") << index.
getFullName() << std::string(
" > 0){");
1744 iLoop << std::string(
"}\n");
1752 iLoop << moduleName <<
"_integrate" 1759 iLoop << moduleName <<
"_integrate" 1798 objVal.
setDoc(
"Value of the objective function." );
1846 if (variableObjS ==
NO)
1886 objVal.
setDoc(
"Value of the objective function." );
1942 if (useArrivalCost ==
NO)
1946 SAC.
setDoc(
"Arrival cost term: inverse of the covariance matrix.");
1948 xAC.
setDoc(
"Arrival cost term: a priori state estimate.");
1957 evReset.
setDoc(
"Reset S_{AC}. Set it to 1 to initialize arrival cost calculation, " 1958 "and later should set it to 0.");
1965 const unsigned AM = 2 *
NX +
NY;
1966 const unsigned AN = 2 *
NX +
NU;
1973 acWL.
setDoc(
"Arrival cost term: Cholesky decomposition, lower triangular, " 1974 " of the inverse of the state noise covariance matrix.");
2005 unsigned indexZ =
NX +
NXA;
2006 unsigned indexGxx = indexZ +
NX *
NX;
2007 unsigned indexGzx = indexGxx + NXA *
NX;
2008 unsigned indexGxu = indexGzx + NX *
NU;
2009 unsigned indexGzu = indexGxu + NXA *
NU;
2010 unsigned indexU = indexGzu +
NU;
2011 unsigned indexNOD = indexU +
NOD;
2041 DMatrix mChol = m.llt().matrixL();
2081 << (
acA == zeros<double>(AM, AN))
2082 << (
acb == zeros<double>(AM, 1))
2083 << std::string(
"\n" );
2114 << (acA.getSubMatrix(0, NX, 0, NX) ==
acP)
2116 << (acA.getSubMatrix(NX, NX + NY, 0, NX) -= (
acVL ^
acHx))
2117 << (acA.getSubMatrix(NX, NX + NY, NX, NX + NU) -= (
acVL ^
acHu))
2119 << (acA.getSubMatrix(NX + NY, NX + NY + NX, 0, NX) -= (
acWL ^
acXx))
2120 << (acA.getSubMatrix(NX + NY, NX + NY + NX, NX, NX + NU) -= (
acWL ^
acXu))
2121 << (acA.getSubMatrix(NX + NY, NX + NY + NX, NX + NU, NX + NU + NX) ==
acWL.
getTranspose());
2160 acSolver.
init(AM, AN, NX,
false,
false, std::string(
"ac"));
returnValue getLSQLinearTerms(LsqLinearElements &_elements) const
GenericVector< T > getCol(unsigned _idx) const
#define LOG(level)
Just define a handy macro for getting the logger.
uint getNumLagrangeTerms() const
Lowest level, the debug level.
Expression symmetricDerivative(const Expression &arg1, const Expression &arg2, const Expression &forward_seed, const Expression &backward_seed, Expression *forward_result, Expression *backward_result)
Data class for storing generic optimization variables.
Allows to setup and evaluate a general function based on SymbolicExpressions.
ExportVariable conValueOut
ExportVariable getRow(const ExportIndex &idx) const
returnValue getLSQEndTerms(LsqElements &_elements) const
virtual returnValue getDataDeclarations(ExportStatementBlock &declarations, ExportStruct dataStruct=ACADO_ANY) const
IntegratorExportPtr integrator
virtual returnValue setup()
ExportFunction shiftStates
returnValue getPathConstraints(Function &function_, DMatrix &lb_, DMatrix &ub_) const
std::shared_ptr< IntegratorExport > IntegratorExportPtr
std::vector< LsqLinearData > LsqLinearElements
returnValue setupArrivalCostCalculation()
ExportVariable getTranspose() const
A matrix or vector expression mapping an existing array of data.
GenericMatrix & appendCols(const GenericMatrix &_arg)
bool initialStateFixed() const
ExportVariable & setup(const std::string &_name, uint _nRows=1, uint _nCols=1, ExportType _type=REAL, ExportStruct _dataStruct=ACADO_LOCAL, bool _callItByValue=false, const std::string &_prefix=std::string())
bool isGiven(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
returnValue init(const std::string &_name="defaultFunctionName", const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
returnValue setLevenbergMarquardt(double _levenbergMarquardt)
Allows to export code of an ACADO function.
returnValue getPointConstraint(const unsigned index, Function &function_, DMatrix &lb_, DMatrix &ub_) const
virtual returnValue setupGetLSQObjective()
Stores and evaluates the constraints of optimal control problems.
Provides a time grid consisting of vector-valued optimization variables at each grid point...
Allows to pass back messages to the calling function.
DVector evaluate(const EvaluationPoint &x, const int &number=0)
GenericMatrix & makeVector()
ExportVariable conValueIn
Expression forwardDerivative(const Expression &arg1, const Expression &arg2)
DVector getUpperBounds(uint pointIdx) const
std::vector< DVector > pocLbStack
returnValue addComment(const std::string &_comment)
returnValue setLSQObjective(const Objective &_objective)
Allows to setup function evaluation points.
returnValue setConstraints(const OCP &_ocp)
ExportVariable objEvFxEnd
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
returnValue init(const VariablesGrid *const _x, const VariablesGrid *const _xa, const VariablesGrid *const _p, const VariablesGrid *const _u, const VariablesGrid *const _w)
Allows to conveniently handle (one-dimensional) grids consisting of time points.
const DMatrix & getGivenMatrix() const
Allows to export code of a for-loop.
Expression getCols(const uint &colIdx1, const uint &colIdx2) const
string toString(T const &value)
returnValue setName(const std::string &_name)
const std::string getName()
ExportVariable getElement(const ExportIndex &rowIdx, const ExportIndex &colIdx) const
#define CLOSE_NAMESPACE_ACADO
returnValue getConstraint(Constraint &constraint_) const
ExportVariable getSubMatrix(const ExportIndex &rowIdx1, const ExportIndex &rowIdx2, const ExportIndex &colIdx1, const ExportIndex &colIdx2) const
returnValue getExpression(Expression &expression) const
unsigned getFunctionDim(void)
Defines a scalar-valued index variable to be used for exporting code.
unsigned weightingMatricesType(void) const
ExportAcadoFunction evaluateStageCost
virtual returnValue getBounds(const OCPiterate &iter)
Base class for all variables within the symbolic expressions family.
Allows to export automatically generated algorithms for fast model predictive control.
ExportAcadoFunction evaluateTerminalCost
bool usingLinearTerms() const
ExportFunction & setup(const std::string &_name="defaultFunctionName", const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
virtual returnValue setDoc(const std::string &_doc)
virtual returnValue setupInitialization()
virtual returnValue setupGetObjective()
ExportFunction updateArrivalCost
ExportFunction modelSimulation
unsigned getGlobalExportVariableSize() const
virtual ExportFunction & doc(const std::string &_doc)
ExportVariable objEvFxxEnd
unsigned getNumPathConstraints(void)
ExportCholeskyDecomposition cholSAC
ExportVariable getCols(const ExportIndex &idx1, const ExportIndex &idx2) const
ExportFunction getObjective
uint getNumMayerTerms() const
virtual returnValue setupGetGeneralObjective()
ExportVariable makeRowVector() const
ExportVariable objValueIn
ExportFunction initializeNodes
std::vector< LsqData > LsqElements
double levenbergMarquardt
bool performsSingleShooting() const
virtual returnValue setupSimulation(void)
ExportStruct getDataStruct() const
virtual ExportFunction & setPrivate(bool _set=true)
std::vector< std::shared_ptr< ExportAcadoFunction > > evaluatePointConstraints
Encapsulates all user interaction for setting options, logging data and plotting results.
Data class for defining optimal control problems.
virtual uint getDim() const
ExportFunction shiftControls
returnValue getMayerTerm(uint index, Function &mayerTerm) const
returnValue setDataStruct(ExportStruct _dataStruct)
returnValue addStatement(const ExportStatement &_statement)
std::string getFullName() const
EIGEN_STRONG_INLINE const Scalar * data() const
Expression transpose() const
returnValue addLinebreak(uint num=1)
returnValue setGeneralObjective(const Objective &_objective)
ExportAcadoFunction evaluatePathConstraints
GenericVector & append(const GenericVector &_arg)
#define ACADOWARNINGTEXT(retval, text)
returnValue setupObjectiveLinearTerms(const Objective &_objective)
returnValue setIntegratorExport(IntegratorExportPtr const _integrator)
ExportFunction & setReturnValue(const ExportVariable &_functionReturnValue, bool _returnAsPointer=false)
virtual ExportFunction & acquire(ExportIndex &obj)
VariableType getVariableType() const
ExportFunction & addVariable(const ExportVariable &_var)
ExportNLPSolver(UserInteraction *_userInteraction=0, const std::string &_commonHeaderName="")
uint getNumPoints() const
returnValue setGlobalExportVariable(const ExportVariable &var)
ExportHouseholderQR acSolver
ExportVariable objSEndTerm
ExportVariable getRows(const ExportIndex &idx1, const ExportIndex &idx2) const
returnValue addDeclaration(const ExportVariable &_data, ExportStruct _dataStruct=ACADO_ANY)
returnValue setupAuxiliaryFunctions()
unsigned getNumComplexConstraints(void)
DVector getLowerBounds(uint pointIdx) const
std::vector< LsqExternData > LsqExternElements
returnValue getLagrangeTerm(uint index, Function &lagrangeTerm) const
returnValue getGrid(Grid &grid_) const
const std::string getNameSolveFunction()
Expression getSubMatrix(const uint &rowIdx1, const uint &rowIdx2, const uint &colIdx1, const uint &colIdx2) const
#define BEGIN_NAMESPACE_ACADO
returnValue clearStaticCounters()
BooleanType isFinite(const T &_value)
NeutralElement isOneOrZero()
std::vector< DVector > pocUbStack
returnValue setObjective(const Objective &_objective)
returnValue setupResidualVariables()
returnValue init(const std::string &_name, unsigned _dim, bool _unrolling=false)
ExportCholeskyDecomposition cholObjS
Allows to export code for a block of statements.
ExportFunction initialize
GenericMatrix getRows(unsigned _start, unsigned _end) const
returnValue init(const uint newDim, const bool &reuse=true, const bool &unrolling=false)
ExportVariable objValueOut
ExportVariable getCol(const ExportIndex &idx) const
returnValue init(const Function &_f, const std::string &_name="acadoFcn", const uint _numX=0, const uint _numXA=0, const uint _numU=0, const uint _numP=0, const uint _numDX=0, const uint _numOD=0)
Stores and evaluates the objective function of optimal control problems.
GenericVector< T > getRow(unsigned _idx) const
returnValue getLSQTerms(LsqElements &_elements) const
ExportFunction & addIndex(const ExportIndex &_index)
std::string getDataStructString() const
#define ACADOERROR(retval)
Defines a matrix-valued variable to be used for exporting code.
#define ACADOERRORTEXT(retval, text)
returnValue addFunctionCall(const std::string &_fName, const ExportArgument &_argument1=emptyConstExportArgument, const ExportArgument &_argument2=emptyConstExportArgument, const ExportArgument &_argument3=emptyConstExportArgument, const ExportArgument &_argument4=emptyConstExportArgument, const ExportArgument &_argument5=emptyConstExportArgument, const ExportArgument &_argument6=emptyConstExportArgument, const ExportArgument &_argument7=emptyConstExportArgument, const ExportArgument &_argument8=emptyConstExportArgument, const ExportArgument &_argument9=emptyConstExportArgument)
virtual ExportVariable getGlobalExportVariable(const uint factor) const
std::string getName() const