function_evaluation_tree.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 
37 
38 using namespace std;
39 
41 
42 
43 
44 //
45 // PUBLIC MEMBER FUNCTIONS:
46 //
47 
49 
50  f = NULL;
51  sub = NULL;
52  lhs_comp = NULL;
53  indexList = new SymbolicIndexList();
54  dim = 0;
55  n = 0;
56 
57  globalExportVariableName = "acado_aux";
58 }
59 
61 
62  int run1;
63 
64  dim = arg.dim;
65  n = arg.n ;
66 
67  globalExportVariableName = arg.globalExportVariableName;
68 
69  if( arg.f == NULL ){
70  f = NULL;
71  }
72  else{
73  f = (Operator**)calloc(dim,sizeof(Operator*));
74 
75  for( run1 = 0; run1 < dim; run1++ ){
76  f[run1] = arg.f[run1]->clone();
77  }
78  }
79 
80  indexList = new SymbolicIndexList(*arg.indexList);
81 
82  if( arg.sub == NULL ){
83  sub = NULL;
84  lhs_comp = NULL;
85  }
86  else{
87  sub = (Operator**)calloc(n,sizeof(Operator*));
88  lhs_comp = (int*)calloc(n,sizeof(int));
89 
90  for( run1 = 0; run1 < n; run1++ ){
91  sub[run1] = arg.sub[run1]->clone();
92  lhs_comp[run1] = arg.lhs_comp[run1];
93  }
94  }
95 
96  safeCopy = arg.safeCopy;
97 }
98 
99 
101 
102  int run1;
103 
104  for( run1 = 0; run1 < dim; run1++ ){
105  delete f[run1];
106  }
107  if( f != NULL){
108  free(f);
109  }
110 
111  for( run1 = 0; run1 < n; run1++ ){
112 
113  delete sub[run1];
114  }
115 
116  if( sub != NULL ){
117  free(sub);
118  }
119 
120  if( lhs_comp != NULL ){
121  free(lhs_comp);
122  }
123 
124  delete indexList;
125 }
126 
127 
129 
130  int run1;
131 
132  if( this != &arg ){
133 
134  for( run1 = 0; run1 < dim; run1++ ){
135  delete f[run1];
136  }
137  if( f != NULL ){
138  free(f);
139  }
140 
141  for( run1 = 0; run1 < n; run1++ ){
142  delete sub[run1];
143  }
144  if( sub != 0 ){
145  free(sub);
146  }
147 
148  if( lhs_comp != NULL ){
149  free(lhs_comp);
150  }
151 
152  delete indexList;
153 
154  dim = arg.dim;
155  n = arg.n ;
156 
157  globalExportVariableName = arg.globalExportVariableName;
158 
159  if( arg.f == NULL ){
160  f = NULL;
161  }
162  else{
163  f = (Operator**)calloc(dim,sizeof(Operator*));
164 
165  for( run1 = 0; run1 < dim; run1++ ){
166  f[run1] = arg.f[run1]->clone();
167  }
168  }
169 
170  indexList = new SymbolicIndexList(*arg.indexList);
171 
172  if( arg.sub == NULL ){
173  sub = NULL;
174  lhs_comp = NULL;
175  }
176  else{
177  sub = (Operator**)calloc(n,sizeof(Operator*));
178  lhs_comp = (int*)calloc(n,sizeof(int));
179 
180  for( run1 = 0; run1 < n; run1++ ){
181  sub[run1] = arg.sub[run1]->clone();
182  lhs_comp[run1] = arg.lhs_comp[run1];
183  }
184  }
185  safeCopy = arg.safeCopy;
186  }
187 
188  return *this;
189 }
190 
191 
193 
194  expression = safeCopy;
195  return SUCCESSFUL_RETURN;
196 }
197 
198 
200 
201  safeCopy << arg;
202 
203  uint run1;
204 
205  for( run1 = 0; run1 < arg.getDim(); run1++ ){
206 
207  int nn;
208 
209  nn = n;
210 
211  f = (Operator**)realloc(f,(dim+1)*sizeof(Operator*));
212 
213  f[dim] = arg.element[run1]->clone();
214  f[dim]-> loadIndices ( indexList );
215 
216  sub = (Operator**)realloc(sub,
217  (indexList->getNumberOfOperators())*sizeof(Operator*));
218  lhs_comp = (int*)realloc(lhs_comp,
219  (indexList->getNumberOfOperators())*sizeof(int));
220 
221  indexList->getOperators( sub, lhs_comp, &n );
222 
223  while( nn < n ){
224 
225  sub[nn]-> enumerateVariables( indexList );
226  nn++;
227  }
228 
229  f[dim]-> enumerateVariables( indexList );
230 
231  dim++;
232  }
233  return SUCCESSFUL_RETURN;
234 }
235 
236 
237 
238 returnValue FunctionEvaluationTree::evaluate( double *x, double *result ){
239 
240  int run1;
241 
242  for( run1 = 0; run1 < n; run1++ ){
243 
244  sub[run1]->evaluate( 0, x, &x[ indexList->index(VT_INTERMEDIATE_STATE,
245  lhs_comp[run1] ) ] );
246  }
247  for( run1 = 0; run1 < dim; run1++ ){
248  f[run1]->evaluate( 0, x, &result[run1] );
249  }
250 
251  return SUCCESSFUL_RETURN;
252 }
253 
254 
255 returnValue FunctionEvaluationTree::evaluate( double *x, double *result, PrintLevel printL ){
256 
257  int run1;
258 
259  if (printL == MEDIUM || printL == HIGH)
260  cout << "Symbolic expression evaluation:" << endl;
261 
262  for( run1 = 0; run1 < n; run1++ ){
263 
264  sub[run1]->evaluate( 0, x, &x[ indexList->index(VT_INTERMEDIATE_STATE,
265  lhs_comp[run1] ) ] );
266  if( printL == HIGH )
267  cout << "sub[" << lhs_comp[ run1 ] << "] = "
268  << scientific << x[ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1]) ]
269  << endl;
270  }
271 
272  for (run1 = 0; run1 < dim; run1++) {
273  f[run1]->evaluate(0, x, &result[run1]);
274 
275  if (printL == HIGH || printL == MEDIUM)
276  cout << "f[" << run1 << "] = " << scientific << result[run1] << endl;
277  }
278 
279  return SUCCESSFUL_RETURN;
280 }
281 
282 
283 
284 returnValue FunctionEvaluationTree::evaluate( int number, double *x, double *result ){
285 
286  int run1;
287 
288  for( run1 = 0; run1 < n; run1++ ){
289  sub[run1]->evaluate( number, x, &x[ indexList->index(VT_INTERMEDIATE_STATE,
290  lhs_comp[run1] ) ] );
291  }
292  for( run1 = 0; run1 < dim; run1++ ){
293  f[run1]->evaluate( number, x, &result[run1] );
294  }
295 
296  return SUCCESSFUL_RETURN;
297 }
298 
299 
300 
302 
304 
305  return this;
306 }
307 
308 
310 
311  int run1;
313 
314  int sub_index = index(variableType_, index_ );
315 
316  tmp.dim = dim;
317  tmp.n = n ;
318 
319  if( f == NULL ){
320  tmp.f = NULL;
321  }
322  else{
323  tmp.f = (Operator**)calloc(dim,sizeof(Operator*));
324 
325  Operator *temp;
326 
327  if( fabs( sub_ ) > 10.0*EPS ){
328  if( fabs( 1.0 - sub_ ) < 10.0*EPS ){
329  temp = new DoubleConstant(1.0,NE_ONE);
330  for( run1 = 0; run1 < dim; run1++ ){
331  tmp.f[run1] = f[run1]->substitute(sub_index,temp);
332  }
333  delete temp;
334  }
335  else{
336  temp = new DoubleConstant(sub_,NE_NEITHER_ONE_NOR_ZERO);
337  for( run1 = 0; run1 < dim; run1++ ){
338  tmp.f[run1] = f[run1]->substitute(sub_index,temp);
339  }
340  delete temp;
341  }
342  }
343  else{
344  temp = new DoubleConstant(0.0,NE_ZERO);
345  for( run1 = 0; run1 < dim; run1++ ){
346  tmp.f[run1] = f[run1]->substitute(sub_index,temp);
347  }
348  delete temp;
349  }
350  }
351 
352  if( sub == NULL ){
353  tmp.sub = NULL;
354  tmp.lhs_comp = NULL;
355  }
356  else{
357  tmp.sub = (Operator**)calloc(n,sizeof(Operator*));
358  tmp.lhs_comp = (int*)calloc(n,sizeof(int));
359 
360  Operator *temp;
361 
362  if( fabs(sub_) > 10.0*EPS ){
363  if( fabs( 1.0-sub_ ) < 10.0*EPS ){
364  temp = new DoubleConstant(1.0,NE_ONE);
365  for( run1 = 0; run1 < n; run1++ ){
366  tmp.sub[run1] = sub[run1]->substitute(sub_index,temp);
367  tmp.lhs_comp[run1] = lhs_comp[run1];
368  }
369  delete temp;
370  }
371  else{
372  temp = new DoubleConstant(sub_,NE_NEITHER_ONE_NOR_ZERO);
373  for( run1 = 0; run1 < n; run1++ ){
374  tmp.sub[run1] = sub[run1]->substitute(sub_index,temp);
375  tmp.lhs_comp[run1] = lhs_comp[run1];
376  }
377  delete temp;
378  }
379  }
380  else{
381  temp = new DoubleConstant(0.0,NE_ZERO);
382  for( run1 = 0; run1 < n; run1++ ){
383  tmp.sub[run1] = sub[run1]->substitute(sub_index,temp);
384  tmp.lhs_comp[run1] = lhs_comp[run1];
385  }
386  delete temp;
387  }
388  }
389 
390  delete tmp.indexList;
391  tmp.indexList = indexList->substitute(variableType_, index_);
392 
393  return tmp;
394 }
395 
396 
397 
399 
400  int run1;
401 
403 
404  if( dim > 0 ){
405  if( f[0]->isOneOrZero() == NE_ZERO ){
406  e = NE_ZERO;
407  }
408  if( f[0]->isOneOrZero() == NE_ONE ){
409  e = NE_ONE;
410  }
411  }
412 
413  if( e == NE_NEITHER_ONE_NOR_ZERO ){
414  return e;
415  }
416 
417  for( run1 = 1; run1 < dim; run1++ ){
418 
419  if( e == NE_ONE ){
420  if( f[run1]->isOneOrZero() != NE_ONE ){
422  }
423  }
424  if( e == NE_ZERO ){
425  if( f[run1]->isOneOrZero() != NE_ZERO ){
427  }
428  }
429  }
430 
431  return e;
432 }
433 
434 
435 
437 
438  int nn = variable.getDim();
439 
440  int run1;
441  BooleanType *implicit_dep = new BooleanType [n ];
442  VariableType *varType = new VariableType[nn];
443  int *component = new int [nn];
444 
445  for( run1 = 0; run1 < nn; run1++ ){
446 
447  Operator *tmp2 = (variable.element[run1])->clone();
448 
449  if( tmp2->isVariable( varType[run1], component[run1] ) == BT_FALSE ){
450 
452  delete tmp2 ;
453  delete[] varType ;
454  delete[] component;
455  return BT_TRUE ;
456  }
457 
458  if( varType[run1] == VT_INTERMEDIATE_STATE ){
459 
461  delete tmp2 ;
462  delete[] varType ;
463  delete[] component;
464  return BT_TRUE ;
465  }
466  delete tmp2;
467  }
468 
469 
470  for( run1 = 0; run1 < n; run1++ ){
471  implicit_dep[run1] = sub[run1]->isDependingOn( 1, varType, component, implicit_dep );
472  }
473  for( run1 = 0; run1 < dim; run1++ ){
474  if( f[run1]->isDependingOn( 1, varType, component, implicit_dep ) == BT_TRUE ){
475  delete[] implicit_dep;
476  delete[] varType ;
477  delete[] component;
478  return BT_TRUE;
479  }
480  }
481 
482  delete[] implicit_dep;
483  delete[] varType ;
484  delete[] component;
485  return BT_FALSE;
486 }
487 
488 
490 
491  int nn = variable.getDim();
492 
493  int run1;
494  BooleanType *implicit_dep = new BooleanType [n ];
495  VariableType *varType = new VariableType[nn];
496  int *component = new int [nn];
497 
498  for( run1 = 0; run1 < nn; run1++ ){
499 
500  Operator *tmp2 = (variable.element[run1])->clone();
501 
502  if( tmp2->isVariable( varType[run1], component[run1] ) == BT_FALSE ){
503 
505  delete tmp2 ;
506  delete[] varType ;
507  delete[] component;
508  return BT_TRUE ;
509  }
510 
511  if( varType[run1] == VT_INTERMEDIATE_STATE ){
512 
514  delete tmp2 ;
515  delete[] varType ;
516  delete[] component;
517  return BT_TRUE ;
518  }
519  delete tmp2;
520  }
521 
522 
523  for( run1 = 0; run1 < n; run1++ ){
524  implicit_dep[run1] = sub[run1]->isLinearIn( 1, varType, component, implicit_dep );
525  }
526  for( run1 = 0; run1 < dim; run1++ ){
527  if( f[run1]->isLinearIn( 1, varType, component, implicit_dep ) == BT_FALSE ){
528  delete[] implicit_dep;
529  delete[] varType ;
530  delete[] component;
531  return BT_FALSE;
532  }
533  }
534 
535  delete[] implicit_dep;
536  delete[] varType ;
537  delete[] component;
538  return BT_TRUE;
539 }
540 
541 
543 
544  int nn = variable.getDim();
545 
546  int run1;
547  BooleanType *implicit_dep = new BooleanType [n ];
548  VariableType *varType = new VariableType[nn];
549  int *component = new int [nn];
550 
551  for( run1 = 0; run1 < nn; run1++ ){
552 
553  Operator *tmp2 = (variable.element[run1])->clone();
554 
555  if( tmp2->isVariable( varType[run1], component[run1] ) == BT_FALSE ){
556 
558  delete tmp2 ;
559  delete[] varType ;
560  delete[] component;
561  return BT_TRUE ;
562  }
563 
564  if( varType[run1] == VT_INTERMEDIATE_STATE ){
565 
567  delete tmp2 ;
568  delete[] varType ;
569  delete[] component;
570  return BT_TRUE ;
571  }
572  delete tmp2;
573  }
574 
575 
576  for( run1 = 0; run1 < n; run1++ ){
577  implicit_dep[run1] = sub[run1]->isPolynomialIn( 1, varType, component, implicit_dep );
578  }
579  for( run1 = 0; run1 < dim; run1++ ){
580  if( f[run1]->isPolynomialIn( 1, varType, component, implicit_dep ) == BT_FALSE ){
581  delete[] implicit_dep;
582  delete[] varType ;
583  delete[] component;
584  return BT_FALSE;
585  }
586  }
587 
588  delete[] implicit_dep;
589  delete[] varType ;
590  delete[] component;
591  return BT_TRUE;
592 }
593 
594 
595 
597 
598  int nn = variable.getDim();
599 
600  int run1;
601  BooleanType *implicit_dep = new BooleanType [n ];
602  VariableType *varType = new VariableType[nn];
603  int *component = new int [nn];
604 
605  for( run1 = 0; run1 < nn; run1++ ){
606 
607  Operator *tmp2 = (variable.element[run1])->clone();
608 
609  if( tmp2->isVariable( varType[run1], component[run1] ) == BT_FALSE ){
610 
612  delete tmp2 ;
613  delete[] varType ;
614  delete[] component;
615  return BT_TRUE ;
616  }
617 
618  if( varType[run1] == VT_INTERMEDIATE_STATE ){
619 
621  delete tmp2 ;
622  delete[] varType ;
623  delete[] component;
624  return BT_TRUE ;
625  }
626  delete tmp2;
627  }
628 
629 
630  for( run1 = 0; run1 < n; run1++ ){
631  implicit_dep[run1] = sub[run1]->isRationalIn( 1, varType, component, implicit_dep );
632  }
633  for( run1 = 0; run1 < dim; run1++ ){
634  if( f[run1]->isRationalIn( 1, varType, component, implicit_dep ) == BT_FALSE ){
635  delete[] implicit_dep;
636  delete[] varType ;
637  delete[] component;
638  return BT_FALSE;
639  }
640  }
641 
642  delete[] implicit_dep;
643  delete[] varType ;
644  delete[] component;
645  return BT_TRUE;
646 }
647 
648 
650 {
651  int run1;
653 
654  for (run1 = 0; run1 < dim; run1++)
655  {
656  int mf = f[run1]->getMonotonicity();
657 
658  switch (mf)
659  {
660  case MT_NONDECREASING:
661  if (m == MT_CONSTANT) m = MT_NONDECREASING;
662  if (m == MT_NONINCREASING) return MT_NONMONOTONIC;
663 
664  break;
665 
666  case MT_NONINCREASING:
667  if (m == MT_CONSTANT) m = MT_NONINCREASING;
668  if (m == MT_NONDECREASING) return MT_NONMONOTONIC;
669 
670  break;
671 
672  case MT_NONMONOTONIC:
673  return MT_NONMONOTONIC;
674 
675  case MT_UNKNOWN:
676  return MT_UNKNOWN;
677  }
678  }
679  return m;
680 }
681 
682 
683 
685 
686  int run1;
688 
689  for( run1 = 0; run1 < dim; run1++ ){
690 
691  CurvatureType mf = f[run1]->getCurvature();
692 
693  if( mf == CT_AFFINE ){
694 
695  if( m == CT_CONSTANT ) m = CT_AFFINE;
696  }
697 
698  if( mf == CT_CONVEX ){
699 
700  if( m == CT_CONSTANT ) m = CT_CONVEX;
701  if( m == CT_AFFINE ) m = CT_CONVEX;
702  if( m == CT_CONCAVE ) return CT_NEITHER_CONVEX_NOR_CONCAVE;
703  }
704 
705  if( mf == CT_CONCAVE ){
706 
707  if( m == CT_CONSTANT ) m = CT_CONCAVE;
708  if( m == CT_AFFINE ) m = CT_CONCAVE;
709  if( m == CT_CONVEX ) return CT_NEITHER_CONVEX_NOR_CONCAVE;
710  }
711 
713  }
714  return m;
715 }
716 
717 
718 
719 returnValue FunctionEvaluationTree::AD_forward( double *x, double *seed, double *ff,
720  double *df ){
721 
722  int run1;
723 
724  for( run1 = 0; run1 < n; run1++ ){
725  sub[run1]->AD_forward( 0, x, seed,
726  &x [ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])],
727  &seed[ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])] );
728  }
729  for( run1 = 0; run1 < dim; run1++ ){
730  f[run1]->AD_forward( 0, x, seed, &ff[run1], &df[run1] );
731  }
732 
733  return SUCCESSFUL_RETURN;
734 }
735 
736 
737 
738 returnValue FunctionEvaluationTree::AD_forward( int number, double *x, double *seed,
739  double *ff, double *df ){
740 
741  int run1;
742 
743  for( run1 = 0; run1 < n; run1++ ){
744  sub[run1]->AD_forward( number, x, seed,
745  &x [ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])],
746  &seed[ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])] );
747  }
748  for( run1 = 0; run1 < dim; run1++ ){
749  f[run1]->AD_forward( number, x, seed, &ff[run1], &df[run1] );
750  }
751 
752  return SUCCESSFUL_RETURN;
753 }
754 
755 
756 returnValue FunctionEvaluationTree::AD_forward( int number, double *seed, double *df ){
757 
758  int run1;
759 
760  for( run1 = 0; run1 < n; run1++ ){
761  sub[run1]->AD_forward( number, seed,
762  &seed[ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])] );
763  }
764  for( run1 = 0; run1 < dim; run1++ ){
765  f[run1]->AD_forward( number, seed, &df[run1] );
766  }
767 
768  return SUCCESSFUL_RETURN;
769 }
770 
771 
773 
774  int run1;
775 
776  for( run1 = dim-1; run1 >= 0; run1-- ){
777  f[run1]->AD_backward( 0, seed[run1], df );
778  }
779 
780 
781  for( run1 = n-1; run1 >= 0; run1-- ){
782 
783  sub[run1]->AD_backward( 0, df[ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])],
784  df
785  );
786  }
787 
788  return SUCCESSFUL_RETURN;
789 }
790 
791 
792 
793 returnValue FunctionEvaluationTree::AD_backward( int number, double *seed, double *df ){
794 
795  int run1;
796 
797  for( run1 = dim-1; run1 >= 0; run1-- ){
798  f[run1]->AD_backward( number, seed[run1], df );
799  }
800 
801  for( run1 = n-1; run1 >= 0; run1-- ){
802  sub[run1]->AD_backward( number,
803  df[ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])],
804  df );
805  }
806 
807  return SUCCESSFUL_RETURN;
808 }
809 
810 
812  double *dseed, double *df,
813  double *ddf ){
814 
815  int run1;
816 
817  for( run1 = 0; run1 < n; run1++ ){
818  sub[run1]->AD_forward2( number, seed, dseed,
819  &seed [ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])],
820  &dseed[ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])] );
821  }
822  for( run1 = 0; run1 < dim; run1++ ){
823  f[run1]->AD_forward2( number, seed, dseed, &df[run1], &ddf[run1] );
824  }
825 
826  return SUCCESSFUL_RETURN;
827 }
828 
829 
830 returnValue FunctionEvaluationTree::AD_backward2( int number, double *seed1, double *seed2,
831  double *df, double *ddf ){
832 
833  int run1;
834 
835  for( run1 = dim-1; run1 >= 0; run1-- ){
836  f[run1]->AD_backward2( number, seed1[run1], seed2[run1], df, ddf );
837  }
838 
839 
840  for( run1 = n-1; run1 >= 0; run1-- ){
841  sub[run1]->AD_backward2( number,
842  df [ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])],
843  ddf [ indexList->index(VT_INTERMEDIATE_STATE, lhs_comp[run1])],
844  df,
845  ddf
846  );
847  }
848 
849  return SUCCESSFUL_RETURN;
850 }
851 
852 
853 
855  const char *fcnName ,
856  const char *realString
857  ) const
858 {
859  stream << "/* This file was auto-generated by ACADO Toolkit. */" << endl << endl;
860 
861  exportForwardDeclarations(stream, fcnName, realString);
862  stream << endl;
863  exportCode(stream, fcnName, realString);
864 
865  return SUCCESSFUL_RETURN;
866 }
867 
869  const char *fcnName,
870  const char *realString
871  ) const
872 {
873  stream <<
874  "\n"
875  "/** Export of an ACADO symbolic function.\n"
876  " *\n"
877  " * \\param in Input to the exported function.\n"
878  " * \\param out Output of the exported function.\n"
879  " */\n"
880  << "void " << fcnName << "(const " << realString << "* in, "
881  << realString << "* out);" << endl;
882 
883  return SUCCESSFUL_RETURN;
884 }
885 
886 
888  const char *fcnName,
889  const char *realString,
890  uint _numX,
891  uint _numXA,
892  uint _numU,
893  uint _numP,
894  uint _numDX,
895  uint _numOD,
896  bool allocateMemory,
897  bool staticMemory
898  ) const{
899 
900  int run1;
901  int nni = 0;
902 
903  for (run1 = 0; run1 < n; run1++)
904  if (lhs_comp[run1] + 1 > nni)
905  nni = lhs_comp[run1] + 1;
906 
907  unsigned numX = _numX > 0 ? _numX : getNX();
908  unsigned numXA = _numXA > 0 ? _numXA : getNXA();
909  unsigned numU = _numU > 0 ? _numU : getNU();
910  unsigned numP = _numP > 0 ? _numP : getNP();
911  unsigned numDX = _numDX > 0 ? _numDX : getNDX();
912  unsigned numOD = _numOD > 0 ? _numOD : getNOD();
913 
914  unsigned offset = 0;
915 
916  stream << "void " << fcnName << "(const " << realString << "* in, " << realString << "* out)\n{\n";
917 
918  if (numX > 0)
919  stream << "const " << realString << "* xd = in;" << endl;
920  offset += numX;
921 
922  if (numXA > 0)
923  stream << "const " << realString << "* xa = in + " << offset << ";" << endl;
924  offset += numXA;
925 
926  if (getNU() > 0)
927  stream << "const " << realString << "* u = in + " << offset << ";" << endl;
928  offset += numU;
929 
930  if (getNUI() > 0)
931  stream << "const " << realString << "* v = in + " << offset << ";" << endl;
932  offset += getNUI();
933 
934  if (numP > 0)
935  stream << "const " << realString << "* p = in + " << offset << ";" << endl;
936  offset += numP;
937 
938  if (getNOD() > 0)
939  stream << "const " << realString << "* od = in + " << offset << ";" << endl;
940  offset += numOD;
941 
942  if (getNPI() > 0)
943  stream << "const " << realString << "* q = in + " << offset << ";" << endl;
944  offset += getNPI();
945 
946  if (getNW() > 0)
947  stream << "const " << realString << "* w = in + " << offset << ";" << endl;
948  offset += getNW();
949 
950  if (numDX > 0)
951  stream << "const " << realString << "* dx = in + " << offset << ";" << endl;
952  offset += numDX;
953 
954  if (getNT() > 0)
955  stream << "const " << realString << "* t = in + " << offset << ";" << endl;
956  offset += getNT();
957 
958  if (n > 0)
959  {
960  stream << "/* Vector of auxiliary variables; number of elements: " << n << ". */" << endl;
961 
962  if ( allocateMemory )
963  {
964  if ( staticMemory )
965  {
966  stream << "static ";
967  }
968  stream << realString << " a[" << n << "];";
969  }
970  else
971  stream << realString << "* a = " << globalExportVariableName << ";";
972  stream << endl << endl;
973 
974  stream << "/* Compute intermediate quantities: */" << endl;
975  }
976 
977  vector< string > auxVarIndividualNames;
978  auxVarIndividualNames.resize( nni );
979  for (run1 = 0; run1 < n; run1++)
980  {
981  stringstream ss;
982  ss << "a" << "[" << run1 << "]";
983  auxVarIndividualNames[ lhs_comp[ run1 ] ] = ss.str();
984  }
985 
986  IoFormatter iof( stream );
987  iof.set(16, iof.width, ios::scientific);
988 
989  // Export intermediate quantities
990  for (run1 = 0; run1 < n; run1++)
991  {
992  // Convert the name for intermediate variables for subexpressions
993  sub[run1]->setVariableExportName(VT_INTERMEDIATE_STATE, auxVarIndividualNames);
994 
995  stream << "a[" << run1 << "] = " << *sub[ run1 ] << ";" << endl;
996  }
997 
998  // Export output quantities
999  stream << endl << "/* Compute outputs: */" << endl;
1000  for (run1 = 0; run1 < dim; run1++)
1001  {
1002  // Convert names for interm. quantities for output expressions
1003  f[run1]->setVariableExportName(VT_INTERMEDIATE_STATE, auxVarIndividualNames);
1004 
1005  stream << "out[" << run1 << "] = " << *f[ run1 ] << ";" << endl;
1006  }
1007 
1008  iof.reset();
1009 
1010  stream << "}" << endl << endl;
1011 
1012  return SUCCESSFUL_RETURN;
1013 }
1014 
1015 
1017 
1018  int run1;
1019  returnValue returnvalue;
1020 
1021  for( run1 = 0; run1 < n; run1++ ){
1022  returnvalue = sub[run1]->clearBuffer();
1023  if( returnvalue != SUCCESSFUL_RETURN ){
1024  return returnvalue;
1025  }
1026  }
1027  for( run1 = 0; run1 < dim; run1++ ){
1028  returnvalue = f[run1]->clearBuffer();
1029  if( returnvalue != SUCCESSFUL_RETURN ){
1030  return returnvalue;
1031  }
1032  }
1033 
1034  return SUCCESSFUL_RETURN;
1035 }
1036 
1037 
1039 
1040  return makeImplicit(dim);
1041 }
1042 
1043 
1045 
1046  int run1;
1047  int var_counter = indexList->makeImplicit(dim_);
1048 
1049  for( run1 = 0; run1 < dim_; run1++ ){
1050 
1051  Operator *tmp = f[run1]->clone();
1052  delete f[run1];
1053  Projection pp;
1055  pp.vIndex = run1 ;
1056  pp.variableIndex = var_counter-dim_+run1 ;
1057 
1058  f[run1] = new Subtraction( pp.clone(), tmp->clone() );
1059  delete tmp;
1060  }
1061 
1062  return SUCCESSFUL_RETURN;
1063 }
1064 
1065 
1067 
1068  return indexList->getNX();
1069 }
1070 
1072 
1073  return indexList->getNXA();
1074 }
1075 
1077 
1078  return indexList->getNDX();
1079 }
1080 
1082 
1083  return indexList->getNU();
1084 }
1085 
1087 
1088  return indexList->getNUI();
1089 }
1090 
1092 
1093  return indexList->getNP();
1094 }
1095 
1097 
1098  return indexList->getNPI();
1099 }
1100 
1102 
1103  return indexList->getNW();
1104 }
1105 
1107 
1108  return indexList->getNT();
1109 }
1110 
1112 
1113  return indexList->getOD();
1114 }
1115 
1116 int FunctionEvaluationTree::index( VariableType variableType_, int index_ ) const{
1117 
1118  return indexList->index( variableType_, index_ );
1119 }
1120 
1121 double FunctionEvaluationTree::scale( VariableType variableType_, int index_ ) const{
1122 
1123  return indexList->scale( variableType_, index_ );
1124 }
1125 
1126 
1128 
1129  return indexList->getNumberOfVariables();
1130 }
1131 
1132 
1134  ) const
1135 {
1136  if ( (int)componentIdx < getDim( ) )
1137  return f[componentIdx]->clone( );
1138  else
1139  return new DoubleConstant( 0.0,NE_ZERO );
1140 }
1141 
1142 
1144 
1145  int run1;
1146  for( run1 = 0; run1 < n; run1++ ){
1147  if( sub[run1]->isSymbolic() == BT_FALSE ) return BT_FALSE;
1148  }
1149  for( run1 = 0; run1 < dim; run1++ ){
1150  if( f[run1]->isSymbolic() == BT_FALSE ) return BT_FALSE;
1151  }
1152  return BT_TRUE;
1153 }
1154 
1155 
1157 {
1159 }
1160 
1162 {
1163  if (_name.size() == 0)
1165 
1166  globalExportVariableName = _name;
1167 
1168  return SUCCESSFUL_RETURN;
1169 }
1170 
1172 {
1173  return globalExportVariableName;
1174 }
1175 
1177 {
1178  return n;
1179 }
1180 
1182 
1183 // end of file.
virtual returnValue AD_backward2(int number, double *seed1, double *seed2, double *df, double *ddf)
Abstract base class for all scalar-valued symbolic operators.
Definition: operator.hpp:60
virtual int getNumberOfVariables() const
VariableType variableType
Definition: projection.hpp:547
virtual returnValue AD_forward(double *x, double *seed, double *f, double *df)
virtual BooleanType isDependingOn(VariableType var) const =0
virtual returnValue AD_forward2(int number, double *seed1, double *seed2, double *df, double *ddf)
BEGIN_NAMESPACE_ACADO const double EPS
SymbolicIndexList * substitute(VariableType variableType_, int index_)
FunctionEvaluationTree * differentiate(int index)
Allows to pass back messages to the calling function.
returnValue exportForwardDeclarations(std::ostream &stream=std::cout, const char *fcnName="ACADOfcn", const char *realString="double") const
virtual Operator * clone() const =0
virtual Operator * substitute(int index, const Operator *sub)=0
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
returnValue C_print(std::ostream &stream=std::cout, const char *fcnName="ACADOfcn", const char *realString="double") const
unsigned getGlobalExportVariableSize() const
virtual returnValue AD_backward(double *seed, double *df)
#define CLOSE_NAMESPACE_ACADO
virtual returnValue operator<<(const Expression &arg)
Manages the indices of SymbolicVariables.
VariableType
Definition: acado_types.hpp:95
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()
virtual Operator * getExpression(uint componentIdx) const
virtual returnValue evaluate(double *x, double *result)
Operator ** element
Definition: expression.hpp:311
virtual NeutralElement isOneOrZero()
virtual BooleanType isRationalIn(const Expression &variable)
virtual BooleanType isDependingOn(const Expression &variable)
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
Implements the scalar subtraction operator within the symbolic operators family.
Definition: subtraction.hpp:54
void set(streamsize _precision, streamsize _width, ios_base::fmtflags _flags)
std::string getGlobalExportVariableName() const
virtual Operator * clone() const
Definition: projection.cpp:142
PrintLevel
virtual double scale(VariableType variableType_, int index_) const
Implements the projection operator within the symbolic operators family.
Definition: projection.hpp:55
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
virtual BooleanType isSymbolic() const
#define BT_TRUE
Definition: acado_types.hpp:47
NeutralElement
Definition: acado_types.hpp:64
virtual BooleanType isVariable(VariableType &varType, int &component) const =0
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
Organizes the evaluation of the function tree.
MonotonicityType
virtual FunctionEvaluationTree substitute(VariableType variableType_, int index_, double sub_)
CurvatureType
FunctionEvaluationTree & operator=(const FunctionEvaluationTree &arg)
#define BEGIN_NAMESPACE_ACADO
#define BT_FALSE
Definition: acado_types.hpp:49
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)=0
returnValue setGlobalExportVariableName(const std::string &_name)
virtual returnValue setScale(double *scale_)
virtual CurvatureType getCurvature()
virtual BooleanType isLinearIn(const Expression &variable)
Implements a scalar constant within the symbolic operators family.
int variableIndex
Definition: projection.hpp:548
#define ACADOERROR(retval)
virtual returnValue makeImplicit()
uint getDim() const
virtual BooleanType isPolynomialIn(const Expression &variable)


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