log_record.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00033 #include <acado/user_interaction/log_record.hpp>
00034 
00035 using namespace std;
00036 
00037 BEGIN_NAMESPACE_ACADO
00038 
00039 
00040 //
00041 // PUBLIC MEMBER FUNCTIONS:
00042 //
00043 
00044 LogRecord::LogRecord(   LogFrequency _frequency,
00045                                                 PrintScheme _printScheme
00046                                                 )
00047 {
00048         aliasIdx = -1;
00049 
00050         frequency      = _frequency;
00051         printScheme    = _printScheme;
00052 }
00053 
00054 LogRecord::~LogRecord( )
00055 {}
00056 
00057 returnValue LogRecord::operator<<(      LogName _name
00058                                                                         )
00059 {
00060         return addItem( _name );
00061 }
00062 
00063 returnValue LogRecord::operator<<(      const Expression& _name
00064                                                                         )
00065 {
00066         return addItem( _name );
00067 }
00068 
00069 
00070 returnValue LogRecord::addItem( LogName _name,
00071                                                                 const char* const _label
00072                                                                 )
00073 {
00074         if (items.count(make_pair(_name, LRT_ENUM)))
00075         {
00076 //              LOG( LVL_DEBUG ) << "Log record "<< _name << " exists!" << endl;
00077                 return SUCCESSFUL_RETURN;
00078         }
00079         items[ make_pair(_name, LRT_ENUM) ] = LogRecordData( _label );
00080 
00081         return SUCCESSFUL_RETURN;
00082 }
00083 
00084 returnValue LogRecord::addItem( const Expression& _name,
00085                                                                 const char* const _label
00086                                                                 )
00087 {
00088         if (items.count(make_pair(_name.getComponent( 0 ), LRT_VARIABLE)))
00089                 return SUCCESSFUL_RETURN;
00090         items[ make_pair(_name.getComponent( 0 ), LRT_VARIABLE) ] = LogRecordData( _label );
00091 
00092         return SUCCESSFUL_RETURN;
00093 }
00094 
00095 returnValue LogRecord::print(   std::ostream& _stream,
00096                                                                 LogPrintMode _mode
00097                                                                 ) const
00098 {
00099         LogRecordItems::const_iterator it;
00100         returnValue status;
00101 
00102         char* startString = 0;
00103         char* endString = 0;
00104         uint width = 0;
00105         uint precision = 0;
00106         char* colSeparator = 0;
00107         char* rowSeparator = 0;
00108 
00109         getGlobalStringDefinitions(printScheme, &startString, &endString, width,
00110                         precision, &colSeparator, &rowSeparator);
00111 
00112         switch (_mode)
00113         {
00114         case PRINT_ITEM_BY_ITEM:
00115                 for (it = items.begin(); it != items.end(); ++it)
00116                 {
00117                         DMatrix tmp;
00118 
00119                         for (uint i = 0; i < it->second.values.getNumPoints(); ++i)
00120                                 tmp.appendRows(it->second.values.getMatrix(i));
00121 
00122                          status = tmp.print(
00123                                         _stream, it->second.label.c_str(),
00124                                         startString, endString, width, precision,
00125                                         colSeparator, rowSeparator);
00126                          if (status != SUCCESSFUL_RETURN)
00127                                  goto LogRecord_print_exit;
00128                 }
00129 
00130                 break;
00131 
00132         case PRINT_ITER_BY_ITER:
00133                 for (unsigned i = 0; i < getMaxNumMatrices(); ++i)
00134                         for (it = items.begin(); it != items.end(); ++it)
00135                         {
00136                                 if (i >= it->second.values.getNumPoints()
00137                                                 || it->second.values.getNumPoints() == 0)
00138                                         break;
00139                                 if (it->second.values.getMatrix( i ).print(
00140                                                 _stream, it->second.label.c_str(),
00141                                                 startString, endString, width, precision,
00142                                                 colSeparator, rowSeparator)
00143                                                 != SUCCESSFUL_RETURN)
00144                                         goto LogRecord_print_exit;
00145                         }
00146 
00147         break;
00148 
00149         case PRINT_LAST_ITER:
00150                 for (it = items.begin(); it != items.end(); ++it)
00151                 {
00152                         if (it->second.values.getNumPoints() == 0)
00153                                 continue;
00154                         if (it->second.values.getMatrix(getMaxNumMatrices() - 1).print(
00155                                         _stream, it->second.label.c_str(),
00156                                         startString, endString, width, precision,
00157                                         colSeparator, rowSeparator) != SUCCESSFUL_RETURN)
00158                                 goto LogRecord_print_exit;
00159                 }
00160 
00161         break;
00162         }
00163 
00164         LogRecord_print_exit:
00165 
00166         if ( startString != 0 )   delete[] startString;
00167         if ( endString != 0 )     delete[] endString;
00168         if ( colSeparator != 0 )  delete[] colSeparator;
00169         if ( rowSeparator != 0 )  delete[] rowSeparator;
00170 
00171         return status;
00172 }
00173 
00174 returnValue LogRecord::printInfo( ) const
00175 {
00176         cout << "LogRecord having the following items: ";
00177 
00178         LogRecordItems::const_iterator it;
00179         for (it = items.begin(); it != items.end(); ++it)
00180                 cout << it->first.first << ", ";
00181 
00182         cout << endl;
00183 
00184         return SUCCESSFUL_RETURN;
00185 }
00186 
00187 
00188 uint LogRecord::getMaxNumMatrices( ) const
00189 {
00190         unsigned maxNumMatrices = 0;
00191 
00192         LogRecordItems::const_iterator it;
00193         for (it = items.begin(); it != items.end(); ++it)
00194         {
00195 //              if ( items[ i ].getNumPoints( ) > maxNumMatrices );
00196                 maxNumMatrices = it->second.values.getNumPoints( );
00197         }
00198 
00199         return maxNumMatrices;
00200 }
00201 
00202 
00203 //
00204 // PROTECTED MEMBER FUNCTIONS:
00205 //
00206 
00207 
00208 returnValue LogRecord::getAll(  uint _name,
00209                                                                 LogRecordItemType _type,
00210                                                                 MatrixVariablesGrid& values
00211                                                                 ) const
00212 {
00213         LogRecordItems::const_iterator it = items.find(make_pair(_name, _type));
00214         if (it == items.end())
00215                 return ACADOERROR( RET_LOG_ENTRY_DOESNT_EXIST );
00216 
00217         values = it->second.values;
00218 
00219         return SUCCESSFUL_RETURN;
00220 }
00221 
00222 
00223 returnValue LogRecord::getFirst(        uint _name,
00224                                                                         LogRecordItemType _type,
00225                                                                         DMatrix& firstValue
00226                                                                         ) const
00227 {
00228         LogRecordItems::const_iterator it = items.find(make_pair(_name, _type));
00229         if (it == items.end())
00230                 return ACADOERROR( RET_LOG_ENTRY_DOESNT_EXIST );
00231 
00232         if (it->second.values.getNumPoints() == 0)
00233                 firstValue = DMatrix();
00234         else
00235                 firstValue = it->second.values.getMatrix( 0 );
00236 
00237         return SUCCESSFUL_RETURN;
00238 }
00239 
00240 
00241 returnValue LogRecord::getLast( uint _name,
00242                                                                 LogRecordItemType _type,
00243                                                                 DMatrix& lastValue
00244                                                                 ) const
00245 {
00246         LogRecordItems::const_iterator it = items.find(make_pair(_name, _type));
00247         if (it == items.end())
00248                 return ACADOERROR( RET_LOG_ENTRY_DOESNT_EXIST );
00249 
00250         if (it->second.values.getNumPoints() == 0)
00251                 lastValue = DMatrix();
00252         else
00253                 lastValue = it->second.values.getMatrix(it->second.values.getNumPoints() - 1);
00254 
00255         return SUCCESSFUL_RETURN;
00256 }
00257 
00258 
00259 
00260 returnValue LogRecord::setAll(  uint _name,
00261                                                                 LogRecordItemType _type,
00262                                                                 const MatrixVariablesGrid& values
00263                                                                 )
00264 {
00265         LogRecordItems::iterator it = items.find(make_pair(_name, _type));
00266         if (it == items.end())
00267                 return ACADOERROR( RET_LOG_ENTRY_DOESNT_EXIST );
00268 
00269         if (it->second.values.getNumPoints( ) == 0)
00270                 return it->second.values.init( );
00271 
00272         switch( frequency )
00273         {
00274         case LOG_AT_START:
00275                 it->second.values.init();
00276                 it->second.values.addMatrix(values.getFirstMatrix( ), values.getFirstTime( ));
00277                 break;
00278 
00279         case LOG_AT_END:
00280                 it->second.values.init();
00281                 it->second.values.addMatrix(values.getLastMatrix( ), values.getFirstTime( ));
00282                 break;
00283 
00284         case LOG_AT_EACH_ITERATION:
00285                 it->second.values = values;
00286                 break;
00287         }
00288 
00289         return SUCCESSFUL_RETURN;
00290 }
00291 
00292 returnValue LogRecord::setLast( uint _name,
00293                                                                 LogRecordItemType _type,
00294                                                                 const DMatrix& value,
00295                                                                 double time
00296                                                                 )
00297 {
00298         LogRecordItems::iterator it = items.find(make_pair(_name, _type));
00299         if (it == items.end())
00300                 return ACADOERROR( RET_LOG_ENTRY_DOESNT_EXIST );
00301 
00302         double logTime = time;
00303 
00304         switch( frequency )
00305         {
00306         case LOG_AT_START:
00307                 // only log if no matrix has been logged so far
00308                 if (it->second.values.getNumPoints() == 0)
00309                 {
00310                         if (acadoIsEqual(logTime, -INFTY) == BT_TRUE)
00311                                 logTime = 0.0;
00312 
00313                         it->second.values.addMatrix(value, logTime);
00314                 }
00315                 break;
00316 
00317         case LOG_AT_END:
00318                 // always overwrite existing matrices in order to keep only the last one
00319                 it->second.values.init();
00320 
00321                 if (acadoIsEqual(logTime, -INFTY) == BT_TRUE)
00322                         logTime = 0.0;
00323 
00324                 it->second.values.addMatrix(value, logTime);
00325                 break;
00326 
00327         case LOG_AT_EACH_ITERATION:
00328                 // add matrix to list
00329                 if (acadoIsEqual(logTime, -INFTY) == BT_TRUE)
00330                         logTime = (double)it->second.values.getNumPoints() + 1.0;
00331 
00332                 it->second.values.addMatrix(value, logTime);
00333                 break;
00334         }
00335         return SUCCESSFUL_RETURN;
00336 }
00337 
00338 returnValue LogRecord::updateLogRecord( LogRecord& _record
00339                                                                                 ) const
00340 {
00341         LogRecordItems::iterator it;
00342 
00343         for (it = _record.items.begin(); it != _record.items.end(); ++it)
00344         {
00345                 if (it->second.writeProtection == true)
00346                         continue;
00347 
00348                 LogRecordItems::const_iterator cit = items.find( it->first );
00349 
00350                 if (cit != items.end())
00351                         it->second = cit->second;
00352         }
00353 
00354         return SUCCESSFUL_RETURN;
00355 }
00356 
00357 CLOSE_NAMESPACE_ACADO
00358 
00359 /*
00360  *      end of file
00361  */


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Sat Jun 8 2019 19:37:53