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

Revision 676, 4.4 kB (checked in by smidl, 15 years ago)

logger refactoring

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>1 ) {
72                        Cfg.addGroup ( input[1],"experiment" );
73                }
74                if ( n_input>2 ) {
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 ) {
88                it_error ( "error: "+string ( e.getPath() ) );
89        }
90
91        //DBG
92        Cfg.writeFile ( "simulator.cfg" );
93
94#else
95int main ( int argc, char* argv[] ) {
96        const char *fname;
97        if ( argc>1 ) {
98                fname = argv[1];
99        } else {
100                fname="estimator.cfg";
101        }
102        UIFile Cfg ( fname );
103#endif
104
105        shared_ptr<DS> Ds = UI::build<DS> ( Cfg, "system" );
106        long Ndat=10;
107        if ( Cfg.exists ( "experiment" ) ) {
108                if ( Cfg.lookupValue ( "experiment.ndat",Ndat ) ) {
109                        bdm_assert ( Ndat<=Ds->max_length(), "Data source has less data then required" );
110                };
111        } else {
112                if ( Ds->max_length() < std::numeric_limits< int >::max() ) {
113                        Ndat=Ds->max_length();
114                }
115                ;// else Ndat=10;
116        }
117        shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional );
118        if ( !L ) {
119#ifdef MEX
120                //mex logger has only from_setting constructor - we  have to fill it...
121                L=new mexlog ( Ndat );
122#else
123                L=new stdlog();
124#endif
125        }
126
127        Ds->log_register ( *L, "DS" );
128        L->init();
129
130        vec dt=zeros ( Ds->_drv()._dsize() );   //data variable
131
132        for ( int tK=0;tK<Ndat;tK++ ) {
133                Ds->getdata ( dt );                                     // read data
134                Ds->log_write (  );
135
136                L->step();
137                Ds->step();                                                     // simulator step
138        }
139
140        L->finalize();
141        // ------------------ End of routine -----------------------------
142
143#ifdef MEX
144        mexlog* mL=dynamic_cast<mexlog*> ( L.get() );
145
146        if ( mL ) { // user wants output!!
147                if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" );
148                output[0] = mL->toCell();
149        }
150#endif
151}
Note: See TracBrowser for help on using the browser.