optimization_algorithm_base.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 
35 #include <acado/ocp/ocp.hpp>
36 
37 using namespace std;
38 
40 
41 
42 
43 //
44 // PUBLIC MEMBER FUNCTIONS:
45 //
46 
47 
49 {
50  ocp = 0;
51  nlpSolver = 0;
52 
53  userInit.allocateAll( );
54 }
55 
56 
58 {
59  ocp = new OCP(ocp_);
60  nlpSolver = 0;
61 
62  userInit.allocateAll( );
63 }
64 
65 
67 {
68  if( arg.ocp != 0 ) ocp = new OCP(*arg.ocp) ;
69  else ocp = 0 ;
70 
71  if( arg.nlpSolver != 0 ) nlpSolver = arg.nlpSolver->clone() ;
72  else nlpSolver = 0 ;
73 
74  iter = arg.iter;
75  userInit = arg.userInit;
76 }
77 
78 
80 {
81  clear( );
82 }
83 
84 
85 
87 
88  if( this != &arg )
89  {
90  clear( );
91 
92  if( arg.ocp != 0 ) ocp = new OCP(*arg.ocp) ;
93  else ocp = 0 ;
94 
95  if( arg.nlpSolver != 0 ) nlpSolver = arg.nlpSolver->clone() ;
96  else nlpSolver = 0 ;
97 
98  iter = arg.iter;
99  userInit = arg.userInit;
100  }
101  return *this;
102 }
103 
104 
105 
107 {
108  VariablesGrid tmp;
109  tmp.read( fileName );
110 
111  if ( tmp.isEmpty() == BT_TRUE )
113 
114  return initializeDifferentialStates(tmp,autoinit);
115 }
116 
117 
119 {
120  VariablesGrid tmp;
121  tmp.read( fileName );
122 
123  if ( tmp.isEmpty() == BT_TRUE )
125 
126  return initializeAlgebraicStates(tmp,autoinit);
127 }
128 
129 
131 {
132  VariablesGrid tmp;
133  tmp.read( fileName );
134 
135  if ( tmp.isEmpty() == BT_TRUE )
137 
138  return initializeParameters(tmp);
139 }
140 
141 
143 {
144  VariablesGrid tmp;
145  tmp.read( fileName );
146 
147  if ( tmp.isEmpty() == BT_TRUE )
149 
150  return initializeControls(tmp);
151 }
152 
153 
155 {
156  VariablesGrid tmp;
157  tmp.read( fileName );
158 
159  if ( tmp.isEmpty() == BT_TRUE )
161 
162  return initializeDisturbances(tmp);
163 }
164 
165 
166 
168 
169  if ( userInit.x != 0 ) delete userInit.x;
170  userInit.x = new VariablesGrid( xd_init_ );
171  if (autoinit == BT_TRUE)
172  userInit.x->enableAutoInit();
173  if (autoinit == BT_FALSE)
174  userInit.x->disableAutoInit();
175 
176  return SUCCESSFUL_RETURN;
177 }
178 
179 
181  if( userInit.x != 0 ) userInit.x ->enableAutoInit();
182  if( userInit.xa != 0 ) userInit.xa->enableAutoInit();
183  return SUCCESSFUL_RETURN;
184 }
185 
186 
188 
189  if ( userInit.xa != 0 ) delete userInit.xa;
190  userInit.xa = new VariablesGrid( xa_init_ );
191  if (autoinit == BT_TRUE)
192  userInit.xa->enableAutoInit();
193  if (autoinit == BT_FALSE){
194 
195  userInit.xa->disableAutoInit();
196  }
197  return SUCCESSFUL_RETURN;
198 }
199 
200 
202 
203  if ( userInit.p != 0 ) delete userInit.p;
204  userInit.p = new VariablesGrid( p_init_ );
205  return SUCCESSFUL_RETURN;
206 }
207 
208 
210 
211  if ( userInit.u != 0 ) delete userInit.u;
212  userInit.u = new VariablesGrid( u_init_ );
213  return SUCCESSFUL_RETURN;
214 }
215 
216 
218 
219  if ( userInit.w != 0 ) delete userInit.w;
220  userInit.w = new VariablesGrid( w_init_ );
221  return SUCCESSFUL_RETURN;
222 }
223 
224 
226 
227  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
228  return nlpSolver->getDifferentialStates( xd_ );
229 }
230 
231 
233 
234  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
235  return nlpSolver->getAlgebraicStates( xa_ );
236 }
237 
238 
240 {
241  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
242  return nlpSolver->getParameters( p_ );
243 }
244 
245 
247 {
248  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
249 
250  VariablesGrid tmp;
251 
252  returnValue returnvalue = nlpSolver->getParameters( tmp );
253  if ( returnvalue != SUCCESSFUL_RETURN )
254  return returnvalue;
255 
256  p_ = tmp.getVector( 0 );
257 
258  return SUCCESSFUL_RETURN;
259 }
260 
261 
263 
264  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
265  return nlpSolver->getControls( u_ );
266 }
267 
268 
270 
271  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
272  return nlpSolver->getDisturbances( w_ );
273 }
274 
275 
277 
278  returnValue returnvalue;
279  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
280 
281  VariablesGrid xx;
282  returnvalue = nlpSolver->getDifferentialStates( xx );
283  if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue;
284 
285  xx.print( fileName );
286 
287  return SUCCESSFUL_RETURN;
288 }
289 
290 
292 
293  returnValue returnvalue;
294  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
295 
296  VariablesGrid xx;
297  returnvalue = nlpSolver->getAlgebraicStates( xx );
298  if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue;
299 
300  xx.print( fileName );
301 
302  return SUCCESSFUL_RETURN;
303 }
304 
305 
307 
308  returnValue returnvalue;
309  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
310 
311  VariablesGrid xx;
312  returnvalue = nlpSolver->getParameters( xx );
313  if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue;
314 
315  xx.print( fileName );
316 
317  return SUCCESSFUL_RETURN;
318 }
319 
320 
322 
323  returnValue returnvalue;
324  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
325 
326  VariablesGrid xx;
327  returnvalue = nlpSolver->getControls( xx );
328  if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue;
329 
330  xx.print( fileName );
331 
332  return SUCCESSFUL_RETURN;
333 }
334 
335 
337 
338  returnValue returnvalue;
339  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
340 
341  VariablesGrid xx;
342  returnvalue = nlpSolver->getDisturbances( xx );
343  if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue;
344 
345  xx.print( fileName );
346 
347  return SUCCESSFUL_RETURN;
348 }
349 
350 
351 double OptimizationAlgorithmBase::getObjectiveValue( const char* fileName ) const
352 {
353  ofstream stream( fileName );
354  stream << scientific << getObjectiveValue();
355  stream.close();
356 
357  return SUCCESSFUL_RETURN;
358 }
359 
360 
362 
363  if( nlpSolver == 0 ) return ACADOWARNING( RET_MEMBER_NOT_INITIALISED );
364  return nlpSolver->getObjectiveValue();
365 }
366 
367 
368 
370  ) const
371 {
372  return nlpSolver->getSensitivitiesX( _sens );
373 }
374 
375 
377  ) const
378 {
379  return nlpSolver->getSensitivitiesXA( _sens );
380 }
381 
383  ) const
384 {
385  return nlpSolver->getSensitivitiesP( _sens );
386 }
387 
388 
390  ) const
391 {
392  return nlpSolver->getSensitivitiesU( _sens );
393 }
394 
395 
397  ) const
398 {
399  return nlpSolver->getSensitivitiesW( _sens );
400 }
401 
402 
403 
405 {
406  return iter.getNX( );
407 }
408 
409 
411 {
412  return iter.getNXA( );
413 }
414 
415 
417 {
418  return iter.getNP( );
419 }
420 
421 
423 {
424  return iter.getNU( );
425 }
426 
427 
429 {
430  return iter.getNW( );
431 }
432 
433 
435 {
436  if ( ocp != 0 )
437  return ocp->getStartTime( );
438  else
439  return -INFTY;
440 }
441 
442 
444 {
445  if ( ocp != 0 )
446  return ocp->getEndTime( );
447  else
448  return -INFTY;
449 }
450 
451 
452 
453 //
454 // PROTECTED MEMBER FUNCTIONS:
455 //
456 
458 {
459  if ( ocp != 0 )
460  {
461  delete ocp;
462  ocp = 0;
463  }
464 
465  if ( nlpSolver != 0 )
466  {
467  delete nlpSolver;
468  nlpSolver = 0;
469  }
470 
471  iter.clear( );
472 
473  userInit.clear( );
474  userInit.allocateAll( );
475 
476  return SUCCESSFUL_RETURN;
477 }
478 
479 
481  )
482 {
483  // EXTRACT INFORMATION PACKED IN THE DATA WRAPPER OCP:
484  // ---------------------------------------------------
485  Objective *objective ;
486  DifferentialEquation **differentialEquation;
487  Constraint *constraint ;
488 
489  Grid unionGrid;
490 
491  if ( extractOCPdata( &objective,&differentialEquation,&constraint,
492  unionGrid
493  ) != SUCCESSFUL_RETURN )
495 
496  // REFORMULATE THE OBJECTIVE IF NECESSARY:
497  // ---------------------------------------
498 
499  if ( setupObjective( objective,differentialEquation,constraint,
500  unionGrid
501  ) != SUCCESSFUL_RETURN )
503 
504  // REFORMULATE THE CONSTRAINT IF NECESSARY:
505  // ----------------------------------------
506 
507  if ( setupDifferentialEquation( objective,differentialEquation,constraint,
508  unionGrid
509  ) != SUCCESSFUL_RETURN )
511 
512  // DISCRETIZE THE DIFFERENTIAL EQUATION IF NECESSARY:
513  // --------------------------------------------------
514 
515  DynamicDiscretization* dynamicDiscretization = 0;
516 
517  if ( setupDynamicDiscretization( _userIteraction,
518  objective,differentialEquation,constraint,
519  unionGrid,
520  &dynamicDiscretization
521  ) != SUCCESSFUL_RETURN )
523 
524  // SETUP OF THE NLP SOLVER:
525  // ------------------------
526  if ( allocateNlpSolver( objective,dynamicDiscretization,constraint ) != SUCCESSFUL_RETURN )
528 
529  // DETERMINE THE DIMENSIONS OF THE OPTIMIZATION VARIABLES:
530  // -------------------------------------------------------
531 
532  uint nx = 0;
533  uint nxa = 0;
534  uint np = 0;
535  uint nu = 0;
536  uint nw = 0;
537 
538  if ( determineDimensions( objective,differentialEquation,constraint, nx,nxa,np,nu,nw ) != SUCCESSFUL_RETURN )
539  {
540  if( differentialEquation != 0 ) delete differentialEquation[0];
541 
542  if( objective != 0 ) delete objective ;
543  if( differentialEquation != 0 ) delete[] differentialEquation ;
544  if( constraint != 0 ) delete constraint ;
545  if( dynamicDiscretization != 0 ) delete dynamicDiscretization;
546 
548  }
549 
550  if ( initializeOCPiterate( constraint,unionGrid,nx,nxa,np,nu,nw ) != SUCCESSFUL_RETURN )
551  {
552  if( differentialEquation != 0 ) delete differentialEquation[0];
553 
554  if( objective != 0 ) delete objective ;
555  if( differentialEquation != 0 ) delete[] differentialEquation ;
556  if( constraint != 0 ) delete constraint ;
557  if( dynamicDiscretization != 0 ) delete dynamicDiscretization;
558 
560  }
561 
562  // ELIMINATE EQUALITY BOUNDS: ??
563  // --------------------------
564 
565  // changes the dimensions again !
566 
567 
568  // DEFINE MULTIPLE SHOOTING NOTES
569  // OR COLLOCATION NOTES IF REQUESTED:
570  // ----------------------------------
571 
572  // ADAPT INITIALIZATION OF X AND XA ?
573 
574  if( iter.p != 0 ) iter.p ->disableAutoInit();
575  if( iter.u != 0 ) iter.u ->disableAutoInit();
576  if( iter.w != 0 ) iter.w ->disableAutoInit();
577 
578 
579  // (COLLOCATION NOT IMPLEMENTED YET)
580 
581 // printf("before!!!\n");
582 // iter.print();
583 
584  // INITIALIZE THE NLP-ALGORITHM:
585  // -----------------------------
586  if ( initializeNlpSolver( iter ) != SUCCESSFUL_RETURN )
588 
589 // printf("after!!!\n");
590 // iter.print();
591 
592  // GIVE THE TEMPORARY MEMORY FREE:
593  // -------------------------------
594  if( differentialEquation != 0 ) delete differentialEquation[0];
595 
596  if( objective != 0 ) delete objective ;
597  if( differentialEquation != 0 ) delete[] differentialEquation ;
598  if( constraint != 0 ) delete constraint ;
599  if( dynamicDiscretization != 0 ) delete dynamicDiscretization;
600 
601  return SUCCESSFUL_RETURN;
602 }
603 
604 
607  Constraint *H
608  ) const
609 {
610 
611  // CONVEXITY DETECTION DOES NOT WORK FOR /examples/michaelis_menten.txt
612  // AUTOMATIC CONVEXITY DETECTION IS DISABLED AT THE MOMENT.
613  // (PLEASE FIX !!!).
614 
615 // return BT_FALSE;
616 
617  // ENABLES AGAIN, BUT MENTIONED BUG IS PROBABLY STILL NOT FIXED
618 
619  if( F != 0 ) if ( F->isQuadratic( ) == BT_FALSE ) return BT_FALSE;
620  if( G != 0 ) if ( G->isAffine ( ) == BT_FALSE ) return BT_FALSE;
621  if( H != 0 ) if ( H->isAffine ( ) == BT_FALSE ) return BT_FALSE;
622  return BT_TRUE;
623 }
624 
625 
627  DifferentialEquation*** differentialEquation,
628  Constraint** constraint,
629  Grid& unionGrid
630  )
631 {
632 
633  returnValue returnvalue;
634 
635  // EXTRACT INFORMATION PACKED IN THE DATA WRAPPER OCP:
636  // ---------------------------------------------------
637 
638  if( ocp->hasObjective() == BT_TRUE ) *objective = new Objective( );
639  else return ACADOERROR( RET_NO_VALID_OBJECTIVE );
640 
641  if( ocp->hasDifferentialEquation() == BT_TRUE ){
642  *differentialEquation = new DifferentialEquation*[1];
643  }
644  else *differentialEquation = 0;
645 
646  if( ocp->hasConstraint() == BT_TRUE )
647  {
648  *constraint = new Constraint( );
649  ocp->getConstraint( **constraint );
650  }
651  else
652  {
653  // CONSTRUCT A CONSTRAINT IF NOT ALLOCATED YET:
654  // --------------------------------------------
655  *constraint = new Constraint(); constraint[0]->init( unionGrid );
656  }
657 
658  ocp->getObjective( **objective );
659  returnvalue = initializeObjective( *objective );
660 
661  if( returnvalue != SUCCESSFUL_RETURN ) return ACADOERROR(returnvalue);
662 
663  if( ocp->hasDifferentialEquation() == BT_TRUE ){
664 
665  differentialEquation[0][0] = new DifferentialEquation();
666  ocp->getModel( *differentialEquation[0][0] );
667  }
668 
669 
670  // OBTAIN THE UNION GRID:
671  // -------------------------
672  ocp->getGrid( unionGrid );
673 
674  return SUCCESSFUL_RETURN;
675 }
676 
677 
679  DifferentialEquation** differentialEquation,
680  Constraint* constraint,
681  Grid unionGrid
682  )
683 {
684  // note that even if the differentialEquation == 0
685  // is satisfied it might still be that the objective
686  // allocates a differentialEquation (e.g. for the case
687  // that the objective is an integral expression.)
688  return objective->init( 1,0,differentialEquation,0,constraint );
689 }
690 
691 
693  DifferentialEquation** differentialEquation,
694  Constraint* constraint,
695  Grid unionGrid
696  )
697 {
698  if( differentialEquation != 0 ){
699  if( differentialEquation[0]->getNumAlgebraicEquations() != 0 ){
700  constraint->add( unionGrid.getNumPoints(), *differentialEquation[0] );
701  }
702  }
703 
704  return SUCCESSFUL_RETURN;
705 }
706 
707 
709  Objective* objective,
710  DifferentialEquation** differentialEquation,
711  Constraint* constraint,
712  Grid unionGrid,
713  DynamicDiscretization** dynamicDiscretization
714  )
715 {
716 
717  if( differentialEquation != 0 ){
718 
719  *dynamicDiscretization = new ShootingMethod( _userIteraction );
720 
721  int intType;
722  _userIteraction->get( INTEGRATOR_TYPE, intType );
723 
724  if( differentialEquation[0]->getNumAlgebraicEquations() != 0 ) intType = (int) INT_BDF ;
725  if( differentialEquation[0]->isImplicit() == BT_TRUE ) intType = (int) INT_BDF ;
726  if( differentialEquation[0]->isDiscretized() == BT_TRUE ) intType = (int) INT_DISCRETE;
727 
728  int sensType;
729  (*dynamicDiscretization)->get( DYNAMIC_SENSITIVITY, sensType );
730 
731  if( differentialEquation[0]->isSymbolic() == BT_FALSE && sensType == (int) BACKWARD_SENSITIVITY ){
732  (*dynamicDiscretization)->set( DYNAMIC_SENSITIVITY, FORWARD_SENSITIVITY );
733  }
734 
735  (*dynamicDiscretization)->addStage( *differentialEquation[0],
736  unionGrid,
737  (IntegratorType)intType );
738  }
739 
740  return SUCCESSFUL_RETURN;
741 }
742 
743 
745  DifferentialEquation** const _differentialEquation,
746  Constraint* const _constraint,
747  uint& _nx,
748  uint& _nxa,
749  uint& _np,
750  uint& _nu,
751  uint& _nw
752  ) const
753 {
754  if( _objective != 0 )
755  {
756  _nx = acadoMax( _objective->getNX (), _nx );
757  _nxa = acadoMax( _objective->getNXA(), _nxa );
758  _np = acadoMax( _objective->getNP (), _np );
759  _nu = acadoMax( _objective->getNU (), _nu );
760  _nw = acadoMax( _objective->getNW (), _nw );
761  }
762  if( _differentialEquation != 0 )
763  {
764  _nx = acadoMax( _differentialEquation[0]->getNX() , _nx );
765  _nxa = acadoMax( _differentialEquation[0]->getNXA(), _nxa );
766  _np = acadoMax( _differentialEquation[0]->getNP (), _np );
767  _nu = acadoMax( _differentialEquation[0]->getNU (), _nu );
768  _nw = acadoMax( _differentialEquation[0]->getNW (), _nw );
769  }
770  if( _constraint != 0 )
771  {
772  _nx = acadoMax( _constraint->getNX (), _nx );
773  _nxa = acadoMax( _constraint->getNXA(), _nxa );
774  _np = acadoMax( _constraint->getNP (), _np );
775  _nu = acadoMax( _constraint->getNU (), _nu );
776  _nw = acadoMax( _constraint->getNW (), _nw );
777  }
778 
779  if( _differentialEquation == 0 )
780  {
781  if( _nx > 0 )
783  }
784  else
785  {
786  _nx = _differentialEquation[0]->getNumDynamicEquations();
787 // if( nxa != (uint) differentialEquation[0]->getNumAlgebraicEquations()){
788 //
789 // for( run1 = 0; run1 < numberOfStages; run1++ )
790 // if( differentialEquation != 0 ) delete differentialEquation[run1];
791 //
792 // if( objective != 0 ) delete objective ;
793 // if( differentialEquation != 0 ) delete[] differentialEquation ;
794 // if( transitions != 0 ) delete[] transitions ;
795 // if( positions != 0 ) delete[] positions ;
796 // if( constraint != 0 ) delete constraint ;
797 // if( dynamicDiscretization != 0 ) delete dynamicDiscretization;
798 //
799 // return ACADOERROR(RET_INCOMPATIBLE_DIMENSIONS);
800 // }
801  }
802 
803  return SUCCESSFUL_RETURN;
804 }
805 
806 
807 
809  const Grid& _unionGrid,
810  uint nx,
811  uint nxa,
812  uint np,
813  uint nu,
814  uint nw
815  )
816 {
817 
818  uint run1, run2;
819 
820  // CONSTRUCT THE OPTIMIZATION VARIABLES:
821  // -------------------------------------
822 
823  iter.clear( );
824 
825  if( nx > 0 ) iter.x = new VariablesGrid( nx , _unionGrid, VT_DIFFERENTIAL_STATE );
826  if( nxa > 0 ) iter.xa = new VariablesGrid( nxa, _unionGrid, VT_ALGEBRAIC_STATE );
827  if( np > 0 ) iter.p = new VariablesGrid( np , _unionGrid, VT_PARAMETER );
828  if( nu > 0 ) iter.u = new VariablesGrid( nu , _unionGrid, VT_CONTROL );
829  if( nw > 0 ) iter.w = new VariablesGrid( nw , _unionGrid, VT_DISTURBANCE );
830 
831  // CHECK OF BOUND CONSISTENCY:
832  // ---------------------------
833 
834  if( _constraint != 0 ){
835 
836  // the following routine initializes the bounds of the optimization
837  // variables. If no bound is given, a lower bound will be initialized
838  // with -INFTY while upper bounds are initialized with +INFTY. Note
839  // that the class Constraint auto-detects bounds. Moreover, if there
840  // are inconsistent bounds detected, i.e. if there is an upper bound
841  // smaller that the corresponding lower bound, the routine
842  // getBounds(...) will return with a corresponding error message which
843  // should be captured and returned to the user.
844 
845  returnValue returnvalue = _constraint->getBounds( iter );
846  if( returnvalue != SUCCESSFUL_RETURN ){
847  return ACADOERROR(returnvalue);
848  }
849  }
850 
851  // LOAD A ROUGH INITIALIZATION FOR ALL VARIABLES FOR THE CASE
852  // THAT NO FURTHER INITIALIZATION IS AVAILABLE:
853  // ----------------------------------------------------------
854 
855  if( iter.x != 0 ) iter.x ->initializeFromBounds(); // inititializes the variables with 0 if no bounds are
856  if( iter.xa != 0 ) iter.xa->initializeFromBounds(); // given. if there is one bound specified this bound
857  if( iter.p != 0 ) iter.p ->initializeFromBounds(); // will be used as an initialization. If there is an
858  if( iter.u != 0 ) iter.u ->initializeFromBounds(); // upper as well as a lower bound specified, then the
859  if( iter.w != 0 ) iter.w ->initializeFromBounds(); // average, i.e. (lb+ub)/2, will be used as an initialization.
860 
861 // printf("OptimizAlg::1 nx = %d \n", nx );
862 // printf("OptimizAlg::1 np = %d \n", np );
863 // printf("OptimizAlg::1 nu = %d \n", nu );
864 
865  // LOAD THE GIVEN INITIALIZATION:
866  // ------------------------------
867 
868  // For the case that the user provides an initialization for one or
869  // the other variable this additional information from the user should
870  // be taken into account. The following routine reads the user data in a
871  // robust way. (Even if the user provides data with wrong dimensions
872  // the code tries to use it as reasonable as possible.)
873 
874  // Added: Copy auto_initialize-values of user initialization
875 
876 // iter.x->print("iter.x");
877 // userInit.x->print("userInit.x");
878 
879  BooleanType ai;
880  if( nx > 0 && userInit.x->getNumPoints() > 0 ){
881  ai=userInit.x->getAutoInit(0);
882  for( run1 = 0; run1 < _unionGrid.getNumPoints(); run1++ ){
883  DVector tmp = userInit.x->linearInterpolation( _unionGrid.getTime(run1) );
884  uint nxx = tmp.getDim();
885  if( nxx > nx ) nxx = nx;
886  for( run2 = 0; run2 < nxx; run2++ )
887  iter.x->operator()( run1, run2 ) = tmp(run2);
888  iter.x->setAutoInit(run1,ai);
889  }
890  }
891 
892  if( nxa > 0 && userInit.xa->getNumPoints() > 0 ){
893  ai=userInit.xa->getAutoInit(0);
894  for( run1 = 0; run1 < _unionGrid.getNumPoints(); run1++ ){
895  DVector tmp = userInit.xa->linearInterpolation( _unionGrid.getTime(run1) );
896  uint nxx = tmp.getDim();
897  if( nxx > nxa ) nxx = nxa;
898  for( run2 = 0; run2 < nxx; run2++ )
899  iter.xa->operator()( run1, run2 ) = tmp(run2);
900  iter.xa->setAutoInit(run1,ai);
901  }
902  }
903 
904  if( nu > 0 && userInit.u->getNumPoints() > 0 ){
905  for( run1 = 0; run1 < _unionGrid.getNumPoints(); run1++ ){
906  DVector tmp = userInit.u->linearInterpolation( _unionGrid.getTime(run1) );
907  uint nxx = tmp.getDim();
908  if( nxx > nu ) nxx = nu;
909  for( run2 = 0; run2 < nxx; run2++ )
910  iter.u->operator()( run1, run2 ) = tmp(run2);
911  }
912  }
913 
914 
915  // MAKE SURE THAT THE PARAMETER IS TIME-CONSTANT:
916  // ----------------------------------------------
917 
918  if( np > 0 && userInit.p->getNumPoints() > 0 ){
919  iter.p->setAllVectors( userInit.p->getVector(0) );
920  }
921 
922 // if( np > 0 && userInit.p->getNumPoints() > 0 ){
923 // for( run1 = 0; run1 < _unionGrid.getNumPoints(); run1++ ){
924 // DVector tmp = userInit.p->getFirstVector( );
925 // uint nxx = tmp.getDim();
926 // if( nxx > np ) nxx = np;
927 // for( run2 = 0; run2 < nxx; run2++ )
928 // iter.p->operator()( run1, run2 ) = tmp(run2);
929 // }
930 // }
931 
932  if( nw > 0 && userInit.w->getNumPoints() > 0 ){
933  for( run1 = 0; run1 < _unionGrid.getNumPoints(); run1++ ){
934  DVector tmp = userInit.w->linearInterpolation( _unionGrid.getTime(run1) );
935  uint nxx = tmp.getDim();
936  if( nxx > nw ) nxx = nw;
937  for( run2 = 0; run2 < nxx; run2++ )
938  iter.w->operator()( run1, run2 ) = tmp(run2);
939  }
940  }
941 
942 
943  return SUCCESSFUL_RETURN;
944 }
945 
946 
947 
949 
950 
951 // end of file.
returnValue print(std::ostream &stream=std::cout, const char *const name=DEFAULT_LABEL, const char *const startString=DEFAULT_START_STRING, const char *const endString=DEFAULT_END_STRING, uint width=DEFAULT_WIDTH, uint precision=DEFAULT_PRECISION, const char *const colSeparator=DEFAULT_COL_SEPARATOR, const char *const rowSeparator=DEFAULT_ROW_SEPARATOR) const
Implements a very rudimentary block sparse matrix class.
OptimizationAlgorithmBase & operator=(const OptimizationAlgorithmBase &arg)
returnValue getSensitivitiesXA(BlockMatrix &_sens) const
double getTime(uint pointIdx) const
int acadoMax(const int x, const int y)
Definition: acado_utils.cpp:64
int getNumDynamicEquations() const
const double INFTY
returnValue initializeControls(const char *fileName)
int getNP() const
int getNX() const
int getNXA() const
virtual returnValue initializeOCPiterate(Constraint *const _constraint, const Grid &_unionGrid, uint nx, uint nxa, uint np, uint nu, uint nw)
returnValue get(OptionsName name, int &value) const
Definition: options.cpp:69
returnValue initializeDisturbances(const char *fileName)
Discretizes a DifferentialEquation by means of single or multiple shooting.
Stores and evaluates the constraints of optimal control problems.
Definition: constraint.hpp:60
Provides a time grid consisting of vector-valued optimization variables at each grid point...
Allows to pass back messages to the calling function.
returnValue getSensitivitiesW(BlockMatrix &_sens) const
int getNX() const
BooleanType isAffine() const
int getNW() 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
BooleanType isEmpty() const
returnValue getParameters(VariablesGrid &u_) const
virtual returnValue setupDynamicDiscretization(UserInteraction *_userIteraction, Objective *objective, DifferentialEquation **differentialEquation, Constraint *constraint, Grid unionGrid, DynamicDiscretization **dynamicDiscretization)
returnValue initializeParameters(const char *fileName)
int getNP() const
#define CLOSE_NAMESPACE_ACADO
int getNW() const
returnValue getSensitivitiesP(BlockMatrix &_sens) const
int getNU() const
Base class for user-interfaces to formulate and solve optimal control problems and static NLPs...
Base class for discretizing a DifferentialEquation for use in optimal control algorithms.
returnValue getAlgebraicStates(VariablesGrid &xa_) const
virtual returnValue getBounds(const OCPiterate &iter)
returnValue add(const double lb_, const Expression &arg, const double ub_)
Definition: constraint.cpp:194
int getNU() const
IntegratorType
virtual NLPsolver * clone() const =0
returnValue getSensitivitiesX(BlockMatrix &_sens) const
const int nu
unsigned getDim() const
Definition: vector.hpp:172
BooleanType isLinearQuadratic(Objective *F, DynamicDiscretization *G, Constraint *H) const
returnValue getSensitivitiesU(BlockMatrix &_sens) const
BooleanType isQuadratic()
returnValue initializeDifferentialStates(const char *fileName, BooleanType autoinit=BT_FALSE)
returnValue getDisturbances(VariablesGrid &w_) const
Encapsulates all user interaction for setting options, logging data and plotting results.
virtual returnValue setupObjective(Objective *objective, DifferentialEquation **differentialEquation, Constraint *constraint, Grid unionGrid)
Data class for defining optimal control problems.
Definition: ocp.hpp:89
virtual returnValue extractOCPdata(Objective **objective, DifferentialEquation ***differentialEquation, Constraint **constraint, Grid &unionGrid)
virtual BooleanType isAffine() const =0
returnValue read(std::istream &stream)
returnValue getControls(VariablesGrid &p_) const
returnValue initializeAlgebraicStates(const char *fileName, BooleanType autoinit=BT_FALSE)
#define BT_TRUE
Definition: acado_types.hpp:47
returnValue init(const Grid &grid_)
Definition: objective.cpp:77
uint getNumPoints() const
DVector getVector(uint pointIdx) const
virtual returnValue setupDifferentialEquation(Objective *objective, DifferentialEquation **differentialEquation, Constraint *constraint, Grid unionGrid)
#define ACADOWARNING(retval)
#define BEGIN_NAMESPACE_ACADO
#define BT_FALSE
Definition: acado_types.hpp:49
virtual returnValue determineDimensions(Objective *const _objective, DifferentialEquation **const _differentialEquation, Constraint *const _constraint, uint &_nx, uint &_nxa, uint &_np, uint &_nu, uint &_nw) const
returnValue init(UserInteraction *_userIteraction)
Stores and evaluates the objective function of optimal control problems.
Definition: objective.hpp:123
returnValue getDifferentialStates(VariablesGrid &xd_) const
#define ACADOERROR(retval)
returnValue init(const Grid &grid_, const int &numberOfStages_=1)
Definition: constraint.cpp:129
int getNXA() const
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.


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