lsq_end_term.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 
26 
27 
36 
37 
39 
40 
41 
42 //
43 // PUBLIC MEMBER FUNCTIONS:
44 //
45 
46 //
47 // PUBLIC MEMBER FUNCTIONS:
48 //
49 
50 
53 
54  S_h_res = 0;
55 }
56 
57 
58 LSQEndTerm::LSQEndTerm( const Grid& grid_, const DMatrix &S_,
59  const Function& m, const DVector &r_ )
60  :ObjectiveElement( grid_ ){
61 
62  fcn = m ;
63  S = S_;
64  r = r_;
65 
66  S_h_res = new double[fcn.getDim()];
67 }
68 
69 
71  :ObjectiveElement( rhs ){
72 
73  S = rhs.S;
74  r = rhs.r;
75 
76  S_h_res = new double[fcn.getDim()];
77 }
78 
79 
81 
82  if( S_h_res != 0 ) delete[] S_h_res;
83 }
84 
85 
87 
88  if ( this != &rhs ){
89 
90  if( S_h_res != 0 ) delete[] S_h_res;
91 
93 
94  S = rhs.S;
95  r = rhs.r;
96 
97  S_h_res = new double[fcn.getDim()];
98  }
99  return *this;
100 }
101 
102 
104 
105  uint run2, run3;
106  const uint nh = fcn.getDim();
107 
109 
110  z.setZ( grid.getLastIndex(), x );
111 
112  DVector h_res = fcn.evaluate( z);
113 
114  // EVALUATE THE OBJECTIVE:
115  // -----------------------
116 
117  obj = 0.0;
118 
119  for( run2 = 0; run2 < nh; run2++ )
120  h_res(run2) -= r.operator()(run2);
121 
122  for( run2 = 0; run2 < nh; run2++ ){
123  S_h_res[run2] = 0.0;
124  for( run3 = 0; run3 < nh; run3++ )
125  S_h_res[run2] += S.operator()(run2,run3)*h_res(run3);
126  }
127 
128  for( run2 = 0; run2 < nh; run2++ )
129  obj += 0.5*h_res(run2)*S_h_res[run2];
130 
131 
132  return SUCCESSFUL_RETURN;
133 }
134 
135 
137 
138  if( hessian == 0 ) return evaluateSensitivitiesGN(0);
140 }
141 
142 
144 
145  int run2, run3, run4;
146  const int N = grid.getNumPoints();
147  const int nh = fcn.getDim();
148 
149  if( bSeed != 0 ){
150 
151  if( xSeed != 0 || pSeed != 0 || uSeed != 0 || wSeed != 0 ||
152  xSeed2 != 0 || pSeed2 != 0 || uSeed2 != 0 || wSeed2 != 0 )
154 
155  double *bseed = new double [nh];
156  double **J = new double*[nh];
157 
158  for( run2 = 0; run2 < nh; run2++ )
159  J[run2] = new double[fcn.getNumberOfVariables() +1];
160 
161  if( bSeed->getNumRows( 0, 0 ) != 1 ) return ACADOWARNING( RET_WRONG_DEFINITION_OF_SEEDS );
162 
163  DMatrix bseed_;
164  bSeed->getSubBlock( 0, 0, bseed_);
165 
166  dBackward.init( 1, 5*N );
167 
168  DMatrix Dx ( 1, nx );
169  DMatrix Dxa( 1, na );
170  DMatrix Dp ( 1, np );
171  DMatrix Du ( 1, nu );
172  DMatrix Dw ( 1, nw );
173 
174  Dx .setZero();
175  Dxa.setZero();
176  Dp .setZero();
177  Du .setZero();
178  Dw .setZero();
179 
180  for( run2 = 0; run2 < nh; run2++ ) bseed[run2] = 0;
181 
182  for( run2 = 0; run2 < nh; run2++ ){
183  for(run3 = 0; (int) run3 < fcn.getNumberOfVariables() +1; run3++ )
184  J[run2][run3] = 0.0;
185 
186  bseed[run2] = 1.0;
187  fcn.AD_backward( 0, bseed, J[run2] );
188  bseed[run2] = 0.0;
189 
190  for( run3 = 0; run3 < nx; run3++ ){
191  Dx( 0, run3 ) += bseed_(0,0)*J[run2][y_index[run3]]*S_h_res[run2];
192  }
193  for( run3 = nx; run3 < nx+na; run3++ ){
194  Dxa( 0, run3-nx ) += bseed_(0,0)*J[run2][y_index[run3]]*S_h_res[run2];
195  }
196  for( run3 = nx+na; run3 < nx+na+np; run3++ ){
197  Dp( 0, run3-nx-na ) += bseed_(0,0)*J[run2][y_index[run3]]*S_h_res[run2];
198  }
199  for( run3 = nx+na+np; run3 < nx+na+np+nu; run3++ ){
200  Du( 0, run3-nx-na-np ) += bseed_(0,0)*J[run2][y_index[run3]]*S_h_res[run2];
201  }
202  for( run3 = nx+na+np+nu; run3 < nx+na+np+nu+nw; run3++ ){
203  Dw( 0, run3-nx-na-np-nu ) += bseed_(0,0)*J[run2][y_index[run3]]*S_h_res[run2];
204  }
205  }
206  if( nx > 0 ) dBackward.setDense( 0, N-1, Dx );
207  if( na > 0 ) dBackward.setDense( 0, 2*N-1, Dxa );
208  if( np > 0 ) dBackward.setDense( 0, 3*N-1, Dp );
209  if( nu > 0 ) dBackward.setDense( 0, 4*N-1, Du );
210  if( nw > 0 ) dBackward.setDense( 0, 5*N-1, Dw );
211 
212 
213  // COMPUTE GAUSS-NEWTON HESSIAN APPROXIMATION IF REQUESTED:
214  // --------------------------------------------------------
215 
216  if( GNhessian != 0 ){
217 
218  const int nnn = nx+na+np+nu+nw;
219  DMatrix tmp( nh, nnn );
220 
221  for( run3 = 0; run3 < nnn; run3++ ){
222  for( run2 = 0; run2 < nh; run2++ ){
223  tmp( run2, run3 ) = 0.0;
224  for( run4 = 0; run4 < nh; run4++ ){
225  tmp( run2, run3 ) += S.operator()(run2,run4)*J[run4][y_index[run3]];
226  }
227  }
228  }
229  DMatrix tmp2;
230  int i,j;
231  int *Sidx = new int[6];
232  int *Hidx = new int[5];
233 
234  Sidx[0] = 0;
235  Sidx[1] = nx;
236  Sidx[2] = nx+na;
237  Sidx[3] = nx+na+np;
238  Sidx[4] = nx+na+np+nu;
239  Sidx[5] = nx+na+np+nu+nw;
240 
241  Hidx[0] = N-1;
242  Hidx[1] = 2*N-1;
243  Hidx[2] = 3*N-1;
244  Hidx[3] = 4*N-1;
245  Hidx[4] = 5*N-1;
246 
247  for( i = 0; i < 5; i++ ){
248  for( j = 0; j < 5; j++ ){
249 
250  tmp2.init(Sidx[i+1]-Sidx[i],Sidx[j+1]-Sidx[j]);
251  tmp2.setZero();
252 
253  for( run3 = Sidx[i]; run3 < Sidx[i+1]; run3++ )
254  for( run4 = Sidx[j]; run4 < Sidx[j+1]; run4++ )
255  for( run2 = 0; run2 < nh; run2++ )
256  tmp2(run3-Sidx[i],run4-Sidx[j]) += J[run2][y_index[run3]]*tmp(run2,run4);
257 
258  if( tmp2.getDim() != 0 ) GNhessian->addDense(Hidx[i],Hidx[j],tmp2);
259  }
260  }
261  delete[] Sidx;
262  delete[] Hidx;
263  }
264  // --------------------------------------------------------
265 
266  for( run2 = 0; run2 < nh; run2++ )
267  delete[] J[run2];
268  delete[] J;
269  delete[] bseed;
270  return SUCCESSFUL_RETURN;
271  }
272 
274 }
275 
276 
277 
278 
280 
281 // end of file.
#define N
Data class for storing generic optimization variables.
Definition: ocp_iterate.hpp:57
returnValue init(const OCPiterate &x)
Implements a very rudimentary block sparse matrix class.
Base class for all kind of objective function terms within optimal control problems.
LSQEndTerm & operator=(const LSQEndTerm &rhs)
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
void init(unsigned _nRows=0, unsigned _nCols=0)
Definition: matrix.hpp:135
returnValue setDense(uint rowIdx, uint colIdx, const DMatrix &value)
Allows to pass back messages to the calling function.
DVector evaluate(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:520
returnValue setZ(const uint &idx, const OCPiterate &iter)
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
Allows to conveniently handle (one-dimensional) grids consisting of time points.
Definition: grid.hpp:58
uint getLastIndex() const
#define CLOSE_NAMESPACE_ACADO
returnValue evaluateSensitivitiesGN(BlockMatrix *GNhessian)
returnValue getSubBlock(uint rowIdx, uint colIdx, DMatrix &value) const
Stores and evaluates LSQ mayer terms within optimal control problems.
returnValue evaluateSensitivities(BlockMatrix *hessian)
unsigned getDim() const
Definition: matrix.hpp:181
returnValue init(uint _nRows, uint _nCols)
int getDim() const
Derived & setZero(Index size)
void rhs(const real_t *x, real_t *f)
returnValue AD_backward(const DVector &seed, EvaluationPoint &df, const int &number=0)
Definition: function.cpp:546
uint getNumPoints() const
double * S_h_res
#define ACADOWARNING(retval)
#define BEGIN_NAMESPACE_ACADO
virtual ~LSQEndTerm()
returnValue evaluate(const OCPiterate &x)
returnValue addDense(uint rowIdx, uint colIdx, const DMatrix &value)
int getNumberOfVariables() const
Definition: function.cpp:264
ObjectiveElement & operator=(const ObjectiveElement &rhs)
#define ACADOERROR(retval)
uint getNumRows() const


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