Changeset 896 for library/bdm/base
- Timestamp:
- 04/09/10 09:31:37 (14 years ago)
- Location:
- library/bdm/base
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.cpp
r889 r896 522 522 // decide if we need to copy val to history 523 523 if ( Hrv._dsize() > 0 ) { 524 v2h_up = rv_hist0.dataind ( rv_up ); // ind eces of elements of rv_up to be copied524 v2h_up = rv_hist0.dataind ( rv_up ); // indices of elements of rv_up to be copied 525 525 } // else v2h_up is empty 526 526 … … 535 535 ivec ind_H; 536 536 ivec ind_h0; 537 Hrv.dataind ( rv1, ind_H, ind_h0 ); // find ind eces of rv in537 Hrv.dataind ( rv1, ind_H, ind_h0 ); // find indices of rv in 538 538 set_subvector ( history, ind_H, hist0 ( ind_h0 ) ); // copy given hist to appropriate places 539 539 } -
library/bdm/base/bdmbase.h
r895 r896 594 594 //!@} 595 595 void from_setting ( const Setting &set ) { 596 root::from_setting( set ); 596 597 shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 597 598 if ( r ) { … … 740 741 class datalink_part : public datalink { 741 742 protected: 742 //! ind eces of values in vector downsize743 //! indices of values in vector downsize 743 744 ivec v2v_down; 744 745 public: … … 760 761 //! rv of the history 761 762 RV Hrv; 762 //! h2v : ind eces in down763 //! h2v : indices in down 763 764 ivec h2v_down; 764 //! h2v : ind eces in history765 //! h2v : indices in history 765 766 ivec h2v_hist; 766 //! v2h: ind eces of up too be pushed to h767 //! v2h: indices of up too be pushed to h 767 768 ivec v2h_up; 768 769 public: … … 932 933 virtual void getdata ( vec &dt ) const = 0; 933 934 934 //! Returns data records at ind eces. Default is inefficent.935 virtual void getdata ( vec &dt, const ivec &ind eces ) {935 //! Returns data records at indices. Default is inefficent. 936 virtual void getdata ( vec &dt, const ivec &indices ) { 936 937 vec tmp(dtsize); 937 938 getdata(tmp); 938 dt = tmp(ind eces);939 dt = tmp(indices); 939 940 }; 940 941 … … 942 943 virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID; 943 944 944 //! Accepts action variables at specific ind eces945 virtual void write ( const vec &ut, const ivec &ind eces ) NOT_IMPLEMENTED_VOID;945 //! Accepts action variables at specific indices 946 virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; 946 947 947 948 //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. … … 960 961 return Urv; 961 962 } 963 962 964 //! set random variables 963 965 virtual void set_drv ( const RV &drv, const RV &urv) { -
library/bdm/base/datasources.cpp
r895 r896 1 2 1 #include "datasources.h" 3 2 4 3 using namespace bdm; 5 4 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 } 5 MemDS::MemDS ( mat &Dat ) : Data( Dat ) { 13 6 } 14 7 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() ) { 8 void MemDS::step( ) { 9 if ( time < max_length() ) { 26 10 time++; 27 11 } 28 12 } 29 13 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 ); 14 void MemDS::getdata ( vec &dt ) const { 15 bdm_assert_debug ( dt.length() == Data.rows(), "MemDS:getdata incompatible dt" ); 16 dt = Data.get_col ( time ); 35 17 } 36 18 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." ); 19 void 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 25 void 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 30 void 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 52 void 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 ); 39 58 40 59 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; 42 72 } 43 73 44 void ITppFileDS::from_setting ( const Setting &set ) {45 shared_ptr<RV> rvtmp = UI::build<RV> ( set, "rv" , UI::compulsory);74 void CsvFileDS::from_setting ( const Setting & set ) { 75 FileDS::from_setting ( set ); 46 76 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 94 void 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 ); 49 102 it >> Data; 50 time = 0;51 //rowid and delays are ignored52 rowid = linspace ( 0, Data.rows() - 1 );53 set_drv ( *rvtmp, RV() );54 103 } 55 104 … … 62 111 } 63 112 64 void PdfDS::getdata ( vec & dt_out ) const {113 void PdfDS::getdata ( vec & dt_out ) const { 65 114 bdm_assert_debug ( dt_out.length() >= dtsize, "Short output vector" ); 66 115 dt_out.set_subvector ( 0, yt ); … … 85 134 } 86 135 87 void StateDS::from_setting ( const Setting & set ) {136 void StateDS::from_setting ( const Setting & set ) { 88 137 IM = UI::build<pdf> ( set, "IM", UI::compulsory ); 89 138 OM = UI::build<pdf> ( set, "OM", UI::compulsory ); -
library/bdm/base/datasources.h
r895 r896 32 32 //! active column in the Data matrix 33 33 int time; 34 //! vector of rows that are presented in Dt 35 ivec rowid; 36 37 public: 34 35 public: 36 //!Default constructor 37 MemDS () {}; 38 39 //! Convenience constructor 40 MemDS ( mat &Dat ); 41 42 //! returns number of data in the file; 38 43 int max_length() { 39 44 return Data.cols(); 40 45 } 46 41 47 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 } 44 57 45 58 void write ( const vec &ut ) { … … 52 65 53 66 void step(); 54 //!Default constructor 55 MemDS () {}; 56 //! Convenience constructor 57 MemDS ( mat &Dat, ivec &rowid0 ); 67 58 68 /*! Create object from the following structure 59 69 \code … … 63 73 drv = {class="RV"; ...} // Identification how rows of the matrix Data will be known to others 64 74 time = 0; // Index of the first column to user_info, 65 rowid = [1,2,3...]; // ids of rows to be used66 75 } 67 76 \endcode … … 69 78 If the optional fields are not given, they will be filled as follows: 70 79 \code 71 rowid= [0, 1, 2, ...number_of_rows_of_Data];72 80 drv = {names=("ch0", "ch1", "ch2", ..."number_of_rows_of_Data"); 73 81 sizes=( 1 1 1 ...); … … 76 84 time = 0; 77 85 \endcode 78 If \c rowid is given, \c drv will be named after ind eces in rowids.86 If \c rowid is given, \c drv will be named after indices in rowids. 79 87 80 88 Hence the data provided by method \c getdata() will be full column of matrix Data starting from the first record. 81 89 */ 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 ); 101 91 }; 102 92 UIREGISTER ( MemDS ); … … 106 96 */ 107 97 class 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 {}; 98 protected: 99 string filename; 100 public: 101 void from_setting ( const Setting & set ); 127 102 }; 128 103 … … 142 117 it >> Data; 143 118 time = 0; 144 // rowid anddelays are ignored119 //delays are ignored 145 120 }; 146 121 … … 165 140 */ 166 141 class CsvFileDS: public FileDS { 167 168 public: 169 //! Constructor - create DS from a CSV file. 170 CsvFileDS ( const string& fname, const string& orientation = "BY_COL" ); 142 public: 143 void from_setting ( const Setting & set ); 171 144 }; 172 145 … … 203 176 virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID; 204 177 205 //! Accepts action variables at specific ind eces206 virtual void write ( const vec &ut, const ivec &ind eces ) NOT_IMPLEMENTED_VOID;178 //! Accepts action variables at specific indices 179 virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; 207 180 208 181 /*! … … 261 234 262 235 263 //! Returns data records at ind eces.264 virtual void getdata ( vec &dt, const ivec &ind eces ) NOT_IMPLEMENTED_VOID;236 //! Returns data records at indices. 237 virtual void getdata ( vec &dt, const ivec &indices ) NOT_IMPLEMENTED_VOID; 265 238 266 239 … … 364 337 } 365 338 366 virtual void write ( const vec &ut, const ivec &ind eces ) NOT_IMPLEMENTED_VOID;339 virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; 367 340 368 341 /*! UI for stateDS