function.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 
39 
40 
41 
43 
44 
45 //
46 // PUBLIC MEMBER FUNCTIONS:
47 //
48 
50 
51  memoryOffset = 0;
52  result = 0;
53 }
54 
55 
57 
60 
61  if ( arg.getDim() != 0 )
62  {
63  result = (double*) calloc( arg.getDim(),sizeof(double) );
64  for( int i=0; i<getDim(); ++i )
65  result[i] = arg.result[i];
66  }
67  else
68  result = 0;
69 }
70 
71 
73  if ( result != 0 )
74  free( result );
75 }
76 
77 
79 
80  if ( this != &arg ){
81 
82  if ( result != 0 )
83  free( result );
84 
87 
88  if ( arg.getDim() != 0 )
89  {
90  result = (double*) calloc( arg.getDim(),sizeof(double) );
91  for( int i=0; i<getDim(); ++i )
92  result[i] = arg.result[i];
93  }
94  else
95  result = 0;
96  }
97 
98  return *this;
99 }
100 
101 
103 
104  evaluationTree.operator<<( arg );
105 
106  result = (double*) realloc( result,getDim()*sizeof(double) );
107 
108  return *this;
109 }
110 
111 
112 Function& Function::operator<<( const double &arg ){
113 
114  Expression tmp;
115  tmp = arg;
116 
117  return operator<<(tmp);
118 }
119 
120 
122 
123  Expression tmp;
124  tmp = arg;
125 
126  return operator<<(tmp);
127 }
128 
129 
131 
132  Expression tmp;
133  tmp = arg;
134 
135  return operator<<(tmp);
136 }
137 
138 
140 
141  return evaluationTree.getExpression( expression );
142 }
143 
144 
146  ) const
147 {
148  Function tmp;
149 
150  if ( (int)idx >= getDim() )
151  {
153  return tmp;
154  }
155 
157  tmp.memoryOffset = memoryOffset ;
158 
159  return tmp;
160 }
161 
162 
164 
166  evaluationTree = tmp;
167  memoryOffset = 0;
168 
169  if ( result != 0 )
170  free( result );
171 
172  return SUCCESSFUL_RETURN;
173 }
174 
175 
176 int Function::index( VariableType variableType_, int index_ ) const{
177 
178  return evaluationTree.index( variableType_, index_ );
179 }
180 
181 
182 double Function::scale( VariableType variableType_, int index_ ) const{
183 
184  return evaluationTree.scale( variableType_, index_ );
185 }
186 
187 int Function::getN (VariableType &variableType_) const{
188  switch(variableType_) {
189  case VT_DIFFERENTIAL_STATE : return getNX(); break;
190  case VT_ALGEBRAIC_STATE : return getNXA(); break;
191  case VT_CONTROL : return getNU(); break;
192  case VT_INTEGER_CONTROL : return getNUI(); break;
193  case VT_PARAMETER : return getNP(); break;
194  case VT_ONLINE_DATA : return getNOD(); break;
195  case VT_INTEGER_PARAMETER : return getNPI(); break;
196  case VT_DISTURBANCE : return getNW(); break;
197  case VT_TIME : return 1; break;
198  case VT_INTERMEDIATE_STATE : return getN(); break;
199  case VT_DDIFFERENTIAL_STATE : return getNDX(); break;
200  case VT_VARIABLE : return getNX()+getNXA()+getNU()+getNUI()+getNP()+getNPI()+getNW()+getNDX(); break;
201  case VT_OUTPUT : return 0; break;
202  case VT_UNKNOWN : return 0;
203  }
204  return evaluationTree.getNX();
205 }
206 
207 int Function::getNX () const{
208 
209  return evaluationTree.getNX();
210 }
211 
212 int Function::getNXA () const{
213 
214  return evaluationTree.getNXA();
215 }
216 
217 int Function::getNDX () const{
218 
219  return evaluationTree.getNDX();
220 }
221 
222 int Function::getNU () const{
223 
224  return evaluationTree.getNU();
225 }
226 
227 int Function::getNUI () const{
228 
229 
230  return evaluationTree.getNUI();
231 }
232 
233 int Function::getNP () const{
234 
235 
236  return evaluationTree.getNP();
237 }
238 
239 int Function::getNPI () const{
240 
241 
242  return evaluationTree.getNPI();
243 }
244 
245 int Function::getNW () const{
246 
247 
248  return evaluationTree.getNW();
249 }
250 
251 int Function::getNT () const{
252 
253 
254  return evaluationTree.getNT();
255 }
256 
257 int Function::getNOD () const{
258 
259 
260  return evaluationTree.getNOD();
261 }
262 
263 
265 
266 
268 }
269 
270 
271 Operator* Function::getExpression( uint componentIdx ) const{
272 
273  return evaluationTree.getExpression( componentIdx );
274 }
275 
276 
277 returnValue Function::evaluate( int number, double *x, double *_result ){
278 
279 // return evaluationTree.evaluate( number+memoryOffset, x, _result );
280 
281  evaluationTree.evaluate( number+memoryOffset, x, _result );
282 
283 
284 
285  return SUCCESSFUL_RETURN;
286 }
287 
288 
289 
290 returnValue Function::substitute( VariableType variableType_, int index_,
291  double sub_ ){
292 
293 
294 
295  if( isSymbolic() == BT_TRUE ){
297  tmp = evaluationTree.substitute(variableType_,index_,sub_);
298  evaluationTree = tmp;
299  return SUCCESSFUL_RETURN;
300  }
301 
303 }
304 
305 
306 
308 
309  return evaluationTree.isOneOrZero();
310 }
311 
312 
313 
315 
316  return evaluationTree.isDependingOn( variable );
317 }
318 
320 
321  return evaluationTree.isLinearIn( variable );
322 }
323 
324 
326 
327  return evaluationTree.isPolynomialIn( variable );
328 }
329 
330 
332 
333  return evaluationTree.isRationalIn( variable );
334 }
335 
336 
338 
339 
340 
343  return BT_TRUE;
344 
345  return BT_FALSE;
346 }
347 
348 
350 
351 
352 
355  return BT_TRUE;
356 
357  return BT_FALSE;
358 }
359 
361 {
363  return BT_TRUE;
364  return BT_FALSE;
365 }
366 
367 
369 
370  // (should be implemented more efficiently)
371 
372  if( isConvex() == BT_TRUE && isConcave() == BT_TRUE )
373  return BT_TRUE;
374  return BT_FALSE;
375 }
376 
377 
379 
380 
381 
385  return BT_TRUE;
386 
387  return BT_FALSE;
388 }
389 
390 
392 
393 
394 
398  return BT_TRUE;
399 
400  return BT_FALSE;
401 }
402 
404  int n=getDim();
405  int N=getNumberOfVariables();
406  returnValue ret;
407 
408  x=DMatrix(getNX(),n);x.setAll(0);
409  //u=DMatrix(getNU(),n);u.setAll(0);
410  //p=DMatrix(getNP(),n);p.setAll(0);
411  //w=DMatrix(getNW(),n);w.setAll(0);
412  double *Jr=new double[N];
413  double *seed=new double[n];
414  for (int i=0;i<n;i++) seed[i]=0;
415  for (int i=0;i<n;i++) {
416  if (i>0) seed[i-1]=0;
417  seed[i]=1;
418  for (int j=0;j<N;j++) Jr[j]=0;
419  ret=AD_backward(0,seed,Jr);
420  if (ret != SUCCESSFUL_RETURN) return ret;
421  for (int j=0;j<getNX();j++) x(j,i)=Jr[index(VT_DIFFERENTIAL_STATE,j)];
422  }
423  delete[] Jr;
424  delete[] seed;
425  return SUCCESSFUL_RETURN;
426 }
427 
428 
429 returnValue Function::AD_forward( int number, double *seed, double *df ){
430 
431  return evaluationTree.AD_forward( number+memoryOffset, seed, df );
432 }
433 
434 
435 returnValue Function::AD_backward( int number, double *seed, double *df ){
436 
437  return evaluationTree.AD_backward( number+memoryOffset, seed, df );
438 }
439 
440 
441 returnValue Function::AD_forward2( int number, double *seed, double *dseed,
442  double *df, double *ddf ){
443 
444  return evaluationTree.AD_forward2( number+memoryOffset, seed, dseed, df, ddf );
445 }
446 
447 
448 returnValue Function::AD_backward2( int number, double *seed1, double *seed2,
449  double *df, double *ddf ){
450 
451  return evaluationTree.AD_backward2( number+memoryOffset, seed1, seed2, df, ddf );
452 }
453 
454 
455 
456 std::ostream& operator<<(std::ostream& stream, const Function& arg)
457 {
458  arg.print( stream );
459 
460  return stream;
461 }
462 
463 returnValue Function::print( std::ostream& stream,
464  const char *fcnName ,
465  const char *realString
466  ) const
467 {
468  if (getDim() > 0)
469  return evaluationTree.C_print(stream, fcnName, realString);
470 
471  return SUCCESSFUL_RETURN;
472 }
473 
475  const char *fcnName ,
476  const char *realString
477  ) const
478 {
479  if (getDim() > 0)
480  return evaluationTree.exportForwardDeclarations(stream, fcnName, realString);
481 
482  return SUCCESSFUL_RETURN;
483 }
484 
485 
486 returnValue Function::exportCode( std::ostream& stream,
487  const char *fcnName,
488  const char *realString,
489  uint _numX,
490  uint _numXA,
491  uint _numU,
492  uint _numP,
493  uint _numDX,
494  uint _numOD,
495  bool allocateMemory,
496  bool staticMemory
497  ) const
498 {
499  if (getDim() > 0)
500  return evaluationTree.exportCode(stream, fcnName, realString,
501  _numX, _numXA, _numU, _numP, _numDX, _numOD, allocateMemory, staticMemory);
502 
503  return SUCCESSFUL_RETURN;
504 }
505 
506 
508 
509  return evaluationTree.clearBuffer();
510 }
511 
512 
513 
514 returnValue Function::setScale( double *scale_ ){
515 
516  return evaluationTree.setScale(scale_);
517 }
518 
519 
521  const int &number ){
522 
523  //double *result = new double[getDim()];
524 
525  evaluate( number, x.getEvaluationPointer(), result );
526  DVector res( getDim(), result );
527 
528  //delete[] result;
529  return res;
530 }
531 
532 
534  const int &number ){
535 
536  //double *result = new double[getDim()];
537 
538  AD_forward( number, x.getEvaluationPointer(), result );
539  DVector res( getDim(), result );
540 
541  //delete[] result;
542  return res;
543 }
544 
545 
547  EvaluationPoint &df ,
548  const int &number ){
549 
550 
551  if( ADisSupported() == BT_TRUE ){
552 
553  int run1;
554  double *seed_ = new double[getDim()];
555 
556  for( run1 = 0; run1 < getDim(); run1++ )
557  seed_[run1] = seed(run1);
558 
559  AD_backward( number, seed_, df.getEvaluationPointer() );
560 
561  delete[] seed_;
562  return SUCCESSFUL_RETURN;
563  }
564 
565  uint run1, run2;
566  double *fseed = new double[getNumberOfVariables()+1];
567  for( run2 = 0; (int) run2 < getNumberOfVariables()+1; run2++ )
568  fseed[run2] = 0.0;
569 
570  double *J;
571  J = new double[seed.getDim()];
572 
573  for( run2 = 0; (int) run2 < getNumberOfVariables()+1; run2++ ){
574 
575  fseed[run2] = 1.0;
576  AD_forward( 0, fseed, J );
577  fseed[run2] = 0.0;
578 
579  df.z[run2] = 0.0;
580  for( run1 = 0; run1 < seed.getDim(); run1++ )
581  df.z[run2] += J[run1]*seed(run1);
582  }
583 
584  delete[] J ;
585  delete[] fseed;
586 
587  return SUCCESSFUL_RETURN;
588 }
589 
591 {
593 }
594 
596 {
598 }
599 
601 {
603 }
604 
605 
607 
608 // end of file.
BooleanType isRationalIn(const Expression &variable)
Definition: function.cpp:331
#define N
std::string getGlobalExportVariableName() const
Definition: function.cpp:590
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
virtual returnValue AD_backward2(int number, double *seed1, double *seed2, double *df, double *ddf)
int getNOD() const
Definition: function.cpp:257
int getNPI() const
Definition: function.cpp:239
Abstract base class for all scalar-valued symbolic operators.
Definition: operator.hpp:60
virtual int getNumberOfVariables() const
virtual returnValue AD_forward(double *x, double *seed, double *f, double *df)
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
int getNDX() const
Definition: function.cpp:217
double * result
Definition: function_.hpp:522
BooleanType isSymbolic() const
Allows to pass back messages to the calling function.
DVector evaluate(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:520
int getNUI() const
Definition: function.cpp:227
returnValue exportForwardDeclarations(std::ostream &stream=std::cout, const char *fcnName="ACADOfcn", const char *realString="double") const
Allows to setup function evaluation points.
int getNU() const
Definition: function.cpp:222
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
void setAll(const T &_value)
Definition: matrix.hpp:141
returnValue C_print(std::ostream &stream=std::cout, const char *fcnName="ACADOfcn", const char *realString="double") const
unsigned getGlobalExportVariableSize() const
BooleanType isConcave()
Definition: function.cpp:391
returnValue AD_backward2(int number, double *seed1, double *seed2, double *df, double *ddf)
Definition: function.cpp:448
virtual returnValue AD_backward(double *seed, double *df)
#define CLOSE_NAMESPACE_ACADO
returnValue substitute(VariableType variableType_, int index_, double sub_)
Definition: function.cpp:290
int getNXA() const
Definition: function.cpp:212
GenericMatrix< double > DMatrix
Definition: matrix.hpp:457
returnValue setGlobalExportVariableName(const std::string &var)
Definition: function.cpp:595
returnValue getExpression(Expression &expression) const
Definition: function.cpp:139
VariableType
Definition: acado_types.hpp:95
returnValue print(std::ostream &stream, const char *fcnName="ACADOfcn", const char *realString="double") const
Definition: function.cpp:463
BooleanType isNonincreasing()
Definition: function.cpp:349
virtual int index(VariableType variableType_, int index_) const
Base class for all variables within the symbolic expressions family.
Definition: expression.hpp:56
virtual MonotonicityType getMonotonicity()
returnValue jacobian(DMatrix &x)
calculate the jacobian of an evaluated function
Definition: function.cpp:403
int getNW() const
Definition: function.cpp:245
virtual Operator * getExpression(uint componentIdx) const
double * getEvaluationPointer() const
virtual returnValue evaluate(double *x, double *result)
virtual NeutralElement isOneOrZero()
virtual BooleanType isRationalIn(const Expression &variable)
BooleanType isLinearIn(const Expression &variable)
Definition: function.cpp:319
unsigned getGlobalExportVariableSize() const
Definition: function.cpp:600
int getNP() const
Definition: function.cpp:233
returnValue setScale(double *scale_)
Definition: function.cpp:514
virtual BooleanType isDependingOn(const Expression &variable)
unsigned getDim() const
Definition: vector.hpp:172
returnValue exportCode(std::ostream &stream=std::cout, const char *fcnName="ACADOfcn", const char *realString="double", uint _numX=0, uint _numXA=0, uint _numU=0, uint _numP=0, uint _numDX=0, uint _numOD=0, bool allocateMemory=true, bool staticMemory=false) const
int getDim() const
returnValue exportCode(std::ostream &stream, const char *fcnName="ACADOfcn", const char *realString="double", uint _numX=0, uint _numXA=0, uint _numU=0, uint _numP=0, uint _numDX=0, uint _numOD=0, bool allocateMemory=true, bool staticMemory=false) const
Definition: function.cpp:486
double scale(VariableType variableType_, int index_) const
Definition: function.cpp:182
FunctionEvaluationTree evaluationTree
Definition: function_.hpp:519
Function & operator=(const Function &rhs)
Definition: function.cpp:78
int getN() const
BooleanType isAffine()
Definition: function.cpp:368
BooleanType isDependingOn(const Expression &variable)
Definition: function.cpp:314
returnValue reset()
Definition: function.cpp:163
returnValue clearBuffer()
Definition: function.cpp:507
int index(VariableType variableType_, int index_) const
Definition: function.cpp:176
std::string getGlobalExportVariableName() const
BooleanType ADisSupported() const
BooleanType isConvex()
Definition: function.cpp:378
int getNT() const
Definition: function.cpp:251
virtual double scale(VariableType variableType_, int index_) const
returnValue AD_backward(const DVector &seed, EvaluationPoint &df, const int &number=0)
Definition: function.cpp:546
#define BT_TRUE
Definition: acado_types.hpp:47
NeutralElement
Definition: acado_types.hpp:64
Organizes the evaluation of the function tree.
DVector AD_forward(const EvaluationPoint &x, const int &number=0)
Definition: function.cpp:533
returnValue exportForwardDeclarations(std::ostream &stream, const char *fcnName="ACADOfcn", const char *realString="double") const
Definition: function.cpp:474
virtual FunctionEvaluationTree substitute(VariableType variableType_, int index_, double sub_)
int getNX() const
Definition: function.cpp:207
#define BEGIN_NAMESPACE_ACADO
virtual ~Function()
Definition: function.cpp:72
#define BT_FALSE
Definition: acado_types.hpp:49
Function()
Definition: function.cpp:49
returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
Definition: function.cpp:441
Function & operator<<(const Expression &arg)
Definition: function.cpp:102
NeutralElement isOneOrZero()
Definition: function.cpp:307
returnValue setGlobalExportVariableName(const std::string &_name)
BooleanType isConstant()
Definition: function.cpp:360
BooleanType isNondecreasing()
Definition: function.cpp:337
int memoryOffset
Definition: function_.hpp:520
Function operator()(uint idx) const
Definition: function.cpp:145
virtual returnValue setScale(double *scale_)
virtual CurvatureType getCurvature()
virtual BooleanType isLinearIn(const Expression &variable)
int getNumberOfVariables() const
Definition: function.cpp:264
#define ACADOERROR(retval)
virtual BooleanType isPolynomialIn(const Expression &variable)
BooleanType isPolynomialIn(const Expression &variable)
Definition: function.cpp:325


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