Changeset 896 for library/bdm/base

Show
Ignore:
Timestamp:
04/09/10 09:31:37 (14 years ago)
Author:
mido
Message:

cleanup of MemDS and its descendants
bdmtoolbox/CMakeLists.txt slightly changed to avoid unnecessary MEX condition
"indeces" replaced by "indices"

Location:
library/bdm/base
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.cpp

    r889 r896  
    522522        // decide if we need to copy val to history 
    523523        if ( Hrv._dsize() > 0 ) { 
    524                 v2h_up = rv_hist0.dataind ( rv_up ); // indeces of elements of rv_up to be copied 
     524                v2h_up = rv_hist0.dataind ( rv_up ); // indices of elements of rv_up to be copied 
    525525        } // else v2h_up is empty 
    526526 
     
    535535        ivec ind_H; 
    536536        ivec ind_h0; 
    537         Hrv.dataind ( rv1, ind_H, ind_h0 ); // find indeces of rv in 
     537        Hrv.dataind ( rv1, ind_H, ind_h0 ); // find indices of rv in 
    538538        set_subvector ( history, ind_H, hist0 ( ind_h0 ) ); // copy given hist to appropriate places 
    539539} 
  • library/bdm/base/bdmbase.h

    r895 r896  
    594594        //!@} 
    595595        void from_setting ( const Setting &set ) { 
     596                root::from_setting( set ); 
    596597                shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 
    597598                if ( r ) { 
     
    740741class datalink_part : public datalink { 
    741742protected: 
    742         //! indeces of values in vector downsize 
     743        //! indices of values in vector downsize 
    743744        ivec v2v_down; 
    744745public: 
     
    760761        //! rv of the history 
    761762        RV Hrv; 
    762         //! h2v : indeces in down 
     763        //! h2v : indices in down 
    763764        ivec h2v_down; 
    764         //! h2v : indeces in history 
     765        //! h2v : indices in history 
    765766        ivec h2v_hist; 
    766         //! v2h: indeces of up too be pushed to h 
     767        //! v2h: indices of up too be pushed to h 
    767768        ivec v2h_up; 
    768769public: 
     
    932933        virtual void getdata ( vec &dt ) const = 0; 
    933934 
    934         //! Returns data records at indeces. Default is inefficent. 
    935         virtual void getdata ( vec &dt, const ivec &indeces ) { 
     935        //! Returns data records at indices. Default is inefficent. 
     936        virtual void getdata ( vec &dt, const ivec &indices ) { 
    936937                vec tmp(dtsize); 
    937938                getdata(tmp); 
    938                 dt = tmp(indeces); 
     939                dt = tmp(indices); 
    939940        }; 
    940941 
     
    942943        virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID; 
    943944 
    944         //! Accepts action variables at specific indeces 
    945         virtual void write ( const vec &ut, const ivec &indeces ) NOT_IMPLEMENTED_VOID; 
     945        //! Accepts action variables at specific indices 
     946        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; 
    946947 
    947948        //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. 
     
    960961                return Urv; 
    961962        } 
     963 
    962964        //! set random variables 
    963965        virtual void set_drv ( const  RV &drv, const RV &urv) { 
  • library/bdm/base/datasources.cpp

    r895 r896  
    1  
    21#include "datasources.h" 
    32 
    43using namespace bdm; 
    54 
    6 void MemDS::getdata ( vec &dt ) const { 
    7         int i; 
    8  
    9         bdm_assert_debug ( dt.length() == rowid.length(), "MemDS:getdata incompatible dt" ); 
    10         for ( i = 0; i < rowid.length(); i++ ) { 
    11                 dt ( i ) = Data ( rowid ( i ), time ); 
    12         } 
     5MemDS::MemDS ( mat &Dat ) : Data( Dat ) { 
    136} 
    147 
    15 void MemDS::getdata ( vec &dt, const ivec &indeces ) { 
    16         int j, i; 
    17         bdm_assert_debug ( dt.length() == indeces.length(), "MemDS:getdata incompatible dt" ); 
    18         for ( i = 0; i < indeces.length(); i++ ) { 
    19                 j = indeces ( i ); 
    20                 dt ( i ) = Data ( rowid ( j ), time ); 
    21         } 
    22 } 
    23  
    24 void MemDS::step() { 
    25         if ( time < Data.cols() ) { 
     8void MemDS::step( ) { 
     9        if ( time < max_length() ) { 
    2610                time++; 
    2711        } 
    2812} 
    2913 
    30 void MemDS::set_drv ( const RV &drv, const RV &urv ) { 
    31         bdm_assert_debug ( drv._dsize() == rowid.length(), "MemDS::set_rvs incompatible drv" ); 
    32         bdm_assert_debug ( urv._dsize() == 0, "MemDS does not support urv." ); 
    33  
    34         DS::set_drv ( drv, urv ); 
     14void MemDS::getdata ( vec &dt ) const { 
     15        bdm_assert_debug ( dt.length() == Data.rows(), "MemDS:getdata incompatible dt" ); 
     16        dt = Data.get_col ( time ); 
    3517} 
    3618 
    37 MemDS::MemDS ( mat &Dat, ivec &rowid0 ) : rowid ( rowid0 ) { 
    38         bdm_assert_debug ( max ( rowid ) <= Dat.rows(), "MemDS rowid is too high for given Dat." ); 
     19void MemDS::getdata ( vec &dt, const ivec &indices ) { 
     20        bdm_assert_debug ( dt.length() == indices.length(), "MemDS:getdata incompatible dt" ); 
     21        vec tmp = Data.get_col ( time ); 
     22        dt = tmp ( indices ); 
     23} 
     24 
     25void MemDS::set_drv ( const RV &drv ) { 
     26        bdm_assert_debug ( drv._dsize() == Data.rows(), "MemDS::set_rvs incompatible drv" ); 
     27        DS::set_drv ( drv, RV() ); 
     28} 
     29 
     30void MemDS::from_setting ( const Setting &set ) { 
     31        DS::from_setting ( set ); 
     32 
     33        UI::get ( Data, set, "Data", UI::compulsory ); 
     34         
     35        if ( !UI::get ( time, set, "time", UI::optional ) ) { 
     36                time = 0; 
     37        } 
     38         
     39        shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
     40        if ( !r ) { 
     41                r = new RV(); 
     42                for ( int i = 0; i < Data.rows(); i++ ) { 
     43                        r->add ( RV ( "ch" + num2str ( i ), 1, 0 ) ); 
     44                } 
     45        } 
     46         
     47        set_drv ( *r ); 
     48        dtsize = r->_dsize(); 
     49        utsize = 0; 
     50} 
     51 
     52void FileDS::from_setting ( const Setting & set ) { 
     53        // here we do not call MemDS::from_setting intentionally as we can not load any Data matrix directly 
     54        DS::from_setting ( set ); 
     55 
     56        string filename; 
     57        UI::get ( filename, set, "filename", UI::compulsory ); 
    3958 
    4059        time = 0; 
    41         Data = Dat; 
     60 
     61        shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
     62        if ( !r ) { 
     63                r = new RV(); 
     64                for ( int i = 0; i < Data.rows(); i++ ) { 
     65                        r->add ( RV ( "ch" + num2str ( i ), 1, 0 ) ); 
     66                } 
     67        } 
     68         
     69        set_drv ( *r ); 
     70        dtsize = r->_dsize(); 
     71        utsize = 0; 
    4272} 
    4373 
    44 void ITppFileDS::from_setting ( const Setting &set ) { 
    45         shared_ptr<RV> rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory ); 
     74void CsvFileDS::from_setting ( const Setting & set ) { 
     75        FileDS::from_setting ( set ); 
    4676 
    47         it_file it ( set["filename"] ); 
    48         it << Name ( set["varname"] ); 
     77        vec data_line; 
     78        string line; 
     79 
     80        ifstream fs; 
     81        fs.open ( filename.c_str() ); 
     82        if ( fs.is_open() ) { 
     83                while ( getline ( fs, line ) ) { 
     84                        data_line.set ( line ); 
     85                        Data.append_row ( data_line ); 
     86                } 
     87        } 
     88 
     89        string orientation; 
     90        if( UI::get ( orientation, set, "orientation", UI::optional ) & orientation == "BY_ROW" )  
     91                transpose ( Data, Data ); 
     92} 
     93 
     94void ITppFileDS::from_setting ( const Setting & set ) { 
     95        FileDS::from_setting ( set ); 
     96 
     97        string varname; 
     98        UI::get ( varname, set, "varname", UI::compulsory ); 
     99 
     100        it_file it ( filename ); 
     101        it << Name ( varname ); 
    49102        it >> Data; 
    50         time = 0; 
    51         //rowid and delays are ignored 
    52         rowid = linspace ( 0, Data.rows() - 1 ); 
    53         set_drv ( *rvtmp, RV() ); 
    54103} 
    55104 
     
    62111} 
    63112 
    64 void PdfDS::getdata ( vec &dt_out ) const { 
     113void PdfDS::getdata ( vec & dt_out ) const { 
    65114        bdm_assert_debug ( dt_out.length() >= dtsize, "Short output vector" ); 
    66115        dt_out.set_subvector ( 0, yt ); 
     
    85134} 
    86135 
    87 void StateDS::from_setting ( const Setting &set ) { 
     136void StateDS::from_setting ( const Setting & set ) { 
    88137        IM = UI::build<pdf> ( set, "IM", UI::compulsory ); 
    89138        OM = UI::build<pdf> ( set, "OM", UI::compulsory ); 
  • library/bdm/base/datasources.h

    r895 r896  
    3232        //! active column in the Data matrix 
    3333        int time; 
    34         //!  vector of rows that are presented in Dt 
    35         ivec rowid; 
    36  
    37 public: 
     34 
     35public: 
     36        //!Default constructor 
     37        MemDS () {}; 
     38 
     39        //! Convenience constructor 
     40        MemDS ( mat &Dat ); 
     41 
     42        //! returns number of data in the file; 
    3843        int max_length() { 
    3944                return Data.cols(); 
    4045        } 
     46 
    4147        void getdata ( vec &dt ) const; 
    42         void getdata ( vec &dt, const ivec &indeces ); 
    43         void set_drv ( const RV &drv, const  RV &urv ); 
     48 
     49        void getdata ( vec &dt, const ivec &indices ); 
     50 
     51        void set_drv ( const RV &drv ); 
     52 
     53        void set_drv ( const RV &drv, const RV &urv ) 
     54        { 
     55                bdm_error ( "MemDS::urv is not supported" ); 
     56        } 
    4457 
    4558        void write ( const vec &ut ) { 
     
    5265 
    5366        void step(); 
    54         //!Default constructor 
    55         MemDS () {}; 
    56         //! Convenience constructor 
    57         MemDS ( mat &Dat, ivec &rowid0 ); 
     67 
    5868        /*! Create object from the following structure 
    5969        \code 
     
    6373           drv = {class="RV"; ...} // Identification how rows of the matrix Data will be known to others 
    6474           time = 0;               // Index of the first column to user_info, 
    65            rowid = [1,2,3...];     // ids of rows to be used 
    6675        } 
    6776        \endcode 
     
    6978        If the optional fields are not given, they will be filled as follows: 
    7079        \code 
    71         rowid= [0, 1, 2, ...number_of_rows_of_Data]; 
    7280        drv = {names=("ch0", "ch1", "ch2", ..."number_of_rows_of_Data"); 
    7381              sizes=( 1    1    1 ...); 
     
    7684        time = 0; 
    7785        \endcode 
    78         If \c rowid is given, \c drv will be named after indeces in rowids. 
     86        If \c rowid is given, \c drv will be named after indices in rowids. 
    7987 
    8088        Hence the data provided by method \c getdata() will be full column of matrix Data starting from the first record. 
    8189        */ 
    82         void from_setting ( const Setting &set ) { 
    83                 UI::get ( Data, set, "Data", UI::compulsory ); 
    84                 if ( !UI::get ( time, set, "time", UI::optional ) ) { 
    85                         time = 0; 
    86                 } 
    87                 if ( !UI::get ( rowid, set, "rowid", UI::optional ) ) { 
    88                         rowid = linspace ( 0, Data.rows() - 1 ); 
    89                 } 
    90                 shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
    91                 if ( !r ) { 
    92                         r = new RV(); 
    93                         for ( int i = 0; i < rowid.length(); i++ ) { 
    94                                 r->add ( RV ( "ch" + num2str ( rowid ( i ) ), 1, 0 ) ); 
    95                         } 
    96                 } 
    97                 set_drv ( *r, RV() ); //empty urv 
    98                 dtsize = r->_dsize(); 
    99                 utsize = 0; 
    100         } 
     90        void from_setting ( const Setting &set ); 
    10191}; 
    10292UIREGISTER ( MemDS ); 
     
    10696*/ 
    10797class FileDS: public MemDS { 
    108  
    109 public: 
    110         void getdata ( vec &dt ) { 
    111                 dt = Data.get_col ( time ); 
    112         } 
    113  
    114         void getdata ( vec &dt, const ivec &indices ) { 
    115                 vec tmp = Data.get_col ( time ); 
    116                 dt = tmp ( indices ); 
    117         } 
    118  
    119         //! returns number of data in the file; 
    120         int ndat() { 
    121                 return Data.cols(); 
    122         } 
    123         //! no sense to log this type 
    124         void log_register ( logger &L, const string &prefix ) {}; 
    125         //! no sense to log this type 
    126         void log_write ( ) const {}; 
     98protected: 
     99        string filename; 
     100public: 
     101        void from_setting ( const Setting & set ); 
    127102}; 
    128103 
     
    142117                it >> Data; 
    143118                time = 0; 
    144                 //rowid and delays are ignored 
     119                //delays are ignored 
    145120        }; 
    146121 
     
    165140*/ 
    166141class CsvFileDS: public FileDS { 
    167  
    168 public: 
    169         //! Constructor - create DS from a CSV file. 
    170         CsvFileDS ( const string& fname, const string& orientation = "BY_COL" ); 
     142public: 
     143        void from_setting ( const Setting & set ); 
    171144}; 
    172145 
     
    203176        virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID; 
    204177 
    205         //! Accepts action variables at specific indeces 
    206         virtual void write ( const vec &ut, const ivec &indeces ) NOT_IMPLEMENTED_VOID; 
     178        //! Accepts action variables at specific indices 
     179        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; 
    207180 
    208181        /*! 
     
    261234 
    262235 
    263         //! Returns data records at indeces. 
    264         virtual void getdata ( vec &dt, const ivec &indeces ) NOT_IMPLEMENTED_VOID; 
     236        //! Returns data records at indices. 
     237        virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID; 
    265238 
    266239 
     
    364337        } 
    365338 
    366         virtual void write ( const vec &ut, const ivec &indeces ) NOT_IMPLEMENTED_VOID; 
     339        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; 
    367340 
    368341        /*! UI for stateDS