Changeset 676 for library/bdm/base
- Timestamp:
- 10/22/09 01:13:47 (15 years ago)
- Location:
- library/bdm/base
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.h
r675 r676 298 298 RV concat ( const RV &rv1, const RV &rv2 ); 299 299 300 /*! 301 @brief Class for storing results (and semi-results) of an experiment 302 303 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. 304 */ 305 class logger : public root { 306 protected: 307 //! RVs of all logged variables. 308 Array<RV> entries; 309 //! Names of logged quantities, e.g. names of algorithm variants 310 Array<string> names; 311 //!separator of prefixes of entries 312 const string separator; 313 public: 314 //!Default constructor 315 logger(const string separator0) : entries ( 0 ), names ( 0 ), separator(separator0) {} 316 317 //! returns an identifier which will be later needed for calling the \c logit() function 318 //! For empty RV it returns -1, this entry will be ignored by \c logit(). 319 virtual int add ( const RV &rv, string prefix = "" ) { 320 int id; 321 if ( rv._dsize() > 0 ) { 322 id = entries.length(); 323 names = concat ( names, prefix ); // diff 324 entries.set_length ( id + 1, true ); 325 entries ( id ) = rv; 326 } else { 327 id = -1; 328 } 329 return id; // identifier of the last entry 330 } 331 332 //! log this vector 333 virtual void logit ( int id, const vec &v ) { 334 bdm_error ( "Not implemented" ); 335 }; 336 //! log this double 337 virtual void logit ( int id, const double &d ) { 338 bdm_error ( "Not implemented" ); 339 }; 340 341 //! Shifts storage position for another time step. 342 virtual void step() { 343 bdm_error ( "Not implemneted" ); 344 }; 345 346 //! Finalize storing information 347 virtual void finalize() {}; 348 349 //! Initialize the storage 350 virtual void init() {}; 351 352 //!separator of prefixes for this logger 353 const string& prefix_sep() {return separator;} 354 }; 355 356 300 357 //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv 301 358 class fnc : public root { … … 532 589 //bdm_assert_debug ( isnamed(), "" ); 533 590 return rv; 591 } 592 //! store values of the epdf on the following levels: 593 //! #1 mean 594 //! #2 mean + lower & upper bound 595 void log_register(logger &L, const string &prefix){ 596 RV r; 597 if ( isnamed() ) { 598 r = _rv(); 599 } else { 600 r = RV ( "", dimension() ); 601 }; 602 root::log_register(L,prefix); 603 logrec->ids.set_size(3); 604 if (log_level >0){ 605 logrec->ids(0) = logrec->L.add ( r, prefix + logrec->L.prefix_sep()+ "mean" ); 606 } 607 if (log_level >1){ 608 logrec->ids(1) = logrec->L.add ( r, prefix + logrec->L.prefix_sep()+ "lb" ); 609 logrec->ids(2) = logrec->L.add ( r, prefix + logrec->L.prefix_sep()+ "ub" ); 610 } 534 611 } 535 612 //!@} … … 861 938 }; 862 939 863 /*!864 @brief Class for storing results (and semi-results) of an experiment865 866 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.867 */868 class logger : public root {869 protected:870 //! RVs of all logged variables.871 Array<RV> entries;872 //! Names of logged quantities, e.g. names of algorithm variants873 Array<string> names;874 public:875 //!Default constructor876 logger() : entries ( 0 ), names ( 0 ) {}877 878 //! returns an identifier which will be later needed for calling the \c logit() function879 //! For empty RV it returns -1, this entry will be ignored by \c logit().880 virtual int add ( const RV &rv, string prefix = "" ) {881 int id;882 if ( rv._dsize() > 0 ) {883 id = entries.length();884 names = concat ( names, prefix ); // diff885 entries.set_length ( id + 1, true );886 entries ( id ) = rv;887 } else {888 id = -1;889 }890 return id; // identifier of the last entry891 }892 893 //! log this vector894 virtual void logit ( int id, const vec &v ) {895 bdm_error ( "Not implemented" );896 };897 //! log this double898 virtual void logit ( int id, const double &d ) {899 bdm_error ( "Not implemented" );900 };901 902 //! Shifts storage position for another time step.903 virtual void step() {904 bdm_error ( "Not implemneted" );905 };906 907 //! Finalize storing information908 virtual void finalize() {};909 910 //! Initialize the storage911 virtual void init() {};912 913 };914 915 940 916 941 //! \brief Combines RVs from a list of mpdfs to a single one. … … 946 971 //!Description of output data 947 972 RV Yrv; // 948 //! Remember its own index in Logger L, [0=dt, 1=ut]949 ivec LIDs;950 973 public: 951 974 //! default constructors 952 DS() : Drv(), Urv(),Yrv() , LIDs(2) {};975 DS() : Drv(), Urv(),Yrv() {log_level=1;}; 953 976 954 977 //! Returns maximum number of provided data, by default it is set to maximum allowed length, shorter DS should overload this method! See, MemDS.max_length(). 955 978 virtual int max_length() {return std::numeric_limits< int >::max();} 956 979 //! Returns full vector of observed data=[output, input] 957 virtual void getdata ( vec &dt ) {980 virtual void getdata ( vec &dt ) const { 958 981 bdm_error ( "abstract class" ); 959 982 } … … 980 1003 981 1004 //! Register DS for logging into logger L 982 virtual void log_ add ( logger &L) {983 bdm_assert ( dtsize == Drv._dsize(), "invalid DS: dtsize (" + num2str ( dtsize ) + ") different from Drv " + num2str ( Drv._dsize() ) );1005 virtual void log_register (logger &L, const string &prefix ) { 1006 bdm_assert ( ytsize == Yrv._dsize(), "invalid DS: ytsize (" + num2str ( ytsize ) + ") different from Drv " + num2str ( Yrv._dsize() ) ); 984 1007 bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); 985 1008 986 LIDs(0) = L.add ( Drv, "" ); 987 LIDs(1) = L.add ( Urv, "" ); 1009 root::log_register(L,prefix); 1010 //we know that 1011 if (log_level >0){ 1012 logrec->ids.set_size(2); 1013 logrec->ids(0) = logrec->L.add ( Yrv, "" ); 1014 logrec->ids(1) = logrec->L.add ( Urv, "" ); 1015 } 988 1016 } 989 1017 //! Register DS for logging into logger L 990 virtual void logit ( logger &L ) { 991 vec tmp ( Drv._dsize() + Urv._dsize() ); 992 getdata ( tmp ); 993 // d is first in getdata 994 L.logit ( LIDs(0), tmp.left ( Drv._dsize() ) ); 995 // u follows after d in getdata 996 L.logit ( LIDs(1), tmp.mid ( Drv._dsize(), Urv._dsize() ) ); 1018 virtual void log_write ( ) const { 1019 if (log_level >0) { 1020 vec tmp ( Yrv._dsize() + Urv._dsize()); 1021 getdata ( tmp ); 1022 // d is first in getdata 1023 logrec->L.logit ( logrec->ids(0), tmp.left ( Yrv._dsize() ) ); 1024 // u follows after d in getdata 1025 logrec->L.logit ( logrec->ids(1), tmp.mid ( Yrv._dsize(), Urv._dsize() ) ); 1026 } 997 1027 } 998 1028 //!access function 999 1029 virtual const RV& _drv() const { 1000 // return concat (Drv, Urv);// why!!! 1001 return Drv;// why!!! 1030 return Drv; 1002 1031 } 1003 1032 //!access function … … 1005 1034 return Urv; 1006 1035 } 1007 1008 1009 1010 1036 //!access function 1037 const RV& _yrv() const { 1038 return Yrv; 1039 } 1011 1040 //! set random variables 1012 1013 1014 1041 virtual void set_drv (const RV &yrv, const RV &urv) { 1042 Yrv = yrv; 1043 Drv = concat(yrv,urv); 1015 1044 Urv = urv; 1016 1045 } … … 1046 1075 //! If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save computational time. 1047 1076 bool evalll; 1077 1048 1078 public: 1049 1079 //! \name Constructors 1050 1080 //! @{ 1051 1081 1052 BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) { 1053 LIDs = -1;/*empty IDs*/ 1054 LFlags = 0; 1055 LFlags ( 0 ) = 1; /*log only mean*/ 1056 }; 1082 BM() : ll ( 0 ), evalll ( true ) { }; 1057 1083 BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 1058 1084 //! \brief Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! … … 1151 1177 //! Set boolean options from a string, recognized are: "logbounds,logll" 1152 1178 virtual void set_options ( const string &opt ) { 1153 LFlags ( 0 ) = 1;1154 1179 if ( opt.find ( "logbounds" ) != string::npos ) { 1155 LFlags ( 1 ) = 1; 1156 LFlags ( 2 ) = 1; 1180 const_cast<epdf&>(posterior()).set_log_level(2) ; 1181 } else { 1182 const_cast<epdf&>(posterior()).set_log_level(1) ; 1157 1183 } 1158 1184 if ( opt.find ( "logll" ) != string::npos ) { 1159 LFlags ( 3 ) = 1; 1160 } 1161 } 1162 //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] 1163 ivec LIDs; 1164 1165 //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs 1166 ivec LFlags; 1185 log_level = 1; 1186 } 1187 } 1188 1167 1189 //! Add all logged variables to a logger 1168 virtual void log_add ( logger &L, const string &name = "" ) { 1169 // internal 1170 RV r; 1171 if ( posterior().isnamed() ) { 1172 r = posterior()._rv(); 1173 } else { 1174 r = RV ( "est", posterior().dimension() ); 1175 }; 1176 1177 // Add mean value 1178 if ( LFlags ( 0 ) ) LIDs ( 0 ) = L.add ( r, name + "mean_" ); 1179 if ( LFlags ( 1 ) ) LIDs ( 1 ) = L.add ( r, name + "lb_" ); 1180 if ( LFlags ( 2 ) ) LIDs ( 2 ) = L.add ( r, name + "ub_" ); 1181 if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name ); //TODO: "local" RV 1182 } 1183 virtual void logit ( logger &L ) { 1184 L.logit ( LIDs ( 0 ), posterior().mean() ); 1185 if ( LFlags ( 1 ) || LFlags ( 2 ) ) { //if one of them is off, its LID==-1 and will not be stored 1186 vec ub, lb; 1187 posterior().qbounds ( lb, ub ); 1188 L.logit ( LIDs ( 1 ), lb ); 1189 L.logit ( LIDs ( 2 ), ub ); 1190 } 1191 if ( LFlags ( 3 ) ) L.logit ( LIDs ( 3 ), ll ); 1190 //! Log levels two digits: xy where 1191 //! * y = 0/1 log-likelihood is to be logged 1192 //! * x = level of the posterior (typically 0/1/2 for nothing/mean/bounds) 1193 virtual void log_register ( logger &L, const string &prefix = "" ) { 1194 root::log_register(L,prefix); 1195 1196 const_cast<epdf&>(posterior()).log_register(L, prefix+L.prefix_sep()+"apost"); 1197 1198 if ((log_level) > 0){ 1199 logrec->ids.set_size(1); 1200 logrec->ids(0) = L.add(RV("ll",1), prefix+L.prefix_sep()+"ll"); 1201 } 1202 } 1203 //! Save results to the given logger, details of what is stored is configured by \c LIDs and \c options 1204 virtual void log_write ( ) const { 1205 posterior().log_write(); 1206 if (log_level >0) { logrec->L.logit ( logrec->ids ( 0 ), ll );} 1192 1207 } 1193 1208 //!@} -
library/bdm/base/datasources.cpp
r665 r676 4 4 using namespace bdm; 5 5 6 void MemDS::getdata ( vec &dt ) {6 void MemDS::getdata ( vec &dt ) const { 7 7 int i; 8 8 -
library/bdm/base/datasources.h
r660 r676 38 38 public: 39 39 int max_length() {return Data.cols();} 40 void getdata ( vec &dt ) ;40 void getdata ( vec &dt ) const; 41 41 void getdata ( vec &dt, const ivec &indeces ); 42 42 void set_drv (const RV &drv,const RV &urv ); … … 89 89 set_drv(*r,RV()); //empty urv 90 90 dtsize=r->_dsize(); 91 ytsize = dtsize; 91 92 utsize=0; 92 93 } … … 109 110 dt=iepdf->sample(); 110 111 } 111 void getdata ( vec &dt_out ) {112 void getdata ( vec &dt_out ) const { 112 113 dt_out = dt; 113 114 } … … 167 168 ut2rgr.step(ut); //u is now history 168 169 } 169 void getdata ( vec &dt_out ) {170 void getdata ( vec &dt_out ) const { 170 171 bdm_assert_debug(dt_out.length()>=utsize+ytsize,"Short output vector"); 171 172 dt_out.set_subvector(0, yt); … … 249 250 } 250 251 //! no sense to log this type 251 void log_ add ( logger &L ){};252 void log_register(logger &L, const string &prefix){}; 252 253 //! no sense to log this type 253 void log it ( logger &L ){};254 void log_write ( ) const {}; 254 255 }; 255 256 … … 341 342 } 342 343 343 virtual void log_add ( logger &L ) { 344 DS::log_add ( L ); 345 L_xt = L.add ( IM->_rv(), "true" ); 346 } 347 virtual void logit ( logger &L ) { 348 DS::logit ( L ); 349 L.logit ( L_xt, xt ); 344 virtual void log_register(logger &L, const string &prefix){ 345 DS::log_register ( L, prefix ); //ids 0 and 1 346 347 logrec->ids.set_size(3,true);//copy 348 logrec->ids(2)=logrec->L.add ( IM->_rv(), "true" ); 349 } 350 virtual void log_write () { 351 DS::log_write ( ); 352 logrec->L.logit ( logrec->ids(2), xt ); 350 353 } 351 354 -
library/bdm/base/loggers.cpp
r565 r676 78 78 // for non-empty names 79 79 if ( names ( i ).length() > 0 ) { 80 scalarnames ( ii ) = names ( i ) + "_";80 scalarnames ( ii ) = names ( i ) + separator; 81 81 } 82 82 // add name … … 91 91 for ( k = 0; k < rvsize; k++ ) { //for all scalars in given RV 92 92 sprintf ( num, "%d", k ); 93 scalarnames ( ii ) += ( std::string ) "_"+ num;93 scalarnames ( ii ) += separator + num; 94 94 ii++; 95 95 } -
library/bdm/base/loggers.h
r660 r676 36 36 public: 37 37 //! convenience constructor 38 memlog ( int maxlen0, string itf = "" ) : maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {}38 memlog ( int maxlen0, string itf = "" ) :logger("."), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 39 39 40 40 //!Default constructor 41 memlog() : maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {}41 memlog() : logger("."), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} 42 42 43 43 //! Initialize storage