log_record.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 
34 
35 using namespace std;
36 
38 
39 
40 //
41 // PUBLIC MEMBER FUNCTIONS:
42 //
43 
45  PrintScheme _printScheme
46  )
47 {
48  aliasIdx = -1;
49 
50  frequency = _frequency;
51  printScheme = _printScheme;
52 }
53 
55 {}
56 
58  )
59 {
60  return addItem( _name );
61 }
62 
64  )
65 {
66  return addItem( _name );
67 }
68 
69 
71  const char* const _label
72  )
73 {
74  if (items.count(make_pair(_name, LRT_ENUM)))
75  {
76 // LOG( LVL_DEBUG ) << "Log record "<< _name << " exists!" << endl;
77  return SUCCESSFUL_RETURN;
78  }
79  items[ make_pair(_name, LRT_ENUM) ] = LogRecordData( _label );
80 
81  return SUCCESSFUL_RETURN;
82 }
83 
85  const char* const _label
86  )
87 {
88  if (items.count(make_pair(_name.getComponent( 0 ), LRT_VARIABLE)))
89  return SUCCESSFUL_RETURN;
90  items[ make_pair(_name.getComponent( 0 ), LRT_VARIABLE) ] = LogRecordData( _label );
91 
92  return SUCCESSFUL_RETURN;
93 }
94 
95 returnValue LogRecord::print( std::ostream& _stream,
96  LogPrintMode _mode
97  ) const
98 {
99  LogRecordItems::const_iterator it;
100  returnValue status;
101 
102  char* startString = 0;
103  char* endString = 0;
104  uint width = 0;
105  uint precision = 0;
106  char* colSeparator = 0;
107  char* rowSeparator = 0;
108 
109  getGlobalStringDefinitions(printScheme, &startString, &endString, width,
110  precision, &colSeparator, &rowSeparator);
111 
112  switch (_mode)
113  {
114  case PRINT_ITEM_BY_ITEM:
115  for (it = items.begin(); it != items.end(); ++it)
116  {
117  DMatrix tmp;
118 
119  for (uint i = 0; i < it->second.values.getNumPoints(); ++i)
120  tmp.appendRows(it->second.values.getMatrix(i));
121 
122  status = tmp.print(
123  _stream, it->second.label.c_str(),
124  startString, endString, width, precision,
125  colSeparator, rowSeparator);
126  if (status != SUCCESSFUL_RETURN)
127  goto LogRecord_print_exit;
128  }
129 
130  break;
131 
132  case PRINT_ITER_BY_ITER:
133  for (unsigned i = 0; i < getMaxNumMatrices(); ++i)
134  for (it = items.begin(); it != items.end(); ++it)
135  {
136  if (i >= it->second.values.getNumPoints()
137  || it->second.values.getNumPoints() == 0)
138  break;
139  if (it->second.values.getMatrix( i ).print(
140  _stream, it->second.label.c_str(),
141  startString, endString, width, precision,
142  colSeparator, rowSeparator)
144  goto LogRecord_print_exit;
145  }
146 
147  break;
148 
149  case PRINT_LAST_ITER:
150  for (it = items.begin(); it != items.end(); ++it)
151  {
152  if (it->second.values.getNumPoints() == 0)
153  continue;
154  if (it->second.values.getMatrix(getMaxNumMatrices() - 1).print(
155  _stream, it->second.label.c_str(),
156  startString, endString, width, precision,
157  colSeparator, rowSeparator) != SUCCESSFUL_RETURN)
158  goto LogRecord_print_exit;
159  }
160 
161  break;
162  }
163 
164  LogRecord_print_exit:
165 
166  if ( startString != 0 ) delete[] startString;
167  if ( endString != 0 ) delete[] endString;
168  if ( colSeparator != 0 ) delete[] colSeparator;
169  if ( rowSeparator != 0 ) delete[] rowSeparator;
170 
171  return status;
172 }
173 
175 {
176  cout << "LogRecord having the following items: ";
177 
178  LogRecordItems::const_iterator it;
179  for (it = items.begin(); it != items.end(); ++it)
180  cout << it->first.first << ", ";
181 
182  cout << endl;
183 
184  return SUCCESSFUL_RETURN;
185 }
186 
187 
189 {
190  unsigned maxNumMatrices = 0;
191 
192  LogRecordItems::const_iterator it;
193  for (it = items.begin(); it != items.end(); ++it)
194  {
195 // if ( items[ i ].getNumPoints( ) > maxNumMatrices );
196  maxNumMatrices = it->second.values.getNumPoints( );
197  }
198 
199  return maxNumMatrices;
200 }
201 
202 
203 //
204 // PROTECTED MEMBER FUNCTIONS:
205 //
206 
207 
209  LogRecordItemType _type,
210  MatrixVariablesGrid& values
211  ) const
212 {
213  LogRecordItems::const_iterator it = items.find(make_pair(_name, _type));
214  if (it == items.end())
216 
217  values = it->second.values;
218 
219  return SUCCESSFUL_RETURN;
220 }
221 
222 
224  LogRecordItemType _type,
225  DMatrix& firstValue
226  ) const
227 {
228  LogRecordItems::const_iterator it = items.find(make_pair(_name, _type));
229  if (it == items.end())
231 
232  if (it->second.values.getNumPoints() == 0)
233  firstValue = DMatrix();
234  else
235  firstValue = it->second.values.getMatrix( 0 );
236 
237  return SUCCESSFUL_RETURN;
238 }
239 
240 
242  LogRecordItemType _type,
243  DMatrix& lastValue
244  ) const
245 {
246  LogRecordItems::const_iterator it = items.find(make_pair(_name, _type));
247  if (it == items.end())
249 
250  if (it->second.values.getNumPoints() == 0)
251  lastValue = DMatrix();
252  else
253  lastValue = it->second.values.getMatrix(it->second.values.getNumPoints() - 1);
254 
255  return SUCCESSFUL_RETURN;
256 }
257 
258 
259 
261  LogRecordItemType _type,
262  const MatrixVariablesGrid& values
263  )
264 {
265  LogRecordItems::iterator it = items.find(make_pair(_name, _type));
266  if (it == items.end())
268 
269  if (it->second.values.getNumPoints( ) == 0)
270  return it->second.values.init( );
271 
272  switch( frequency )
273  {
274  case LOG_AT_START:
275  it->second.values.init();
276  it->second.values.addMatrix(values.getFirstMatrix( ), values.getFirstTime( ));
277  break;
278 
279  case LOG_AT_END:
280  it->second.values.init();
281  it->second.values.addMatrix(values.getLastMatrix( ), values.getFirstTime( ));
282  break;
283 
285  it->second.values = values;
286  break;
287  }
288 
289  return SUCCESSFUL_RETURN;
290 }
291 
293  LogRecordItemType _type,
294  const DMatrix& value,
295  double time
296  )
297 {
298  LogRecordItems::iterator it = items.find(make_pair(_name, _type));
299  if (it == items.end())
301 
302  double logTime = time;
303 
304  switch( frequency )
305  {
306  case LOG_AT_START:
307  // only log if no matrix has been logged so far
308  if (it->second.values.getNumPoints() == 0)
309  {
310  if (acadoIsEqual(logTime, -INFTY) == BT_TRUE)
311  logTime = 0.0;
312 
313  it->second.values.addMatrix(value, logTime);
314  }
315  break;
316 
317  case LOG_AT_END:
318  // always overwrite existing matrices in order to keep only the last one
319  it->second.values.init();
320 
321  if (acadoIsEqual(logTime, -INFTY) == BT_TRUE)
322  logTime = 0.0;
323 
324  it->second.values.addMatrix(value, logTime);
325  break;
326 
328  // add matrix to list
329  if (acadoIsEqual(logTime, -INFTY) == BT_TRUE)
330  logTime = (double)it->second.values.getNumPoints() + 1.0;
331 
332  it->second.values.addMatrix(value, logTime);
333  break;
334  }
335  return SUCCESSFUL_RETURN;
336 }
337 
339  ) const
340 {
341  LogRecordItems::iterator it;
342 
343  for (it = _record.items.begin(); it != _record.items.end(); ++it)
344  {
345  if (it->second.writeProtection == true)
346  continue;
347 
348  LogRecordItems::const_iterator cit = items.find( it->first );
349 
350  if (cit != items.end())
351  it->second = cit->second;
352  }
353 
354  return SUCCESSFUL_RETURN;
355 }
356 
358 
359 /*
360  * end of file
361  */
returnValue getGlobalStringDefinitions(PrintScheme _printScheme, char **_startString, char **_endString, uint &_width, uint &_precision, char **_colSeparator, char **_rowSeparator)
returnValue addItem(LogName _name, const char *const _label=DEFAULT_LABEL)
Definition: log_record.cpp:70
double getFirstTime() const
BooleanType acadoIsEqual(double x, double y, double TOL)
Definition: acado_utils.cpp:88
const double INFTY
DMatrix getFirstMatrix() const
returnValue operator<<(LogName _name)
Definition: log_record.cpp:57
Allows to pass back messages to the calling function.
returnValue setAll(LogName _name, const MatrixVariablesGrid &values)
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
GenericMatrix & appendRows(const GenericMatrix &_arg)
Definition: matrix.cpp:62
uint getMaxNumMatrices() const
Definition: log_record.cpp:188
LogRecordItems items
Definition: log_record.hpp:694
#define CLOSE_NAMESPACE_ACADO
GenericMatrix< double > DMatrix
Definition: matrix.hpp:457
returnValue getLast(LogName _name, DMatrix &lastValue) const
Base class for all variables within the symbolic expressions family.
Definition: expression.hpp:56
LogPrintMode
returnValue printInfo() const
Definition: log_record.cpp:174
virtual returnValue print(std::ostream &_stream=std::cout, const std::string &_name=DEFAULT_LABEL, const std::string &_startString=DEFAULT_START_STRING, const std::string &_endString=DEFAULT_END_STRING, uint _width=DEFAULT_WIDTH, uint _precision=DEFAULT_PRECISION, const std::string &_colSeparator=DEFAULT_COL_SEPARATOR, const std::string &_rowSeparator=DEFAULT_ROW_SEPARATOR) const
Definition: matrix.cpp:254
Provides a time grid consisting of matrix-valued optimization variables at each grid point...
LogRecordItemType
PrintScheme
DMatrix getLastMatrix() const
LogName
LogFrequency
LogRecord(LogFrequency _frequency=LOG_AT_EACH_ITERATION, PrintScheme _printScheme=PS_DEFAULT)
Definition: log_record.cpp:44
#define BT_TRUE
Definition: acado_types.hpp:47
uint getComponent(const unsigned int idx) const
Allows to setup and store user-specified log records of algorithmic information.
Definition: log_record.hpp:72
#define BEGIN_NAMESPACE_ACADO
returnValue print(std::ostream &_stream=std::cout, LogPrintMode _mode=PRINT_ITEM_BY_ITEM) const
Definition: log_record.cpp:95
returnValue setLast(LogName _name, const DMatrix &value, double time=-INFTY)
returnValue getAll(LogName _name, MatrixVariablesGrid &values) const
returnValue getFirst(LogName _name, DMatrix &firstValue) const
#define ACADOERROR(retval)
returnValue updateLogRecord(LogRecord &_record) const
Definition: log_record.cpp:338


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