root/library/bdm/design/ctrlbase.h @ 508

Revision 508, 3.0 kB (checked in by smidl, 15 years ago)

LQG basic implementation

Line 
1/*!
2  \file
3  \brief Base classes for designers of control strategy
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#include "../base/bdmbase.h"
14
15namespace bdm{
16
17//! Base class of designers of control strategy
18class Designer : public root {
19        public:
20                //! Redesign control strategy
21                virtual void redesign(){it_error("Not implemented"); };
22                //! apply control strategy to obtain control input
23                virtual vec apply(const vec &cond){it_error("Not implemented"); return vec(0);}
24};
25
26//! Linear Quadratic Gaussian designer for constant penalizations and constant target
27//! Its internals are very close to Kalman estimator
28class LQG : public Designer {
29        protected:
30                //! dimension of state
31                int dimx;
32                //! dimension of inputs
33                int dimu;
34                //! dimension of output
35                int dimy;
36
37                //! matrix A of the linear system
38                mat A;
39                //! matrix B of the linear system
40                mat B;
41                //! matrix C of the linear system
42                mat C;
43                //! required value of the output y at time t (assumed constant)
44                vec y_req;
45               
46                //! Control horizon, set to maxint for infinite horizons
47                int horizon;
48                //! penalization matrix Qy
49                mat Qy;
50                //! penalization matrix Qu
51                mat Qu;
52                //! time of the design step - from horizon->0
53                int td;
54                //! controller parameters
55                mat L;
56               
57                //!@{ \name temporary storage for ricatti
58                //!  parameters
59                mat pr;
60                //! penalization
61                mat qux;
62                //! penalization
63                mat qyx;
64                //! internal quadratic form
65                mat s;
66                //! penalization
67                mat qy;
68                //! pre_qr part
69                mat hqy;
70                //! pre qr matrix
71                mat pre_qr;
72                //! post qr matrix
73                mat post_qr;
74                //!@}
75               
76        public:
77                //! set system parameters from given matrices
78                void set_system_parameters(const mat &A, const mat &B, const mat &C);
79                //! set penalization matrices and control horizon
80                void set_control_parameters(const mat &Qy0, const mat &Qu0, const vec y_req0, int horizon0);
81                //! set system parameters from Kalman filter
82//              void set_system_parameters(const Kalman &K);
83                //! refresh temporary storage - inefficient can be improved
84                void prepare_qr();
85                //! function for future use which is called at each time td; Should call prepare_qr()!
86                virtual void update_state(){};
87                //! redesign one step of the
88                void ricatti_step(){
89                        pre_qr.set_submatrix(0,0,s*pr);
90                        pre_qr.set_submatrix(dimx+dimu+dimy, dimu+dimx, -Qy*y_req);
91                        if (!qr(pre_qr,post_qr)) it_warning("QR in LQG unstable");
92                        triu(post_qr);
93        // hn(m+1:2*m+n+r,m+1:2*m+n+r);
94                        s=post_qr.get(dimu, 2*dimu+dimx+dimy-1, dimu, 2*dimu+dimx+dimy-1);
95                };
96                void redesign(){
97                        for(td=horizon; td>0; td--){
98                                update_state();
99                                ricatti_step();
100                        }
101/*                      ws=hn(1:m,m+1:2*m+n+r);
102                        wsd=hn(1:m,1:m);
103                        Lklq=-inv(wsd)*ws;*/
104                        L = -inv(post_qr.get(0,dimu-1, 0,dimu-1)) * post_qr.get(0,dimu-1, dimu, 2*dimu+dimx+dimy-1);
105                }
106                vec apply(const vec &state, const vec &ukm){vec pom=concat(state, ones(dimy), ukm); return L*pom;}
107        } ;
108
109} // namespace
Note: See TracBrowser for help on using the browser.