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/particles.h> |
---|
16 | #include <estim/ekf_template.h> |
---|
17 | #include <stat/functions.h> |
---|
18 | |
---|
19 | |
---|
20 | #include "../pmsm.h" |
---|
21 | #include "simulator.h" |
---|
22 | #include "../sim_profiles.h" |
---|
23 | #include "user_info.h" |
---|
24 | #include "stat/loggers.h" |
---|
25 | |
---|
26 | using namespace bdm; |
---|
27 | |
---|
28 | int main ( int argc, char* argv[] ) { |
---|
29 | const char *fname; |
---|
30 | if ( argc>1 ) {fname = argv[1]; } |
---|
31 | else { fname = "unitsteps.cfg"; } |
---|
32 | UIFile F ( fname ); |
---|
33 | |
---|
34 | double h = 1e-6; |
---|
35 | int Nsimstep = 125; |
---|
36 | |
---|
37 | |
---|
38 | // Kalman filter |
---|
39 | int Ndat; |
---|
40 | int Npart; |
---|
41 | F.lookupValue ( "ndat", Ndat ); |
---|
42 | F.lookupValue ( "Npart",Npart ); |
---|
43 | mpdf* evolQ = UI::build<mpdf>( F, "Qrw" ); |
---|
44 | vec Qdiag; |
---|
45 | vec Rdiag; |
---|
46 | UI::get( Qdiag, F, "dQ" ); //( "1e-6 1e-6 0.001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
47 | UI::get( Rdiag, F, "dR" );// ( "1e-8 1e-8" ); //var(diff(xth)) = "0.034 0.034" |
---|
48 | |
---|
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 ) ) ); |
---|
63 | KFE.set_rv ( rx ); |
---|
64 | |
---|
65 | RV rQ ( "{Q }","4" ); |
---|
66 | EKFCh_dQ KFEp ; |
---|
67 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
68 | KFEp.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
69 | |
---|
70 | MPF<EKFCh_dQ> M; |
---|
71 | M.set_parameters ( evolQ,evolQ,Npart ); |
---|
72 | // initialize |
---|
73 | evolQ->condition ( 10*Qdiag ); //Zdenek default |
---|
74 | M.set_statistics ( evolQ->_e() , &KFEp ); |
---|
75 | // |
---|
76 | |
---|
77 | M.set_rv ( concat ( rQ,rx ) ); |
---|
78 | |
---|
79 | dirfilelog *L = UI::build<dirfilelog>( F, "logger" );// ( "exp/mpf_test",100 ); |
---|
80 | int l_X = L->add ( rx, "xt" ); |
---|
81 | int l_D = L->add ( concat ( ry,ru ), "" ); |
---|
82 | int l_Q= L->add ( rQ, "" ); |
---|
83 | |
---|
84 | KFE.set_options ( "logbounds" ); |
---|
85 | KFE.log_add ( *L,"KF" ); |
---|
86 | M.set_options ( "logbounds" ); |
---|
87 | M.log_add ( *L,"M" ); |
---|
88 | L->init(); |
---|
89 | |
---|
90 | // SET SIMULATOR |
---|
91 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
92 | vec dt ( 2 ); |
---|
93 | vec ut ( 2 ); |
---|
94 | vec xt ( 4 ); |
---|
95 | vec xtm=zeros ( 4 ); |
---|
96 | double Ww=0.0; |
---|
97 | vec vecW; |
---|
98 | UI::get( vecW, F, "profile" ); |
---|
99 | |
---|
100 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
101 | //Number of steps of a simulator for one step of Kalman |
---|
102 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
103 | //simulator |
---|
104 | sim_profile_vec01t ( Ww,vecW ); |
---|
105 | pmsmsim_step ( Ww ); |
---|
106 | }; |
---|
107 | ut ( 0 ) = KalmanObs[4]; |
---|
108 | ut ( 1 ) = KalmanObs[5]; |
---|
109 | xt = fxu.eval ( xtm,ut ) + diag ( sqrt ( Qdiag ) ) *randn ( 4 ); |
---|
110 | dt = hxu.eval ( xt,ut ); |
---|
111 | xtm = xt; |
---|
112 | |
---|
113 | //Variances |
---|
114 | if ( tK==1000 ) Qdiag ( 0 ) *=10; |
---|
115 | if ( tK==2000 ) Qdiag ( 0 ) /=10; |
---|
116 | if ( tK==3000 ) Qdiag ( 1 ) *=10; |
---|
117 | if ( tK==4000 ) Qdiag ( 1 ) /=10; |
---|
118 | if ( tK==5000 ) Qdiag ( 2 ) *=10; |
---|
119 | if ( tK==6000 ) Qdiag ( 2 ) /=10; |
---|
120 | if ( tK==7000 ) Qdiag ( 3 ) *=10; |
---|
121 | if ( tK==8000 ) Qdiag ( 3 ) /=10; |
---|
122 | |
---|
123 | //estimator |
---|
124 | KFE.bayes ( concat ( dt,ut ) ); |
---|
125 | M.bayes ( concat ( dt,ut ) ); |
---|
126 | |
---|
127 | L->logit ( l_X,xt ); |
---|
128 | L->logit ( l_D,concat ( dt,ut ) ); |
---|
129 | L->logit ( l_Q,Qdiag ); |
---|
130 | |
---|
131 | KFE.logit ( *L ); |
---|
132 | M.logit ( *L ); |
---|
133 | L->step(); |
---|
134 | } |
---|
135 | L->finalize(); |
---|
136 | //Exit program: |
---|
137 | |
---|
138 | delete L; |
---|
139 | return 0; |
---|
140 | } |
---|