root/bdm/stat/loggers.h @ 260

Revision 260, 3.4 kB (checked in by smidl, 15 years ago)

working UI example

  • 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 "libBM.h"
17
18namespace bdm{
19using std::string;
20
21/*!
22@brief Class for storing results (and semi-results) of an experiment
23
24This 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.
25*/
26class logger : public bdmroot{
27protected:
28        //! RVs of all logged variables.
29        Array<RV> entries;
30        //! Names of logged quantities, e.g. names of algorithm variants
31        Array<string> names;
32public:
33        //!Default constructor
34        logger ( ) : entries(0),names ( 0 ) {}
35
36        //! returns an identifier which will be later needed for calling the log() function
37        virtual int add (const RV &rv, string name="" ) {
38                int id=entries.length();
39                names=concat ( names, name ); // diff
40                entries.set_length(id+1,true);
41                entries(id)= rv;
42                return id; // identifier of the last entry
43        }
44
45        //! log this vector
46        virtual void logit ( int id, const vec &v ) =0;
47
48        //! Shifts storage position for another time step.
49        virtual void step() =0;
50
51        //! Finalize storing information
52        virtual void finalize() {};
53
54        //! Initialize the storage
55        virtual void init(){};
56       
57        //! for future use
58        virtual ~logger() {};
59};
60
61
62/*!
63* \brief Logging into matrices in data format in memory
64
65* More?...
66*/
67
68class memlog : public logger {
69
70protected:
71        //! Maximum length of vectors stored in memory
72        int maxlen;
73        //! Currect record to be written
74        int ind;
75        //! Storage
76        Array<mat> vectors;
77        //!
78public:
79        //!Default constructor
80        memlog ( int maxlen0 ) : logger(),maxlen ( maxlen0 ),ind ( 0 ),vectors ( 0 ) {}
81        //! Initialize storage
82        void init() {
83                int i; int n =entries.length();
84                vectors.set_size ( n ); 
85                for ( i=0;i<n;i++ ) {vectors(i).set_size (maxlen,entries(i).count() );}
86                ;
87        }
88        void step() {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );}
89        void logit ( int id, const vec &v ) {
90                it_assert_debug(id<vectors.length(),"Logger was not initialized, run init().");
91                vectors ( id ).set_row ( ind,v );}
92        //! Save values into an itfile named after \c fname.
93        void itsave(const char* fname);
94};
95
96/*!
97* \brief Logging into dirfile with buffer in memory
98
99* Dirfile is a special format used by the kst program. See documentation of kst for description.
100*
101* This format is used to store scalars, hence multivariate RVs must be separated.
102*/
103
104class dirfilelog : public memlog {
105
106protected:
107        //!name of the directory
108        string dirname;
109        //! Automatically generated
110        Array<string> scalarnames;
111public:
112        /*!\brief Default constructor
113        @param dirname0 name of the directory in which to store the results
114        @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */
115        dirfilelog ( std::string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {}
116        //! Initialize storage
117        void init();
118        void step();
119        void finalize();
120        /*! \brief Write memory storage to disk.
121        @param Len length of buffer to be written, if 0 the file is truncated at 0.
122        */
123        void write_buffers ( int Len );
124};
125
126}
127#endif // LGR_H
Note: See TracBrowser for help on using the browser.