1 | /*! |
---|
2 | \file |
---|
3 | \brief Loggers for storing results of experiments |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | ----------------------------------- |
---|
7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
8 | |
---|
9 | Using IT++ for numerical operations |
---|
10 | ----------------------------------- |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef LGR_H |
---|
14 | #define LGR_H |
---|
15 | |
---|
16 | #include <itpp/itbase.h> |
---|
17 | #include "libBM.h" |
---|
18 | #include "../itpp_ext.h" |
---|
19 | |
---|
20 | using namespace itpp; |
---|
21 | using std::string; |
---|
22 | |
---|
23 | /*! |
---|
24 | @brief Class for storing results (and semi-results) of an experiment |
---|
25 | |
---|
26 | This class abstracts logging of results from implementation. This class replaces direct logging of results (e.g. to files or to global variables) by calling methods of a logger. Specializations of this abstract class for specific storage method are designed. |
---|
27 | */ |
---|
28 | class logger { |
---|
29 | protected: |
---|
30 | //! RVs of all logged variables. |
---|
31 | Array<RV> entries; |
---|
32 | //! Names of logged quantities, e.g. names of algorithm variants |
---|
33 | Array<string> names; |
---|
34 | public: |
---|
35 | //!Default constructor |
---|
36 | logger ( ) : entries(0),names ( 0 ) {} |
---|
37 | |
---|
38 | //! returns an identifier which will be later needed for calling the log() function |
---|
39 | virtual int add ( RV rv, string name="" ) { |
---|
40 | int id=entries.length(); |
---|
41 | names=concat ( names, name ); // diff |
---|
42 | entries.set_length(id+1,true); |
---|
43 | entries(id)= rv; |
---|
44 | return id; // identifier of the last entry |
---|
45 | } |
---|
46 | |
---|
47 | //! log this vector |
---|
48 | virtual void logit ( int id, vec v ) =0; |
---|
49 | |
---|
50 | //! consider this record to be complete |
---|
51 | virtual void step(bool final=false) =0; |
---|
52 | |
---|
53 | //! for future use |
---|
54 | virtual ~logger() {}; |
---|
55 | }; |
---|
56 | |
---|
57 | |
---|
58 | /*! |
---|
59 | * \brief Logging into matrices in data format in memory |
---|
60 | |
---|
61 | * More?... |
---|
62 | */ |
---|
63 | |
---|
64 | class memlog : public logger { |
---|
65 | |
---|
66 | protected: |
---|
67 | //! Maximum length of vectors stored in memory |
---|
68 | int maxlen; |
---|
69 | //! Currect record to be written |
---|
70 | int ind; |
---|
71 | //! Storage |
---|
72 | Array<mat> vectors; |
---|
73 | //! |
---|
74 | public: |
---|
75 | //!Default constructor |
---|
76 | memlog ( int maxlen0 ) : logger(),maxlen ( maxlen0 ),ind ( 0 ),vectors ( 0 ) {} |
---|
77 | //! Initialize storage |
---|
78 | void init() { |
---|
79 | int i; int n =entries.length(); |
---|
80 | vectors.set_size ( n ); |
---|
81 | for ( i=0;i<n;i++ ) {vectors(i).set_size (maxlen,entries(i).count() );} |
---|
82 | ; |
---|
83 | } |
---|
84 | void step(bool final=false) {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );} |
---|
85 | void logit ( int id, vec v ) {vectors ( id ).set_row ( ind,v );} |
---|
86 | }; |
---|
87 | |
---|
88 | /*! |
---|
89 | * \brief Logging into dirfile with buffer in memory |
---|
90 | |
---|
91 | * Dirfile is a special format used by the kst program. See documentation of kst for description. |
---|
92 | * |
---|
93 | * This format is used to store scalars, hence multivariate RVs must be separated. |
---|
94 | */ |
---|
95 | |
---|
96 | class dirfilelog : public memlog { |
---|
97 | |
---|
98 | protected: |
---|
99 | //!name of the directory |
---|
100 | string dirname; |
---|
101 | //! Automatically generated |
---|
102 | Array<string> scalarnames; |
---|
103 | public: |
---|
104 | /*!\brief Default constructor |
---|
105 | @param dirname0 name of the directory in which to store the results |
---|
106 | @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */ |
---|
107 | dirfilelog ( std::string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {} |
---|
108 | //! Initialize storage |
---|
109 | void init(); |
---|
110 | void step(bool final=false); |
---|
111 | /*! \brief Write memory storage to disk. |
---|
112 | @param Len length of buffer to be written, if 0 the file is truncated at 0. |
---|
113 | */ |
---|
114 | void write_buffers ( int Len ); |
---|
115 | }; |
---|
116 | |
---|
117 | |
---|
118 | #endif // LGR_H |
---|