| 1 | /*! |
|---|
| 2 | \file |
|---|
| 3 | \brief Test file for dual control of ARX models |
|---|
| 4 | |
|---|
| 5 | Todo, more info... |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include "estim/arx.h" |
|---|
| 9 | #include "base/datasources.h" |
|---|
| 10 | #include "base/loggers.h" |
|---|
| 11 | |
|---|
| 12 | #include "arx1_ctrl.h" |
|---|
| 13 | |
|---|
| 14 | using namespace bdm; |
|---|
| 15 | |
|---|
| 16 | #ifdef MEX |
|---|
| 17 | #include <itpp/itmex.h> |
|---|
| 18 | #include "mex/mex_BM.h" |
|---|
| 19 | #include "mex/mex_logger.h" |
|---|
| 20 | #include "mex/mex_datasource.h" |
|---|
| 21 | |
|---|
| 22 | void mexFunction ( int n_output, mxArray *output[], int n_input, const mxArray *input[] ) { |
|---|
| 23 | // Check the number of inputs and output arguments |
|---|
| 24 | if ( n_input<1 ) mexErrMsgTxt ( "Usage:\n" |
|---|
| 25 | " result=iterativemc(config_str)\n" |
|---|
| 26 | "with config_str with fields .b, .y0, .ndat, controller" |
|---|
| 27 | ); |
|---|
| 28 | |
|---|
| 29 | RV::clear_all(); |
|---|
| 30 | //zde se zpracuji vstupni data |
|---|
| 31 | UImxArray Cfg(input[0]); |
|---|
| 32 | //DBG |
|---|
| 33 | Cfg.writeFile ( "iterativemc.cfg" ); |
|---|
| 34 | |
|---|
| 35 | #else |
|---|
| 36 | int main ( int argc, char* argv[] ) { |
|---|
| 37 | const char *fname; |
|---|
| 38 | if ( argc>1 ) { |
|---|
| 39 | fname = argv[1]; |
|---|
| 40 | } else { |
|---|
| 41 | fname="iterativemc.cfg"; |
|---|
| 42 | } |
|---|
| 43 | UIFile Cfg ( fname ); |
|---|
| 44 | #endif |
|---|
| 45 | |
|---|
| 46 | // parametry vyskytujici se v algoritmu |
|---|
| 47 | double b; |
|---|
| 48 | double sigma; |
|---|
| 49 | double ytm=0.0; |
|---|
| 50 | double yr; |
|---|
| 51 | int Ndat; |
|---|
| 52 | |
|---|
| 53 | // cteni ze souboru |
|---|
| 54 | UI::get(b,Cfg,"b",UI::compulsory); |
|---|
| 55 | UI::get(sigma,Cfg,"sigma",UI::compulsory); |
|---|
| 56 | if(!UI::get(ytm,Cfg,"yt0",UI::optional)){ |
|---|
| 57 | ytm=0.0; // start at zero |
|---|
| 58 | } |
|---|
| 59 | if(!UI::get(yr,Cfg,"yr",UI::optional)){ |
|---|
| 60 | yr=1.0; // requested value |
|---|
| 61 | } |
|---|
| 62 | if(!UI::get(Ndat,Cfg,"ndat",UI::optional)){ |
|---|
| 63 | Ndat=30; |
|---|
| 64 | } |
|---|
| 65 | shared_ptr<Controller> C=UI::build<Controller>(Cfg,"controller",UI::compulsory); |
|---|
| 66 | |
|---|
| 67 | int seed; |
|---|
| 68 | if(UI::get(seed,Cfg,"seed",UI::optional)){ |
|---|
| 69 | RNG_reset(seed); |
|---|
| 70 | } else { |
|---|
| 71 | RNG_randomize(); |
|---|
| 72 | } |
|---|
| 73 | // zpusob ukladani vysledku, pokud se zada jiny, lze zadat jako polocku logger v konfiguraci |
|---|
| 74 | shared_ptr<logger> L = UI::build<logger> ( Cfg, "logger",UI::optional ); |
|---|
| 75 | if ( !L ) { |
|---|
| 76 | #ifdef MEX |
|---|
| 77 | //mex logger has only from_setting constructor - we have to fill it... |
|---|
| 78 | L=new mexlog ( Ndat ); |
|---|
| 79 | #else |
|---|
| 80 | L=new stdlog(); |
|---|
| 81 | #endif |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | // create ARX system |
|---|
| 85 | mlnorm<fsqmat> Sys; |
|---|
| 86 | mat theta=ones(1,2); |
|---|
| 87 | theta(1) = b; |
|---|
| 88 | Sys.set_parameters(theta,vec_1(0.0),sigma*sigma*eye(1)); |
|---|
| 89 | |
|---|
| 90 | // decide what will be stored |
|---|
| 91 | RV y("y",1); // name random variable |
|---|
| 92 | RV u("u",1); // name random variable |
|---|
| 93 | int L_yt=L->add_vector(y,"S"); |
|---|
| 94 | int L_ut=L->add_vector(u,"S"); |
|---|
| 95 | C->log_register(*L,"C"); |
|---|
| 96 | L->init(); |
|---|
| 97 | |
|---|
| 98 | vec psi(2); // regressor |
|---|
| 99 | double ut=0.0; // input |
|---|
| 100 | vec yt; |
|---|
| 101 | |
|---|
| 102 | for ( int tK=0;tK<Ndat;tK++ ) { |
|---|
| 103 | //yt has now meaning of yt-1 |
|---|
| 104 | //design controller |
|---|
| 105 | C->adapt(vec_2(ytm,ut)); |
|---|
| 106 | ut = C->ctrlaction(vec_2(ytm,ut))(0); // the best controller there is |
|---|
| 107 | |
|---|
| 108 | //prepare regressor |
|---|
| 109 | psi(0) = ytm; // now it is t-1 |
|---|
| 110 | psi(1) = ut; |
|---|
| 111 | // simulate system |
|---|
| 112 | yt = Sys.samplecond(psi); |
|---|
| 113 | ytm = yt(0); |
|---|
| 114 | |
|---|
| 115 | //save results |
|---|
| 116 | L->logit(L_yt, yt); |
|---|
| 117 | L->logit(L_ut, vec_1(ut)); |
|---|
| 118 | C->log_write(); |
|---|
| 119 | L->step(); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | L->finalize(); |
|---|
| 123 | // ------------------ End of routine ----------------------------- |
|---|
| 124 | |
|---|
| 125 | #ifdef MEX |
|---|
| 126 | mexlog* mL=dynamic_cast<mexlog*> ( L.get() ); |
|---|
| 127 | |
|---|
| 128 | if ( mL ) { // user wants output!! |
|---|
| 129 | if ( n_output<1 ) mexErrMsgTxt ( "Wrong number of output variables!" ); |
|---|
| 130 | output[0] = mL->toCell(); |
|---|
| 131 | } |
|---|
| 132 | #endif |
|---|
| 133 | } |
|---|