c_operator.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 
38 
39 
40 
42 
43 
44 int COperator::counter = 0;
45 
47 {
48  result = 0;
49  d_result = 0;
50  cresult = 0;
51  d_cresult = 0;
52  component = -1;
53  bufferSize = 0;
54  idx = 0;
55 
56  first = BT_FALSE;
58 
59  nCount = 0;
60 }
61 
62 
63 COperator::COperator( const CFunction &fcn, const Expression &arg, int component_ ) :SmoothOperator( )
64 {
65 
66  uint run1;
67 
68  cFunction = fcn;
69  argument = arg;
70 
71  component = component_;
72 
73  first = BT_FALSE;
75 
76  idx = new int[cFunction.getDim()];
77 
78  bufferSize = 1;
79 
80  result = (double**)calloc(bufferSize,sizeof(double*));
81  d_result = (double**)calloc(bufferSize,sizeof(double*));
82 
83  for( run1 = 0; run1 < bufferSize; run1++ ){
84  result [run1] = new double[argument.getDim()];
85  d_result[run1] = new double[argument.getDim()];
86  }
87 
88  cresult = (double**)calloc(bufferSize,sizeof(double*));
89  d_cresult = (double**)calloc(bufferSize,sizeof(double*));
90 
91  for( run1 = 0; run1 < bufferSize; run1++ ){
92  cresult [run1] = new double[cFunction.getDim()];
93  d_cresult[run1] = new double[cFunction.getDim()];
94  }
95 
96  nCount = 0;
97 }
98 
99 
100 COperator::COperator( const COperator &arg ){ copy(arg); }
101 
103 
104 
106 
107  if( this != &arg ){
108 
109  deleteAll( );
110  copy (arg);
111  }
112  return *this;
113 }
114 
115 
116 int COperator::increaseID(){ return counter++; }
117 
118 
119 void COperator::copy( const COperator &arg ){
120 
121  uint run1, run2;
122 
123  bufferSize = arg.bufferSize ;
124  cFunction = arg.cFunction ;
125  argument = arg.argument ;
126  component = arg.component ;
127 
128  first = arg.first ;
129  globalTypeID = arg.globalTypeID ;
130 
131  idx = new int[cFunction.getDim()];
132  for( run1 = 0; run1 < cFunction.getDim(); run1++ )
133  idx[run1] = arg.idx[run1];
134 
135  result = (double**)calloc(bufferSize,sizeof(double*));
136  d_result = (double**)calloc(bufferSize,sizeof(double*));
137 
138  for( run1 = 0; run1 < bufferSize; run1++ ){
139 
140  result [run1] = new double[argument.getDim()];
141  d_result[run1] = new double[argument.getDim()];
142 
143  for( run2 = 0; run2 < argument.getDim(); run2++ ){
144  result[run1][run2] = arg.result [run1][run2];
145  d_result[run1][run2] = arg.d_result[run1][run2];
146  }
147  }
148 
149  cresult = (double**)calloc(bufferSize,sizeof(double*));
150  d_cresult = (double**)calloc(bufferSize,sizeof(double*));
151 
152  for( run1 = 0; run1 < bufferSize; run1++ ){
153 
154  cresult [run1] = new double[cFunction.getDim()];
155  d_cresult[run1] = new double[cFunction.getDim()];
156 
157  for( run2 = 0; run2 < cFunction.getDim(); run2++ ){
158  cresult[run1][run2] = arg.cresult [run1][run2];
159  d_cresult[run1][run2] = arg.d_cresult[run1][run2];
160  }
161  }
162 
163  nCount = 0;
164 }
165 
166 
168 
169  uint run1;
170 
171  for( run1 = 0; run1 < bufferSize; run1++ ){
172  delete[] result [run1];
173  delete[] d_result[run1];
174  }
175 
176  free( result );
177  free( d_result );
178 
179  for( run1 = 0; run1 < bufferSize; run1++ ){
180  delete[] cresult [run1];
181  delete[] d_cresult[run1];
182  }
183 
184  free( cresult );
185  free( d_cresult );
186 
187  delete[] idx;
188 }
189 
190 
191 returnValue COperator::evaluate( int number, double *x, double *result_ ){
192 
193  uint run1;
194 
195  ASSERT( component >= 0 );
196 
197  if( first == BT_FALSE ){
198  result_[0] = x[idx[component]];
199  }
200  else{
201 
202  if( number >= (int) bufferSize ){
203 
204  int oldSize = bufferSize;
205  bufferSize += number;
206  result = (double**)realloc(result ,bufferSize*sizeof(double*));
207  d_result = (double**)realloc(d_result ,bufferSize*sizeof(double*));
208  cresult = (double**)realloc(cresult ,bufferSize*sizeof(double*));
209  d_cresult = (double**)realloc(d_cresult,bufferSize*sizeof(double*));
210 
211  for( run1 = oldSize; run1 < bufferSize; run1++ ){
212  result [run1] = new double[argument.getDim() ];
213  d_result [run1] = new double[argument.getDim() ];
214  cresult [run1] = new double[cFunction.getDim()];
215  d_cresult[run1] = new double[cFunction.getDim()];
216  }
217  }
218 
219  for( run1 = 0; run1 < argument.getDim(); run1++ )
220  argument.element[run1]->evaluate( number, x , &result[number][run1] );
221  cFunction.evaluate( number, result[number], cresult[number] );
222  result_[0] = cresult[number][component];
223  for( run1 = 0; run1 < cFunction.getDim(); run1++ )
224  x[idx[run1]] = cresult[number][run1];
225  }
226  return SUCCESSFUL_RETURN;
227 }
228 
229 
231 
232  ASSERT( 1 == 0 );
233  return ACADOERROR(RET_ASSERTION);
234 }
235 
236 
237 
239 
240  ASSERT( 1 == 0 );
242  return 0;
243 }
244 
245 
246 
248  VariableType *varType,
249  int *component_,
250  Operator **seed,
251  int &nNewIS,
252  TreeProjection ***newIS ){
253 
254  ASSERT( 1 == 0 );
256  return 0;
257 }
258 
259 
261  VariableType *varType ,
262  int *component_,
263  Operator *seed ,
264  Operator **df ,
265  int &nNewIS ,
266  TreeProjection ***newIS_ ){
267 
268  ASSERT( 1 == 0 );
269  return ACADOERROR(RET_ASSERTION);
270 }
271 
272 
274  VariableType *varType ,
275  int *component_,
276  Operator *l ,
277  Operator **S ,
278  int dimS ,
279  Operator **dfS ,
280  Operator **ldf ,
281  Operator **H ,
282  int &nNewLIS ,
283  TreeProjection ***newLIS ,
284  int &nNewSIS ,
285  TreeProjection ***newSIS ,
286  int &nNewHIS ,
287  TreeProjection ***newHIS ){
288 
289  ASSERT( 1 == 0 );
290  return ACADOERROR(RET_ASSERTION);
291 }
292 
293 
294 Operator* COperator::substitute( int index, const Operator *sub ){
295 
297  return 0;
298 }
299 
300 
302 
304 }
305 
306 
307 
309 
310  uint run1;
311 
312  for( run1 = 0; run1 < argument.getDim(); run1++ )
313  if( argument.element[run1]->isDependingOn(var) == BT_TRUE )
314  return BT_TRUE;
315  return BT_FALSE;
316 }
317 
318 
320  VariableType *varType,
321  int *component_,
322  BooleanType *implicit_dep ){
323 
324  uint run1;
325 
326  for( run1 = 0; run1 < argument.getDim(); run1++ )
327  if( argument.element[run1]->isDependingOn( dim, varType, component_, implicit_dep ) == BT_TRUE )
328  return BT_TRUE;
329  return BT_FALSE;
330 }
331 
332 
334  VariableType *varType,
335  int *component_,
336  BooleanType *implicit_dep ){
337 
338  return BT_FALSE;
339 }
340 
341 
343  VariableType *varType,
344  int *component_,
345  BooleanType *implicit_dep ){
346 
347  return BT_FALSE;
348 }
349 
350 
352  VariableType *varType,
353  int *component_,
354  BooleanType *implicit_dep ){
355 
356  return BT_FALSE;
357 }
358 
359 
361 
362  return MT_NONMONOTONIC;
363 }
364 
365 
367 
368  return CT_UNKNOWN;
369 }
370 
371 
373 
374  return ACADOERROR( RET_UNKNOWN_BUG );
375 }
376 
377 
379 
380  return ACADOERROR( RET_UNKNOWN_BUG );
381 }
382 
383 
384 returnValue COperator::AD_forward( int number, double *x, double *seed,
385  double *f, double *df ){
386 
387  uint run1;
388 
389  ASSERT( component >= 0 );
390 
391  if( first == BT_FALSE ){
392  f[0] = x[idx[component]];
393  df[0] = seed[idx[component]];
394  }
395  else{
396 
397  if( number >= (int) bufferSize ){
398 
399  int oldSize = bufferSize;
400  bufferSize += number;
401  result = (double**)realloc(result ,bufferSize*sizeof(double*));
402  d_result = (double**)realloc(d_result ,bufferSize*sizeof(double*));
403  cresult = (double**)realloc(cresult ,bufferSize*sizeof(double*));
404  d_cresult = (double**)realloc(d_cresult,bufferSize*sizeof(double*));
405 
406  for( run1 = oldSize; run1 < bufferSize; run1++ ){
407  result [run1] = new double[argument.getDim() ];
408  d_result [run1] = new double[argument.getDim() ];
409  cresult [run1] = new double[cFunction.getDim()];
410  d_cresult[run1] = new double[cFunction.getDim()];
411  }
412  }
413 
414  for( run1 = 0; run1 < argument.getDim(); run1++ )
415  argument.element[run1]->AD_forward( number, x, seed, &result[number][run1], &d_result[number][run1] );
416  cFunction.AD_forward( number, result[number], d_result[number], cresult[number], d_cresult[number] );
417  f[0] = cresult[number][component];
418  df[0] = d_cresult[number][component];
419  for( run1 = 0; run1 < cFunction.getDim(); run1++ ){
420  x [idx[run1]] = cresult[number][run1];
421  seed[idx[run1]] = d_cresult[number][run1];
422  }
423  }
424  return SUCCESSFUL_RETURN;
425 }
426 
427 
428 
429 returnValue COperator::AD_forward( int number, double *seed, double *df ){
430 
431  uint run1;
432 
433  ASSERT( component >= 0 );
434 
435  if( first == BT_FALSE ){
436  df[0] = seed[idx[component]];
437  }
438  else{
439  for( run1 = 0; run1 < argument.getDim(); run1++ )
440  argument.element[run1]->AD_forward( number, seed, &d_result[number][run1] );
441  cFunction.AD_forward( number, d_result[number], d_cresult[number] );
442  df[0] = d_cresult[number][component];
443  for( run1 = 0; run1 < cFunction.getDim(); run1++ )
444  seed[idx[run1]] = d_cresult[number][run1];
445  }
446  return SUCCESSFUL_RETURN;
447 }
448 
449 
450 returnValue COperator::AD_backward( int number, double seed, double *df ){
451 
453 }
454 
455 
456 returnValue COperator::AD_forward2( int number, double *seed, double *dseed,
457  double *df, double *ddf ){
458 
459  uint run1;
460 
461  ASSERT( component >= 0 );
462 
463  if( first == BT_FALSE ){
464  df[0] = seed[idx[component]];
465  ddf[0] = dseed[idx[component]];
466  }
467  else{
468 
469  double *d_result2 = new double[argument.getDim() ];
470  double *d_cresult2 = new double[cFunction.getDim()];
471 
472  double *dd_result = new double[argument.getDim() ];
473  double *dd_cresult = new double[cFunction.getDim()];
474 
475  for( run1 = 0; run1 < argument.getDim(); run1++ )
476  argument.element[run1]->AD_forward2( number, seed, dseed, &d_result2[run1], &dd_result[run1] );
477 
478  cFunction.AD_forward2( number, d_result2, dd_result, d_cresult2, dd_cresult );
479 
480  df[0] = d_cresult2[component];
481  ddf[0] = dd_cresult[component];
482 
483  for( run1 = 0; run1 < cFunction.getDim(); run1++ ){
484  seed[idx[run1]] = d_cresult2[run1];
485  dseed[idx[run1]] = dd_cresult[run1];
486  }
487 
488  delete[] d_result2 ;
489  delete[] d_cresult2 ;
490  delete[] dd_result ;
491  delete[] dd_cresult ;
492  }
493  return SUCCESSFUL_RETURN;
494 }
495 
496 
497 returnValue COperator::AD_backward2( int number, double seed1, double seed2,
498  double *df, double *ddf ){
499 
501 }
502 
503 
504 std::ostream& COperator::print( std::ostream& stream ) const
505 {
506  return stream << "C functions can not be printed";
507 }
508 
509 
510 
512 
513  return new COperator(*this);
514 }
515 
516 
518 
519  if( bufferSize > 1 ){
520  bufferSize = 1;
521  result = (double**)realloc(result ,bufferSize*sizeof(double*));
522  d_result = (double**)realloc(d_result ,bufferSize*sizeof(double*));
523  cresult = (double**)realloc(cresult ,bufferSize*sizeof(double*));
524  d_cresult = (double**)realloc(d_cresult,bufferSize*sizeof(double*));
525  }
526  return cFunction.clearBuffer();
527 }
528 
529 
531 
532  uint run1;
533  returnValue returnvalue;
534 
535  for( run1 = 0; run1 < argument.getDim(); run1++ ){
536 
537  returnvalue = argument.element[run1]->enumerateVariables( indexList );
538  if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue;
539  }
540 
542 
543  return SUCCESSFUL_RETURN;
544 }
545 
546 
548 
549  return ON_CEXPRESSION;
550 }
551 
552 
553 BooleanType COperator::isVariable( VariableType &varType, int &component_ ) const{
554 
555  return BT_FALSE;
556 }
557 
558 
560 
561  uint run1;
562  returnValue returnvalue;
563 
564  for( run1 = 0; run1 < argument.getDim(); run1++ ){
565  returnvalue = argument.element[run1]->loadIndices( indexList );
566  if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue;
567  }
568 
569  return SUCCESSFUL_RETURN;
570 }
571 
572 
574 
575  return BT_FALSE;
576 }
577 
578 
580 
581 // end of file.
virtual returnValue evaluate(int number, double *x, double *result)
Definition: c_operator.cpp:191
OperatorName
Definition: acado_types.hpp:72
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)=0
virtual BooleanType isSymbolic() const
Definition: c_operator.cpp:573
virtual BooleanType isVariable(VariableType &varType, int &component) const
Definition: c_operator.cpp:553
int globalTypeID
Definition: c_operator.hpp:474
virtual std::ostream & print(std::ostream &stream) const
Definition: c_operator.cpp:504
Abstract base class for all scalar-valued symbolic operators.
Definition: operator.hpp:60
virtual returnValue AD_backward(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)
Definition: c_operator.cpp:260
double ** cresult
Definition: c_operator.hpp:465
virtual BooleanType isDependingOn(VariableType var) const =0
double ** d_result
Definition: c_operator.hpp:463
Allows to pass back messages to the calling function.
virtual returnValue loadIndices(SymbolicIndexList *indexList)=0
virtual returnValue enumerateVariables(SymbolicIndexList *indexList)
Definition: c_operator.cpp:530
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
Definition: c_function.cpp:426
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
virtual returnValue clearBuffer()
Definition: c_operator.cpp:517
virtual MonotonicityType getMonotonicity()
Definition: c_operator.cpp:360
BooleanType determineCExpressionIndices(uint dimension, uint ID, int *idx)
#define CLOSE_NAMESPACE_ACADO
Manages the indices of SymbolicVariables.
VariableType
Definition: acado_types.hpp:95
Expression argument
Definition: c_operator.hpp:460
virtual returnValue AD_symmetric(int dim, VariableType *varType, int *component, Operator *l, Operator **S, int dimS, Operator **dfS, Operator **ldf, Operator **H, int &nNewLIS, TreeProjection ***newLIS, int &nNewSIS, TreeProjection ***newSIS, int &nNewHIS, TreeProjection ***newHIS)
Definition: c_operator.cpp:273
Base class for all variables within the symbolic expressions family.
Definition: expression.hpp:56
virtual Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)=0
virtual returnValue enumerateVariables(SymbolicIndexList *indexList)=0
Operator ** element
Definition: expression.hpp:311
virtual BooleanType isDependingOn(VariableType var) const
Definition: c_operator.cpp:308
virtual Operator * clone() const
Definition: c_operator.cpp:511
CFunction cFunction
Definition: c_operator.hpp:459
uint bufferSize
Definition: c_operator.hpp:469
void deleteAll()
Definition: c_operator.cpp:167
virtual returnValue evaluate(double *x, double *result)
Definition: c_function.cpp:205
virtual uint getDim() const
Definition: c_function.cpp:199
virtual Operator * substitute(int index, const Operator *sub)
Definition: c_operator.cpp:294
double ** result
Definition: c_operator.hpp:462
virtual OperatorName getName()
Definition: c_operator.cpp:547
The class COperator is an auxiliary class to use C-Functions within a function evaluation tree...
Definition: c_operator.hpp:58
virtual returnValue evaluate(int number, double *x, double *result)=0
#define ASSERT(x)
#define BT_TRUE
Definition: acado_types.hpp:47
NeutralElement
Definition: acado_types.hpp:64
Abstract base class for all scalar-valued symbolic operators.
MonotonicityType
virtual CurvatureType getCurvature()
Definition: c_operator.cpp:366
virtual Operator * differentiate(int index)
Definition: c_operator.cpp:238
Implements the tree-projection operator within the family of SymbolicOperators.
void copy(const COperator &arg)
Definition: c_operator.cpp:119
static int counter
Definition: c_operator.hpp:475
(no description yet)
Definition: c_function.hpp:54
int increaseID()
Definition: c_operator.cpp:116
CurvatureType
#define BEGIN_NAMESPACE_ACADO
COperator & operator=(const COperator &arg)
Definition: c_operator.cpp:105
Abstract base class for templated evaluation of operators.
#define BT_FALSE
Definition: acado_types.hpp:49
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
Definition: c_operator.cpp:456
virtual returnValue clearBuffer()
Definition: c_function.cpp:518
virtual returnValue loadIndices(SymbolicIndexList *indexList)
Definition: c_operator.cpp:559
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: c_operator.cpp:333
virtual Operator * AD_forward(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)
Definition: c_operator.cpp:247
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: c_operator.cpp:342
double ** d_cresult
Definition: c_operator.hpp:466
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: c_operator.cpp:351
virtual returnValue AD_backward2(int number, double seed1, double seed2, double *df, double *ddf)
Definition: c_operator.cpp:497
virtual returnValue AD_forward(double *x, double *seed, double *f, double *df)
Definition: c_function.cpp:267
virtual NeutralElement isOneOrZero() const
Definition: c_operator.cpp:301
BooleanType first
Definition: c_operator.hpp:471
#define ACADOERROR(retval)
virtual returnValue setMonotonicity(MonotonicityType monotonicity_)
Definition: c_operator.cpp:372
virtual returnValue setCurvature(CurvatureType curvature_)
Definition: c_operator.cpp:378
uint getDim() const


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