root/applications/bdmtoolbox/mex/simulator.cpp @ 617

Revision 617, 4.1 kB (checked in by smidl, 15 years ago)

simulator + doc

Line 
1/*!
2\file
3\brief Scenario Simulator
4
5The task of simulation is defined here as follows:
6\dot
7digraph 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
14Here,
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
18Naming of the data provided by the datasource is done via mechanism of random variables (class RV).
19
20\section  cmd Command-line usage
21Execute command:
22\code
23$> simulator config_file.cfg
24\endcode
25
26Full description of the experiment is in the file config_file.cfg which is expected to have the following structure:
27\code
28system = {type = "DS_offspring", ...};      // definition of a data source
29logger = {type = "logger_type",...};        // definition of a logger
30experiment = {ndat = 11000; };              // definition of number of data records
31\endcode
32
33\section ex Matlab usage
34Execute command:
35\code
36>> simulator(system,experiment,logger);
37\endcode
38Here, 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
48using 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
56void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) {
57        // Check the number of inputs and output arguments
58        if(n_input<1) mexErrMsgTxt("Usage:\n" 
59                "result=simulator(system,  experiment, logger)\n"
60                "  system     = struct('class','datasource',...);  % Estimated system\n"
61                "  === optional ==="
62                "  experiment = struct('ndat',10);                 % number of data in experiment, full length of finite datasources, 10 otherwise \n"
63                "  logger     = struct('class','mexlogger');       % How to store results, default=mexlog, i.e. matlab structure\n\n"
64                "see documentation of classes datasource and mexlogger and their offsprings in BDM.");
65       
66        RV::clear_all();
67        //CONFIG
68        UImxArray Cfg;
69        try{
70        Cfg.addGroup(input[0],"system");
71        if (n_input>2){
72                Cfg.addGroup(input[1],"experiment");
73        }
74        if (n_input>3){
75                Cfg.addGroup(input[2],"logger");
76        }/*else{
77                // define logger as mexlog
78                Setting &S=Cfg.getRoot();
79                S.add("logger",Setting::TypeGroup);
80                S["logger"].add("class",Setting::TypeString);
81                S["logger"]["class"]="mexlog";
82                S["logger"].add("maxlen",Setting::TypeInt);
83                int maxlen;
84                S["experiment"].lookupValue("ndat",maxlen);
85                S["logger"]["maxlen"]=maxlen;
86        }*/
87        } catch(SettingException e){it_error("error: "+string(e.getPath()));}
88
89        //DBG
90        Cfg.writeFile("simulator.cfg");
91
92#else
93int main ( int argc, char* argv[] ) {
94        const char *fname;
95        if ( argc>1 ) {
96                fname = argv[1];
97        } else {
98                fname="estimator.cfg";
99        }
100        UIFile Cfg ( fname );
101#endif
102       
103        shared_ptr<DS> Ds = UI::build<DS>( Cfg, "system" );
104        long Ndat=10;
105        if (Cfg.exists("experiment")) {
106                if (Cfg.lookupValue ( "experiment.ndat",Ndat )) {
107                        bdm_assert(Ndat<=Ds->max_length(), "Data source has less data then required");
108                };
109        } else {
110                if (Ds->max_length() < std::numeric_limits< int >::max()) {
111                        Ndat=Ds->max_length();
112                };// else Ndat=10;
113        }
114        shared_ptr<logger> L = UI::build<logger>( Cfg, "logger",UI::optional);
115        if (!L) {
116                #ifdef MEX
117                //mex logger has only from_setting constructor - we  have to fill it...
118                L=new mexlog(Ndat);
119                #else
120                L=new stdlog();
121                #endif
122        }
123       
124        Ds->log_add ( *L );
125        L->init();
126
127        vec dt=zeros ( Ds->_drv()._dsize() );   //data variable
128
129        for ( int tK=0;tK<Ndat;tK++ ) {
130                Ds->getdata ( dt );                                     // read data
131                Ds->logit ( *L );
132               
133                L->step();
134                Ds->step();                                                     // simulator step
135        }
136
137        L->finalize();
138        // ------------------ End of routine -----------------------------
139
140#ifdef MEX
141        mexlog* mL=dynamic_cast<mexlog*>(L.get());
142
143        if (mL) { // user wants output!!
144                if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" );
145                output[0] = mL->toCell();
146        }
147#endif
148}
Note: See TracBrowser for help on using the browser.