tree_projection.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 
42 
43 
44 
45 int TreeProjection::count = 0;
46 
48  :Projection(){
49 
51  vIndex = 0 ;
52  variableIndex = 0 ;
53  argument = 0 ;
54  ne = NE_ZERO ;
55 }
56 
57 
58 TreeProjection::TreeProjection( const std::string &name_ )
59  :Projection( name_ ){
60 
62  vIndex = 0 ;
63  variableIndex = 0 ;
64  argument = 0 ;
65  ne = NE_ZERO ;
66 }
67 
68 
69 
71  :Projection(){
72 
73  copy(arg);
74 
75  if( arg.argument == 0 ){
76  argument = 0;
77  }
78  else{
79  argument = arg.argument;
80  argument->nCount++;
81  }
82 
83  ne = arg.ne;
84 }
85 
86 
88 
89  if( argument != 0 ){
90 
91  if( argument->nCount == 0 ){
92  delete argument;
93  argument = 0;
94  }
95  else{
96  argument->nCount--;
97  }
98  }
99 }
100 
101 
102 
104 
105  if( this != &arg ){
106 
107  if( argument != 0 ){
108  if( argument->nCount == 0 ){
109  delete argument;
110  argument = 0;
111  }
112  else{
113  argument->nCount--;
114  }
115  }
116 
117  Operator *arg_tmp = arg.clone();
118  TreeProjection *tp = dynamic_cast<TreeProjection *>(arg_tmp);
119  Projection *p = dynamic_cast<Projection *>(arg_tmp);
120  if( tp != 0 ) {
121  // special case: argument is a treeprojection
122  if( tp->argument != 0 ) {
123  argument = tp->argument->clone();
124  ne = argument->isOneOrZero();
125  }
126  else {
127  argument = 0;
129  }
130  copy(*tp);
131 
132  }
133  else {
134  if( p != 0 ) {
135  // special case: argument is a projection
136  argument = 0;
138  copy(*p);
139  }
140  else {
141  // no special case: create a new treeprojection
142  argument = arg.clone() ;
143  vIndex = count++;
145 
146  curvature = CT_UNKNOWN; // argument->getCurvature();
147  monotonicity = MT_UNKNOWN; // argument->getMonotonicity();
148 
149  ne = argument->isOneOrZero();
150 
151  if( curvature == CT_CONSTANT )
152  scale = argument->getValue();
153  }
154  }
155  delete arg_tmp;
156  }
157 
158  return *this;
159 }
160 
161 
163 
164  ASSERT( arg.getDim() == 1 );
165 
166  if( argument != 0 ){
167  if( argument->nCount == 0 ){
168  delete argument;
169  argument = 0;
170  }
171  else{
172  argument->nCount--;
173  }
174  }
175 
176  argument = arg.getOperatorClone(0);
177 
178  vIndex = count++;
180 
181  curvature = CT_UNKNOWN; // argument->getCurvature();
182  monotonicity = MT_UNKNOWN; // argument->getMonotonicity();
183 
184  ne = argument->isOneOrZero();
185 
186  if( curvature == CT_CONSTANT )
187  scale = argument->getValue();
188 
189  return *this;
190 }
191 
192 
193 Operator& TreeProjection::operator=( const double& arg ){
194 
195  scale = arg;
196 
197  Expression tmp( arg );
198  return this->operator=( tmp );
199 }
200 
201 
203 
204  return new TreeProjection( *this );
205 }
206 
207 
209 
210  return new TreeProjection( *this );
211 }
212 
213 
214 
216  VariableType *varType,
217  int *component,
218  Operator **seed,
219  int &nNewIS,
220  TreeProjection ***newIS ){
221 
222  if (argument == 0) {
223  return Projection::ADforwardProtected( dim, varType, component, seed, nNewIS, newIS );
224  }
225  else {
226 
227  int run1 = 0;
228 
229  while( run1 < dim ){
230 
231  if( varType[run1] == variableType && component[run1] == vIndex ){
232  return seed[run1]->clone();
233  }
234  run1++;
235  }
236 
237  if( vIndex >= nNewIS ){
238 
239  *newIS = (TreeProjection**)realloc(*newIS,(vIndex+1)*sizeof(TreeProjection*));
240 
241  for( run1 = nNewIS; run1 < vIndex + 1; run1++ )
242  newIS[0][run1] = 0;
243 
244  nNewIS = vIndex+1;
245  }
246 
247  if( newIS[0][vIndex] != 0 ){
248 
249  return newIS[0][vIndex]->clone();
250  }
251 
252  Operator *tmp = argument->AD_forward(dim,varType,component,seed,nNewIS,newIS);
253 
254  newIS[0][vIndex] = new TreeProjection();
255 
256  newIS[0][vIndex]->operator=(*tmp);
257 
258  newIS[0][vIndex]->setCurvature( CT_UNKNOWN );
259 
260  delete tmp;
261  return newIS[0][vIndex]->clone();
262  }
263 }
264 
265 
266 
268  VariableType *varType ,
269  int *component,
270  Operator *seed ,
271  Operator **df ,
272  int &nNewIS ,
273  TreeProjection ***newIS ){
274 
275  if (argument == 0) {
276  return Projection::ADbackwardProtected( dim, varType, component, seed, df, nNewIS, newIS );
277  }
278  else {
279 
280  int run1;
281 
282  if( (vIndex+1)*dim-1 >= nNewIS ){
283 
284  *newIS = (TreeProjection**)realloc(*newIS,((vIndex+1)*dim)*sizeof(TreeProjection*));
285 
286  for( run1 = nNewIS; run1 < (vIndex+1)*dim; run1++ )
287  newIS[0][run1] = 0;
288 
289  nNewIS = (vIndex+1)*dim;
290  }
291 
292  if( newIS[0][vIndex*dim] == 0 ){
293 
294  Operator **results = new Operator*[dim];
295  for( run1 = 0; run1 < dim; run1++ ){
296  results[run1] = new DoubleConstant(0.0,NE_ZERO);
297  }
298  Operator *aux = new DoubleConstant(1.0,NE_ONE);
299  argument->AD_backward(dim,varType,component, aux, results, nNewIS, newIS );
300  for( run1 = 0; run1 < dim; run1++ ){
301  newIS[0][vIndex*dim+run1] = new TreeProjection();
302  newIS[0][vIndex*dim+run1]->operator=(*results[run1]);
303  newIS[0][vIndex*dim+run1]->setCurvature( CT_UNKNOWN );
304  delete results[run1];
305  }
306  delete[] results;
307  }
308 
309  if( seed->isOneOrZero() != NE_ZERO ){
310 
311  for( run1 = 0; run1 < dim; run1++ ){
312 
313  Operator *tmp = df[run1]->clone();
314  delete df[run1];
315 
316  if( seed->isOneOrZero() == NE_ONE ){
317  df[run1] = myAdd( newIS[0][vIndex*dim+run1], tmp );
318  }
319  else{
320  Operator *projTmp = myProd(newIS[0][vIndex*dim+run1],seed);
321  df[run1] = myAdd( projTmp, tmp );
322  delete projTmp;
323  }
324  delete tmp;
325  }
326  }
327  delete seed;
328 
329  return SUCCESSFUL_RETURN;
330  }
331 }
332 
333 
334 
336  VariableType *varType ,
337  int *component ,
338  Operator *l ,
339  Operator **S ,
340  int dimS ,
341  Operator **dfS ,
342  Operator **ldf ,
343  Operator **H ,
344  int &nNewLIS ,
345  TreeProjection ***newLIS ,
346  int &nNewSIS ,
347  TreeProjection ***newSIS ,
348  int &nNewHIS ,
349  TreeProjection ***newHIS ){
350 
351 
352  if (argument == 0) {
353  return Projection::ADsymmetricProtected( dim, varType, component, l, S, dimS, dfS, ldf, H, nNewLIS, newLIS, nNewSIS, newSIS, nNewHIS, newHIS );
354  }
355  else {
356 
357  int run1;
358 
359  if( (vIndex+1)*dim-1 >= nNewLIS ){
360 
361  *newLIS = (TreeProjection**)realloc(*newLIS,((vIndex+1)*dim)*sizeof(TreeProjection*));
362  for( run1 = nNewLIS; run1 < (vIndex+1)*dim; run1++ )
363  newLIS[0][run1] = 0;
364  nNewLIS = (vIndex+1)*dim;
365  }
366 
367  if( (vIndex+1)*dimS-1 >= nNewSIS ){
368 
369  *newSIS = (TreeProjection**)realloc(*newSIS,((vIndex+1)*dimS)*sizeof(TreeProjection*));
370  for( run1 = nNewSIS; run1 < (vIndex+1)*dimS; run1++ )
371  newSIS[0][run1] = 0;
372  nNewSIS = (vIndex+1)*dimS;
373  }
374 
375  if( (vIndex+1)*dimS*dimS-1 >= nNewHIS ){
376 
377  *newHIS = (TreeProjection**)realloc(*newHIS,((vIndex+1)*dimS*dimS)*sizeof(TreeProjection*));
378  for( run1 = nNewHIS; run1 < (vIndex+1)*dimS*dimS; run1++ )
379  newHIS[0][run1] = 0;
380  nNewHIS = (vIndex+1)*dimS*dimS;
381  }
382 
383  // ============================================================================
384 
385  if( newLIS[0][vIndex*dim] == 0 ){
386 
387  Operator **lres = new Operator*[dim];
388  for( run1 = 0; run1 < dim; run1++ ){
389  lres[run1] = new DoubleConstant(0.0,NE_ZERO);
390  }
391  Operator *aux = new DoubleConstant(1.0,NE_ONE);
392 
393  Operator **Sres = new Operator*[dimS];
394  for( run1 = 0; run1 < dimS; run1++ ){
395  Sres[run1] = new DoubleConstant(0.0,NE_ZERO);
396  }
397 
398  Operator **Hres = new Operator*[dimS*dimS];
399  for( run1 = 0; run1 < dimS*dimS; run1++ ){
400  Hres[run1] = new DoubleConstant(0.0,NE_ZERO);
401  }
402 
403  argument->AD_symmetric( dim, varType, component, aux, S, dimS,
404  Sres, lres, Hres,
405  nNewLIS, newLIS, nNewSIS, newSIS, nNewHIS, newHIS );
406 
407 
408  for( run1 = 0; run1 < dim; run1++ ){
409  newLIS[0][vIndex*dim+run1] = new TreeProjection();
410  newLIS[0][vIndex*dim+run1]->operator=(*lres[run1]);
411  newLIS[0][vIndex*dim+run1]->setCurvature( CT_UNKNOWN );
412  delete lres[run1];
413  }
414  delete[] lres;
415 
416  for( run1 = 0; run1 < dimS; run1++ ){
417  newSIS[0][vIndex*dimS+run1] = new TreeProjection();
418  newSIS[0][vIndex*dimS+run1]->operator=(*Sres[run1]);
419  newSIS[0][vIndex*dimS+run1]->setCurvature( CT_UNKNOWN );
420  delete Sres[run1];
421  }
422  delete[] Sres;
423 
424  for( run1 = 0; run1 < dimS*dimS; run1++ ){
425  newHIS[0][vIndex*dimS*dimS+run1] = new TreeProjection();
426  newHIS[0][vIndex*dimS*dimS+run1]->operator=(*Hres[run1]);
427  newHIS[0][vIndex*dimS*dimS+run1]->setCurvature( CT_UNKNOWN );
428  delete Hres[run1];
429  }
430  delete[] Hres;
431  }
432 
433  // ============================================================================
434 
435  if( l->isOneOrZero() != NE_ZERO ){
436 
437  for( run1 = 0; run1 < dim; run1++ ){
438 
439  Operator *tmp = ldf[run1]->clone();
440  delete ldf[run1];
441 
442  if( l->isOneOrZero() == NE_ONE ){
443  ldf[run1] = myAdd( newLIS[0][vIndex*dim+run1], tmp );
444  }
445  else{
446  Operator *projTmp = myProd( newLIS[0][vIndex*dim+run1], l );
447  ldf[run1] = myAdd( projTmp, tmp );
448  delete projTmp;
449  }
450  delete tmp;
451  }
452  }
453 
454  for( run1 = 0; run1 < dimS; run1++ ){
455  delete dfS[run1];
456  dfS[run1] = newSIS[0][vIndex*dimS+run1]->clone();
457  }
458 
459  for( run1 = 0; run1 < dimS*dimS; run1++ ){
460 
461  delete H[run1];
462 
463  if( l->isOneOrZero() == NE_ONE ){
464  H[run1] = newHIS[0][vIndex*dimS*dimS+run1]->clone();
465  }
466  else{
467  H[run1] = myProd(newHIS[0][vIndex*dimS*dimS+run1],l);
468  }
469  }
470 
471  delete l;
472  return SUCCESSFUL_RETURN;
473  }
474 }
475 
476 
477 
479 
480  returnValue returnvalue = SUCCESSFUL_RETURN;
481 
482  if( argument == 0 ){
483  return Projection::loadIndices( indexList );
484  }
485 
486  if( indexList->addNewElement( VT_INTERMEDIATE_STATE, vIndex ) == BT_TRUE ){
487 
488  returnvalue = argument->loadIndices( indexList );
489 
490  indexList->addOperatorPointer( argument, vIndex );
491  }
492 
493  if (name.empty())
494  {
495  std::stringstream ss;
496  ss << "a" << "[" << vIndex << "]";
497  name = ss.str();
498  }
499 
500  return returnvalue;
501 }
502 
503 
505  VariableType *varType,
506  int *component,
507  BooleanType *implicit_dep ){
508 
509  if( argument == 0 ){
510  return Projection::isDependingOn( dim, varType, component, implicit_dep );
511  }
512  return implicit_dep[vIndex];
513 }
514 
515 
517  VariableType *varType,
518  int *component,
519  BooleanType *implicit_dep ){
520 
521  if( argument == 0 ){
522  return Projection::isLinearIn( dim, varType, component, implicit_dep );
523  }
524  return implicit_dep[vIndex];
525 }
526 
527 
529  VariableType *varType,
530  int *component,
531  BooleanType *implicit_dep ){
532 
533  if( argument == 0 ){
534  return Projection::isPolynomialIn( dim, varType, component, implicit_dep );
535  }
536  return implicit_dep[vIndex];
537 }
538 
539 
541  VariableType *varType,
542  int *component,
543  BooleanType *implicit_dep ){
544 
545  if( argument == 0 ){
546  return Projection::isRationalIn( dim, varType, component, implicit_dep );
547  }
548  return implicit_dep[vIndex];
549 }
550 
551 
553 
554  count = 0;
555  return SUCCESSFUL_RETURN;
556 }
557 
558 
560 
561  if( argument != 0 ) return argument->clone();
562  return 0;
563 }
564 
565 
567 
568  return ne;
569 }
570 
571 
572 
573 void TreeProjection::copy( const Projection &arg ){
574 
575  Projection::copy(arg);
576 }
577 
578 
580 
581  return argument;
582 }
583 
584 
586  const std::vector< std::string >& _name
587  )
588 {
589  if (argument != 0 && argument->getName() == ON_POWER_INT)
590  argument->setVariableExportName(_type, _name);
591 
592  return Projection::setVariableExportName(_type, _name);
593 }
594 
595 
597 
598  return argument != 0;
599 }
600 
601 
603 
604  if( !initialized && argument != 0 ) {
606  return argument->initDerivative();
607  }
608  else {
609  return SUCCESSFUL_RETURN;
610  }
611 }
612 
613 
615 
616 // end of file.
Operator * getOperatorClone(uint idx) const
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: projection.cpp:292
virtual BooleanType isDependingOn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
virtual BooleanType isPolynomialIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
virtual returnValue loadIndices(SymbolicIndexList *indexList)
virtual ~TreeProjection()
Abstract base class for all scalar-valued symbolic operators.
Definition: operator.hpp:60
VariableType variableType
Definition: projection.hpp:547
virtual NeutralElement isOneOrZero() const =0
virtual void copy(const Projection &arg)
Definition: projection.cpp:148
CurvatureType curvature
Definition: projection.hpp:555
Allows to pass back messages to the calling function.
virtual returnValue loadIndices(SymbolicIndexList *indexList)=0
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: projection.cpp:301
virtual Operator * clone() const =0
virtual returnValue ADsymmetricProtected(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)
virtual BooleanType isTrivial() const
virtual Operator * ADforwardProtected(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)
virtual returnValue setVariableExportName(const VariableType &_type, const std::vector< std::string > &_name)
Definition: operator.cpp:223
virtual double getValue() const
Definition: operator.cpp:211
virtual TreeProjection * cloneTreeProjection() const
virtual returnValue ADbackwardProtected(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)
#define CLOSE_NAMESPACE_ACADO
Manages the indices of SymbolicVariables.
VariableType
Definition: acado_types.hpp:95
virtual OperatorName getName()=0
BooleanType initialized
Definition: operator.hpp:642
int nCount
Definition: operator.hpp:556
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
NeutralElement ne
virtual returnValue setVariableExportName(const VariableType &_type, const std::vector< std::string > &_name)
Definition: projection.cpp:569
Operator * getArgument() const
double scale
Definition: projection.hpp:550
virtual Operator * myProd(Operator *a, Operator *b)
Definition: operator.cpp:232
virtual returnValue loadIndices(SymbolicIndexList *indexList)
Definition: projection.cpp:444
virtual returnValue AD_backward(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)=0
virtual returnValue ADsymmetricProtected(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: projection.cpp:518
virtual returnValue initDerivative()
Definition: operator.cpp:519
std::string name
Definition: projection.hpp:551
virtual returnValue initDerivative()
virtual Operator * myAdd(Operator *a, Operator *b)
Definition: operator.cpp:246
Implements the projection operator within the symbolic operators family.
Definition: projection.hpp:55
virtual NeutralElement isOneOrZero() const
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
MonotonicityType monotonicity
Definition: projection.hpp:556
#define ASSERT(x)
#define BT_TRUE
Definition: acado_types.hpp:47
NeutralElement
Definition: acado_types.hpp:64
returnValue clearStaticCounters()
virtual Operator * passArgument() const
virtual Operator * ADforwardProtected(int dim, VariableType *varType, int *component, Operator **seed, int &nNewIS, TreeProjection ***newIS)
Definition: projection.cpp:457
virtual returnValue ADbackwardProtected(int dim, VariableType *varType, int *component, Operator *seed, Operator **df, int &nNewIS, TreeProjection ***newIS)
Definition: projection.cpp:479
Implements the tree-projection operator within the family of SymbolicOperators.
virtual returnValue setVariableExportName(const VariableType &_type, const std::vector< std::string > &_name)
virtual void copy(const Projection &arg)
#define BEGIN_NAMESPACE_ACADO
virtual BooleanType isDependingOn(VariableType var) const
Definition: projection.cpp:256
virtual TreeProjection * clone() const
virtual returnValue setCurvature(CurvatureType curvature_)
Definition: projection.cpp:329
BooleanType addNewElement(VariableType variableType_, int index_)
virtual BooleanType isRationalIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
virtual BooleanType isLinearIn(int dim, VariableType *varType, int *component, BooleanType *implicit_dep)
Definition: projection.cpp:283
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)=0
Implements a scalar constant within the symbolic operators family.
int variableIndex
Definition: projection.hpp:548
Operator & operator=(const Operator &arg)
int addOperatorPointer(Operator *intermediateOperator, int comp_)
uint getDim() const


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