Changeset 685

Show
Ignore:
Timestamp:
10/29/09 23:23:56 (14 years ago)
Author:
smidl
Message:

mex tutorial cleanup + working mexDS

Files:
7 modified
4 moved

Legend:

Unmodified
Added
Removed
  • applications/bdmtoolbox/mex/estimator.cpp

    r676 r685  
    145145        Setting &S=Cfg; 
    146146        for ( int i=0; i<Es.length(); i++ ) { 
    147                 try { 
    148                         UI::get ( Ename, S["estimators"][i], "name" ); 
    149                 } catch ( UIException e ) { 
     147                if (!UI::get ( Ename, S["estimators"][i], "name" ,UI::optional)){ 
    150148                        Ename="Est"+num2str ( i ); 
    151149                } 
     
    157155        vec dt=zeros ( Ds->_drv()._dsize() );   //data variable 
    158156        Array<datalink*> Dls ( Es.length() ); 
     157        Array<datalink*> Dlsc ( Es.length() ); 
    159158        Array<datalink_buffered*> Dls_buf (0); 
    160159        for ( int i=0; i<Es.length(); i++ ) { 
    161                 if (Es ( i )->_drv().mint()<0){  
     160                //connect actual data 
     161                Dls ( i ) = new datalink ( Es ( i )->_yrv(),Ds->_drv() ); //datalink between a datasource and estimator 
     162                //connect data in condition 
     163                if (Es ( i )->_rvc().mint()<0){  
    162164                        //delayed values are required 
    163165                         
     
    167169                        Dls_buf(ith_buf) = new datalink_buffered();  
    168170                        //add dl to list of buffered DS 
    169                         Dls(i) = Dls_buf(ith_buf); 
    170                         Dls(i)->set_connection ( Es ( i )->_drv(),Ds->_drv() ); //datalink between a datasource and estimator 
     171                        Dlsc(i) = Dls_buf(ith_buf); 
     172                        Dlsc(i)->set_connection ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator 
    171173                         
    172174                } else { 
    173                         Dls ( i ) = new datalink ( Es ( i )->_drv(),Ds->_drv() ); //datalink between a datasource and estimator 
     175                        Dlsc ( i ) = new datalink ( Es ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator 
    174176                } 
    175177        } 
     
    180182 
    181183                for ( int i=0; i<Es.length(); i++ ) { 
    182                         Es ( i )->bayes ( Dls ( i )->pushdown ( dt ) );         // update estimates 
     184                        Es ( i )->bayes ( Dls ( i )->pushdown ( dt ), Dlsc(i) ->pushdown(dt) );         // update estimates 
    183185                        Es ( i )->log_write (); 
    184186                } 
     
    204206        } 
    205207#endif 
    206         for (int i=0;i<Dls.length(); i++){delete Dls(i);} 
     208        for (int i=0;i<Dls.length(); i++){delete Dls(i); delete Dlsc(i);} 
    207209} 
  • applications/bdmtoolbox/mex/merger.cpp

    r622 r685  
    5858#endif 
    5959        // Sources 
    60         Array<shared_ptr<mpdf> > Sources; 
    61         //abuse Mer to store sources 
    62         Setting& _Sources = Cfg.lookup ( "Sources" ); 
    63         int Slen = _Sources.getLength(); 
    64         Sources.set_size ( Slen ); 
    65         for ( int i = 0; i < Slen; i++ ) { 
    66                 try { 
    67                         shared_ptr<mpdf> mtmp = UI::build<mpdf> ( _Sources, i ); 
    68                         Sources ( i ) = mtmp; 
    69                 } catch ( UIException ) { 
    70                         // it is not mpdf - see if it is epdf 
    71                         try { 
    72                                 shared_ptr<epdf> etmp = UI::build<epdf> ( _Sources, i ); 
    73                                 if ( etmp ) { 
    74                                         Sources ( i ) = new mepdf ( etmp ); // hopefully OK 
    75                                 } 
    76                         } catch ( UIException &e ) { 
    77                                 it_error ( "No mpdfs or epdfs found! " + string ( e.what() ) ); 
    78                         } catch ( std::exception e ) { 
    79                                 it_error ( "Error in UI at " + _Sources[i].getPath() ); 
    80                         } 
    81                 } catch ( std::exception &e ) { 
    82                         it_error ( "Error in UI at " + _Sources[i].getPath() ); 
    83                 } 
    84         } 
    85  
     60        Array<shared_ptr<mpdf> >  Sources; 
     61        UI::get(Sources, Cfg, "Sources", UI::compulsory); 
    8662        shared_ptr<merger_base> Merger = UI::build<merger_base> ( Cfg, "Merger" ); 
    8763 
    8864        // Support 
    89         try { 
    90                 shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> ( Cfg, "Support" ); 
     65        try{ 
     66        shared_ptr<rectangular_support> RecSup = UI::build<rectangular_support> ( Cfg, "Support" ,UI::optional); 
     67        if(RecSup){ 
    9168                Merger->set_support ( *RecSup ); 
    92         } catch ( UIException &e ) { 
    93                 shared_ptr<discrete_support> DisSup = UI::build<discrete_support> ( Cfg, "Support" ); 
    94                 Merger->set_support ( *DisSup ); 
     69        }  
     70        } catch (UIException &e) { 
     71                shared_ptr<discrete_support> DisSup = UI::build<discrete_support> ( Cfg, "Support", UI::optional ); 
     72                if (DisSup){ 
     73                        Merger->set_support ( *DisSup ); 
     74                } else{bdm_error("Support not defined");} 
    9575        } 
    9676// COMPUTE RESULTS 
  • library/bdm/base/bdmbase.h

    r682 r685  
    594594                } 
    595595        } 
     596        void log_write() const { 
     597                if (log_level>0) { 
     598                        logrec->L.logit( logrec->ids(0), mean() ); 
     599                } 
     600                if (log_level>2) { 
     601                        vec lb; vec ub; 
     602                        qbounds(lb,ub); 
     603                        logrec->L.logit( logrec->ids(1), lb ); 
     604                        logrec->L.logit( logrec->ids(2), ub ); 
     605                } 
     606        } 
    596607        //!@} 
    597608 
     
    951962public: 
    952963        //! default constructors 
    953                 DS() : Drv(), Urv(),Yrv() {log_level=1;}; 
     964        DS() : dtsize(0),utsize(0),ytsize(0),Drv(), Urv(),Yrv() {log_level=1;}; 
    954965 
    955966        //! 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(). 
     
    9891000                if (log_level >0){ 
    9901001                        logrec->ids.set_size(2); 
    991                         logrec->ids(0) = logrec->L.add ( Yrv, "" ); 
    992                         logrec->ids(1) = logrec->L.add ( Urv, "" ); 
     1002                        logrec->ids(0) = logrec->L.add ( Yrv, prefix ); 
     1003                        logrec->ids(1) = logrec->L.add ( Urv, prefix ); 
    9931004                } 
    9941005        } 
     
    11941205        //!@} 
    11951206        void from_setting ( const Setting &set ) { 
    1196                 shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
     1207                shared_ptr<RV> r = UI::build<RV> ( set, "yrv", UI::optional ); 
    11971208                if ( r ) { 
    11981209                        set_yrv ( *r ); 
    11991210                } 
     1211                shared_ptr<RV> r2 = UI::build<RV> ( set, "rvc", UI::optional ); 
     1212                if ( r2 ) { 
     1213                        rvc =   *r2; 
     1214                } 
     1215                shared_ptr<RV> r3 = UI::build<RV> ( set, "rv", UI::optional ); 
     1216                if ( r3 ) { 
     1217                        set_rv(*r3); 
     1218                } 
     1219                 
    12001220                string opt; 
    12011221                if ( UI::get ( opt, set, "options", UI::optional ) ) { 
  • library/bdm/base/loggers.h

    r676 r685  
    3636public: 
    3737        //! convenience constructor 
    38         memlog ( int maxlen0, string itf = "" ) :logger("."), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 
     38        memlog ( int maxlen0, string itf = "" ) :logger("_"), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {} 
    3939 
    4040        //!Default constructor 
    41         memlog() : logger("."), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} 
     41        memlog() : logger("_"), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {} 
    4242 
    4343        //! Initialize storage 
  • library/bdm/mex/mex_BM.h

    r679 r685  
    8080                        mxArray *in[2]; 
    8181                        in[0] = data; 
    82                         in[1] = mxCreateDoubleMatrix(dt.size(), 1, mxREAL); 
    83                         vec2mxArray(dt, in[1]); 
     82                        in[1] = mxCreateDoubleMatrix(yt.size(), 1, mxREAL); 
     83                        vec2mxArray(yt, in[1]); 
    8484                        mexCallMATLAB(1, &tmp, 2, in, (name+"_bayes").c_str()); 
    8585                        old = data; 
  • library/bdm/mex/mex_datasource.h

    r676 r685  
    9191        \code 
    9292        system={ 
    93                 type="mexDS"; 
     93                class="mexDS"; 
    9494                step_name="";              // name of function to call 
    9595                input_name="";             // name of workspace variable where inputs are written 
    96                 rv_out = {class='RV',...}  // identification of outputs 
    97                 rv_in = {class='RV',...}   // identification of inputs 
     96                yrv = {class='RV',...}  // identification of outputs 
     97                urv = {class='RV',...}   // identification of inputs 
    9898                }; 
    9999        \endcode 
     
    106106                UI::get(input_name, set, "input_name", UI::compulsory); 
    107107                 
    108                 shared_ptr<RV> ry = UI::build<RV> ( set, "rv_out", UI::compulsory ); 
    109                 shared_ptr<RV> ru = UI::build<RV> ( set, "rv_in", UI::compulsory); 
     108                shared_ptr<RV> ry = UI::build<RV> ( set, "yrv", UI::compulsory ); 
     109                shared_ptr<RV> ru = UI::build<RV> ( set, "urv", UI::compulsory); 
    110110 
    111                 dtsize=ry->_dsize(); 
     111                ytsize=ry->_dsize(); 
    112112                utsize=ru->_dsize(); 
    113  
     113                dtsize = ytsize+utsize; 
     114                 
    114115                set_drv(*ry, *ru); 
     116                validate(); 
    115117        } 
    116118         
    117119        void step() { 
    118120                mxArray* tmp; 
    119                 mxArray* tmp2; 
     121                mxArray* dummy=NULL; 
    120122                // write inputs to variable input_name 
    121123                mxArray* mxinp= mexGetVariable ( "global", input_name.c_str()) ; 
     124                if (mxinp){ 
     125                        if((int)mxGetM(mxinp)!=utsize || (int)mxGetN(mxinp)!=utsize) { 
     126                                // mxinp is invalid - create new one 
     127                                mxDestroyArray(mxinp); 
     128                                mxinp=mxCreateDoubleMatrix(utsize,1,mxREAL); 
     129                        } 
     130                         
     131                } else { 
     132                        mxinp=mxCreateDoubleMatrix(utsize,1,mxREAL); 
     133                } 
    122134                vec2mxArray(ut, mxinp); 
     135                mexPutVariable("global",input_name.c_str(),mxinp); 
    123136                // call function step_name 
    124                 mexCallMATLAB(1, &tmp, 0, (mxArray **) &tmp2, step_name.c_str()); 
     137                mexCallMATLAB(1, &tmp, 0, (mxArray **) &dummy, step_name.c_str()); 
    125138                // save its results 
    126                 dt=mxArray2vec(tmp); 
     139                bdm_assert_debug((int)mxGetM(tmp)==ytsize || (int)mxGetN(tmp)==ytsize,"mexDS.step() expected return vector of length " + num2str(dtsize) + 
     140                "got vector " + num2str((int)mxGetM(tmp)) + "x" + num2str((int)mxGetN(tmp))); 
     141                //write  y 
     142                dt.set_subvector(0,mxArray2vec(tmp)); 
     143                //write u 
     144                dt.set_subvector(ytsize,ut); 
    127145        } 
    128         void write(const vec &ut0){ ut=ut0;} 
    129         void getdata(vec &dt_out) const {dt_out = dt;   } 
     146        void write(const vec &ut0){  
     147                bdm_assert_debug(ut0.length()==ut.length(),"mexDS: Incompatible input vector"); 
     148                ut=ut0; 
     149        } 
     150        void getdata(vec &dt_out) const { 
     151                bdm_assert_debug(dt_out.length()==dt.length(),"mexDS: Incompatible output vector"); 
     152                dt_out = dt;     
     153        } 
    130154 
     155        void validate() { 
     156                dt=zeros(dtsize); 
     157                ut=zeros(utsize); 
     158        } 
    131159 
    132160        // TODO dodelat void to_setting( Setting &set ) const; 
  • library/bdm/mex/mex_logger.h

    r665 r685  
    6262                        istart = 0; 
    6363                        for ( int j = 0; j < entries ( i ).length(); j++ ) { // same for as in add!!! 
    64                                 vec_name = names ( i ) + entries ( i ).name ( j ); 
     64                                vec_name = names ( i ) +prefix_sep()+ entries ( i ).name ( j ); 
    6565                                iend = istart + entries ( i ).size ( j ) - 1; 
    6666                                M = vectors ( i ).get_cols ( istart, iend );