1 | /*! |
---|
2 | \file |
---|
3 | \brief Application Estimator |
---|
4 | |
---|
5 | The general task of estimation is defined on the following scheme: |
---|
6 | \dot |
---|
7 | digraph estimation{ |
---|
8 | node [shape=box]; |
---|
9 | subgraph cl0 { |
---|
10 | "Data Source" -> "Controller" [label="observations"]; |
---|
11 | "Controller" -> "Data Source" [label="actions"]; |
---|
12 | } |
---|
13 | {rank="same"; "Controller"; "Result Logger"} |
---|
14 | "Controller" -> "Result Logger" [label="internals"]; |
---|
15 | "Data Source" -> "Result Logger" [label="Simulated\n data"]; |
---|
16 | } |
---|
17 | \enddot |
---|
18 | |
---|
19 | Here, |
---|
20 | \li Data Source is an object (class DS) providing sequential data, \f$ [d_1, d_2, \ldots d_t] \f$. |
---|
21 | \li Bayesian Model is an object (class BM) performing Bayesian filtering, |
---|
22 | \li Result Logger is an object (class logger) dedicated to storing important data from the experiment. |
---|
23 | |
---|
24 | \section cmd Command-line usage |
---|
25 | Execute command: |
---|
26 | \code |
---|
27 | $> estimator config_file.cfg |
---|
28 | \endcode |
---|
29 | |
---|
30 | Full description of the experiment is in the file config_file.cfg which is expected to have the following structure: |
---|
31 | \code |
---|
32 | system = {type = "DS_offspring", ...}; // definition of a data source |
---|
33 | estimator = {type = "BM_offspring", ...}; // definition of an estimator |
---|
34 | logger = {type = "logger_type",...}; // definition of a logger |
---|
35 | experiment = {ndat = 11000; }; // definition of number of data records |
---|
36 | \endcode |
---|
37 | |
---|
38 | The above description must be specialized to specific classes. See, \subpage arx_ui how to do it for estimation of an ARX model. |
---|
39 | |
---|
40 | \section ex Matlab usage |
---|
41 | Execute command: |
---|
42 | \code |
---|
43 | >> estimator('config_file.cfg'); |
---|
44 | \endcode |
---|
45 | when using loggers storing results on hard drives, and |
---|
46 | \code |
---|
47 | >> Res=estimator('config_file.cfg'); |
---|
48 | \endcode |
---|
49 | when using logger of the type \c "mex_logger". The results will be stored in structure \c M. |
---|
50 | |
---|
51 | */ |
---|
52 | |
---|
53 | #include <estim/arx.h> |
---|
54 | #include <stat/emix.h> |
---|
55 | #include <base/datasources.h> |
---|
56 | #include <base/loggers.h> |
---|
57 | #include <design/arx_ctrl.h> |
---|
58 | |
---|
59 | //#include "mex_datasource.h" |
---|
60 | |
---|
61 | using namespace bdm; |
---|
62 | |
---|
63 | #ifdef MEX |
---|
64 | #include <itpp/itmex.h> |
---|
65 | #include <mex/mex_BM.h> |
---|
66 | #include <mex/mex_logger.h> |
---|
67 | #include <mex/mex_datasource.h> |
---|
68 | #include <mex/mex_function.h> |
---|
69 | |
---|
70 | void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { |
---|
71 | // Check the number of inputs and output arguments |
---|
72 | if ( n_input<2 ) mexErrMsgTxt ( "Usage:\n" |
---|
73 | "result=controlloop(system, controllers, experiment, logger)\n" |
---|
74 | " system = struct('class','datasource',...); % Estimated system\n" |
---|
75 | " controllers= {struct('class','controller',...), % Controllers\n" |
---|
76 | " struct('class','controller',...),...} \n" |
---|
77 | " === optional ===" |
---|
78 | " experiment = struct('ndat',100,... % number of data in experiment, full length of finite datasources, 100 otherwise \n" |
---|
79 | " 'seed',[],... % seed for random number generator\n" |
---|
80 | " 'burnin',10,... % initial time with different control\n" |
---|
81 | " 'burn_pdf', struct('class','epdf_offspring') );\n" |
---|
82 | " % sampler of the initial control\n" |
---|
83 | " logger = struct('class','mexlogger'); % How to store results, default=mexlog, i.e. matlab structure\n\n" |
---|
84 | "see documentation of classes datasource, BM, and mexlogger and their offsprings in BDM." ); |
---|
85 | |
---|
86 | RV::clear_all(); |
---|
87 | //CONFIG |
---|
88 | UImxArray Cfg; |
---|
89 | try { |
---|
90 | Cfg.addGroup ( input[0],"system" ); |
---|
91 | Cfg.addList ( input[1],"controllers" ); |
---|
92 | if ( n_input>2 ) { |
---|
93 | Cfg.addGroup ( input[2],"experiment" ); |
---|
94 | } |
---|
95 | if ( n_input>3 ) { |
---|
96 | Cfg.addGroup ( input[3],"logger" ); |
---|
97 | } |
---|
98 | } catch ( SettingException e ) { |
---|
99 | it_error ( "error: "+string ( e.getPath() ) ); |
---|
100 | } |
---|
101 | |
---|
102 | //DBG |
---|
103 | Cfg.writeFile ( "controlloop.cfg" ); |
---|
104 | |
---|
105 | #else |
---|
106 | int main ( int argc, char* argv[] ) { |
---|
107 | const char *fname; |
---|
108 | if ( argc>1 ) { |
---|
109 | fname = argv[1]; |
---|
110 | } else { |
---|
111 | fname="controlloop.cfg"; |
---|
112 | } |
---|
113 | UIFile Cfg ( fname ); |
---|
114 | #endif |
---|
115 | |
---|
116 | RNG_randomize(); |
---|
117 | |
---|
118 | shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" ); |
---|
119 | Array<shared_ptr<Controller> > Cs; |
---|
120 | UI::get ( Cs,Cfg, "controllers" ); |
---|
121 | int Ndat=100; |
---|
122 | int burnin=0; |
---|
123 | shared_ptr<epdf> burn_pdf; |
---|
124 | |
---|
125 | if ( Cfg.exists ( "experiment" ) ) { |
---|
126 | Setting &exper=Cfg.getRoot()["experiment"]; |
---|
127 | // get number of data |
---|
128 | if (UI::get(Ndat, exper, "Ndat", UI::optional ) ) { |
---|
129 | bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" ); |
---|
130 | }; |
---|
131 | // check for seed |
---|
132 | int seed; |
---|
133 | if (UI::get(seed, exper, "seed", UI::optional)){ |
---|
134 | RNG_reset(seed); |
---|
135 | } |
---|
136 | // process burnin |
---|
137 | if (UI::get(burnin, exper, "burnin",UI::optional )){ |
---|
138 | burn_pdf = UI::build<epdf>(exper,"burn_pdf", UI::compulsory); |
---|
139 | if (burn_pdf){ |
---|
140 | bdm_assert(burn_pdf->dimension()==Ds->_urv()._dsize(),"Given burn_pdf does not match the DataSource"); |
---|
141 | } else { |
---|
142 | bdm_error("burn_pdf not specified!"); |
---|
143 | } |
---|
144 | |
---|
145 | } |
---|
146 | } else { |
---|
147 | if ( Ds->max_length() < std::numeric_limits< int >::max() ) { |
---|
148 | Ndat=Ds->max_length(); |
---|
149 | } |
---|
150 | ;// else Ndat=10; |
---|
151 | } |
---|
152 | shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); |
---|
153 | if ( !L ) { |
---|
154 | #ifdef MEX |
---|
155 | //mex logger has only from_setting constructor - we have to fill it... |
---|
156 | L=new mexlog ( Ndat ); |
---|
157 | #else |
---|
158 | L=new stdlog(); |
---|
159 | #endif |
---|
160 | } |
---|
161 | |
---|
162 | Ds->log_register ( *L, "DS" ); |
---|
163 | bdm_assert((Ds->_urv()._dsize() > 0), "Given DataSource is not controllable"); |
---|
164 | string Cname; |
---|
165 | Setting &S=Cfg; |
---|
166 | for ( int i=0; i<Cs.length(); i++ ) { |
---|
167 | if (!UI::get ( Cname, S["controllers"][i], "name" ,UI::optional)){ |
---|
168 | Cname="Ctrl"+num2str ( i ); |
---|
169 | } |
---|
170 | |
---|
171 | Cs ( i )->log_register ( *L,Cname ); // estimate |
---|
172 | } |
---|
173 | L->init(); |
---|
174 | |
---|
175 | vec dt=zeros ( Ds->_drv()._dsize() ); //data variable |
---|
176 | Array<datalink_part*> Dlsu ( Cs.length() ); |
---|
177 | Array<datalink*> Dlsc ( Cs.length() ); |
---|
178 | Array<datalink_buffered*> Dls_buf (0); |
---|
179 | for ( int i=0; i<Cs.length(); i++ ) { |
---|
180 | //connect actual data |
---|
181 | Dlsu ( i ) = new datalink_part; |
---|
182 | Dlsu(i)->set_connection( Ds->_urv(), Cs ( i )->_rv()); //datalink controller -> datasource |
---|
183 | //connect data in condition: datasource -> controller |
---|
184 | if (Cs ( i )->_rvc().mint()<0){ |
---|
185 | //delayed values are required |
---|
186 | |
---|
187 | //create delayed dl |
---|
188 | int ith_buf=Dls_buf.size(); |
---|
189 | Dls_buf.set_size( ith_buf + 1, true); |
---|
190 | Dls_buf(ith_buf) = new datalink_buffered(); |
---|
191 | //add dl to list of buffered DS |
---|
192 | Dlsc(i) = Dls_buf(ith_buf); |
---|
193 | Dlsc(i)->set_connection ( Cs ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
194 | |
---|
195 | bdm_assert_debug(Dlsc(i)->_downsize() == Cs ( i )->_rvc()._dsize(), "Data required by Controler[" + num2str(i) + "], " + |
---|
196 | Cs(i)->_rvc().to_string() + ", are not available in DS drv:" + Ds->_drv().to_string();); |
---|
197 | |
---|
198 | } else { |
---|
199 | Dlsc ( i ) = new datalink ( Cs ( i )->_rvc(),Ds->_drv() ); //datalink between a datasource and estimator |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | vec ut(Ds->_urv()._dsize()); |
---|
204 | for ( int tK=0;tK<Ndat;tK++ ) { |
---|
205 | Ds->getdata ( dt ); // read data |
---|
206 | Ds->log_write ( ); |
---|
207 | |
---|
208 | for ( int i=0; i<Cs.length(); i++ ) { |
---|
209 | if (tK + Cs ( i )->_rvc().mint() > 0 ) { |
---|
210 | Cs(i) -> redesign(); |
---|
211 | Cs(i) -> adapt( Dlsc(i) ->pushdown(dt)); |
---|
212 | if (tK >= burnin){ |
---|
213 | vec uti=Cs ( i )->ctrlaction ( Dlsc(i) ->pushdown(dt) ); // update estimates |
---|
214 | Dlsu(i)->filldown(uti, ut); |
---|
215 | } |
---|
216 | } |
---|
217 | if(tK<burnin) { |
---|
218 | ut = burn_pdf->sample(); |
---|
219 | } |
---|
220 | |
---|
221 | Cs ( i )->log_write (); |
---|
222 | } |
---|
223 | Ds->write(ut); |
---|
224 | |
---|
225 | L->step(); |
---|
226 | Ds->step(); // simulator step |
---|
227 | //update buffered fdat links |
---|
228 | for (int i=0; i<Dls_buf.length(); i++){ |
---|
229 | Dls_buf(i)->store_data(dt); |
---|
230 | } |
---|
231 | |
---|
232 | } |
---|
233 | |
---|
234 | L->finalize(); |
---|
235 | // ------------------ End of routine ----------------------------- |
---|
236 | |
---|
237 | #ifdef MEX |
---|
238 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); |
---|
239 | |
---|
240 | if ( mL ) { // user wants output!! |
---|
241 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
---|
242 | output[0] = mL->toCell(); |
---|
243 | if (n_output>1) { |
---|
244 | mL->_setting_conf().setAutoConvert(true); |
---|
245 | output[1]= UImxArray::create_mxArray(mL->_setting_conf().getRoot()); |
---|
246 | } |
---|
247 | } |
---|
248 | #endif |
---|
249 | for (int i=0;i<Dlsu.length(); i++){delete Dlsu(i); delete Dlsc(i);} |
---|
250 | } |
---|