| 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 | |
| 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 | } |
863 | | /*! |
864 | | @brief Class for storing results (and semi-results) of an experiment |
865 | | |
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 variants |
873 | | Array<string> names; |
874 | | public: |
875 | | //!Default constructor |
876 | | logger() : entries ( 0 ), names ( 0 ) {} |
877 | | |
878 | | //! returns an identifier which will be later needed for calling the \c logit() function |
879 | | //! 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 ); // diff |
885 | | entries.set_length ( id + 1, true ); |
886 | | entries ( id ) = rv; |
887 | | } else { |
888 | | id = -1; |
889 | | } |
890 | | return id; // identifier of the last entry |
891 | | } |
892 | | |
893 | | //! log this vector |
894 | | virtual void logit ( int id, const vec &v ) { |
895 | | bdm_error ( "Not implemented" ); |
896 | | }; |
897 | | //! log this double |
898 | | 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 information |
908 | | virtual void finalize() {}; |
909 | | |
910 | | //! Initialize the storage |
911 | | virtual void init() {}; |
912 | | |
913 | | }; |
914 | | |
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 | } |
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 );} |