root/bdm/stat/loggers.h @ 347

Revision 347, 2.6 kB (checked in by smidl, 15 years ago)

change in loggers! old experiments may stop working

  • 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 Logging into matrices in data format in memory
23
24* More?...
25*/
26
27class memlog : public logger {
28
29protected:
30        //! Maximum length of vectors stored in memory
31        int maxlen;
32        //! Currect record to be written
33        int ind;
34        //! Storage
35        Array<mat> vectors;
36        //! name of it file to save results
37        string itfilename;
38public:
39        //!Default constructor
40        memlog ( int maxlen0, string itf="" ) : logger(),maxlen ( maxlen0 ),ind ( 0 ),vectors ( 0 ),itfilename(itf) {}
41        //! Initialize storage
42        void init() {
43                int i; int n =entries.length();
44                vectors.set_size ( n ); 
45                for ( i=0;i<n;i++ ) {vectors(i).set_size (maxlen,entries(i)._dsize() );}
46        }
47        void step() {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );}
48        void logit ( int id, const vec &v ) {
49                it_assert_debug(id<vectors.length(),"Logger was not initialized, run init().");
50                if(id>=0){ vectors ( id ).set_row ( ind,v );}
51        }
52        void logit ( int id, const double &d ) {
53                it_assert_debug(id<vectors.length(),"Logger was not initialized, run init().");
54                it_assert_debug(vectors(id).cols()==1,"Vector expected");
55                if(id>=0){ vectors ( id ) ( ind )=d;}
56        }
57        //! Save values into an itfile named after \c fname.
58        void itsave(const char* fname);
59        //!
60        void finalize() {if (itfilename.length()>0) itsave(itfilename.c_str());};
61
62};
63
64/*!
65* \brief Logging into dirfile with buffer in memory
66
67* Dirfile is a special format used by the kst program. See documentation of kst for description.
68*
69* This format is used to store scalars, hence multivariate RVs must be separated.
70*/
71
72class dirfilelog : public memlog {
73
74protected:
75        //!name of the directory
76        string dirname;
77        //! Automatically generated
78        Array<string> scalarnames;
79public:
80        /*!\brief Default constructor
81        @param dirname0 name of the directory in which to store the results
82        @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */
83        dirfilelog ( std::string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {}
84        //! Initialize storage
85        void init();
86        void step();
87        void finalize();
88        /*! \brief Write memory storage to disk.
89        @param Len length of buffer to be written, if 0 the file is truncated at 0.
90        */
91        void write_buffers ( int Len );
92};
93
94};
95#endif // LGR_H
Note: See TracBrowser for help on using the browser.