| 26 | | // Check the number of inputs and output arguments |
| 27 | | if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" |
| 28 | | "[Res,estimators,Res2]=estimator(system, estimators, experiment, logger)\n" |
| 29 | | " system = struct('class','datasource',...); % Estimated system\n" |
| 30 | | " estimators = {struct('class','estimator',...), % Estimators\n" |
| 31 | | " struct('class','estimator',...),...} \n" |
| 32 | | " === optional ===" |
| 33 | | " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n" |
| 34 | | " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n" |
| 35 | | "Output:\n" |
| 36 | | " Res Matlab structure with logged results, \n" |
| 37 | | " estimators Array of estimators updated with data \n" |
| 38 | | " Res2 When logfull log_level is on, this structure is filled with structures of posterior densities\n\n" |
| 39 | | "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); |
| | 26 | // Check the number of inputs and output arguments |
| | 27 | if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" |
| | 28 | "[Res,estimators,Res2]=estimator(system, estimators, experiment, logger)\n" |
| | 29 | " system = struct('class','datasource',...); % Estimated system\n" |
| | 30 | " estimators = {struct('class','estimator',...), % Estimators\n" |
| | 31 | " struct('class','estimator',...),...} \n" |
| | 32 | " === optional ===" |
| | 33 | " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n" |
| | 34 | " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n" |
| | 35 | "Output:\n" |
| | 36 | " Res Matlab structure with logged results, \n" |
| | 37 | " estimators Array of estimators updated with data \n" |
| | 38 | " Res2 When logfull log_level is on, this structure is filled with structures of posterior densities\n\n" |
| | 39 | "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); |
| 41 | | RV::clear_all(); |
| 42 | | //CONFIG |
| 43 | | UImxArray F; |
| 44 | | try { |
| 45 | | F.addGroup ( input[0],"system" ); |
| 46 | | F.addList ( input[1],"estimators" ); |
| 47 | | if ( n_input>2 ) { |
| 48 | | F.addGroup ( input[2],"experiment" ); |
| 49 | | } |
| 50 | | if ( n_input>3 ) { |
| 51 | | F.addGroup ( input[3],"logger" ); |
| 52 | | } |
| 53 | | } catch ( SettingException e ) { |
| 54 | | it_error ( "error: "+string ( e.getPath() ) ); |
| 55 | | } |
| | 41 | RV::clear_all(); |
| | 42 | //CONFIG |
| | 43 | UImxArray F; |
| | 44 | try { |
| | 45 | F.addGroup ( input[0],"system" ); |
| | 46 | F.addList ( input[1],"estimators" ); |
| | 47 | if ( n_input>2 ) { |
| | 48 | F.addGroup ( input[2],"experiment" ); |
| | 49 | } |
| | 50 | if ( n_input>3 ) { |
| | 51 | F.addGroup ( input[3],"logger" ); |
| | 52 | } |
| | 53 | } catch ( SettingException e ) { |
| | 54 | it_error ( "error: "+string ( e.getPath() ) ); |
| | 55 | } |
| 67 | | |
| 68 | | shared_ptr<logger> L = UI::build <logger>( F, "logger" ); |
| 69 | | shared_ptr<DS> pDS = UI::build<DS> ( F, "system" ); |
| 70 | | Array<shared_ptr<BM> > Es; // array of estimators |
| 71 | | UI::get( Es, F, "estimators" ); |
| 72 | | int nE = Es.length(); //number of estimators |
| 73 | | int Ndat; //number of data records |
| 74 | | F.lookupValue ( "experiment.ndat",Ndat ); |
| 75 | | |
| 76 | | if ( !L ) { |
| 77 | | #ifdef MEX |
| 78 | | //mex logger has only from_setting constructor - we have to fill it... |
| 79 | | L=new mexlog ( Ndat ); |
| 80 | | #else |
| 81 | | L=new stdlog(); |
| 82 | | #endif |
| | 72 | |
| | 73 | shared_ptr<logger> L = UI::build <logger>( F, "logger" ); |
| | 74 | shared_ptr<DS> pDS = UI::build<DS> ( F, "system" ); |
| | 75 | Array<shared_ptr<BM> > Es; // array of estimators |
| | 76 | UI::get( Es, F, "estimators" ); |
| | 77 | int nE = Es.length(); //number of estimators |
| | 78 | int Ndat; //number of data records |
| | 79 | F.lookupValue ( "experiment.ndat",Ndat ); |
| | 80 | |
| | 81 | if ( !L ) { |
| | 82 | #ifdef MEX |
| | 83 | //mex logger has only from_setting constructor - we have to fill it... |
| | 84 | L=new mexlog ( Ndat ); |
| | 85 | #else |
| | 86 | L=new stdlog(); |
| | 87 | #endif |
| | 88 | } |
| | 89 | pDS->log_register ( *L, "true" ); |
| | 90 | string Ename; |
| | 91 | for (int i=0; i<nE; i++) { |
| | 92 | try { |
| | 93 | UI::get ( Ename, F.getRoot()["estimators"][i], "name",UI::optional ); |
| | 94 | } catch ( ...) { |
| | 95 | Ename="Est"+num2str ( i ); |
| | 96 | } |
| | 97 | Es(i)->log_register(*L,Ename); // estimate |
| | 98 | } |
| | 99 | L->init(); |
| | 100 | |
| | 101 | vec dt=zeros ( pDS->_drv()._dsize() ); //data variable |
| | 102 | Array<datalink*> Dls(nE); |
| | 103 | Array<datalink*> Dlsc(nE); |
| | 104 | Array<datalink_buffered*> Dls_buf (0); |
| | 105 | for ( int i=0; i<Es.length(); i++ ) { |
| | 106 | //check if they are properly named |
| | 107 | bdm_assert(Es(i)->_yrv()._dsize() == Es(i)->dimensiony(), "Estimator["+num2str(i)+"] does not name yrv properly." |
| | 108 | "size(yrv)="+num2str(Es(i)->_yrv()._dsize() ) + ", declared dimension of y="+num2str(Es(i)->dimensiony())); |
| | 109 | bdm_assert(Es(i)->_rvc()._dsize() == Es(i)->dimensionc(), "Estimator["+num2str(i)+"] does not name rvc properly." |
| | 110 | "size(rvc)="+num2str(Es(i)->_rvc()._dsize() ) + ", declared dimension of rvc="+num2str(Es(i)->dimensionc())); |
| | 111 | //connect actual data |
| | 112 | Dls ( i ) = new datalink ( Es ( i )->_yrv(),pDS->_drv() ); //datalink between a datasource and estimator |
| | 113 | //connect data in condition |
| | 114 | if (Es ( i )->_rvc().mint()<0) { |
| | 115 | //delayed values are required |
| | 116 | |
| | 117 | //create delayed dl |
| | 118 | int ith_buf=Dls_buf.size(); |
| | 119 | Dls_buf.set_size( ith_buf + 1, true); |
| | 120 | Dls_buf(ith_buf) = new datalink_buffered(); |
| | 121 | //add dl to list of buffered DS |
| | 122 | Dlsc(i) = Dls_buf(ith_buf); |
| | 123 | Dlsc(i)->set_connection ( Es ( i )->_rvc(),pDS->_drv() ); //datalink between a datasource and estimator |
| | 124 | |
| | 125 | bdm_assert_debug(Dlsc(i)->_downsize() == Es ( i )->_rvc()._dsize(), "Data required by Est[" + num2str(i) + "], " + |
| | 126 | Es(i)->_rvc().to_string() + ", are not available in DS drv:" + pDS->_drv().to_string();); |
| | 127 | } else { |
| | 128 | Dlsc ( i ) = new datalink ( Es ( i )->_rvc(),pDS->_drv() ); //datalink between a datasource and estimator |
| | 129 | } |
| | 130 | } |
| | 131 | |
| | 132 | // Main cycle |
| | 133 | for ( int tK=1;tK<Ndat;tK++ ) { |
| | 134 | // Data Source |
| | 135 | pDS->getdata ( dt ); // read data |
| | 136 | pDS->log_write (); |
| | 137 | |
| | 138 | // Estimators |
| | 139 | for (int i=0; i<nE; i++) { |
| | 140 | Es(i)->bayes ( Dls(i)->pushdown ( dt ), Dlsc(i)->pushdown(dt) ); // update estimates |
| | 141 | |
| | 142 | Es(i)->log_write (); |
| | 143 | } |
| | 144 | // Regulators |
| | 145 | L->step(); |
| | 146 | pDS->step(); // simulator step |
| | 147 | for (int i=0; i<Dls_buf.length(); i++){ |
| | 148 | Dls_buf(i)->store_data(dt); |
| 84 | | pDS->log_register ( *L, "true" ); |
| 85 | | string Ename; |
| 86 | | for (int i=0; i<nE; i++){ |
| 87 | | try { |
| 88 | | UI::get ( Ename, F.getRoot()["estimators"][i], "name",UI::optional ); |
| 89 | | } catch ( ...) { |
| 90 | | Ename="Est"+num2str ( i ); |
| 91 | | } |
| 92 | | Es(i)->log_register(*L,Ename); // estimate |
| 93 | | } |
| 94 | | L->init(); |
| 95 | | |
| 96 | | vec dt=zeros ( pDS->_drv()._dsize() ); //data variable |
| 97 | | Array<datalink*> Dls(nE); |
| 98 | | Array<datalink*> Dlsc(nE); |
| 99 | | for (int i=0; i<nE; i++){ |
| 100 | | Dls(i)=new datalink( Es(i)->_yrv(),pDS->_drv() ); //datalink between a datasource and estimator |
| 101 | | Dlsc(i)=new datalink( Es(i)->_rvc(),pDS->_drv() ); //datalink between a datasource and estimator |
| 102 | | } |
| 103 | | |
| 104 | | // Main cycle |
| 105 | | for ( int tK=1;tK<Ndat;tK++ ) { |
| 106 | | // Data Source |
| 107 | | pDS->step(); // simulator step |
| 108 | | pDS->getdata ( dt ); // read data |
| 109 | | pDS->log_write (); |
| 110 | | |
| 111 | | // Estimators |
| 112 | | for (int i=0; i<nE; i++){ |
| 113 | | Es(i)->bayes ( Dls(i)->pushdown ( dt ), Dlsc(i)->pushdown(dt) ); // update estimates |
| 114 | | |
| 115 | | Es(i)->log_write (); |
| 116 | | } |
| 117 | | // Regulators |
| 118 | | L->step(); |
| 125 | | if ( mL ) { // user wants output!! |
| 126 | | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
| 127 | | output[0] = mL->toCell(); |
| 128 | | if (n_output>1){ // write estimators |
| 129 | | UImxArray Ests; |
| 130 | | UI::save(Es, Ests,"estimators"); |
| 131 | | output[1]=UImxArray::create_mxArray(Ests); |
| 132 | | } |
| 133 | | if (n_output>2) { |
| 134 | | mL->_setting_conf().setAutoConvert(true); |
| 135 | | output[2]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); |
| 136 | | } |
| 137 | | } |
| | 156 | if ( mL ) { // user wants output!! |
| | 157 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
| | 158 | output[0] = mL->toCell(); |
| | 159 | if (n_output>1) { // write estimators |
| | 160 | UImxArray Ests; |
| | 161 | UI::save(Es, Ests,"estimators"); |
| | 162 | output[1]=UImxArray::create_mxArray(Ests); |
| | 163 | } |
| | 164 | if (n_output>2) { |
| | 165 | mL->_setting_conf().setAutoConvert(true); |
| | 166 | output[2]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); |
| | 167 | } |
| | 168 | } |