1 | /*! |
---|
2 | \file |
---|
3 | \brief Scenario Simulator |
---|
4 | |
---|
5 | The task of simulation is defined here as follows: |
---|
6 | \dot |
---|
7 | digraph estimation{ |
---|
8 | node [shape=box]; |
---|
9 | {rank="same"; "Data Source"; "Result Logger"} |
---|
10 | "Data Source" -> "Result Logger" [label="Simulated\n data"]; |
---|
11 | } |
---|
12 | \enddot |
---|
13 | |
---|
14 | Here, |
---|
15 | \li Data Source is an object (class DS) providing sequential data, \f$ [d_1, d_2, \ldots d_t] \f$. |
---|
16 | \li Result Logger is an object (class logger) dedicated to storing important data from the experiment. |
---|
17 | |
---|
18 | Naming of the data provided by the datasource is done via mechanism of random variables (class RV). |
---|
19 | |
---|
20 | \section cmd Command-line usage |
---|
21 | Execute command: |
---|
22 | \code |
---|
23 | $> simulator config_file.cfg |
---|
24 | \endcode |
---|
25 | |
---|
26 | Full description of the experiment is in the file config_file.cfg which is expected to have the following structure: |
---|
27 | \code |
---|
28 | system = {type = "DS_offspring", ...}; // definition of a data source |
---|
29 | logger = {type = "logger_type",...}; // definition of a logger |
---|
30 | experiment = {ndat = 11000; }; // definition of number of data records |
---|
31 | \endcode |
---|
32 | |
---|
33 | \section ex Matlab usage |
---|
34 | Execute command: |
---|
35 | \code |
---|
36 | >> simulator(system,experiment,logger); |
---|
37 | \endcode |
---|
38 | Here, the only accepted field of experiment is \c experiment.ndat. If loger is not specified, the mexlogger is used, i.e. the data are stored as matlab structure with field names given by RV.names. |
---|
39 | */ |
---|
40 | |
---|
41 | #include <estim/arx.h> |
---|
42 | #include <stat/emix.h> |
---|
43 | #include <base/datasources.h> |
---|
44 | #include <base/loggers.h> |
---|
45 | |
---|
46 | //#include "mex_datasource.h" |
---|
47 | |
---|
48 | using namespace bdm; |
---|
49 | |
---|
50 | #ifdef MEX |
---|
51 | #include <itpp/itmex.h> |
---|
52 | #include "mex/mex_BM.h" |
---|
53 | #include "mex/mex_logger.h" |
---|
54 | #include "mex/mex_datasource.h" |
---|
55 | #include "mex/mex_function.h" |
---|
56 | |
---|
57 | void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { |
---|
58 | // Check the number of inputs and output arguments |
---|
59 | if ( n_input<1 ) mexErrMsgTxt ( "Usage:\n" |
---|
60 | "result=simulator(system, experiment, logger)\n" |
---|
61 | " system = struct('class','datasource',...); % Estimated system\n" |
---|
62 | " === optional ===" |
---|
63 | " experiment = struct('ndat',10); % number of data in experiment, full length of finite datasources, 10 otherwise \n" |
---|
64 | " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n" |
---|
65 | "see documentation of classes datasource and mexlogger and their offsprings in BDM." ); |
---|
66 | |
---|
67 | RV::clear_all(); |
---|
68 | //CONFIG |
---|
69 | UImxArray Cfg; |
---|
70 | try { |
---|
71 | Cfg.addGroup ( input[0],"system" ); |
---|
72 | if ( n_input>1 ) { |
---|
73 | Cfg.addGroup ( input[1],"experiment" ); |
---|
74 | } |
---|
75 | if ( n_input>2 ) { |
---|
76 | Cfg.addGroup ( input[2],"logger" ); |
---|
77 | }/*else{ |
---|
78 | // define logger as mexlog |
---|
79 | Setting &S=Cfg.getRoot(); |
---|
80 | S.add("logger",Setting::TypeGroup); |
---|
81 | S["logger"].add("class",Setting::TypeString); |
---|
82 | S["logger"]["class"]="mexlog"; |
---|
83 | S["logger"].add("maxlen",Setting::TypeInt); |
---|
84 | int maxlen; |
---|
85 | S["experiment"].lookupValue("ndat",maxlen); |
---|
86 | S["logger"]["maxlen"]=maxlen; |
---|
87 | }*/ |
---|
88 | } catch ( SettingException e ) { |
---|
89 | it_error ( "error: "+string ( e.getPath() ) ); |
---|
90 | } |
---|
91 | |
---|
92 | //DBG |
---|
93 | // Cfg.writeFile ( "simulator.cfg" ); |
---|
94 | |
---|
95 | #else |
---|
96 | int main ( int argc, char* argv[] ) { |
---|
97 | const char *fname; |
---|
98 | if ( argc>1 ) { |
---|
99 | fname = argv[1]; |
---|
100 | } else { |
---|
101 | fname="simulator.cfg"; |
---|
102 | } |
---|
103 | UIFile Cfg ( fname ); |
---|
104 | #endif |
---|
105 | |
---|
106 | shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" ); |
---|
107 | int Ndat=10; |
---|
108 | if ( Cfg.exists ( "experiment" ) ) { |
---|
109 | if ( Cfg.lookupValue ( "experiment.ndat",Ndat ) ) { |
---|
110 | bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" ); |
---|
111 | }; |
---|
112 | } else { |
---|
113 | if ( Ds->max_length() < std::numeric_limits< int >::max() ) { |
---|
114 | Ndat=Ds->max_length(); |
---|
115 | } |
---|
116 | ;// else Ndat=10; |
---|
117 | } |
---|
118 | shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); |
---|
119 | if ( !L ) { |
---|
120 | #ifdef MEX |
---|
121 | //mex logger has only from_setting constructor - we have to fill it... |
---|
122 | L=new mexlog ( Ndat ); |
---|
123 | #else |
---|
124 | L=new stdlog(); |
---|
125 | #endif |
---|
126 | } |
---|
127 | |
---|
128 | Ds->log_register ( *L, "DS" ); |
---|
129 | L->init(); |
---|
130 | |
---|
131 | vec dt=zeros ( Ds->_drv()._dsize() ); //data variable |
---|
132 | |
---|
133 | for ( int tK=0;tK<Ndat;tK++ ) { |
---|
134 | Ds->getdata ( dt ); // read data |
---|
135 | Ds->log_write ( ); |
---|
136 | |
---|
137 | L->step(); |
---|
138 | Ds->step(); // simulator step |
---|
139 | } |
---|
140 | |
---|
141 | L->finalize(); |
---|
142 | // ------------------ End of routine ----------------------------- |
---|
143 | |
---|
144 | #ifdef MEX |
---|
145 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); |
---|
146 | |
---|
147 | if ( mL ) { // user wants output!! |
---|
148 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
---|
149 | output[0] = mL->toCell(); |
---|
150 | } |
---|
151 | #endif |
---|
152 | } |
---|