root/applications/pmsm/TR2245/wishart.cpp @ 706

Revision 706, 3.9 kB (checked in by smidl, 15 years ago)

eol-native

  • Property svn:eol-style set to native
Line 
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#include "../pmsm.h"
20#include "simulator.h"
21#include "../sim_profiles.h"
22#include "user_info.h"
23#include "stat/loggers.h"
24
25using namespace bdm;
26
27int 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        double h = 1e-6;
34        int Ndat;
35        int Npart;
36        F.lookupValue ( "ndat", Ndat );
37        F.lookupValue ( "Npart",Npart );
38        int Nsimstep = 125;
39
40                // Kalman filter
41        vec Qdiag;
42        UI::get( Qdiag, F, "dQ" ); //( "1e-6 1e-6 0.001 0.0001" ); //zdenek: 0.01 0.01 0.0001 0.0001
43               
44        vec Rdiag;
45        UI::get( Rdiag, F, "dR" );// ( "1e-8 1e-8" ); //var(diff(xth)) = "0.034 0.034"
46
47        // internal model
48        IMpmsm fxu;
49        //                  Rs    Ls        dt       Fmag(Ypm)    kp   p    J     Bf(Mz)
50        fxu.set_parameters ( 0.28, 0.003465, Nsimstep*h, 0.1989, 1.5 ,4.0, 0.04, 0.0 );
51        // observation model
52        OMpmsm hxu;
53
54        vec mu0= "0.0 0.0 0.0 0.0";
55        chmat Q ( Qdiag );
56        chmat R ( Rdiag );
57        EKFCh KFE ;
58        KFE.set_parameters ( &fxu,&hxu,Q,R );
59        KFE.set_est ( mu0, chmat ( zeros ( 4 ) ) );
60        KFE.set_rv ( rx );
61
62        RV rQ ( "{Q }","16" );
63        EKFCh_chQ KFEp ;
64        KFEp.set_parameters ( &fxu,&hxu,Q,R );
65        KFEp.set_est ( mu0, chmat ( zeros ( 4 ) ) );
66
67        rwiWishartCh* evolQw = new rwiWishartCh; 
68        evolQw->set_parameters(4, 0.1, sqrt(Qdiag),0.99);
69        MPF<EKFCh_chQ> M;
70        M.set_parameters ( evolQw,evolQw,Npart );
71        // initialize
72        chmat Ch0(diag(Qdiag));
73        evolQw->condition ( vec(Ch0._Ch()._data(),16) ); //Zdenek default
74        M.set_statistics ( evolQw->_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        int l_fullQ= L->add ( rQ, "full" );
84
85        KFE.set_options ( "logbounds" );
86        KFE.log_add ( *L,"KF" );
87        M.set_options ( "logbounds" );
88        M.log_add ( *L,"M" );
89        L->init();
90
91        // SET SIMULATOR
92        pmsmsim_set_parameters ( 0.28,0.003465,0.1989,0.0,4,1.5,0.04, 200., 3e-6, h );
93        vec dt ( 2 );
94        vec ut ( 2 );
95        vec xt ( 4 );
96        vec xtm=zeros ( 4 );
97        double Ww=0.0;
98        vec vecW;
99        UI::get( vecW, F ,"profile" );
100       
101        mat tQ=diag(Qdiag);
102        mat tChQ=chol(tQ);
103
104        for ( int tK=1;tK<Ndat;tK++ ) {
105                //Number of steps of a simulator for one step of Kalman
106                for ( int ii=0; ii<Nsimstep;ii++ ) {
107                        //simulator
108                        sim_profile_vec01t ( Ww,vecW );
109                        pmsmsim_step ( Ww );
110                };
111                ut ( 0 ) = KalmanObs[4];
112                ut ( 1 ) = KalmanObs[5];
113                xt = fxu.eval ( xtm,ut ) + tChQ.T() *randn ( 4 );
114                dt = hxu.eval ( xt,ut );
115                xtm = xt;
116
117                //Variances
118/*              if ( tK==1000 )  tQ ( 0,0 ) *=10;
119                if ( tK==2000 ) tQ ( 0,0 ) /=10;
120                if ( tK==3000 )  tQ( 1,1 ) *=10;
121                if ( tK==4000 ) tQ( 1,1 ) /=10;
122                if ( tK==5000 )  tQ( 2,2 ) *=10;
123                if ( tK==6000 ) tQ( 2,2 ) /=10;
124                if ( tK==7000 )  tQ( 3,3 ) *=10;
125                if ( tK==8000 ) tQ( 3,3 ) /=10;*/
126               
127                if (tK>1000) {tQ(0,1)=0.5*sqrt(tQ(0,0)*tQ(1,1));tQ(1,0)=tQ(0,1);}
128                if (tK>2000) {tQ(0,1)=0; tQ(1,0)=tQ(0,1);}
129               
130                if (tK>3000) {tQ(2,3)=-0.5*sqrt(tQ(2,2)*tQ(3,3)); tQ(3,2)=tQ(2,3);}
131                if (tK>4000) {tQ(2,3)=0; tQ(3,2)=tQ(2,3);}
132               
133                if (tK>5000) {tQ(0,2)=0.9*sqrt(tQ(0,0)*tQ(2,2)); tQ(2,0)=tQ(0,2);}
134                if (tK>6000) {tQ(0,2)=0; tQ(2,0)=tQ(0,2);}
135
136                tChQ=chol(tQ);
137               
138                //estimator
139                KFE.bayes ( concat ( dt,ut ) );
140                M.bayes ( concat ( dt,ut ) );
141
142                L->logit ( l_X,xt );
143                L->logit ( l_D,concat ( dt,ut ) );
144                mat Q=diag(Qdiag);
145                L->logit ( l_Q,vec(tQ._data(),16) );
146               
147                mat chQ(4,4);
148                copy_vector(16,M._e()->mean()._data(),chQ._data());
149                mat fQ=chQ.T()*chQ;
150                L->logit ( l_fullQ,vec(fQ._data(),16) );
151
152                KFE.logit ( *L );
153                M.logit ( *L );
154                L->step();
155        }
156        L->finalize();
157        //Exit program:
158
159        delete L;
160        return 0;
161}
Note: See TracBrowser for help on using the browser.