export_exact_hessian_cn2.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
35 
36 using namespace std;
37 
39 
41  const std::string& _commonHeaderName
42  ) : ExportGaussNewtonCN2( _userInteraction,_commonHeaderName )
43 {}
44 
46 {
47  if (performFullCondensing() == true && initialStateFixed() == false)
48  return ACADOERRORTEXT( RET_INVALID_OPTION, "Impossible to perform full condensing, when the initial state is not fixed. You can use regular condensing instead." );
49  if (performFullCondensing() == false && initialStateFixed() == true)
51  if (performsSingleShooting() == true)
53 
54  LOG( LVL_DEBUG ) << "Solver: setup initialization... " << endl;
56  LOG( LVL_DEBUG ) << "done!" << endl;
57 
58  LOG( LVL_DEBUG ) << "Solver: setup variables... " << endl;
60  LOG( LVL_DEBUG ) << "done!" << endl;
61 
62  LOG( LVL_DEBUG ) << "Solver: setup multiplication routines... " << endl;
64  LOG( LVL_DEBUG ) << "done!" << endl;
65 
66  LOG( LVL_DEBUG ) << "Solver: setup model simulation... " << endl;
68  LOG( LVL_DEBUG ) << "done!" << endl;
69 
70  LOG( LVL_DEBUG ) << "Solver: setup objective evaluation... " << endl;
72  LOG( LVL_DEBUG ) << "done!" << endl;
73 
74  LOG( LVL_DEBUG ) << "Solver: setup condensing... " << endl;
76  LOG( LVL_DEBUG ) << "done!" << endl;
77 
78  LOG( LVL_DEBUG ) << "Solver: setup constraints... " << endl;
80  LOG( LVL_DEBUG ) << "done!" << endl;
81 
82  LOG( LVL_DEBUG ) << "Solver: setup hessian regularization... " << endl;
84  LOG( LVL_DEBUG ) << "done!" << endl;
85 
86  LOG( LVL_DEBUG ) << "Solver: setup evaluation... " << endl;
88  LOG( LVL_DEBUG ) << "done!" << endl;
89 
90  LOG( LVL_DEBUG ) << "Solver: setup auxiliary functions... " << endl;
92  LOG( LVL_DEBUG ) << "done!" << endl;
93 
94  return SUCCESSFUL_RETURN;
95 }
96 
98  ) const
99 {
101 
102  declarations.addDeclaration( regularization );
103 
104  return SUCCESSFUL_RETURN;
105 }
106 
107 //
108 // PROTECTED FUNCTIONS:
109 //
110 
112 {
113  evaluateObjective.setup("evaluateObjective");
114 
115  int gradientUp;
116  get( LIFTED_GRADIENT_UPDATE, gradientUp );
117  bool gradientUpdate = (bool) gradientUp;
118 
119  //
120  // A loop the evaluates objective and corresponding gradients
121  //
122  ExportIndex runObj( "runObj" );
123  ExportForLoop loopObjective( runObj, 0, N );
124 
125  evaluateObjective.addIndex( runObj );
126 
127  unsigned offset = performFullCondensing() == true ? 0 : NX;
128 
129  int sensitivityProp;
130  get( DYNAMIC_SENSITIVITY, sensitivityProp );
131  bool adjoint = ((ExportSensitivityType) sensitivityProp == BACKWARD);
132 
133  if( evaluateStageCost.getFunctionDim() > 0 ) {
134  loopObjective.addStatement( objValueIn.getCols(0, getNX()) == x.getRow( runObj ) );
135  loopObjective.addStatement( objValueIn.getCols(NX, NX + NU) == u.getRow( runObj ) );
136  loopObjective.addStatement( objValueIn.getCols(NX + NU, NX + NU + NOD) == od.getRow( runObj ) );
137  loopObjective.addLinebreak( );
138 
139  // Evaluate the objective function
141  loopObjective.addLinebreak( );
142 
143  ExportVariable tmpFxx, tmpFxu, tmpFuu;
144  tmpFxx.setup("tmpFxx", NX, NX, REAL, ACADO_LOCAL);
145  tmpFxu.setup("tmpFxu", NX, NU, REAL, ACADO_LOCAL);
146  tmpFuu.setup("tmpFuu", NU, NU, REAL, ACADO_LOCAL);
147 
148  //
149  // Optional computation of Q1, Q2
150  //
151  ExportVariable tmpEH;
152  tmpEH.setup("tmpEH", NX+NU, NX+NU, REAL, ACADO_LOCAL);
153 
154  setObjQ1Q2.setup("addObjTerm", tmpFxx, tmpFxu, tmpFuu, tmpEH);
155  setObjQ1Q2.addStatement( tmpEH.getSubMatrix(0,NX,0,NX) += tmpFxx );
156  setObjQ1Q2.addStatement( tmpEH.getSubMatrix(0,NX,NX,NX+NU) += tmpFxu );
157  setObjQ1Q2.addStatement( tmpEH.getSubMatrix(NX,NX+NU,0,NX) += tmpFxu.getTranspose() );
158  setObjQ1Q2.addStatement( tmpEH.getSubMatrix(NX,NX+NU,NX,NX+NU) += tmpFuu );
159 
160  loopObjective.addFunctionCall(
162  objValueOut.getAddress(0, 1+NX+NU+NX*(NX+NU)), objS.getAddress(runObj*(NX+NU), 0) );
163 
164  ExportVariable tmpDx, tmpDu, tmpDF;
165  tmpDx.setup("tmpDx", NX, 1, REAL, ACADO_LOCAL);
166  tmpDu.setup("tmpDu", NU, 1, REAL, ACADO_LOCAL);
167  tmpDF.setup("tmpDF", NX+NU, 1, REAL, ACADO_LOCAL);
168  setObjR1R2.setup("addObjLinearTerm", tmpDx, tmpDu, tmpDF);
169  setObjR1R2.addStatement( tmpDx == tmpDF.getRows(0,NX) );
170  setObjR1R2.addStatement( tmpDu == tmpDF.getRows(NX,NX+NU) );
171 
172  if( gradientUpdate || adjoint ) {
173  loopObjective.addStatement( objValueOut.getCols(1,1+NX+NU) += objg.getRows(runObj*(NX+NU),(runObj+1)*(NX+NU)).getTranspose() );
174  }
175  loopObjective.addFunctionCall(
176  setObjR1R2, QDy.getAddress(runObj * NX), g.getAddress(offset+runObj * NU, 0), objValueOut.getAddress(0, 1) );
177 
178  loopObjective.addLinebreak( );
179  }
180  else if( gradientUpdate || adjoint ) {
181  loopObjective.addStatement( QDy.getRows(runObj*NX, runObj*NX+NX) == objg.getRows(runObj*(NX+NU),runObj*(NX+NU)+NX) );
182  loopObjective.addStatement( g.getRows(offset+runObj*NU, offset+runObj*NU+NU) == objg.getRows(runObj*(NX+NU)+NX,(runObj+1)*(NX+NU)) );
183  }
184  else {
185  DMatrix Du(NU,1); Du.setAll(0);
186  DMatrix Dx(NX,1); Dx.setAll(0);
187  loopObjective.addStatement( g.getRows(offset+runObj*NU, offset+runObj*NU+NU) == Du );
188  loopObjective.addStatement( QDy.getRows(runObj*NX, runObj*NX+NX) == Dx );
189  }
190 
191  evaluateObjective.addStatement( loopObjective );
192 
193  //
194  // Evaluate the quadratic Mayer term
195  //
199 
200  // Evaluate the objective function, last node.
203 
204  ExportVariable tmpFxxEnd;
205  tmpFxxEnd.setup("tmpFxxEnd", NX, NX, REAL, ACADO_LOCAL);
206 
207  //
208  // Optional computation of QN1
209  //
210  ExportVariable tmpEH_N;
211  tmpEH_N.setup("tmpEH_N", NX, NX, REAL, ACADO_LOCAL);
212 
213  setObjQN1QN2.setup("addObjEndTerm", tmpFxxEnd, tmpEH_N);
214  setObjQN1QN2.addStatement( tmpEH_N == tmpFxxEnd );
215 
218 
220 
222  }
223  else {
224  DMatrix hess(NX,NX); hess.setAll(0);
226 
227  DMatrix Dx(NX,1); Dx.setAll(0);
228  evaluateObjective.addStatement( QDy.getRows(N * NX, (N + 1) * NX) == Dx );
229  }
230 
231  return SUCCESSFUL_RETURN;
232 }
233 
235 {
236  string moduleName;
237  get(CG_MODULE_NAME, moduleName);
238 
239  ExportVariable block( "hessian_block", NX+NU, NX+NU );
240  regularization = ExportFunction( "regularize", block );
241  regularization.doc( "EVD-based regularization of a Hessian block." );
243 
244  regularizeHessian.setup( "regularizeHessian" );
245  regularizeHessian.doc( "Regularization procedure of the computed exact Hessian." );
246 
247  int hessianRegularization;
248  get( HESSIAN_REGULARIZATION, hessianRegularization );
249 
250  ExportIndex oInd;
251  regularizeHessian.acquire( oInd );
252 
253  ExportForLoop loopObjective(oInd, 0, N);
254  if( (HessianRegularizationMode) hessianRegularization == BLOCK_REG ) {
255  loopObjective.addFunctionCall( regularization, objS.getAddress(oInd*(NX+NU),0) );
256  }
257  loopObjective.addStatement( Q1.getRows(oInd*NX, oInd*NX+NX) == objS.getSubMatrix(oInd*(NX+NU), oInd*(NX+NU)+NX, 0, NX) );
258  loopObjective.addStatement( S1.getRows(oInd*NX, oInd*NX+NX) == objS.getSubMatrix(oInd*(NX+NU), oInd*(NX+NU)+NX, NX, NX+NU) );
259  loopObjective.addStatement( R1.getRows(oInd*NU, oInd*NU+NU) == objS.getSubMatrix(oInd*(NX+NU)+NX, oInd*(NX+NU)+NX+NU, NX, NX+NU) );
260  regularizeHessian.addStatement( loopObjective );
261 
263 
264  return SUCCESSFUL_RETURN;
265 }
266 
virtual returnValue setupCondensing()
virtual returnValue setupMultiplicationRoutines()
#define LOG(level)
Just define a handy macro for getting the logger.
Lowest level, the debug level.
ExportVariable getRow(const ExportIndex &idx) const
ExportVariable objS
ExportVariable getTranspose() const
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())
virtual returnValue getFunctionDeclarations(ExportStatementBlock &declarations) const
uint getNX() const
Allows to pass back messages to the calling function.
HessianRegularizationMode
Block< Derived > block(Index startRow, Index startCol, Index blockRows, Index blockCols)
Definition: BlockMethods.h:56
void setAll(const T &_value)
Definition: matrix.hpp:141
virtual returnValue setupConstraintsEvaluation(void)
Allows to export code of a for-loop.
#define CLOSE_NAMESPACE_ACADO
ExportVariable objg
ExportVariable getSubMatrix(const ExportIndex &rowIdx1, const ExportIndex &rowIdx2, const ExportIndex &colIdx1, const ExportIndex &colIdx2) const
Defines a scalar-valued index variable to be used for exporting code.
ExportAcadoFunction evaluateStageCost
ExportAcadoFunction evaluateTerminalCost
ExportFunction regularization
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 setupEvaluation()
virtual returnValue setupInitialization()
virtual ExportFunction & doc(const std::string &_doc)
ExportVariable getCols(const ExportIndex &idx1, const ExportIndex &idx2) const
virtual returnValue setupHessianRegularization(void)
ExportVariable objValueIn
bool performsSingleShooting() const
virtual returnValue setupSimulation(void)
ExportVariable QN1
Encapsulates all user interaction for setting options, logging data and plotting results.
Allows to export code of an arbitrary function.
An OCP solver based on the N^2 condensing algorithm.
returnValue addStatement(const ExportStatement &_statement)
returnValue addLinebreak(uint num=1)
virtual ExportFunction & acquire(ExportIndex &obj)
ExportVariable objSEndTerm
virtual returnValue getFunctionDeclarations(ExportStatementBlock &declarations) const
ExportVariable getRows(const ExportIndex &idx1, const ExportIndex &idx2) const
returnValue addDeclaration(const ExportVariable &_data, ExportStruct _dataStruct=ACADO_ANY)
returnValue setupAuxiliaryFunctions()
#define BEGIN_NAMESPACE_ACADO
virtual returnValue setupVariables()
Allows to export code for a block of statements.
ExportArgument getAddress(const ExportIndex &_rowIdx, const ExportIndex &_colIdx=emptyConstExportIndex) const
ExportExactHessianCN2(UserInteraction *_userInteraction=0, const std::string &_commonHeaderName="")
ExportVariable objValueOut
ExportFunction & addIndex(const ExportIndex &_index)
ExportFunction regularizeHessian
#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 returnValue setupObjectiveEvaluation(void)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:33