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 | #include <stat/libEF_ui.h> |
---|
21 | |
---|
22 | #include "../pmsm.h" |
---|
23 | #include "simulator.h" |
---|
24 | #include "../sim_profiles.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 | int Ndat; |
---|
35 | int Npart; |
---|
36 | double h = 1e-6; |
---|
37 | int Nsimstep = 125; |
---|
38 | |
---|
39 | vec Qdiag; |
---|
40 | vec Rdiag; |
---|
41 | |
---|
42 | mpdf* evolQ ; |
---|
43 | try { |
---|
44 | // Kalman filter |
---|
45 | F.lookupValue ( "ndat", Ndat ); |
---|
46 | F.lookupValue ( "Npart",Npart ); |
---|
47 | |
---|
48 | UIbuild ( F.lookup ( "Qrw" ),evolQ ); |
---|
49 | Qdiag= getvec ( F.lookup ( "dQ" ) ); //( "1e-6 1e-6 0.001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001 |
---|
50 | Rdiag=getvec ( F.lookup ( "dR" ) );// ( "1e-8 1e-8" ); //var(diff(xth)) = "0.034 0.034" |
---|
51 | } |
---|
52 | catch UICATCH; |
---|
53 | // internal model |
---|
54 | |
---|
55 | IMpmsm fxu; |
---|
56 | // Rs Ls dt Fmag(Ypm) kp p J Bf(Mz) |
---|
57 | fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 ); |
---|
58 | // observation model |
---|
59 | OMpmsm hxu; |
---|
60 | |
---|
61 | vec mu0= "0.0 0.0 0.0 0.0"; |
---|
62 | chmat Q ( Qdiag ); |
---|
63 | chmat R ( Rdiag ); |
---|
64 | EKFCh KFE ; |
---|
65 | KFE.set_parameters ( &fxu,&hxu,Q,R ); |
---|
66 | KFE.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
67 | KFE.set_rv ( rx ); |
---|
68 | |
---|
69 | RV rQ ( "{Q }","4" ); |
---|
70 | EKFCh_dQ KFEp ; |
---|
71 | KFEp.set_parameters ( &fxu,&hxu,Q,R ); |
---|
72 | KFEp.set_est ( mu0, chmat ( zeros ( 4 ) ) ); |
---|
73 | |
---|
74 | MPF<EKFCh_dQ> M; |
---|
75 | M.set_parameters ( evolQ,evolQ,Npart ); |
---|
76 | // initialize |
---|
77 | evolQ->condition ( 10*Qdiag ); //Zdenek default |
---|
78 | M.set_statistics ( evolQ->_e() , &KFEp ); |
---|
79 | // |
---|
80 | |
---|
81 | M.set_rv ( concat ( rQ,rx ) ); |
---|
82 | |
---|
83 | dirfilelog *L; UIbuild ( F.lookup ( "logger" ), L );// ( "exp/mpf_test",100 ); |
---|
84 | int l_X = L->add ( rx, "xt" ); |
---|
85 | int l_D = L->add ( concat ( ry,ru ), "" ); |
---|
86 | int l_Q= L->add ( rQ, "" ); |
---|
87 | |
---|
88 | KFE.set_options ( "logbounds" ); |
---|
89 | KFE.log_add ( L,"KF" ); |
---|
90 | M.set_options ( "logbounds" ); |
---|
91 | M.log_add ( L,"M" ); |
---|
92 | L->init(); |
---|
93 | |
---|
94 | // SET SIMULATOR |
---|
95 | pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h ); |
---|
96 | vec dt ( 2 ); |
---|
97 | vec ut ( 2 ); |
---|
98 | vec xt ( 4 ); |
---|
99 | vec xtm=zeros ( 4 ); |
---|
100 | double Ww=0.0; |
---|
101 | vec vecW=getvec ( F.lookup ( "profile" ) ); |
---|
102 | |
---|
103 | for ( int tK=1;tK<Ndat;tK++ ) { |
---|
104 | //Number of steps of a simulator for one step of Kalman |
---|
105 | for ( int ii=0; ii<Nsimstep;ii++ ) { |
---|
106 | //simulator |
---|
107 | sim_profile_vec01t ( Ww,vecW ); |
---|
108 | pmsmsim_step ( Ww ); |
---|
109 | }; |
---|
110 | ut ( 0 ) = KalmanObs[4]; |
---|
111 | ut ( 1 ) = KalmanObs[5]; |
---|
112 | xt = fxu.eval ( xtm,ut ) + diag ( sqrt ( Qdiag ) ) *randn ( 4 ); |
---|
113 | dt = hxu.eval ( xt,ut ); |
---|
114 | xtm = xt; |
---|
115 | |
---|
116 | //Variances |
---|
117 | if ( tK==1000 ) Qdiag ( 0 ) *=10; |
---|
118 | if ( tK==2000 ) Qdiag ( 0 ) /=10; |
---|
119 | if ( tK==3000 ) Qdiag ( 1 ) *=10; |
---|
120 | if ( tK==4000 ) Qdiag ( 1 ) /=10; |
---|
121 | if ( tK==5000 ) Qdiag ( 2 ) *=10; |
---|
122 | if ( tK==6000 ) Qdiag ( 2 ) /=10; |
---|
123 | if ( tK==7000 ) Qdiag ( 3 ) *=10; |
---|
124 | if ( tK==8000 ) Qdiag ( 3 ) /=10; |
---|
125 | |
---|
126 | //estimator |
---|
127 | KFE.bayes ( concat ( dt,ut ) ); |
---|
128 | M.bayes ( concat ( dt,ut ) ); |
---|
129 | |
---|
130 | L->logit ( l_X,xt ); |
---|
131 | L->logit ( l_D,concat ( dt,ut ) ); |
---|
132 | L->logit ( l_Q,Qdiag ); |
---|
133 | |
---|
134 | KFE.logit ( L ); |
---|
135 | M.logit ( L ); |
---|
136 | L->step(); |
---|
137 | } |
---|
138 | L->finalize(); |
---|
139 | //Exit program: |
---|
140 | |
---|
141 | delete L; |
---|
142 | return 0; |
---|
143 | } |
---|