Changeset 676
- Timestamp:
- 10/22/09 01:13:47 (15 years ago)
- Files:
-
- 14 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/bdmtoolbox/mex/estimator.cpp
r658 r676 141 141 } 142 142 143 Ds->log_ add ( *L);143 Ds->log_register ( *L, "DS" ); 144 144 string Ename; 145 145 Setting &S=Cfg; … … 151 151 } 152 152 153 Es ( i )->log_ add( *L,Ename ); // estimate153 Es ( i )->log_register ( *L,Ename ); // estimate 154 154 } 155 155 L->init(); … … 177 177 for ( int tK=0;tK<Ndat;tK++ ) { 178 178 Ds->getdata ( dt ); // read data 179 Ds->log it ( *L);179 Ds->log_write ( ); 180 180 181 181 for ( int i=0; i<Es.length(); i++ ) { 182 182 Es ( i )->bayes ( Dls ( i )->pushdown ( dt ) ); // update estimates 183 Es ( i )->log it ( *L);183 Es ( i )->log_write (); 184 184 } 185 185 -
applications/bdmtoolbox/mex/simulator.cpp
r627 r676 125 125 } 126 126 127 Ds->log_ add ( *L);127 Ds->log_register ( *L, "DS" ); 128 128 L->init(); 129 129 … … 132 132 for ( int tK=0;tK<Ndat;tK++ ) { 133 133 Ds->getdata ( dt ); // read data 134 Ds->log it ( *L);134 Ds->log_write ( ); 135 135 136 136 L->step(); -
applications/pmsm/cfg/sim.cfg
r654 r676 9 9 params = "pmsm107@zcu.cfg"; 10 10 tstep = 1.0; // steps for profile in [s] 11 profileW = [1, 5, 0, -3, 0, 0, 0, 1, 0, 0 ];11 profileW = [1, 40, 0, -40, 0, 0, 0, 1, 0, 0 ]; 12 12 opt = "u"; 13 13 }; -
applications/pmsm/pmsmDS.h
r654 r676 52 52 opt_modu = ( opt.find ( "modelu" ) !=string::npos ); 53 53 } 54 void getdata ( vec &dt ) 54 void getdata ( vec &dt ) const 55 55 { 56 56 dt.set_subvector(0,vec ( KalmanObs,6 )); … … 114 114 }; 115 115 116 void log_ add( logger &L )116 void log_register ( logger &L ) 117 117 { 118 118 L_x = L.add ( rx, "x" ); … … 127 127 } 128 128 129 void log it( logger &L )129 void log_write ( logger &L ) 130 130 { 131 131 L.logit ( L_x, vec ( x,4 ) ); -
applications/pmsm/pmsm_estim.cpp
r666 r676 30 30 F.lookupValue ( "experiment.ndat",Ndat ); 31 31 32 pDS->log_ add ( *L);33 string nic="";32 pDS->log_register ( *L, "true" ); 33 string Ename; 34 34 for (int i=0; i<nE; i++){ 35 Es(i)->log_add(*L,nic); // estimate 35 try { 36 UI::get ( Ename, F.getRoot()["estimators"][i], "name",UI::optional ); 37 } catch ( ...) { 38 Ename="Est"+num2str ( i ); 39 } 40 Es(i)->log_register(*L,Ename); // estimate 36 41 } 37 42 L->init(); … … 48 53 pDS->step(); // simulator step 49 54 pDS->getdata ( dt ); // read data 50 pDS->log it ( *L);55 pDS->log_write (); 51 56 52 57 // Estimators … … 54 59 Es(i)->bayes ( Dls(i)->pushdown ( dt ) ); // update estimates 55 60 56 Es(i)->log it (*L);61 Es(i)->log_write (); 57 62 } 58 63 // Regulators -
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 -
library/bdm/bdmroot.h
r673 r676 1 1 2 /*! 2 3 \file … … 24 25 25 26 namespace bdm { 26 27 28 //forward declaration 29 class logger; 30 31 //! information about connection to a logger 32 class log_record { 33 public: 34 //!remember which logger is registered 35 logger &L; 36 //! vector of log IDs - one element for each entry 37 ivec ids; 38 39 //!default constructor 40 log_record(logger &L0): L(L0),ids(0){} 41 }; 42 27 43 //! Root class of BDM objects 28 44 class root { 29 45 protected: 30 //! level of details that will be logged to logger 46 //! record of connections to the logger 47 log_record* logrec; 48 //! level of details that will be logged to a logger 31 49 int log_level; 32 //! vector of log IDs - one element for each entry 33 ivec log_ids; 50 34 51 public: 52 //!default constructor 53 root() : logrec(NULL),log_level(0) {}; 54 35 55 //! make sure this is a virtual object 36 56 virtual ~root() { 57 if (logrec) delete logrec; 37 58 } 38 59 39 //! This method returns a basicinfo about the current instance60 //! Returns a basic textual info about the current instance 40 61 virtual string to_string() const { 41 62 return ""; 42 63 } 64 //! Register itself in a logger, i.e. allocate space for data from this class 65 //! The level of details (parameter \c level ) is individual for each class. 66 virtual void log_register(logger &L, const string &prefix){ 67 logrec = new log_record(L); 68 } 69 70 //! Write current information into the given logger 71 virtual void log_write() const { 72 } 73 //! set level of details to be logged - needs to be called before log_register! 74 virtual void set_log_level(int level) {log_level = level;} 43 75 44 //! This method arrangeinstance properties according the data stored in the Setting structure76 //! Read instance properties according the data stored in the Setting structure 45 77 virtual void from_setting ( const Setting &set ) { 46 78 } 47 79 48 //! This method save all the instance properties into the Setting structure80 //! Save all the instance properties into the Setting structure 49 81 virtual void to_setting ( Setting &set ) const { 50 82 } 51 83 52 //! This method checks that all internal structures has been correctly set-up, always call at the ned of from_setting84 //! Check that all internal structures has been correctly set-up. Called at the end of from_setting. 53 85 virtual void validate() { 54 86 } 87 55 88 }; 56 89 -
library/bdm/design/ctrlbase.h
r660 r676 146 146 const RV& _drv() {return drv;} 147 147 //! register this controller with given datasource under name "name" 148 virtual void log_ add ( logger &L, const string &name = "") { }148 virtual void log_register (logger &L, int level, const string &prefix ) { } 149 149 //! write requested values into the logger 150 virtual void log it ( logger &L ){ }150 virtual void log_write ( ) const { } 151 151 152 152 }; -
library/bdm/estim/particles.h
r675 r676 50 50 //! \name Options 51 51 //!@{ 52 53 //! Log all samples54 bool opt_L_smp;55 //! Log all samples56 bool opt_L_wei;57 52 //!@} 58 53 … … 60 55 //! \name Constructors 61 56 //!@{ 62 PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ), opt_L_smp ( false ), opt_L_wei ( false ) { 63 LIDs.set_size ( 5 ); 57 PF ( ) : est(), _w ( est._w() ), _samples ( est._samples() ) { 64 58 }; 65 59 … … 89 83 void set_options ( const string &opt ) { 90 84 BM::set_options ( opt ); 91 opt_L_wei = ( opt.find ( "logweights" ) != string::npos );92 opt_L_smp = ( opt.find ( "logsamples" ) != string::npos );93 85 } 94 86 //! bayes I - generate samples and add their weights to lls -
library/bdm/mex/mex_datasource.h
r660 r676 45 45 rowid = linspace(0,Data.rows()-1); 46 46 dtsize=rowid.length(); 47 ytsize=rowid.length(); 47 48 utsize=0; 48 49 … … 126 127 } 127 128 void write(const vec &ut0){ ut=ut0;} 128 void getdata(vec &dt_out) {dt_out = dt; }129 void getdata(vec &dt_out) const {dt_out = dt; } 129 130 130 131