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 | #include "../estim/kalman.h" |
---|
15 | |
---|
16 | namespace bdm{ |
---|
17 | |
---|
18 | //! Base class for adaptive controllers |
---|
19 | //! The base class is, however, non-adaptive, method \c adapt() is empty. |
---|
20 | //! \note advanced Controllers will probably include estimator as their internal attribute (e.g. dual controllers) |
---|
21 | class Controller : public root { |
---|
22 | protected: |
---|
23 | //! identifier of the designed action; |
---|
24 | RV rv; |
---|
25 | //! identifier of the conditioning variables; |
---|
26 | RV rvc; |
---|
27 | public: |
---|
28 | //! function processing new observations and adapting control strategy accordingly |
---|
29 | virtual void redesign(const vec &data){}; |
---|
30 | //! returns designed control action |
---|
31 | virtual vec ctrlaction(const vec &cond){return vec(0);} |
---|
32 | |
---|
33 | void from_setting(const Setting &set){ |
---|
34 | UI::get(rv,set,"rv",UI::optional); |
---|
35 | UI::get(rvc,set,"rvc",UI::optional); |
---|
36 | } |
---|
37 | //! access function |
---|
38 | const RV& _rv() {return rv;} |
---|
39 | //! access function |
---|
40 | const RV& _rvc() {return rvc;} |
---|
41 | //! register this controller with given datasource under name "name" |
---|
42 | virtual void log_register (logger &L, int level, const string &prefix ) { } |
---|
43 | //! write requested values into the logger |
---|
44 | virtual void log_write ( ) const { } |
---|
45 | |
---|
46 | }; |
---|
47 | |
---|
48 | //! Linear Quadratic Gaussian designer for constant penalizations and constant target |
---|
49 | //! Its internals are very close to Kalman estimator |
---|
50 | class LQG : public Controller { |
---|
51 | protected: |
---|
52 | //! StateSpace model from which we read data |
---|
53 | shared_ptr<StateSpace<fsqmat> > S; |
---|
54 | //! required value of the output y at time t (assumed constant) |
---|
55 | vec y_req; |
---|
56 | //! required value of the output y at time t (assumed constant) |
---|
57 | vec u_req; |
---|
58 | |
---|
59 | //! Control horizon, set to maxint for infinite horizons |
---|
60 | int horizon; |
---|
61 | //! penalization matrix Qy |
---|
62 | mat Qy; |
---|
63 | //! penalization matrix Qu |
---|
64 | mat Qu; |
---|
65 | //! time of the design step - from horizon->0 |
---|
66 | int td; |
---|
67 | //! controller parameters |
---|
68 | mat L; |
---|
69 | |
---|
70 | //!@{ \name temporary storage for ricatti - use initialize |
---|
71 | //! convenience parameters |
---|
72 | int dimx; |
---|
73 | //! convenience parameters |
---|
74 | int dimy; |
---|
75 | //! convenience parameters |
---|
76 | int dimu; |
---|
77 | |
---|
78 | //! parameters |
---|
79 | mat pr; |
---|
80 | //! penalization |
---|
81 | mat qux; |
---|
82 | //! penalization |
---|
83 | mat qyx; |
---|
84 | //! internal quadratic form |
---|
85 | mat s; |
---|
86 | //! penalization |
---|
87 | mat qy; |
---|
88 | //! pre_qr part |
---|
89 | mat hqy; |
---|
90 | //! pre qr matrix |
---|
91 | mat pre_qr; |
---|
92 | //! post qr matrix |
---|
93 | mat post_qr; |
---|
94 | //!@} |
---|
95 | |
---|
96 | public: |
---|
97 | //! set system parameters from given matrices |
---|
98 | void set_system(shared_ptr<StateSpace<fsqmat> > S0); |
---|
99 | //! set penalization matrices and control horizon |
---|
100 | void set_control_parameters(const mat &Qy0, const mat &Qu0, const vec &y_req0, int horizon0); |
---|
101 | //! set system parameters from Kalman filter |
---|
102 | // void set_system_parameters(const Kalman &K); |
---|
103 | //! refresh temporary storage - inefficient can be improved |
---|
104 | void initialize(); |
---|
105 | //! validation procedure |
---|
106 | void validate(); |
---|
107 | //! function for future use which is called at each time td; Should call initialize()! |
---|
108 | virtual void update_state(){}; |
---|
109 | //! redesign one step of the |
---|
110 | void ricatti_step(){ |
---|
111 | pre_qr.set_submatrix(0,0,s*pr); |
---|
112 | pre_qr.set_submatrix(dimx+dimu+dimy, dimu+dimx, -Qy*y_req); |
---|
113 | if (!qr(pre_qr,post_qr)){ bdm_warning("QR in LQG unstable");} |
---|
114 | triu(post_qr); |
---|
115 | // hn(m+1:2*m+n+r,m+1:2*m+n+r); |
---|
116 | s=post_qr.get(dimu, 2*dimu+dimx+dimy-1, dimu, 2*dimu+dimx+dimy-1); |
---|
117 | }; |
---|
118 | void redesign(){ |
---|
119 | for(td=horizon; td>0; td--){ |
---|
120 | update_state(); |
---|
121 | ricatti_step(); |
---|
122 | } |
---|
123 | /* ws=hn(1:m,m+1:2*m+n+r); |
---|
124 | wsd=hn(1:m,1:m); |
---|
125 | Lklq=-inv(wsd)*ws;*/ |
---|
126 | L = -inv(post_qr.get(0,dimu-1, 0,dimu-1)) * post_qr.get(0,dimu-1, dimu, 2*dimu+dimx+dimy-1); |
---|
127 | } |
---|
128 | //! compute control action |
---|
129 | vec ctrlaction(const vec &state, const vec &ukm){vec pom=concat(state, ones(dimy), ukm); return L*pom;} |
---|
130 | } ; |
---|
131 | |
---|
132 | |
---|
133 | } // namespace |
---|