variables_grid.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 
36 
37 
39 
40 
41 
42 //
43 // PUBLIC MEMBER FUNCTIONS:
44 //
45 
47 {
48 }
49 
50 
52  const Grid& _grid,
53  VariableType _type,
54  const char** const _names,
55  const char** const _units,
56  const DVector* const _scaling,
57  const DVector* const _lb,
58  const DVector* const _ub,
59  const BooleanType* const _autoInit
60  ) : MatrixVariablesGrid( _dim,1,_grid,_type,_names,_units,_scaling,_lb,_ub,_autoInit )
61 {
62 }
63 
64 
66  uint _nPoints,
67  VariableType _type,
68  const char** const _names,
69  const char** const _units,
70  const DVector* const _scaling,
71  const DVector* const _lb,
72  const DVector* const _ub,
73  const BooleanType* const _autoInit
74  ) : MatrixVariablesGrid( _dim,1,_nPoints,_type,_names,_units,_scaling,_lb,_ub,_autoInit )
75 {
76 }
77 
78 
80  double _firstTime,
81  double _lastTime,
82  uint _nPoints,
83  VariableType _type,
84  const char** const _names,
85  const char** const _units,
86  const DVector* const _scaling,
87  const DVector* const _lb,
88  const DVector* const _ub,
89  const BooleanType* const _autoInit
90  ) : MatrixVariablesGrid( _dim,1,_firstTime,_lastTime,_nPoints,_type,_names,_units,_scaling,_lb,_ub,_autoInit )
91 {
92 }
93 
94 
96  const Grid& _grid,
97  VariableType _type
98  ) : MatrixVariablesGrid( DMatrix(arg),_grid,_type )
99 {
100 }
101 
102 
104  VariableType _type
105  ) : MatrixVariablesGrid( arg.getNumCols()-1,1,arg.getNumRows(),_type )
106 {
107  uint i,j;
108 
109  for( i=0; i<arg.getNumRows(); ++i )
110  {
111  setTime( i,arg( i,0 ) );
112 
113  for( j=1; j<arg.getNumCols(); ++j )
114  operator()( i,j-1 ) = arg( i,j );
115  }
116 }
117 
118 
120  ) : MatrixVariablesGrid( rhs )
121 {
122 }
123 
124 
126  ) : MatrixVariablesGrid( rhs )
127 {
128  ASSERT( rhs.getNumCols() <= 1 );
129 }
130 
131 
132 
134 {
135 }
136 
137 
139 {
140  if ( this != &rhs )
141  {
143  }
144 
145  return *this;
146 }
147 
148 
150 {
151  ASSERT( rhs.getNumCols() <= 1 );
152 
153  if ( this != &rhs )
154  {
156  }
157 
158  return *this;
159 }
160 
161 
163 {
165  return *this;
166 }
167 
168 
170  ) const
171 {
172  ASSERT( values != 0 );
173  if ( rowIdx >= getNumRows( ) )
174  {
176  return VariablesGrid();
177  }
178 
179  Grid tmpGrid;
180  getGrid( tmpGrid );
181 
182  VariablesGrid rowGrid( 1,tmpGrid,getType( ) );
183 
184  for( uint run1 = 0; run1 < getNumPoints(); run1++ )
185  rowGrid( run1,0 ) = values[run1]->operator()( rowIdx,0 );
186 
187  return rowGrid;
188 }
189 
190 
192  ) const
193 {
194  ASSERT( values != 0 );
195  if ( pointIdx >= getNumPoints( ) )
196  {
198  return VariablesGrid();
199  }
200 
201  VariablesGrid pointGrid;
202  pointGrid.addMatrix( *(values[pointIdx]),getTime( pointIdx ) );
203 
204  return pointGrid;
205 }
206 
207 
209 {
210  return MatrixVariablesGrid::init( );
211 }
212 
213 
215  const Grid& _grid,
216  VariableType _type,
217  const char** const _names,
218  const char** const _units,
219  const DVector* const _scaling,
220  const DVector* const _lb,
221  const DVector* const _ub,
222  const BooleanType* const _autoInit
223  )
224 {
225  return MatrixVariablesGrid::init( _dim,1,_grid,_type,_names,_units,_scaling,_lb,_ub,_autoInit );
226 }
227 
228 
230  uint _nPoints,
231  VariableType _type,
232  const char** const _names,
233  const char** const _units,
234  const DVector* const _scaling,
235  const DVector* const _lb,
236  const DVector* const _ub,
237  const BooleanType* const _autoInit
238  )
239 {
240  return MatrixVariablesGrid::init( _dim,1,_nPoints,_type,_names,_units,_scaling,_lb,_ub,_autoInit );
241 }
242 
243 
245  double _firstTime,
246  double _lastTime,
247  uint _nPoints,
248  VariableType _type,
249  const char** const _names,
250  const char** const _units,
251  const DVector* const _scaling,
252  const DVector* const _lb,
253  const DVector* const _ub,
254  const BooleanType* const _autoInit
255  )
256 {
257  return MatrixVariablesGrid::init( _dim,1,_firstTime,_lastTime,_nPoints,_type,_names,_units,_scaling,_lb,_ub,_autoInit );
258 }
259 
260 
262  const Grid& _grid,
263  VariableType _type
264  )
265 {
266  return MatrixVariablesGrid::init( DMatrix(arg),_grid,_type );
267 }
268 
269 
270 
272  double newTime
273  )
274 {
275  return MatrixVariablesGrid::addMatrix( DMatrix(newVector),newTime );
276 }
277 
278 
280  const DVector& _values
281  )
282 {
283  if ( pointIdx >= getNumPoints( ) )
285 
286  if ( _values.getDim( ) != getNumRows( pointIdx ) )
288 
289  for( uint j=0; j<getNumRows( ); ++j )
290  operator()( pointIdx,j ) = _values( j );
291 
292  return SUCCESSFUL_RETURN;
293 }
294 
295 
297  )
298 {
299  for( uint i = 0; i < getNumPoints(); i++ )
300  ACADO_TRY( setVector( i,_values ) );
301 
302  return SUCCESSFUL_RETURN;
303 }
304 
305 
307  ) const
308 {
309  if ( ( values == 0 ) || ( pointIdx >= getNumPoints() ) )
310  return emptyVector;
311 
312  return values[pointIdx]->getCol( 0 );
313 }
314 
315 
317 {
318  if ( getNumPoints( ) <= 0 )
319  return emptyVector;
320 
321  return getVector( 0 );
322 }
323 
324 
326 {
327  if ( getNumPoints( ) <= 0 )
328  return emptyVector;
329 
330  return getVector( getNumPoints( )-1 );
331 }
332 
333 
334 
336  )
337 {
338  Grid::shiftTimes( timeShift );
339  return *this;
340 }
341 
342 
344 {
345 
346  if( lastValue.isEmpty() == BT_FALSE ){
347 
348  DMatrix aux( lastValue.getDim(), 1 );
349  aux.setCol( 0, lastValue );
350 
352  return *this;
353  }
354 
355 
357  return *this;
358 }
359 
360 
361 
363  MergeMethod _mergeMethod
364  )
365 {
366  // nothing to do for empty grids
367  if ( arg.getNumPoints( ) == 0 )
368  return SUCCESSFUL_RETURN;
369 
370  if ( getNumPoints( ) == 0 )
371  {
372  *this = arg;
373  return SUCCESSFUL_RETURN;
374  }
375 
376  // consistency check
377  if ( acadoIsGreater( arg.getFirstTime( ),getLastTime( ) ) == BT_FALSE )
379 
380  if ( acadoIsEqual( getLastTime( ),arg.getFirstTime( ) ) == BT_FALSE )
381  {
382  // simply append
383  for( uint i=0; i<arg.getNumPoints( ); ++i )
384  addMatrix( *(arg.values[i]),arg.getTime( i ) );
385  }
386  else
387  {
388  // if last and first time point coincide, merge as specified
389  switch ( _mergeMethod )
390  {
391  case MM_KEEP:
392  break;
393 
394  case MM_REPLACE:
396  break;
397 
398  case MM_DUPLICATE:
399  addMatrix( *(arg.values[0]),arg.getTime( 0 ) );
400  break;
401  }
402 
403  // simply append all remaining points
404  for( uint i=1; i<arg.getNumPoints( ); ++i )
405  addMatrix( *(arg.values[i]),arg.getTime( i ) );
406  }
407 
408  return SUCCESSFUL_RETURN;
409 }
410 
411 
413  MergeMethod _mergeMethod
414  )
415 {
416  VariablesGrid tmp = arg;
417  return appendTimes( tmp,_mergeMethod );
418 }
419 
420 
422 {
423  // setup new grid if current grid is empty
424  if ( getNumPoints( ) == 0 )
425  {
426  *this = arg;
427  return SUCCESSFUL_RETURN;
428  }
429 
430  if ( getNumPoints( ) != arg.getNumPoints( ) )
432 
433  DVector tmp1,tmp2;
434 
435  for( uint i=0; i<getNumPoints(); ++i )
436  {
437  values[i]->appendRows( *(arg.values[i]) );
438  values[i]->appendSettings( *(arg.values[i]) );
439  }
440 
441  return SUCCESSFUL_RETURN;
442 }
443 
444 
445 // uses a simple O(n^2) algorithm for sorting
447  MergeMethod _mergeMethod,
448  BooleanType keepOverlap
449  )
450 {
451  if ( ( keepOverlap == BT_FALSE ) && ( _mergeMethod == MM_DUPLICATE ) )
453 
454  // nothing to do if arg or object itself is empty
455  if ( arg.getNumPoints( ) == 0 )
456  return SUCCESSFUL_RETURN;
457 
458  if ( getNumPoints( ) == 0 )
459  {
460  *this = arg;
461  return SUCCESSFUL_RETURN;
462  }
463 
464  // use append if grids do not overlap
465  if ( acadoIsSmaller( getLastTime( ),arg.getFirstTime( ) ) == BT_TRUE )
466  return appendTimes( arg,_mergeMethod );
467 
468 
469  // construct merged grid
470  VariablesGrid mergedGrid;
471  uint j = 0;
472  BooleanType overlapping = BT_FALSE;
473 
474  for( uint i=0; i<getNumPoints( ); ++i )
475  {
476  if ( keepOverlap == BT_FALSE )
477  overlapping = arg.isInInterval( getTime(i) );
478 
479  // add all grid points of argument grid that are smaller
480  // then current one of original grid
481  while ( ( j < arg.getNumPoints( ) ) &&
482  ( acadoIsStrictlySmaller( arg.getTime( j ),getTime( i ) ) == BT_TRUE ) )
483  {
484  if ( ( overlapping == BT_FALSE ) ||
485  ( ( overlapping == BT_TRUE ) && ( _mergeMethod == MM_REPLACE ) ) )
486  {
487  mergedGrid.addMatrix( *(arg.values[j]),arg.getTime( j ) );
488  }
489 
490  ++j;
491  }
492 
493  // merge current grid points if they are at equal times
494  if ( acadoIsEqual( arg.getTime( j ),getTime( i ) ) == BT_TRUE )
495  {
496  switch ( _mergeMethod )
497  {
498  case MM_KEEP:
499  mergedGrid.addMatrix( *(values[i]),getTime( i ) );
500  break;
501 
502  case MM_REPLACE:
503  mergedGrid.addMatrix( *(arg.values[j]),arg.getTime( j ) );
504  break;
505 
506  case MM_DUPLICATE:
507  mergedGrid.addMatrix( *(values[i]),getTime( i ) );
508  mergedGrid.addMatrix( *(arg.values[j]),arg.getTime( j ) );
509  break;
510  }
511  ++j;
512  }
513  else
514  {
515  // add current grid point of original grid
516  if ( ( overlapping == BT_FALSE ) ||
517  ( ( overlapping == BT_TRUE ) && ( _mergeMethod == MM_KEEP ) ) )
518  {
519  mergedGrid.addMatrix( *(values[i]),getTime( i ) );//arg.
520  }
521  }
522  }
523 
524  // add all remaining grid points of argument grid
525  while ( j < arg.getNumPoints( ) )
526  {
527  if ( acadoIsStrictlyGreater( arg.getTime(j),getLastTime() ) == BT_TRUE )
528  mergedGrid.addMatrix( *(arg.values[j]),arg.getTime( j ) );
529 
530  ++j;
531  }
532 
533  // merged grid becomes current grid
534  *this = mergedGrid;
535 
536  return SUCCESSFUL_RETURN;
537 }
538 
539 
540 
541 
543  uint endIdx
544  ) const
545 {
546  VariablesGrid newVariablesGrid;
547 
548  if ( ( startIdx >= getNumPoints( ) ) || ( endIdx >= getNumPoints( ) ) )
549  return newVariablesGrid;
550 
551  if ( startIdx > endIdx )
552  return newVariablesGrid;
553 
554  for( uint i=startIdx; i<=endIdx; ++i )
555  newVariablesGrid.addMatrix( *(values[i]),getTime( i ) );
556 
557  return newVariablesGrid;
558 }
559 
560 
562  double endTime
563  ) const
564 {
565  uint startIdx = getCeilIndex( startTime );
566  uint endIdx = getFloorIndex( endTime );
567 
568  VariablesGrid newVariablesGrid;
569 
570  if ( ( isInInterval( startTime ) == BT_FALSE ) || ( isInInterval( endTime ) == BT_FALSE ) )
571  return newVariablesGrid;
572 
573  if ( ( startIdx >= getNumPoints( ) ) || ( endIdx >= getNumPoints( ) ) )
574  return newVariablesGrid;
575 
576 // if ( startIdx > endIdx )
577 // return newVariablesGrid;
578 
579  // add all matrices in interval (constant interpolation)
580  if ( ( hasTime( startTime ) == BT_FALSE ) && ( startIdx > 0 ) )
581  newVariablesGrid.addMatrix( *(values[ startIdx-1 ]),startTime );
582 
583  for( uint i=startIdx; i<=endIdx; ++i )
584  newVariablesGrid.addMatrix( *(values[i]),getTime( i ) );
585 
586  if ( hasTime( endTime ) == BT_FALSE )
587  newVariablesGrid.addMatrix( *(values[ endIdx ]),endTime );
588 
589  return newVariablesGrid;
590 }
591 
592 
594  uint endIdx
595  ) const
596 {
597  VariablesGrid newVariablesGrid;
598 
599  if ( ( startIdx >= getNumValues( ) ) || ( endIdx >= getNumValues( ) ) )
600  return newVariablesGrid;
601 
602  if ( startIdx > endIdx )
603  return newVariablesGrid;
604 
605  for( uint i=0; i<getNumPoints( ); ++i )
606  newVariablesGrid.addMatrix( values[i]->getRows( startIdx,endIdx ),getTime( i ) );
607 
608  return newVariablesGrid;
609 }
610 
611 
612 
614  ) const
615 {
616  sum.setZero();
617 
618  for( uint i=0; i<getNumPoints( ); ++i )
619  sum += getVector( i );
620 
621  return SUCCESSFUL_RETURN;
622 }
623 
624 
626  DVector& value
627  ) const
628 {
629  value.setZero();
630 
631  switch( mode )
632  {
633  case IM_CONSTANT:
634  for( uint i=0; i<getNumIntervals( ); ++i )
635  {
636  for( uint j=0; j<getNumValues( ); ++j )
637  {
638  //value(j) += ( getIntervalLength( i ) / getIntervalLength( ) ) * operator()( i,j );
639  value(j) += getIntervalLength( i ) * operator()( i,j );
640  }
641  }
642  break;
643 
644  case IM_LINEAR:
645  for( uint i=0; i<getNumIntervals( ); ++i )
646  {
647  for( uint j=0; j<getNumValues( ); ++j )
648  {
649  //value(j) += ( getIntervalLength( i ) / getIntervalLength( ) ) * ( operator()( i,j ) + operator()( i+1,j ) ) / 2.0;
650  value(j) += getIntervalLength( i ) * ( operator()( i,j ) + operator()( i+1,j ) ) / 2.0;
651  }
652  }
653  break;
654 
655  default:
657  }
658 
659  return SUCCESSFUL_RETURN;
660 }
661 
662 //
663 // PROTECTED MEMBER FUNCTIONS:
664 //
665 
667 {
668  uint run1, run2;
669 
670  for( run1 = 0; run1 < getNumPoints(); run1++ ){
671  for( run2 = 0; run2 < getNumValues(); run2++ ){
672 
673  if( fabs( getLowerBound(run1,run2) ) < 0.999*INFTY &&
674  fabs( getUpperBound(run1,run2) ) < 0.999*INFTY ){
675 
676  operator()(run1,run2) = 0.5*( getLowerBound(run1,run2) + getUpperBound(run1,run2) );
677 
678  }
679 
680  if( fabs( getLowerBound(run1,run2) ) >= 0.999*INFTY &&
681  fabs( getUpperBound(run1,run2) ) < 0.999*INFTY ){
682 
683  operator()(run1,run2) = getUpperBound(run1,run2);
684  }
685 
686  if( fabs( getLowerBound(run1,run2) ) < 0.999*INFTY &&
687  fabs( getUpperBound(run1,run2) ) >= 0.999*INFTY ){
688 
689  operator()(run1,run2) = getLowerBound(run1,run2);
690  }
691 
692  if( fabs( getLowerBound(run1,run2) ) >= 0.999*INFTY &&
693  fabs( getUpperBound(run1,run2) ) >= 0.999*INFTY ){
694 
695  operator()(run1,run2) = 0.0;
696  }
697  }
698  }
699 
700  return SUCCESSFUL_RETURN;
701 }
702 
703 VariablesGrid::operator DMatrix() const
704 {
705  DMatrix tmp(getNumPoints( ), getNumValues( ) + 1);
706 
707  for (uint run1 = 0; run1 < getNumPoints(); ++run1)
708  {
709  tmp(run1, 0) = getTime(run1);
710 
711  for (uint run2 = 0; run2 < getNumValues(); ++run2)
712  tmp(run1, 1 + run2) = operator()(run1, run2);
713  }
714 
715  return tmp;
716 }
717 
718 
720 
721 
722 /*
723  * end of file
724  */
VariablesGrid & shiftTimes(double timeShift)
GenericVector< T > getCol(unsigned _idx) const
Definition: matrix.hpp:205
returnValue setTime(double _time)
Definition: grid.cpp:307
returnValue appendSettings(const VariableSettings &rhs)
VariablesGrid & shiftBackwards(DVector lastValue=emptyVector)
VariablesGrid & operator=(const VariablesGrid &rhs)
returnValue addMatrix(const DMatrix &newMatrix, double newTime=-INFTY)
double getTime(uint pointIdx) const
double getFirstTime() const
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
const double INFTY
uint getCeilIndex(double time) const
Definition: grid.cpp:625
GenericMatrix & setCol(unsigned _idx, const GenericVector< T > &_arg)
Definition: matrix.hpp:224
Provides a time grid consisting of vector-valued optimization variables at each grid point...
returnValue getSum(DVector &sum) const
Allows to pass back messages to the calling function.
VariablesGrid getValuesSubGrid(uint startIdx, uint endIdx) const
double getLowerBound(uint pointIdx, uint valueIdx) const
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
GenericMatrix & appendRows(const GenericMatrix &_arg)
Definition: matrix.cpp:62
uint getLastIndex() const
BooleanType acadoIsGreater(double x, double y, double TOL)
#define CLOSE_NAMESPACE_ACADO
double getUpperBound(uint pointIdx, uint valueIdx) const
bool isEmpty() const
Definition: vector.hpp:176
BooleanType isInInterval(double _time) const
GenericMatrix< double > DMatrix
Definition: matrix.hpp:457
VariableType
Definition: acado_types.hpp:95
MatrixVariablesGrid & shiftBackwards(DMatrix lastValue=emptyMatrix)
uint getFloorIndex(double time) const
Definition: grid.cpp:593
#define ACADO_TRY(X)
BooleanType acadoIsStrictlySmaller(double x, double y, double TOL)
returnValue appendValues(const VariablesGrid &arg)
double getIntervalLength() const
VariableType getType() const
returnValue merge(const VariablesGrid &arg, MergeMethod _mergeMethod=MM_DUPLICATE, BooleanType keepOverlap=BT_TRUE)
returnValue setVector(uint pointIdx, const DVector &_values)
Provides a time grid consisting of matrix-valued optimization variables at each grid point...
returnValue addVector(const DVector &newVector, double newTime=-INFTY)
unsigned getDim() const
Definition: vector.hpp:172
DVector getFirstVector() const
returnValue getGrid(Grid &_grid) const
returnValue setAllVectors(const DVector &_values)
DVector getLastVector() const
VariablesGrid getTimeSubGrid(uint startIdx, uint endIdx) const
returnValue init()
Derived & setZero(Index size)
BooleanType acadoIsSmaller(double x, double y, double TOL)
returnValue appendTimes(const VariablesGrid &arg, MergeMethod _mergeMethod=MM_DUPLICATE)
void rhs(const real_t *x, real_t *f)
unsigned getNumRows() const
Definition: matrix.hpp:185
MatrixVariable getRows(uint startIdx, uint endIdx) const
#define ASSERT(x)
#define BT_TRUE
Definition: acado_types.hpp:47
Grid & shiftTimes(double timeShift)
Definition: grid.cpp:449
unsigned getNumCols() const
Definition: matrix.hpp:189
uint getNumIntervals() const
VariablesGrid operator[](const uint pointIdx) const
MatrixVariablesGrid & operator=(const MatrixVariablesGrid &rhs)
uint getNumPoints() const
returnValue initializeFromBounds()
DVector getVector(uint pointIdx) const
BooleanType hasTime(double _time) const
Definition: grid.cpp:520
double getLastTime() const
static DVector emptyVector
Definition: vector.hpp:335
#define BEGIN_NAMESPACE_ACADO
#define BT_FALSE
Definition: acado_types.hpp:49
double & operator()(uint pointIdx, uint rowIdx)
MergeMethod
BooleanType acadoIsStrictlyGreater(double x, double y, double TOL)
returnValue getIntegral(InterpolationMode mode, DVector &value) const
#define ACADOERROR(retval)
InterpolationMode


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