root/library/bdm/base/loggers.h @ 863

Revision 766, 4.6 kB (checked in by mido, 14 years ago)

abstract methods restored wherever they are meaningful
macros NOT_IMPLEMENTED and NOT_IMPLEMENTED_VOID defined to make sources shorter
emix::set_parameters and mmix::set_parameters removed, corresponding acces methods created and the corresponding validate methods improved appropriately
some compilator warnings were avoided
and also a few other things cleaned up

  • Property svn:eol-style set to native
Line 
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 "bdmbase.h"
17#include "user_info.h"
18
19namespace bdm {
20using std::string;
21
22/*!
23 * Logging into matrices in data format in memory, optionally persisted into a file.
24 */
25class memlog : public logger {
26
27protected:
28        //! Maximum length of vectors stored in memory
29        int maxlen;
30        //! Currect record to be written
31        int ind;
32        //! Storage
33        Array<mat> vectors;
34        //! name of  file to save results (IT file for vectors and cfg for settings)
35        string filename;
36public:
37        //! convenience constructor
38        memlog ( int maxlen0, string fn = "" ) : logger ( "_" ), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), filename ( fn ) {}
39
40        //!Default constructor
41        memlog() : logger ( "_" ), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {}
42
43        //! Initialize storage
44        void init();
45
46        void step() {
47                if ( ind < maxlen ) ind++;
48                else bdm_error ( "memlog::ind is too high;" );
49        }
50
51        void log_vector ( int id, const vec &v ) {
52                bdm_assert ( id < vectors.length(), "Logger was not initialized, run init()." );
53                if ( id >= 0 ) {
54                        vectors ( id ).set_row ( ind, v );
55                }
56        }
57        void logit ( int id, const double &d ) {
58                bdm_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." );
59                bdm_assert_debug ( vectors ( id ).cols() == 1, "Vector expected" );
60                if ( id >= 0 ) {
61                        vectors ( id ) ( ind ) = d;
62                }
63        }
64        //! Save values into an itfile named after \c fname.
65        void itsave ( const char* fname );
66        //!
67        void finalize() {
68                if ( filename.length() > 0 ) {
69                        itsave ( ( filename + ".it" ).c_str() );
70                        if ( settings.length() > 0 ) {
71                                setting_conf.writeFile ( ( filename + ".cfg" ).c_str() );
72                        }
73                }
74
75        };
76
77
78        /*! \brief UI for memlog
79
80        \code
81        logger = {
82                class = "memlog";
83                filename = "file_name"; // resulting filename with vectors in it format and setting in cfg
84                maxlen = 100;           // size of memory buffer
85        }
86        \endcode
87         */
88        void from_setting ( const Setting &set );
89
90        // TODO dodelat void to_setting( Setting &set ) const;
91};
92
93UIREGISTER ( memlog );
94SHAREDPTR ( memlog );
95
96
97/*! \brief Simple logger used for debugging
98All data records are written out to std from where they could be send to file
99*/
100class stdlog: public memlog {
101public:
102        //! default constructor
103        stdlog() : memlog ( 1 ) {};
104
105        void init() {
106                memlog::init();
107                for ( int i = 0; i < entries.length(); i++ ) {
108                        if ( entries ( i )._dsize() == 1 ) {
109                                cout << names ( i ) << entries ( i ).name ( 0 ) << "\t";
110                        } else
111                                for ( int j = 0; j < vectors ( i ).cols(); j++ ) {
112                                        cout << names ( i ) << entries ( i ).scalarname ( j ) << "\t";
113                                }
114                }
115                cout << endl;
116
117
118        }
119        //!writes everything out
120        void step() {
121                for ( int i = 0; i < vectors.length(); i++ ) {
122                        for ( int j = 0; j < vectors ( i ).cols(); j++ ) {
123                                cout << vectors ( i ) ( 0, j ) << "\t";
124                        }
125                }
126                cout << endl;
127        }
128        /*! \brief UI for stdlog
129
130        \code
131        logger = {
132                class = "stdlog"; // no parameterization needed
133        }
134        \endcode
135        */
136        void from_setting ( const Setting &set ) {
137        }
138};
139UIREGISTER ( stdlog );
140
141/*!
142* \brief Logging into dirfile with buffer in memory
143
144* Dirfile is a special format used by the kst program. See documentation of kst for description.
145*
146* This format is used to store scalars, hence multivariate RVs must be separated.
147*/
148class dirfilelog : public memlog {
149
150protected:
151        //!name of the directory
152        string dirname;
153        //! Automatically generated
154        Array<string> scalarnames;
155public:
156        /*!\brief Default constructor
157        @param dirname0 name of the directory in which to store the results
158        @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */
159        dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {}
160
161        dirfilelog() {}
162
163        //! Initialize storage
164        void init();
165        void step();
166        void finalize();
167        /*! \brief Write memory storage to disk.
168        @param Len length of buffer to be written, if 0 the file is truncated at 0.
169        */
170        void write_buffers ( int Len );
171
172        /*! \brief UI for dirfilelog (Kst file format)
173        \code
174        logger = {
175                class = "dirfilelog";
176                dirname = "directory_for_files"; // resulting files will be stored there
177                maxlen = 100;                    // size of memory buffer, when full results are written to disk
178        }
179        \endcode
180        */
181        void from_setting ( const Setting &set );
182
183        // TODO dodelat void to_setting( Setting &set ) const;
184};
185
186UIREGISTER ( dirfilelog );
187SHAREDPTR ( dirfilelog );
188
189};
190#endif // LGR_H
Note: See TracBrowser for help on using the browser.