[281] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief TR 2525 file for testing Toy Problem of mpf for Covariance Estimation |
---|
| 4 | \author Vaclav Smidl. |
---|
| 5 | |
---|
| 6 | ----------------------------------- |
---|
| 7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
| 8 | |
---|
| 9 | Using IT++ for numerical operations |
---|
| 10 | ----------------------------------- |
---|
| 11 | */ |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | #include <estim/libPF.h> |
---|
| 16 | #include <estim/ekf_templ.h> |
---|
| 17 | #include <stat/libFN.h> |
---|
| 18 | |
---|
| 19 | #include <stat/loggers_ui.h> |
---|
| 20 | |
---|
| 21 | #include "../pmsm.h" |
---|
| 22 | #include "simulator.h" |
---|
| 23 | #include "../sim_profiles.h" |
---|
| 24 | |
---|
| 25 | using namespace bdm; |
---|
| 26 | |
---|
| 27 | int main ( int argc, char* argv[] ) { |
---|
| 28 | const char *fname; |
---|
| 29 | if ( argc>1 ) {fname = argv[1]; } |
---|
| 30 | else { fname = "unitsteps.cfg"; } |
---|
| 31 | UIFile F ( fname ); |
---|
| 32 | |
---|
| 33 | int Ndat; |
---|
| 34 | int Npart; |
---|
| 35 | double h = 1e-6; |
---|
| 36 | int Nsimstep = 125; |
---|
| 37 | |
---|
| 38 | vec Qdiag; |
---|
| 39 | vec Rdiag; |
---|
| 40 | try { |
---|
| 41 | // Kalman filter |
---|
| 42 | F.lookupValue ( "ndat", Ndat ); |
---|
| 43 | F.lookupValue ( "Npart",Npart ); |
---|
| 44 | |
---|
| 45 | Qdiag= getvec ( F.lookup ( "dQ" ) ); //( "1e-6 1e-6 0.001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
| 46 | Rdiag=getvec ( F.lookup ( "dR" ) );// ( "1e-8 1e-8" ); //var(diff(xth)) = "0.034 0.034" |
---|
| 47 | } |
---|
| 48 | catch UICATCH; |
---|
| 49 | // internal model |
---|
| 50 | |
---|
| 51 | IMpmsm fxu; |
---|
| 52 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
| 53 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
| 54 | // observation model |
---|
| 55 | OMpmsm hxu; |
---|
| 56 | |
---|
| 57 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
| 58 | chmat Q ( Qdiag ); |
---|
| 59 | chmat R ( Rdiag ); |
---|
| 60 | EKFCh KFE ; |
---|
| 61 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
| 62 | KFE.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
[283] | 63 | KFE.set_rv(rx); |
---|
[281] | 64 | |
---|
| 65 | RV rQ ( "{Q }","4" ); |
---|
| 66 | EKFCh_unQ KFEp ; |
---|
| 67 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
| 68 | KFEp.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
[283] | 69 | |
---|
[281] | 70 | migamma_fix evolQ ; |
---|
[283] | 71 | MPF<EKFCh_unQ> M; |
---|
| 72 | M.set_parameters( &evolQ,&evolQ,Npart); |
---|
[281] | 73 | // initialize |
---|
| 74 | evolQ.set_parameters ( 0.1, 10*Qdiag, 1.0 ); //sigma = 1/10 mu |
---|
| 75 | evolQ.condition ( 10*Qdiag ); //Zdenek default |
---|
[283] | 76 | M.set_statistics ( evolQ._e() , &KFEp ); |
---|
[281] | 77 | evolQ.set_parameters ( 0.10, 10*Qdiag,0.999 ); //sigma = 1/10 mu |
---|
| 78 | // |
---|
| 79 | |
---|
[283] | 80 | M.set_rv(concat(rQ,rx)); |
---|
| 81 | |
---|
[281] | 82 | dirfilelog *L; UIbuild(F.lookup("logger"), L);// ( "exp/mpf_test",100 ); |
---|
| 83 | int l_X = L->add ( rx, "xt" ); |
---|
| 84 | int l_D = L->add ( concat ( ry,ru ), "" ); |
---|
| 85 | int l_Q= L->add ( rQ, "" ); |
---|
[283] | 86 | |
---|
| 87 | KFE.set_options("logbounds"); |
---|
| 88 | KFE.log_add(L,"KF"); |
---|
| 89 | M.set_options("logbounds"); |
---|
| 90 | M.log_add(L,"M"); |
---|
[281] | 91 | L->init(); |
---|
| 92 | |
---|
| 93 | // SET SIMULATOR |
---|
| 94 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
| 95 | vec dt ( 2 ); |
---|
| 96 | vec ut ( 2 ); |
---|
| 97 | vec xt ( 4 ); |
---|
| 98 | vec xtm=zeros ( 4 ); |
---|
| 99 | double Ww=0.0; |
---|
| 100 | vec vecW=getvec(F.lookup("profile")); |
---|
| 101 | |
---|
| 102 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
| 103 | //Number of steps of a simulator for one step of Kalman |
---|
| 104 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
| 105 | //simulator |
---|
| 106 | sim_profile_vec01t ( Ww,vecW ); |
---|
| 107 | pmsmsim_step ( Ww ); |
---|
| 108 | }; |
---|
| 109 | ut ( 0 ) = KalmanObs[4]; |
---|
| 110 | ut ( 1 ) = KalmanObs[5]; |
---|
| 111 | xt = fxu.eval ( xtm,ut ) + diag ( sqrt ( Qdiag ) ) *randn ( 4 ); |
---|
| 112 | dt = hxu.eval ( xt,ut ); |
---|
| 113 | xtm = xt; |
---|
| 114 | |
---|
| 115 | //Variances |
---|
| 116 | if ( tK==1000 ) Qdiag ( 0 ) *=10; |
---|
| 117 | if ( tK==2000 ) Qdiag ( 0 ) /=10; |
---|
| 118 | if ( tK==3000 ) Qdiag ( 1 ) *=10; |
---|
| 119 | if ( tK==4000 ) Qdiag ( 1 ) /=10; |
---|
| 120 | if ( tK==5000 ) Qdiag ( 2 ) *=10; |
---|
| 121 | if ( tK==6000 ) Qdiag ( 2 ) /=10; |
---|
| 122 | if ( tK==7000 ) Qdiag ( 3 ) *=10; |
---|
| 123 | if ( tK==8000 ) Qdiag ( 3 ) /=10; |
---|
| 124 | |
---|
| 125 | //estimator |
---|
| 126 | KFE.bayes ( concat ( dt,ut ) ); |
---|
| 127 | M.bayes ( concat ( dt,ut ) ); |
---|
| 128 | |
---|
| 129 | L->logit ( l_X,xt ); |
---|
| 130 | L->logit ( l_D,concat ( dt,ut ) ); |
---|
| 131 | L->logit ( l_Q,Qdiag ); |
---|
[283] | 132 | |
---|
| 133 | KFE.logit(L); |
---|
| 134 | M.logit(L); |
---|
[281] | 135 | L->step(); |
---|
| 136 | } |
---|
| 137 | L->finalize(); |
---|
| 138 | //Exit program: |
---|
| 139 | |
---|
| 140 | delete L; |
---|
| 141 | return 0; |
---|
| 142 | } |
---|